-
Notifications
You must be signed in to change notification settings - Fork 0
/
TikTakToe.txt
133 lines (121 loc) · 2.79 KB
/
TikTakToe.txt
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
#include<iostream>
#include<Windows.h>
#include<iomanip>
#include<time.h>
using namespace std;
char board[9] = { '-','-', '-', '-', '-', '-', '-', '-', '-' };//массив поля
void menu();
void SetColor(int color);
void View_Board()
{
cout << "Game Desk" << endl;
cout << "|-" << board[6] << "-|-" << board[7] << "-|-" << board[8] << "-|" << endl;
cout << "|-" << board[3] << "-|-" << board[4] << "-|-" << board[5] << "-|" << endl;
cout << "|-" << board[0] << "-|-" << board[1] << "-|-" << board[2] << "-|" << endl;
}
bool has_won(char player)
{
int wins[][3] = { {0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6} };
for (int i = 0; i < 8; i++)
{
int count = 0;//для підрахунку виграшних елементів
for (int j = 0; j < 3; j++)
{
if (board[wins[i][j]] == player)
{
count++;
}
if (count == 3)
{
return true;
}
}
}
return false;
}
void Navigator()
{
cout << "Navigator" << endl;
cout << "|-7-|-8-|-9-|" << endl;
cout << "|-4-|-5-|-6-|" << endl;
cout << "|-1-|-2-|-3-|" << endl;
}
int get_move()//введення числа та перевірка чи воно правильне
{
system("cls");
menu();
Navigator();
View_Board();
int move;
cout << "Make move,please!!!" << endl;
cin >> move;
while (move > 9 || move < 1 || board[move - 1] != '-') //перевірка введеного числа
{
cout << "Enter move again (1-9)\n";
cin >> move;
}
return move;
}
char play()//игра
{
int turn = 0;
while (!has_won('X')&& !has_won('O') && turn < 9)
{
int move = get_move();
if (turn % 2 == 0)
{
board[move - 1] = 'X';
if (has_won('X'))
{
cout << "Congratulation player X! You have won" << endl;
return 'X';
}
}
else
{
board[move - 1] = 'O';
if (has_won('O'))
{
cout << "Congratulation player O! You have won" << endl;
return 'O';
}
}
turn++;
}
cout << "Draw" << endl;
return 'D';
}
void main()
{
play();
system("Title TikTakToe");
system("pause");
}
void SetColor(int color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
}
void menu()
{
SetColor(3); cout << "\t\t\t\t" << char(201);
for (int i = 0; i < 40; i++)
{
cout << char(205);
}
SetColor(3); cout << char(187) << endl;
cout << "\t\t\t\t" << char(186) << "\t\t\t\t\t " << char(186) << endl;
cout << "\t\t\t\t" << char(186) << "\t\t\t\t\t " << char(186) << endl;
cout << "\t\t\t\t" << char(186);
SetColor(10); cout << "\t TikTak\t\t\t ";
SetColor(3);
cout << char(186) << endl;
cout << "\t\t\t\t" << char(186) << "\t\t\t\t\t " << char(186) << endl;
cout << "\t\t\t\t" << char(186) << "\t\t\t\t\t " << char(186) << endl;
cout << "\t\t\t\t" << char(200);
for (int i = 0; i < 40; i++)
{
cout << char(205);
}
cout << char(188) ;
SetColor(10); cout << endl;
}