-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
103 lines (102 loc) · 1.92 KB
/
Source.cpp
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
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
void showMap(int map[][3], int SIZE_ROW, int SIZE_COL);
bool gameOver(int map[][3],int SIZE_ROW, int SIZE_COL, int valum);
void moveComputer(int map[][3], int SIZE_ROW, int SIZE_COL);
int main()
{
setlocale(LC_ALL, "ru");
const int SIZE_ROW = 3;
const int SIZE_COL = 3;
int map[SIZE_ROW][SIZE_COL] = {};
int x = 0, y = 0;
int choose = 0;
showMap(map,SIZE_ROW,SIZE_COL);
while (true)
{
cout << " enter the coordinates to make a move :\n";
//j i
cin >> x >> y;
//j i
if(map[x][y]!=2)
map[x][y] = 1;
showMap(map,SIZE_ROW,SIZE_COL);
if (gameOver( map,SIZE_ROW,SIZE_COL,1)==true)
{
cout << " you won !!!\n\n";
break;
}
cout << " computer running :\n";
moveComputer(map, SIZE_ROW, SIZE_COL);
showMap(map, SIZE_ROW, SIZE_COL);
if (gameOver(map, SIZE_ROW, SIZE_COL, 2) == true)
{
cout << " you lose :\n";
break;
}
}
}
void showMap(int arr[][3],int SIZE_ROW,int SIZE_COL)
{
for (int i = 0; i < SIZE_ROW; i++)
{
for (int j = 0; j < SIZE_COL; j++)
{
cout << arr[j][i] << "\t";
}
cout << endl << endl;
}
}
bool gameOver(int map[][3], int SIZE_ROW, int SIZE_COL, int valum)
{
int countRow = 0;
int countCol = 0;
int countDiag = 0;
int countDiag2 = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (map[j][i] == valum)
{
countRow++;
}
if (map[i][j] == valum)
{
countCol++;
}
if (map[j][j] == valum)
{
countDiag++;
}
if (map[2 - j][j] == valum)
{
countDiag2++;
}
}
if (countRow == 3 || countCol == 3 || countDiag == 3 || countDiag2 == 3)
{
return true;
}
countRow = 0;
countCol = 0;
countDiag = 0;
countDiag2 = 0;
return false;
}
}
void moveComputer(int map[][3], int SIZE_ROW, int SIZE_COL)
{
int x = 0, y = 0;
do
{
srand(time(0));
x = rand() % 3;
y = rand() % 3;
} while (map[x][y] != 0);
{
map[x][y] = 2;
}
}