-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.c
More file actions
178 lines (156 loc) · 5.25 KB
/
board.c
File metadata and controls
178 lines (156 loc) · 5.25 KB
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
#include "tic_tac_toe.h"
/*
To Initialize the Tic-Tac-Toe at the Start of a New game and provide the Instructions to the User.
Input - The Board Array.
Output - NIL.
*/
void initialize_board (char board [SIZE][SIZE])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board [i][j] = '*';
}
}
printf ("\t\t\t\t\t\t INSTRUCTIONS\n");
printf("---------------------------------------------------------------------------------------------------------------\n");
printf ("INFO: While playing your Turn; please enter the Cell number between 1-9 to choose the Cell to mark your Move.\n");
printf ("INFO: The Cells are numbered 1 to 9 sequentially Row-wise.\n");
printf ("INFO: The Markers are as follows: '*' -> Empty Cell, 'X' / 'O' -> Filled Cell.\n");
printf("---------------------------------------------------------------------------------------------------------------\n");
return;
}
/*
To Display the Current Status of the Tic-Tac-Toe Board.
Input - The Board Array.
Output - NIL.
*/
void print_board (char board [SIZE][SIZE])
{
printf ("INFO: The Current Board Status is:\n");
printf ("-------------\n");
for (int i = 0; i < 3; i++)
{
printf ("| ");
for (int j = 0; j < 3; j++)
{
printf ("%c | ", board [i][j]);
}
printf ("\n-------------\n");
}
printf ("\n");
return;
}
/*
To Check if a Particular Cell on the Tic-Tac-Toe Board is Empty or not.
Input - The Board Array and the Cell Position.
Output - SUCCESS or FAILURE.
*/
int check_cell (char board [SIZE][SIZE], int pos)
{
int row = (pos - 1) / SIZE; //To get the Row position of the Cell on the Board.
int col = (pos - 1) % SIZE; //To get the Column position of the Cell on the Board.
if (board [row][col] == '*') //If the give Cell is marked as '*', then it is Empty.
return SUCCESS;
else
return FAILURE;
}
/*
To Mark a Particular Cell on the Tic-Tac-Toe Board.
Input - The Board Array, the Marker of the User and the Cell Position.
Output - NIL.
*/
void mark_board (char board [SIZE][SIZE], char marker, int pos)
{
int row = (pos - 1) / SIZE; //To get the Row position of the Cell on the Board.
int col = (pos - 1) % SIZE; //To get the Column position of the Cell on the Board.
board [row][col] = marker; //To mark the Particular Cell position on the Board with the given Marking.
return;
}
/*
To De-Mark a Particular Cell on the Tic-Tac-Toe Board.
Input - The Board Array and the Cell Position.
Output - NIL.
*/
void demark_board (char board [SIZE][SIZE], int pos)
{
int row = (pos - 1) / SIZE; //To get the Row position of the Cell on the Board.
int col = (pos - 1) % SIZE; //To get the Column position of the Cell on the Board.
board [row][col] = '*'; //To de-mark the Particular Cell position on the Board.
return;
}
/*
To Update the Marker of the Users for the next Move.
Input - The Current User's Marker.
Output - The Next User's Marker.
*/
char change_marker (char marker)
{
if (marker == FIRST) //If the Current Marker belongs to the User-1; switch the Marker to User-2.
marker = SECOND;
else //If the Current Marker belongs to the User-2; switch the Marker to User-1.
marker = FIRST;
return marker;
}
/*
To Check if a Particular User has won the Game by marking all the Cells in a Particular Row.
Input - The Board Array.
Output - SUCCESS or FAILURE.
*/
int row_win (char board [SIZE][SIZE])
{
for (int i = 0; i < SIZE; i++)
{
/* If any of the Player marks all the cells of a particular Row on the Board */
if ((board [i][0] == board [i][1]) && (board [i][1] == board [i][2]) && (board [i][0] != '*'))
return SUCCESS;
}
return FAILURE;
}
/*
To Check if a Particular User has won the Game by marking all the Cells in a Particular Column.
Input - The Board Array.
Output - SUCCESS or FAILURE.
*/
int col_win (char board [SIZE][SIZE])
{
for (int i = 0; i < SIZE; i++)
{
/* If any of the Player marks all the cells of a particular Column on the Board */
if ((board [0][i] == board [1][i]) && (board [1][i] == board [2][i]) && (board [0][i] != '*'))
return SUCCESS;
}
return FAILURE;
}
/*
To Check if a Particular User has won the Game by marking all the Cells in the Principal or Counter Diagonal.
Input - The Board Array.
Output - SUCCESS or FAILURE.
*/
int diagonal_win (char board [SIZE][SIZE])
{
/* If any of the Player marks all the cells along the Principal Diagonal on the Board */
if ((board [0][0] == board [1][1]) && (board [1][1] == board [2][2]) && (board [0][0] != '*'))
return SUCCESS;
/* If any of the Player marks all the cells along the Counter Diagonal on the Board */
if ((board [0][2] == board [1][1]) && (board [1][1] == board [2][0]) && (board [0][2] != '*'))
return SUCCESS;
return FAILURE;
}
/*
To Check if the Game has ended by any particular Means of Win defined above.
Input - The Board Array.
Output - SUCCESS or FAILURE.
*/
int game_over (char board [SIZE][SIZE])
{
/* The Array of Function pointers shall act as a means to call the various functions check if the Game has ended or not */
int (*win [3]) (char [SIZE][SIZE]) = {&row_win, &col_win, &diagonal_win};
for (int i = 0; i < 3; i++)
{
if (((*win [i]) (board)) == SUCCESS) //If any of the function call returns SUCCESS, the Game is Over.
return SUCCESS;
}
return FAILURE;
}