-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.cpp
More file actions
155 lines (152 loc) · 3.67 KB
/
tictactoe.cpp
File metadata and controls
155 lines (152 loc) · 3.67 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
#include "pthread.h"
#include "iostream"
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "bits/stdc++.h"
#include "ctime"
#include "cstdlib"
using namespace std;
void printboard(int board[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board[i][j] == 0)
cout << "_";
else if (board[i][j] == 1)
cout << "X";
else
cout << "O";
}
cout << endl;
}
cout << endl;
}
bool checkwinner(int board[3][3], int& who)
{
if (board[0][0] == board[0][1] && board[0][0] == board[0][2] && board[0][0] != 0)
{
who = board[0][0];
return true;
}
else if (board[1][0] == board[1][1] && board[1][0] == board[1][2] && board[1][0] != 0)
{
who = board[1][0];
return true;
}
else if (board[2][0] == board[2][1] && board[2][0] == board[2][2] && board[2][0] != 0)
{
who = board[2][0];
return true;
}
else if (board[0][0] == board[1][0] && board[0][0] == board[2][0] && board[0][0] != 0)
{
who = board[0][0];
return true;
}
else if (board[0][1] == board[1][1] && board[0][1] == board[2][1] && board[0][1] != 0)
{
who = board[0][1];
return true;
}
else if (board[0][2] == board[1][2] && board[0][2] == board[2][2] && board[0][2] != 0)
{
who = board[0][2];
return true;
}
else if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != 0)
{
who = board[0][0];
return true;
}
else if (board[0][2] == board[1][1] && board[0][0] == board[2][0] && board[0][2] != 0)
{
who = board[0][2];
return true;
}
else
{
return false;
}
}
void findloc(int board[3][3], int& row, int& col, int opp)
{
bool winner = false;
int who;
for (int i = 0; i < 3 && winner == false; i++)
{
for (int j = 0; j < 3 && winner == false; j++)
{
if (board[i][j] == 0)
{
row = i;
col = j;
board[i][j] = opp;
winner = checkwinner(board, who);
board[i][j] = 0;
}
}
}
}
int main()
{
int m;
float n = 1;
bool winner = false;
int board[3][3] = { 0 };
printboard(board);
int player = 1, row, col, who;
for (int i = 0; i < 9 && winner == false; i++)
{
if (player == 1)
{
cout << "Press 0 to open someting else" << endl;
cin >> m;
if (m == 0)
{
return 0;
}
else
{
cout << "Enter row and column" << endl;
cin >> row >> col;
findloc(board, row, col, 2);
board[row][col] = 1;
player = 2;
}
}
else
{
cout << "Enter row and column" << endl;
cin >> row >> col;
while (board[row][col] != 0)
{
cout << "\nreenter\n";
cin >> row >> col;
}
board[row][col] = 2;
player = 1;
}
printboard(board);
winner = checkwinner(board, who);
if (winner)
{
cout << "the winner is player " << who << endl;
}
int L;
cout << "If you want to quit press 0" << endl;
cout << "Press 1 to continue" << endl;
cin >> L;
if (L == 0)
{
break;
}
else if (L == 1)
{
cout << "You may continue" << endl;
}
}
return 0;
}