-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic-tac.cpp
More file actions
101 lines (83 loc) · 2.6 KB
/
tic-tac.cpp
File metadata and controls
101 lines (83 loc) · 2.6 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
#include<iostream>
#include<string>
char array[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
int choice;
int row,column;
char turn='x';
bool draw=false;
void box(){
std::cout<<array[0][0]<<"|"<<array[0][1]<<"|"<<array[0][2]<<std::endl;
std::cout<<"------------"<<std::endl;
std::cout<<array[1][0]<<"|"<<array[1][1]<<"|"<<array[1][2]<<std::endl;
std::cout<<"------------"<<std::endl;
std::cout<<array[2][0]<<"|"<<array[2][1]<<"|"<<array[2][2]<<std::endl;
std::cout<<"------------"<<std::endl;
}
void selection(){
if(turn=='x'){
std::cout<<"Player 1 turn\n";
}
else if (turn=='o')
{
std::cout<<"Player 2 turn\n";
}
std::cout<<"Enter the position:\n";
std::cin>>choice;
switch(choice)
{
case 1: row=0; column=0; break;
case 2: row=0; column=1; break;
case 3: row=0; column=2; break;
case 4: row=1; column=0; break;
case 5: row=1; column=1; break;
case 6: row=1; column=2; break;
case 7: row=2; column=0; break;
case 8: row=2; column=1; break;
case 9: row=2; column=2; break;
default:
std::cout<<"Invalid Move";
}
if( (turn=='x')&&array[row][column] != 'x' && array[row][column] != 'o'){
array[row][column]='x';
turn='o';
}
else if((turn=='o')&&array[row][column] != 'x' && array[row][column] != 'o')
{
array[row][column]='o';
turn='x';
}
else{
std::cout<<"Already filled!";
selection();
}
box();
}
bool gameover()
{
for(int i=0; i<3; i++)
if(array[i][0] == array[i][1] && array[i][0] == array[i][2] || array[0][i] == array[1][i] && array[0][i] == array[2][i])
return false;
if(array[0][0]==array[1][1]&&array[0][0]==array[2][2]||array[0][2]==array[1][1]&&array[0][2]==array[2][0])
return false;
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
if(array[i][j] != 'X' && array[i][j] != 'O')
return true;
draw = true;
return false;
}
int main(){
while(gameover()){
box();
selection();
gameover();
}
if(turn == 'x' && draw == false){
std::cout<<"nnCongratulations!Player 'O' has won the game";
}
else if(turn == 'o' && draw == false){
std::cout<<"nnCongratulations!Player 'X' has won the game";
}
else
std::cout<<"nnGAME DRAW!!!nn";
}