-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_tictactoe.cpp
151 lines (116 loc) · 3.07 KB
/
dynamic_tictactoe.cpp
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
/*
* TicTacToe.c
* Date: 23/05/2022
* Author: RikkoRicardo
*/
#include <stdio.h>
#include <String.h>
#include <stdlib.h>
#define TABLE_SIZE 3
/*
if you subtract one and add another
you have one diagonal, if you use
the one that goes from 0 to TABLE_SIZE
you get another
0,0| * |0,2
--------
*|1,1|*
--------
2,0|* |2,2
*/
int searchDiagonal(char board[TABLE_SIZE][TABLE_SIZE],char ch){
int x=0,y=0,diagonalScore[2] = {0,0};
for(x = 0, y = TABLE_SIZE-1; x <=TABLE_SIZE-1;x++,y--){
if(board[x][y] == ch && x+y == TABLE_SIZE)
diagonalScore[1]++;
if(board[x][x] == ch && x+y == TABLE_SIZE)
diagonalScore[0]++;
}
return (diagonalScore[0] >= TABLE_SIZE || diagonalScore[1] >= TABLE_SIZE);
}
int searchVertical(char board[][TABLE_SIZE],int index,char ch){
//Checks the placed mark's column
int i=0,counter=0;
while(board[i++][index] == ch)
counter++;
return counter == TABLE_SIZE;
}
/*
searchHorizontal is too redundant,
can be optimized easily using:
char v[] = {ch};
return strcspn(board,v) >=3;
Which works but i need to rewrite the change turn function
since theres a weird bug that gives the win to the wrong player
*/
int searchHorizontal(char board[],char ch){
//Compares the 3 char bytes for duplicates
char temp[TABLE_SIZE];
memset(temp,ch,TABLE_SIZE);
return !memcmp(board,temp,TABLE_SIZE * sizeof(char));
}
void resetboard(char board[TABLE_SIZE][TABLE_SIZE]){
//just clears the entire board
memset(board,' ',TABLE_SIZE * TABLE_SIZE);
}
void showboard(char board[TABLE_SIZE][TABLE_SIZE]){
int boardY,boardX;
for(boardY = 0; boardY < TABLE_SIZE; boardY++){
//Draw Two elements of the board, check if it needs separation
for(boardX = 0; boardX < TABLE_SIZE; boardX++)
printf("%c %c", board[boardY][boardX], boardX != TABLE_SIZE-1 ? '|':' ') ;
//Check if last and draw line
if(boardY != TABLE_SIZE-1)
printf("\n--------\n");
}
putchar('\n');
}
const char SPACE = ' ';
short turnNumber = 0;
char ch = 'X';
int main(int argc, char *argv[])
{
//Table size limit
if(TABLE_SIZE < 3)
return 1;
//initiate and populate the board
char board[TABLE_SIZE][TABLE_SIZE];
resetboard(board);
//initialize all game variables
char message[50] = "TicTacToe time!\n";
short row,col;
do{
//clear console and Show UI
system ("CLS");
puts(message);
showboard(board);
//Ask for input
printf("\nPick two cordinates (x, y): ");
scanf("%hu %hu",&row,&col);
//Check if the position is picked
if(board[row-1][col-1] != SPACE){
strcpy(message,"Position picked, try again!\n");
//"continue" resets the while loop
continue;
}
//Draw the player choice
board[row-1][col-1] = ch;
//Check for a win
if(searchHorizontal(board[row-1],ch))
break;
if(searchVertical(board,col-1,ch))
break;
if(searchDiagonal(board,ch)){
break;
}
//Change player turn
ch = (ch == 'O') ? 'X' : 'O';
}while(++turnNumber != sizeof(board));
// end game when the board is full;
if(turnNumber == sizeof(board)){
printf("Tie!");
return 0;
}
printf("\n%c won in %d turns!\n",ch,turnNumber);
return 0;
}