forked from malmhaug/C_AbsBegin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC6E4_2DTicTacToe.c
147 lines (125 loc) · 4.58 KB
/
C6E4_2DTicTacToe.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
/* Challanges Ch6E4 - 2DTicTacToe */
// By Jim-Kristian Malmhaug | Date: 8 February 2014
//Notes:
//References:
#include <stdio.h>
/* Function prototypes */
void printBoard2D();
int checkForWinner();
int checkForDraw();
/* Global variables */
char board2D[3][3] = {{'1','4','7'},{'2','5','8'},{'3','6','9'}};
char cNextPlayer = 'X';
/* Main function */
int main() {
int iNextPlayer = 0; //Variable used to switch between players.
int result = 0; //Variable used for finding winners.
int positionX, positionY; //Variable for the board.
int nextPlayerOK = 0; //Variable used for checking square selection.
int freeSquare = 0;
printBoard2D(); //Prints the 2D board.
while(result != 1){
nextPlayerOK = 0;
iNextPlayer %= 2; //Next Player selection via modulus, 0 = X, 1 = O.
if(iNextPlayer == 0){ //Sets the player.
printf("\nPlayer X turn! Choose Your position.");
cNextPlayer = 'X';
}
else {
printf("\nPlayer O turn! Choose Your position.");
cNextPlayer = 'O';
}
printf("\nColumn (X-position): ");
scanf("%d", &positionX); //Player sets the x-position for its mark.
printf("\nRow (Y-position): ");
scanf("%d", &positionY); //Player sets the y.position for its mark.
if(board2D[positionX-1][positionY-1] != 'X' && board2D
[positionX-1][positionY-1] != 'O'){ //Check for open square.
board2D[positionX-1][positionY-1]=cNextPlayer; //Sets mark.
result = checkForWinner(); //Checks for winner.
}
else{ //Player selected an taken square.
printf("\nPlace taken!");
nextPlayerOK = 1; //Sets an flag for wrong selection.
}
freeSquare = checkForDraw(); //Search for free square.
if(result == 1){ //The game has a winner.
printBoard2D();
printf("\nWinner is %c!\n", cNextPlayer);
break;
}
else if((result == 0) && (freeSquare == 9)){ //The game is a draw.
printBoard2D();
printf("\nDraw!\n");
break;
}
printBoard2D();
if(nextPlayerOK == 0)
iNextPlayer++; //Sets the next player.
}
return 0; //Return 0 if no winner is found.
}
/* Function definition - printBoard2D()*/
void printBoard2D(){
int i,j;
printf("\nSquare\tX Y");
printf("\n 1\t1 1");
printf("\n 2\t2 1");
printf("\n 3\t3 1");
printf("\n 4\t1 2");
printf("\n 5\t2 2");
printf("\n 6\t3 2");
printf("\n 7\t1 3");
printf("\n 8\t2 3");
printf("\n 9\t3 3");
printf("\n-----------");
for(i = 0; i < 3; i++){ //Prints the board on screen.
printf("\n |");
for(j = 0; j < 3; j++){
printf("%c|", board2D[j][i]);
}
}
}
/* Function definition - checkForWinner()*/
int checkForWinner(){ //This function searches for three in a row.
if (board2D[0][0] == cNextPlayer && board2D[0][1] == cNextPlayer &&
board2D[0][2] == cNextPlayer)
return 1;
else if (board2D[0][0] == cNextPlayer && board2D[1][0] == cNextPlayer &&
board2D[2][0] == cNextPlayer)
return 1;
else if (board2D[1][0] == cNextPlayer && board2D[1][1] == cNextPlayer &&
board2D[1][2] == cNextPlayer)
return 1;
else if (board2D[2][0] == cNextPlayer && board2D[2][1] == cNextPlayer &&
board2D[2][2] == cNextPlayer)
return 1;
else if (board2D[0][1] == cNextPlayer && board2D[1][1] == cNextPlayer &&
board2D[2][1] == cNextPlayer)
return 1;
else if (board2D[0][2] == cNextPlayer && board2D[1][2] == cNextPlayer &&
board2D[2][2] == cNextPlayer)
return 1;
else if (board2D[0][0] == cNextPlayer && board2D[1][1] == cNextPlayer &&
board2D[2][2] == cNextPlayer)
return 1;
else if (board2D[0][2] == cNextPlayer && board2D[1][1] == cNextPlayer &&
board2D[2][0] == cNextPlayer)
return 1;
else
return 0;
}
/* Function definition - checkForDraw()*/
int checkForDraw(){ //This function searches for draw.
int k,m;
int squareTaken = 0;
for(k = 0; k < 3; k++){
for(m = 0; m < 3; m++){
if(board2D[m][k] == 'X' || board2D[m][k] == 'O'){
squareTaken++;
}
}
}
//printf("\n\nSquare taken : %d\n", squareTaken);
return squareTaken;
}