-
Notifications
You must be signed in to change notification settings - Fork 0
/
a08.h
272 lines (272 loc) · 7.43 KB
/
a08.h
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <stdio.h>
// Prototypes
char* flat_char_matrix(char[],int,int,int,int);
void flatten_char_matrix(char[ROWS][COLS],char[],int,int);
void fill_char_matrix(char[],char[ROWS][COLS],int,int);
void InitializeBoard(char[ROWS][COLS]);
void DisplayBoard(char[ROWS][COLS]);
int PlayerMove(int, int, char[ROWS][COLS], char);
int VictoryCheck(int, char[ROWS][COLS]);
int DisplayVictoryMessage(int);
// Function Implementations
char* flat_char_matrix(char array[], int Rows, int Cols, int row, int col) {
return &array[ row*Cols + col ];
}
void flatten_char_matrix(char matrix[ROWS][COLS], char array[], int Rows, int Cols) {
int i;
for (i = 0; i < Rows; i++) {
int k;
for (k = 0; k < Cols; k++) {
*flat_char_matrix(array, Rows, Cols, i, k) = matrix[i][k];
}
}
}
void fill_char_matrix(char array[], char matrix[ROWS][COLS], int Rows, int Cols) {
int i;
for (i = 0; i < Rows; i++) {
int k;
for (k = 0; k < Cols; k++) {
matrix[i][k] = *flat_char_matrix(array, Rows, Cols, i, k);
}
}
}
void InitializeBoard(char board[ROWS][COLS]) {
int i;
for (i = 0; i < ROWS; i++) {
int k;
for (k = 0; k < COLS; k++)
board[i][k] = BLANK;
}
}
void top() {
int col;
printf("\tTICK ATTACKS TOE\n\n");
printf(" ");
for (col = 1; col <= COLS; col++) {
printf(" %1d", col );
}
printf("\n\n");
}
void middleRow() {
int col;
printf(" -");
for (col = 2; col <= COLS; col++) {
printf("+-");
}
printf("\n");
}
void majorRow(char board[ROWS][COLS], int row) {
int col;
printf("%d %c", row, board[row-1][0] );
for (col = 2; col <= COLS; col++) {
printf("|%c", board[row-1][col - 1]);
}
printf("\n");
}
void DisplayBoard(char board[ROWS][COLS]) {
int row;
// Display function components
// The actual display
top();
majorRow(board, 1);
for (row = 2; row <= ROWS; row++) {
middleRow();
majorRow( board, row );
}
}
int PlayerMove(int row, int col, char board[ROWS][COLS], char symbol) {
// Check for move legality
if ((row>0 && row<=ROWS) && (col>0 && col<=COLS)) {
if (board[row-1][col-1] == BLANK) {
board[row-1][col-1] = symbol;
return TRUE;
} else {
printf( "That space is already occupied.\n", row, col );
return FALSE;
}
} else {
printf( "That move is not on the board.\n" );
return FALSE;
}
}
int lineVictoryCheck(char array[], int Rows, int Cols, int row, int winRequirement, char mark) {
int ticker = 0;
int col;
for (col = 0; col < Cols; col++) {
if (*flat_char_matrix(array, Rows, Cols, row, col)==mark) {
++ticker;
} else {
ticker = 0;
}
if(ticker >= winRequirement) return 1;
}
return 0;
}
int arrayVictoryCheck(char array[], int Rows, int Cols, int winRequirement, char mark) {
int row;
for (row = 0; row < Rows; row++) {
if (lineVictoryCheck(array, Rows, Cols, row, winRequirement, mark) == 1) return 1;
}
return 0;
}
int VictoryCheck(int winRequirement, char board[ROWS][COLS]) {
// Create 2 arrays to check for diagonal wins
char diagArray1[(ROWS + COLS - 1)*MIN];
char diagArray2[(ROWS + COLS - 1)*MIN];
char verticalArray[COLS*ROWS];
char flat_board[ROWS*COLS];
// Could eventually use CONSECUTIVE_MARKS_REQUIRED to make diagonal-
// check arrays smaller.
// And then fill them with blanks.
int i;
int kay;
int eye;
int markOneScore, markTwoScore;
int victoryCode = EPIC_FAIL;
for (i = 0; i < (ROWS + COLS - 1); i++) {
int k;
for (k = 0; k < MIN; k++) {
*flat_char_matrix(diagArray1, ROWS + COLS - 1, MIN, i, k ) = BLANK;
*flat_char_matrix(diagArray2, ROWS + COLS - 1, MIN, i, k ) = BLANK;
}
}
// Calculate diagArray1
// First set
for (i = 0; i < ROWS; i++) {
int k;
for (k = 0; (k <= i) && (k < COLS); k++) {
*flat_char_matrix(diagArray1, ROWS + COLS - 1, MIN, i, k ) = board[i-k][k];
}
}
// Second set
for (i = ROWS; i < (ROWS + COLS - 1); i++) {
int jay = i - ROWS + 1;
int k;
for (k = 0; (ROWS - k - 1 >= 0) && (k+jay < COLS); k++) {
*flat_char_matrix(diagArray1, ROWS + COLS - 1, MIN, i, k ) = board[ROWS-k-1][k+jay];
}
}
// Calculate diagArray2
// First set
i = 0;
for (kay = COLS - 1; kay >= 0; kay--) {
int k = 0;
int eye;
for (eye = 0; kay + eye < COLS; eye++) {
*flat_char_matrix(diagArray2, ROWS + COLS - 1, MIN, i, k ) = board[eye][kay + eye];
k++;
}
i++;
}
// Second set
for (eye = 1; eye < ROWS; eye++) {
int k = 0;
for (kay = 0; kay+eye < ROWS; kay++) {
*flat_char_matrix(diagArray2, ROWS + COLS - 1, MIN, i, k ) = board[kay + eye][kay];
k++;
}
i++;
}
#if TESTING
// print test of diagArray1
for (i = 0; i < (ROWS + COLS - 1); i++) {
int k;
for (k = 0; k < MIN; k++) {
printf("%c ", *flat_char_matrix(diagArray1, ROWS + COLS - 1, MIN, i, k ) );
}
printf("\n");
}
printf("\n");
// print test of diagArray2
for (i = 0; i < (ROWS + COLS - 1); i++) {
int k;
for (k = 0; k < MIN; k++) {
printf("%c ", *flat_char_matrix(diagArray2, ROWS + COLS - 1, MIN, i, k ) );
}
printf("\n");
}
printf("\n");
#endif
// Create an array to check for vertical wins
// Fill it with a transposed version of main board
for (i = 0; i < COLS; i++) {
int k;
for (k = 0; k < ROWS; k++)
*flat_char_matrix(verticalArray, COLS, ROWS, i, k ) = board[k][i];
}
#if TESTING
// print test of verticalArray
for (i = 0; i < COLS; i++) {
int k;
for (k = 0; k < ROWS; k++) {
printf("%c ", *flat_char_matrix(verticalArray, COLS, ROWS, i, k ) );
}
printf("\n");
}
printf("\n");
#endif
// Your Implementation Goes Here
markOneScore = 0;
markTwoScore = 0;
flatten_char_matrix(board, flat_board, ROWS, COLS);
markOneScore += arrayVictoryCheck(flat_board, ROWS, COLS, CONSECUTIVE_MARKS_REQUIRED, MARKONE);
markOneScore += arrayVictoryCheck(verticalArray, COLS, ROWS, CONSECUTIVE_MARKS_REQUIRED, MARKONE);
markOneScore += arrayVictoryCheck(diagArray1, ROWS + COLS -1, MIN, CONSECUTIVE_MARKS_REQUIRED, MARKONE);
markOneScore += arrayVictoryCheck(diagArray2, ROWS + COLS -1, MIN, CONSECUTIVE_MARKS_REQUIRED, MARKONE);
markTwoScore += arrayVictoryCheck(flat_board, ROWS, COLS, CONSECUTIVE_MARKS_REQUIRED, MARKTWO);
markTwoScore += arrayVictoryCheck(verticalArray, COLS, ROWS, CONSECUTIVE_MARKS_REQUIRED, MARKTWO);
markTwoScore += arrayVictoryCheck(diagArray1, ROWS + COLS -1, MIN, CONSECUTIVE_MARKS_REQUIRED, MARKTWO);
markTwoScore += arrayVictoryCheck(diagArray2, ROWS + COLS -1, MIN, CONSECUTIVE_MARKS_REQUIRED, MARKTWO);
for (i = 0; i < ROWS; i++) {
int k;
for (k = 0; k < COLS; k++) {
if (board[i][k]==BLANK) {
victoryCode=NOWIN;
k=COLS;
i=ROWS;
}
}
}
if (markTwoScore>0) {
victoryCode=MARKTWOVICTORY;
}
if (markOneScore>0) {
if (markTwoScore>0) {
victoryCode=ERROR;
} else {
victoryCode=MARKONEVICTORY;
}
}
if (victoryCode==EPIC_FAIL) {
victoryCode=TIE;
}
// Scan for remaining blanks
// Run a However-Many-In-a-Row function on all four arrays
return victoryCode;
}
int DisplayVictoryMessage(int victoryCode) {
// display the victory condition results
switch(victoryCode) {
case NOWIN:
printf("There is still no winner.\n");
return 1;
case MARKONEVICTORY:
printf("X has won the game!\n");
return 0;
case MARKTWOVICTORY:
printf("O has won the game!\n");
return 0;
case TIE:
printf("The game is a draw.\n");
return 0;
case ERROR:
printf("Something bad happened... X and O have both won. :(\n");
return 0;
case EPIC_FAIL:
printf("Something bad happened... VictoryCheck() has produced an impossible\ncombination of return code indicators.\n");
return 0;
default:
printf("DisplayVictoryMessage() was passed an invalid victoryCode.\n");
return 0;
}
}