-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5x5_tictactoe.c
257 lines (257 loc) · 7.03 KB
/
5x5_tictactoe.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
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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
char arr[5][5];
void startboard(){
int i,j;
for(i=0;i<5;i++){
for(j=0;j<5;j++){
arr[i][j]='-';
}
}
}
void print(){
printf("\nCurrent state of board:\n");
int i,j;
printf(" 1 2 3 4 5\n");
for(i=0;i<5;i++){
printf("%d",i+1);
for(j=0;j<5;j++){
printf("%c",arr[i][j]);
if(j<4){
printf("|");
}
}
printf("\n");
if(i<4){
printf(" ---------");
printf("\n");
}
}
}
int valid(int x,int y){
if(x<0||x>=5||y<0||y>=5){
return 0;
}
if(arr[x][y]!='-'){
return 0;
}
return 1;
}
int end(){
int i,j;
for (i=0;i<5;i++) {
if(arr[i][0]==arr[i][1]&&arr[i][1]==arr[i][2]&&arr[i][2]==arr[i][3]&&arr[i][3]!='-'){
return 1;
}
if(arr[0][i]==arr[1][i]&&arr[1][i]==arr[2][i]&&arr[2][i]==arr[3][i]&&arr[3][i]!='-'){
return 1;
}
}
if((arr[0][0]==arr[1][1]&&arr[1][1]==arr[2][2]&&arr[2][2]==arr[3][3]&&arr[3][3]!='-')||(arr[0][4]==arr[1][3]&&arr[1][3]==arr[2][2]&&arr[2][2]==arr[3][1]&&arr[3][1]!='-')){
return 1;
}
return 0;
}
int toss(){
srand(time(NULL));
int toss1=rand()%2+1;
return toss1;
}
int main(){
printf("WELCOME TO 5x5 TIC-TAC-TOE!\nby Kingsuk Panda\n\n\nThis is your tic-tac-toe board:\n\n");
startboard();
print();
printf("\n\nWhen asked for a position to play your move, enter the row and column for the position.\nWhen you score 4 consecutive Xs or Os, you win! Get ready for the game!!\n\nSelect your game mode(type in the option number):\n1. Player vs Player\n2. Player vs Computer\n\nMode: ");
int mode;
scanf("%d",&mode);
if(mode==1){
printf("\n\n\nMODE: PLAYER VS PLAYER\n\nThe LUCK GODS have decided that ");
int x,y;
char currentplayer;
int toss2=toss();
if(toss2==1){
currentplayer='X';
printf("player X goes first!\n\nPlayer X, enter the position where you want to place your X:\nRow: ");
scanf("%d",&x);
printf("\nColumn:");
scanf("%d",&y);
x--;
y--;
arr[x][y]=currentplayer;
currentplayer='O';
while(!end()){
print();
printf("\nPlayer %c, enter the position where you want to place your %c:\nRow: ",currentplayer,currentplayer);
scanf("%d",&x);
printf("\nColumn: ");
scanf("%d",&y);
x--;
y--;
if(valid(x,y)){
arr[x][y]=currentplayer;
if(currentplayer=='O'){
currentplayer='X';
}
else{
currentplayer='O';
}
}
else{
printf("\nINVALID POSITION OR MOVE!");
}
}
print();
if(end()==0){
printf("\nIt's a draw!!");
return 0;
}
printf("\nPlayer %c wins!!",currentplayer);
}
else{
currentplayer='O';
printf("player O goes first!\n\nPlayer O, enter the position where you want to place your X:\nRow: ");
scanf("%d",&x);
printf("\nColumn:");
scanf("%d",&y);
x--;
y--;
arr[x][y]=currentplayer;
currentplayer='X';
while(!end()){
print();
printf("\nPlayer %c, enter the position where you want to place your %c:\nRow: ",currentplayer,currentplayer);
scanf("%d",&x);
printf("\nColumn: ");
scanf("%d",&y);
x--;
y--;
if(valid(x,y)){
arr[x][y]=currentplayer;
if(currentplayer=='O'){
currentplayer='X';
}
else{
currentplayer='O';
}
}
else{
printf("\nINVALID POSITION OR MOVE!");
}
}
print();
if(end()==0){
printf("\nIt's a draw!!");
return 0;
}
printf("\nPlayer %c wins!!",currentplayer);
}
}
else{
printf("\n\n\nMODE: PLAYER (X) VS COMPUTER (O)\n\nThe LUCK GODS have decided that ");
int x,y;
char currentplayer;
int toss2=toss();
if(toss2==1){
currentplayer='X';
printf("player (X) goes first!\n\nPlayer, enter the position where you want to place your X:\nRow: ");
scanf("%d",&x);
printf("\nColumn:");
scanf("%d",&y);
x--;
y--;
arr[x][y]='X';
currentplayer='O';
while(!end()){
print();
if(currentplayer=='X'){
printf("\nPlayer, enter the position where you want to place your X:\nRow: ");
scanf("%d",&x);
printf("\nColumn: ");
scanf("%d",&y);
}
else{//computer er turn ta ekhane dibo
x=rand()%5+1;
y=rand()%5+1;
printf("\nComputer's move:\nRow: %d\nColumn: %d\n",x,y);
}
x--;
y--;
if(valid(x,y)){
arr[x][y]=currentplayer;
if(currentplayer=='O'){
currentplayer='X';
}
else{
currentplayer='O';
}
}
else{
printf("\nINVALID POSITION OR MOVE!");
}
}
print();
if(end()==0){
printf("\nIt's a draw!!");
return 0;
}
if(currentplayer=='O'){
printf("\nPlayer wins!!");
}
else{
printf("\nComputer wins!!");
}
}
else{
currentplayer='O';
printf("Computer (O) goes first!");
x=rand()%5+1;
y=rand()%5+1;
x--;
y--;
printf("\nComputer's move:\nRow: %d\nColumn: %d\n",x,y);
arr[x][y]='O';
print();
currentplayer='X';
while(!end()){
if(currentplayer=='X'){
printf("\nPlayer, enter the position where you want to place your %c:\nRow: ",currentplayer);
scanf("%d",&x);
printf("\nColumn: ");
scanf("%d",&y);
}
else{//computer er turn ta ekhane dibo
x=rand()%5+1;
y=rand()%5+1;
printf("\nComputer's move:\nRow: %d\nColumn: %d\n",x,y);
}
x--;
y--;
if(valid(x,y)){
arr[x][y]=currentplayer;
if(currentplayer=='O'){
currentplayer='X';
}
else{
currentplayer='O';
}
}
else{
printf("\nINVALID POSITION OR MOVE!");
}
print();
}
if(end()==0){
printf("\nIt's a draw!!");
return 0;
}
if(currentplayer=='O'){
printf("\nPlayer wins!!");
}
else{
printf("\nComputer wins!!");
}
}
}
return 0;
}