-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic-Tac-Toe_no_global_var.c
186 lines (171 loc) · 5.51 KB
/
Tic-Tac-Toe_no_global_var.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Tic-Tac-Toe Project written in C
* Made by: Ivan Herran
* 2023-08-10 */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/*Check OS*/
#ifdef __WIN32
#define OS "Windows"
#define clear system.cls()
#elif __linux__
#define OS "Linux"
#define clear system("clear")
#elif __APPLE__
#define OS "Apple"
#define clear system("clear")
#else
#define OS NULL
#define clear = NULL
#endif
void gameBoard(void);
void rules(void);
const char* checkOS(void);
int checkWinner(char gameField[], unsigned int gameFieldTracker);
void board(char gameField[]);
bool checkInput(unsigned int coordinate,unsigned int* gameFieldTracker, bool playerOneTurn, char gameField[]);
int main(void)
{
char selection;
printf("Welcome to Tic-Tac-Toe written in C\n");
printf("Game Made By: Ivan Herran\n");
printf("Currently running program on %s\n", checkOS());
rules();
do{
printf("\nPress 1 to start the game, press 2 to show the rules, press 3 to exit the game \n");
printf("Selection: ");
selection = getchar();
getchar();
switch (selection) {
case '1':
clear;
gameBoard();
break;
case '2':
rules();
break;
case '3':
printf("\nThanks for playing!");
break;
default:
printf("\nYou should press one of the 3 options!");
break;
}
}while(selection != '3');
return EXIT_SUCCESS;
}
void rules(void){
char selection;
do{
printf("\nThe game consists of a 3 by 3 board in which two players have to \n");
printf ("match 3 consecutive slots with their respective symbols.\n");
printf("The Symbols being \"X\" and \"o\" one for each player. \n");
printf("Players chose which symbol starts first\n");
printf("You have to chose the gameField of an empy spot you cant overwrite \n");
printf("You cant overwrite spots which already have a symbol \n");
printf("Good luck and have fun!\n");
printf("Press any key to continue... \n");
selection = getchar();
getchar();
}while(selection == '@');
}
const char* checkOS(void){
return (OS == NULL) ? "Null" : OS;
}
void gameBoard(){
char playerOne[20], playerTwo[20] ;
unsigned int winner, coordinate, gameFieldTracker = 0;
char gameField[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
bool gameState = true, playerOneTurn = true;
printf("\nPlease enter name of player 1 - X: ");
fgets(playerOne, 20, stdin);
printf("\nPlease enter name of player 2 - O: ");
fgets(playerTwo, 20, stdin);
do{
if (playerOneTurn == true) {
printf("%s's Turn\n\n", playerOne );
do{
printf("Insert coordinate from 1 to 9: \n");
scanf(" %d", &coordinate);
getchar();
}while(checkInput(coordinate, &gameFieldTracker, playerOneTurn, gameField) == false);
board(gameField);
playerOneTurn = false;
}else if (playerOneTurn == false) {
printf("%s Turn\n\n", playerTwo);
do{
printf("Insert coordinate from 1 to 9: \n");
scanf(" %d", &coordinate);
getchar();
}while(checkInput(coordinate, &gameFieldTracker, playerOneTurn, gameField) == false);
board(gameField);
playerOneTurn = true;
}
winner = checkWinner(gameField, gameFieldTracker);
if (winner == 1 && playerOneTurn == false) {
printf("%sWINS!\n", playerOne);
gameState = false;
}else if (winner == 1 && playerOneTurn == true){
printf("%sWINS!\n", playerTwo);
gameState = false;
}else if (winner == 2){
printf("Game ends in a DRAW!\n");
gameState = false;
}
}while(gameState == true);
}
void board(char gameField[]){
printf(" | | \n");
printf(" %c | %c | %c \n", gameField[0], gameField[1], gameField[2]);
printf(" | | \n");
printf("----|----|----\n");
printf(" | | \n");
printf(" %c | %c | %c \n", gameField[3], gameField[4], gameField[5]);
printf(" | | \n");
printf("----|----|----\n");
printf(" | | \n");
printf(" %c | %c | %c \n", gameField[6], gameField[7], gameField[8]);
printf(" | | \n\n\n");
}
bool checkInput(unsigned int coordinate, unsigned int* gameFieldTracker, bool playerOneTurn, char gameField[]){
unsigned int i ;
if (gameField[coordinate - 1] == 'X' || gameField[coordinate - 1] == 'O'){
printf("\nSpot already used try another one!\n");
return false;
}
if (playerOneTurn == true && coordinate > 0 && coordinate <= 9){
*gameFieldTracker += 1;
gameField[coordinate - 1] = 'X';
return true;
}else if(playerOneTurn == false && coordinate > 0 && coordinate <= 9){
*gameFieldTracker += 1;
gameField[coordinate - 1] = 'O';
return true;
}else {
printf("\nInvalid coordinate, no changes to the game field!\n\n");
return false;
}
return false;
}
int checkWinner(char gameField[], unsigned int gameFieldTracker){
if (gameField[0] == gameField[1] && gameField[1] == gameField[2]) {
return 1;
}else if (gameField[3] == gameField[4] && gameField[4] == gameField[5]) {
return 1;
}else if (gameField[6] == gameField[7] && gameField[7] == gameField[8]) {
return 1;
}else if (gameField[0] == gameField[3] && gameField[3] == gameField[6]) {
return 1;
}else if (gameField[1] == gameField[4] && gameField[4] == gameField[7]) {
return 1;
}else if (gameField[2] == gameField[5] && gameField[5] == gameField[8]) {
return 1;
}else if (gameField[0] == gameField[4] && gameField[4] == gameField[8]) {
return 1;
}else if (gameField[6] == gameField[4] && gameField[4] == gameField[2]) {
return 1;
}else if (gameFieldTracker == 9){
return 2;
}
return 0;
}