-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield2.cpp
More file actions
129 lines (90 loc) · 3.29 KB
/
field2.cpp
File metadata and controls
129 lines (90 loc) · 3.29 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
#include "field2.h"
#include <iostream>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
using namespace std;
Field2::Field2()
{
BOARD_SIZE = 16;
for (int i = 0; i < BOARD_SIZE; i++)
board.push_back(0);
}
static char convertPlayerChar(int n)
{
if (n == 1)
return 'X';
if (n == 2)
return 'O';
return ' ';
}
void Field2::updateBoard()
{
cout << endl << endl;
cout << "14(" << convertPlayerChar(board[13]) << ")---------15(" << convertPlayerChar(board[14]) << ")--------16(" << convertPlayerChar(board[15]) << ")" << endl
<< " | | |" << endl
<< " | | |" << endl
<< " | 11(" << convertPlayerChar(board[10]) << ")--12(" << convertPlayerChar(board[11]) << ")--13(" << convertPlayerChar(board[12]) << ") |" << endl
<< " | | | |" << endl
<< " 7(" << convertPlayerChar(board[6]) << ")---8(" << convertPlayerChar(board[7]) << ") 9(" << convertPlayerChar(board[8]) << ")-10(" << convertPlayerChar(board[9]) << ")" << endl
<< " | | | |" << endl
<< " | 4(" << convertPlayerChar(board[3]) << ")---5(" << convertPlayerChar(board[4]) << ")---6(" << convertPlayerChar(board[5]) << ") |" << endl
<< " | | |" << endl
<< " | | |" << endl
<< " 1(" << convertPlayerChar(board[0]) << ")----------2(" << convertPlayerChar(board[1]) << ")---------3(" << convertPlayerChar(board[2]) << ")" << endl;
}
bool Field2::checkForWining(int n)
{
if ((board)[0] == n && (board)[1] == n && (board)[2] == n || (board)[3] == n && (board)[4] == n && (board)[5] == n || (board)[10] == n && (board)[11] == n && (board)[12] == n || (board)[13] == n && (board)[14] == n && (board)[15] == n || (board)[0] == n && (board)[6] == n && (board)[13] == n || (board)[3] == n && (board)[7] == n && (board)[10] == n || (board)[5] == n && (board)[8] == n && (board)[12] == n || (board)[2] == n && (board)[9] == n && (board)[15] == n) {
return true;
}
return false;
}
bool Field2::setPlace(int place, int p)
{
if (board[place - 1] != 0)
return false;
board[place - 1] = p;
return true;
}
bool Field2::checkForDraw()
{
int countAllGone = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
if ((board)[i] != 0)
{
countAllGone++;
}
}
if (countAllGone == BOARD_SIZE) {
return true;
}
return false;
}
int Field2::getInput(int p)
{
int Input;
bool validInput = false;
// cout << "PLAYER 1 TURN!" << endl;////
while (!validInput) {
cout << "Type the number of your position:)" << endl;
cin >> Input;
if (Input == 0)
break;
if ((board)[Input] != 0 ||
Input > 15 ||
Input < 0) {
cout << "Not Valid Input. Spot is already taken!" << endl;
}
else {
validInput = true;
}
}
if (Input != 0)
setPlace(Input, p);
return Input;
}