-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTicTacToc.cpp
More file actions
122 lines (108 loc) · 2.57 KB
/
TicTacToc.cpp
File metadata and controls
122 lines (108 loc) · 2.57 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
#include <iostream>
#include <conio.h>
using namespace std;
void printInputMatrix();
void printBoard();
int addMark();
int check();
char board[3][3] = {' ',' ',' ',
' ',' ',' ',
' ',' ',' '};
int turn = 1; //1-Player 1 | 0-Player 2
char mark = 'O'; //O-Player 1 | X-Player 2
int input;
int main(){
int won = 0;
int validInput = 0;
for(int i=0; i<9; i++){
system("cls");
printBoard();
if(turn) cout<<"Player 1 Turn (Symbol: O)"<<endl;
else cout<<"Player 2 Turn (Symbol: X)"<<endl;
printInputMatrix();
cout<<"Enter Input from Input Matrix: ";
cin>>input;
while(input<0 || input>9){
cout<<"Invalid Input. Please Re-Enter input: ";
cin>>input;
}
if(turn) mark = 'O';
else mark = 'X';
validInput = addMark();
if( !validInput ){
i--;
continue;
}
won = check();
if( won){
system("cls");
printBoard();
if(turn) cout<<endl<<"*** Player 1 - You Won ***";
else cout<<endl<<"*** Player 2 - You Won ***";
getch();
break;
}
if(i==8){
system("cls");
printBoard();
cout<<endl<<"*** MATCH DRAW ***";
}
turn = !turn;
}
system("cls");
return 0;
}
void printInputMatrix(){
cout<<endl<<endl<<"INPUT MATRIX"<<endl;
cout<<" 1 | 2 | 3 "<<endl;
cout<<"------------"<<endl;
cout<<" 4 | 5 | 6 "<<endl;
cout<<"------------"<<endl;
cout<<" 7 | 8 | 9 "<<endl;
}
void printBoard(){
cout<<" "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<" "<<endl;
cout<<"------------"<<endl;
cout<<" "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<" "<<endl;
cout<<"------------"<<endl;
cout<<" "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<" "<<endl;
}
int addMark(){
for(int i=0,k=1; i<3; i++ ){
for(int j=0; j<3; j++,k++){
if( k==input )
if(board[i][j] == ' '){
board[i][j] = mark;
return 1;
}
else{
cout<<"Invalid Input";
getch();
return 0;
}
}
}
}
int check(){
//checking rows
if(board[0][0]==mark && board[0][1]==mark && board[0][2]==mark)
return 1;
if(board[1][0]==mark && board[1][1]==mark && board[1][2]==mark)
return 1;
if(board[2][0]==mark && board[2][1]==mark && board[2][2]==mark)
return 1;
//checking Columns
if(board[0][0]==mark && board[1][0]==mark && board[2][0]==mark)
return 1;
if(board[0][1]==mark && board[1][1]==mark && board[2][1]==mark)
return 1;
if(board[0][2]==mark && board[1][2]==mark && board[2][2]==mark)
return 1;
//checking diagonals
if(board[0][0]==mark && board[1][1]==mark && board[2][2]==mark)
return 1;
if(board[0][2]==mark && board[1][1]==mark && board[2][0]==mark)
return 1;
return 0;
}
//TIC TAC TOC Game using c++