-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
412 lines (342 loc) · 9.27 KB
/
main.c
File metadata and controls
412 lines (342 loc) · 9.27 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//COMP120 TicTacToe Project - Kayla (Max) Hynes - 03/25/24
// https://replit.com/@max295/TicTacToe#main.c
/*
Welcome to my feild programmable two-dimensional TTT game, witten in C.
While it is not perfect, I thought it would be interesting to exceed the project specifications to design arbitrary dimenions into the program, and build all of the functions accordingly.
*/
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Constants for TTT game board symbols. */
#define EMPTY ' '
#define USER 'X'
#define COMPUTER 'O'
const int x2max = 46340;
#define TRUE 1
#define FALSE 0
/* function prototypes
*/
// Game engine function protos'
void initializeStrings(int arrLength, char gameArr[]);
void renderTemplateBoard(int width, int height);
void renderBoardState(int width, int height, char gameArr[]);
void displayRows(int arrLength, char gameArr[]);
char isWinCondition(int width, int height, char gameArr[]);
int gameOver(char gameArr[], int addressMax);
int userMove(char gameArr[], int addressMax);
int aiMove(char gameArr[], int addressMax);
/* I/O functions*/
void displayHorizontalLine();
int getIntInputInRange(int min, int max);
int valueInRange(int value, int min, int max);
int getIntInput();
void clearInputStream();
int userSaysYes(char[]);
char getCharInput();
// Main Function
int main() {
printf("Welcome to the Tic-Tac-Toe Game!\n\n\n");
// Instantiate vars
printf("Please specify number of rows: \n");
// Get desired rows
int rows = getIntInputInRange(1, x2max);
printf("Please specify number of columns: \n");
//Get desired columns
int columns = getIntInputInRange(1, x2max);
printf("\nBoard dimesnions sucessfully set! \n");
// Calculate game memory alloc
int addressMax = (rows * columns);
//Allocate memory
char gameArr[addressMax];
char input = '\0';
int gameState = 0;
//Clear game array
initializeStrings(addressMax, gameArr);
//Render template board
renderTemplateBoard(columns, rows);
//Being main game loop
while(1){
//Get first turn
printf("\nWould you like to go first? (Y/N): ");
input = getCharInput();
if(input == ('N') || input == ('n')) {
aiMove(gameArr, addressMax);
renderBoardState(columns, rows, gameArr);
}
//Begin in-play loop until win/draw
while(1) {
renderTemplateBoard(columns, rows);
printf("Please choose a position on the board to place your mark.\n");
userMove(gameArr, addressMax);
renderBoardState(columns, rows, gameArr);
if(isWinCondition(columns, rows, gameArr) == 'X'){
printf("\nYou win!");
break;
}
aiMove(gameArr, addressMax);
printf("\nAI has moved");
renderBoardState(columns, rows, gameArr);
if(isWinCondition(columns, rows, gameArr) == 'O'){
printf("\nAI wins!");
break;
}
if(gameOver(gameArr, addressMax) == 0){
printf("\nGame Over, nobody won! xD");
break;
}
}
//Kill game if finsihed
printf("\nWould you like to play again? (Y/N): ");
input = getCharInput();
if(input == ('N') || input == ('n')) {
break;
}
initializeStrings(addressMax, gameArr);
}
}
//Function Definitions:
//If all spaces taken, kill game
int gameOver(char gameArr[], int addressMax){
int vacantSpaces = 0;
for(int i = 0; i <= addressMax; i++){
if(gameArr[i] == ' '){
vacantSpaces++;
}
}
if(vacantSpaces > 0){
return 1;
}
else {
return 0;
}
}
//If game still vaid, permit user to move
int userMove(char gameArr[], int addressMax) {
while(gameOver(gameArr, addressMax) == 1) {
int userPosition = getIntInputInRange(0, addressMax - 1);
if (gameArr[userPosition] == EMPTY) {
gameArr[userPosition] = USER;
printf("You have successfully chosen position %d.\n", userPosition);
return 1;
}
else {
printf("That position is already taken. Please choose another position.\n");
}
}
return(0);
}
//Permit
int aiMove(char gameArr[], int addressMax) {
int computerPosition;
while(gameOver(gameArr, addressMax) == 1) {
computerPosition = rand() % addressMax;
if (gameArr[computerPosition] == EMPTY) {
gameArr[computerPosition] = COMPUTER;
return 1;
break;
}
}
}
void initializeStrings(int arrLength, char gameArr[]){
for(int i = 0; i < arrLength; i++) {
gameArr[i] = ' ';
}
}
char getCharInput() {
char input;
input = getchar();
while (input == '\n') {
input = getchar();
}
return input;
}
int getIntInput() {
int num;
char term;
while (scanf("%d%c", &num, &term) != 2 || term != '\n') {
printf("\nImproper input. Please enter an integer: ");
clearInputStream();
}
return num;
}
int valueInRange(int value, int min, int max) {
/* Your code goes here */
if (value >= min && value <= max) {
return TRUE;
}
else {
return FALSE;
}
}
//Get int from user and validate
int getIntInputInRange(int min, int max) {
int i;
printf("Please enter a value between %d and %d: ", min, max);
while(1){
i = getIntInput();
if (valueInRange(i, min, max)) {
return i;
break;
}
else {
printf("Invalid input range! Please enter a value between %d and %d: ", min, max);
}
}
}
// Detects if board is in a win state, and returns winner if true.
char isWinCondition(int width, int height, char gameArr[]) {
int oCount = 0;
int xCount = 0;
//Check for horizontal wins
for (int i = 0; i <= height; i++){
// Re-Zero counters
oCount = 0;
xCount = 0;
//Generate Y-offset
int yPos = i * width;
for (int j = 0; j < width; j++){
if(gameArr[(yPos + j)] == 'O') {
oCount++;
}
if(gameArr[(yPos + j)] == 'X'){
xCount++;
}
if (oCount == width) {
printf("O Wins with a horizontal of %d Spaces!", oCount);
return 'O';
}
if (xCount == width) {
printf("X Wins with a horizontal of %d Spaces!", xCount);
return 'X';
}
}
}
// Check for vertical wins
for (int i = 0; i <= width; i++){
// Re-Zero counters
oCount = 0;
xCount = 0;
for (int j = 0; j < height; j++){
if(gameArr[(i + (j*width))] == 'O') {
oCount++;
}
if(gameArr[(i + (j*width))] == 'X') {
xCount++;
}
if (oCount == height) {
printf("O Wins with a vertical of %d Spaces!", oCount);
return 'O';
}
if (xCount == height) {
printf("X Wins with a vertical of %d Spaces!", xCount);
return 'X';
}
}
}
// Check for diagonal wins
// Check for right diagonal wins
for (int i = 0; i <= (height - width); i++){
// Re-Zero counters
oCount = 0;
xCount = 0;
for (int j = 0; j < width; j++){
//Generate y-offset
int yPos = ((i * width) + (j * width + j));
if(gameArr[yPos] == 'O') {
oCount++;
}
if(gameArr[yPos] == 'X') {
xCount++;
}
if (oCount == width) {
printf("O Wins with a right diagonal of %d Spaces!", oCount);
return 'O';
}
if (xCount == width) {
printf("X Wins with a right diagonal of %d Spaces!", xCount);
return 'X';
}
}
}
// Check for left diagonal wins.
// Re-Zero counters
for (int i = 0; i <= (height - width); i++){
// Re-Zero counters
oCount = 0;
xCount = 0;
for (int j = 0; j < width; j++){
//Generate y-offset
int yPos = ((i * width) + ((j * width) + width - j - 1));
if(gameArr[yPos] == 'O') {
oCount++;
}
if(gameArr[yPos] == 'X') {
xCount++;
}
if (oCount == width) {
printf("O Wins with a left diagonal of %d Spaces!", oCount);
return 'O';
}
if (xCount == width) {
printf("X Wins with a left diagonal of %d Spaces!", xCount);
return 'X';
}
}
}
return 0;
}
//It display's lines horizontally lol
void displayHorizontalLines(int width) {
printf("\n");
for(int i = 0; i < width; i++) {
printf("------");
}
printf("\n");
}
// Renders gameboard from gameArr[] array to shell
void renderBoardState(int width, int height, char gameArr[]) {
for (int i = 0; i < height; i++) {
displayHorizontalLines(width);
int offset = i * width;
for (int z = 0; z < width; z++) {
if (gameArr[offset + z] == ' ') {
printf(" ");
}
else {
printf(" %c ", gameArr[offset + z]);
}
if (z < width - 1)
printf("|");
}
}
displayHorizontalLines(width);
}
// Render address display board to shell
void renderTemplateBoard(int width, int height) {
int address;
for (int i = 0; i < height; i++) {
displayHorizontalLines(width);
for (int j = 0; j < width; j++) {
address = (i * width + j);
printf(" %d ", address);
if (j < width - 1) {
if(address < 10) {
printf(" |");
}
else {
printf("|");
}
}
}
}
displayHorizontalLines(width);
}
//Clears input
void clearInputStream() {
char input = getchar();
while (input != '\n') {
input = getchar();
}
}