-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
179 lines (153 loc) · 5.11 KB
/
main.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
#include <stdio.h>
#include <string.h>
#include"functions.h"
#define N 3
int main(void)
{
char grid[N][N], sym, userPlayAgain[257], *pos;
int x, y, n, counter, player, reciveCoordinate, coordinateLetter, draw, gameEnds=0, invalidPlay, changeFirstPlayer=0, repiteQuestion, playAgain;
/* ↓ The ganme starts ↓ */
welcomeMSG();
do
{
importantStuffMSG();
startingGameMSG();
inicializeGrid(grid);
printGrid(grid);
counter=1;
while(counter <=9 || gameEnds != 1)
{
/* ↓ Players order ↓ */
if(changeFirstPlayer == 1)
{
if (counter % 2 == 0)
{
sym = 'X';
player = 1;
}
else if (counter % 2 != 0)
{
sym = 'O';
player = 0;
}
}
else
{
if (counter % 2 == 0)
{
sym = 'O';
player = 0;
}
else if (counter % 2 != 0)
{
sym = 'X';
player = 1;
}
}
printChar(64, '-');
printf("\n");
printf("\t\t |-- PLAYER %d --|\n", player);
printf("\n");
/* ↓ Start of the move ↓ */
do
{
/* ↓ Asks for a valid input ↓ */
if(invalidPlay == 1 || invalidPlay == 2 || coordinateLetter == 1)
{
invalidCoordinatesMSG();
}
printf("COORDINATES TO PUT THE '%c':\n", sym);
printf(" --> Line number (0 a 2): ");
/* ↓ Check if the input is a letter ↓ */
reciveCoordinate = scanf("%d", &x);
if(reciveCoordinate != 1)
{
getchar();
coordinateLetter = 1;
}
else
coordinateLetter = 0;
printf(" --> Column number (0 a 2): ");
/* ↓ Check if the input is a letter ↓ */
reciveCoordinate = scanf("%d", &y);
if(reciveCoordinate != 1)
{
getchar();
coordinateLetter = 1;
}
else
coordinateLetter = 0;
invalidPlay = play(grid, x, y, player);
}while(invalidPlay == 1 || invalidPlay == 2 || coordinateLetter == 1);
play(grid, x, y, player);
printf("\n");
printGrid(grid);
/* ↓ If the number of moves is above 4, checks if there is any 3 in a row ↓ */
if(counter>4)
gameEnds = threeInRow(grid, x, y);
/* ↓ If he finds a 3 in a row, the game is done ↓ */
if(gameEnds == 1)
break;
if(gameEnds == -1)
printf("Invalid coordinates!!\n");
if(counter == 9)
{
/* ↓ If the number of moves is 8, then no one wins - Draw ↓ */
draw = 1;
break;
}
counter++;
}
/* ↓ End of the game ↓ */
printf("\n");
printChar(64, '*');
printf("\n");
printChar(20, ' ');
/* ↓ Draw message ↓ */
if(draw == 1)
{
printf("** GAME DRAW **\n");
printf(" None of the players placed 3 symbols in a row. \n");
}
/* ↓ Winning message ↓ */
else
{
printf("** PLAYER %d WON !! **\n", player);
printf("\n");
printf(" PLAYER %d lined 3 symbols and won the game! \n", player);
}
printf("\n");
printGrid(grid);
printf("\n");
limpaInput();
do
{
printf("Do you want to play again? [YES/NO] --> ");
fgets(userPlayAgain, 257, stdin);
pos = strchr(userPlayAgain, '\n');
*pos = '\0';
if((strcmp(userPlayAgain, "YES") == 0) || (strcmp(userPlayAgain, "yes") == 0))
{
playAgain = 1;
repiteQuestion = 0;
}
else if((strcmp(userPlayAgain, "NO") == 0) || (strcmp(userPlayAgain, "no") == 0))
{
playAgain = 0;
repiteQuestion = 0;
}
else
repiteQuestion = 1;
}while(repiteQuestion == 1);
if(playAgain == 1)
{
printChar(64, '*');
printf("\n\n");
changeFirstPlayer = !changeFirstPlayer;
gameEnds = 0;
}
}while(playAgain == 1);
/* ↓ The program ends ↓ */
endingMSG();
return 0;
}