-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.cpp
More file actions
183 lines (143 loc) · 5.64 KB
/
tictactoe.cpp
File metadata and controls
183 lines (143 loc) · 5.64 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
#include <iostream>
#include <stdlib.h>
using namespace std;
const char Pcharx = 'X';
const char Pcharo = 'O';
void OutPut(char tictac[3][3]);
void TakeInput(int &turn, char tictac[3][3], char Pcharo, char Pcharx, char &position);
void SwitchCoordinates(char &position, char tictac[3][3], char Pcharo, char Pcharx, int &turn);
void WinCondition(char tictac[3][3], char Pcharo, char Pcharx, int &WinCheck);
int main() {
int turnno = 0, turn, WinCheck = 0;
char position, tictac[3][3] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
turn = (rand()%2) + 1 ;
cout<<"After A Coin Flip It Was Decided Player "<<turn<<" Will Play First!\n\n";
do {
TakeInput(turn, tictac, Pcharo, Pcharx, position);
SwitchCoordinates(position, tictac, Pcharo, Pcharx, turn);
turnno = turnno + 1;
if (turn ==1) {
turn++;
}
else if (turn == 2) {
turn--;
}
WinCondition(tictac,Pcharo, Pcharx,WinCheck);
} while (WinCheck != 1 && WinCheck != 2 && turnno < 9);
if (turnno == 9) {
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\nThe match has ended in a draw!\n\n"<<endl;
OutPut(tictac);
}
if (WinCheck == 1) {
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\nPlayer 1 has won the game!\n-------------------------------------------------------------------\n";
OutPut(tictac);
cout<<"\n\n***CONGRATULATIONS!!!***\n";
} else if (WinCheck == 2) {
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\nPlayer 2 has won the game!\n-------------------------------------------------------------------\n";
OutPut(tictac);
cout<<"\n\n***CONGRATULATIONS!!!***\n";
}
return 0;
}
void OutPut(char tictac[3][3]){
int i, j;
for (i = 0; i < 3; i++){
for (j = 0; j < 3; j++){
if(j == 0){
cout<<" ";
}
cout<<tictac[i][j];
if (j < 2){
cout<<" | ";
}
}
cout<<"\n";
if(i < 2){
cout<<" ---|---|--- ";
}
cout<<"\n";
}
}
void TakeInput(int &turn, char tictac [3][3], char Pcharo, char Pcharx, char &position){
string coordinates = " A | B | C \n ---|---|--- \n D | E | F \n ---|---|--- \n G | H | I ";
if (turn == 1) {
cout<<"It's Player "<<turn<<"'s turn! Enter a letter for the position for "<<Pcharo<<"\n\n";
OutPut(tictac);
cout<<endl<<coordinates<<"\n\nEnter coordinates from A - I: ";
cin>>position;
}
if (turn == 2) {
cout<<"It's Player "<<turn<<"'s turn! Enter a letter for the position for "<<Pcharx<<"\n\n";
OutPut(tictac);
cout<<endl<<coordinates<<"\n\nEnter coordinates from A - I: ";
cin>>position;
}
}
void SwitchCoordinates(char &position, char tictac[3][3], char Pcharo, char Pcharx, int &turn) {
if(turn == 1){
if (position == 'A' || position == 'a'){
tictac[0][0] = Pcharo;
}
else if (position == 'B' || position == 'b'){
tictac[0][1] = Pcharo;
}
else if (position == 'C' || position == 'c'){
tictac[0][2] = Pcharo;
}
else if (position == 'D' || position == 'd'){
tictac[1][0] = Pcharo;
}
else if (position == 'E' || position == 'e'){
tictac[1][1] = Pcharo;
}
else if (position == 'F' || position == 'f'){
tictac[1][2] = Pcharo;
}
else if (position == 'G' || position == 'g'){
tictac[2][0] = Pcharo;
}
else if (position == 'H' || position == 'h'){
tictac[2][1] = Pcharo;
}
else if (position == 'I' || position == 'i'){
tictac[2][2] = Pcharo;
}
}
else if (turn == 2){
if (position == 'A' || position == 'a'){
tictac[0][0] = Pcharx;
}
else if (position == 'B' || position == 'b'){
tictac[0][1] = Pcharx;
}
else if (position == 'C' || position == 'c'){
tictac[0][2] = Pcharx;
}
else if (position == 'D' || position == 'd'){
tictac[1][0] = Pcharx;
}
else if (position == 'E' || position == 'e'){
tictac[1][1] = Pcharx;
}
else if (position == 'F' || position == 'f'){
tictac[1][2] = Pcharx;
}
else if (position == 'G' || position == 'g'){
tictac[2][0] = Pcharx;
}
else if (position == 'H' || position == 'h'){
tictac[2][1] = Pcharx;
}
else if (position == 'I' || position == 'i'){
tictac[2][2] = Pcharx;
}
}
}
void WinCondition(char tictac[3][3], char Pcharo, char Pcharx, int &WinCheck){
if((tictac[0][0] == Pcharo && tictac [1][1] == Pcharo && tictac[2][2] == Pcharo) || (tictac[0][2] == Pcharo && tictac [1][1] == Pcharo && tictac[2][0] == Pcharo) || (tictac[0][0] == Pcharo && tictac [0][1] == Pcharo && tictac[0][2] == Pcharo) || (tictac[1][0] == Pcharo && tictac [1][1] == Pcharo && tictac[1][2] == Pcharo) || (tictac[2][0] == Pcharo && tictac [2][1] == Pcharo && tictac[2][2] == Pcharo) || (tictac[0][0] == Pcharo && tictac [1][0] == Pcharo && tictac[2][0] == Pcharo) || (tictac[0][1] == Pcharo && tictac [1][1] == Pcharo && tictac[2][1] == Pcharo) || (tictac[0][2] == Pcharo && tictac [1][2] == Pcharo && tictac[2][2] == Pcharo)) {
WinCheck = 1;
} else
if((tictac[0][0] == Pcharx && tictac [1][1] == Pcharx && tictac[2][2] == Pcharx) || (tictac[0][2] == Pcharx && tictac [1][1] == Pcharx && tictac[2][0] == Pcharx) || (tictac[0][0] == Pcharx && tictac [0][1] == Pcharx && tictac[0][2] == Pcharx) || (tictac[1][0] == Pcharx && tictac [1][1] == Pcharx && tictac[1][2] == Pcharx) || (tictac[2][0] == Pcharx && tictac [2][1] == Pcharx && tictac[2][2] == Pcharx) || (tictac[0][0] == Pcharx && tictac [1][0] == Pcharx && tictac[2][0] == Pcharx) || (tictac[0][1] == Pcharx && tictac [1][1] == Pcharx && tictac[2][1] == Pcharx) || (tictac[0][2] == Pcharx && tictac [1][2] == Pcharx && tictac[2][2] == Pcharx)) {
WinCheck = 2;
}
}