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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
| #include <iostream>
#include <fstream>
#include <string>
#include "stdio.h"
using namespace std;
class Player
{
public:
string name;
char mark;
Player(string name, char mark)
{
this->name = name;
this->mark = mark;
}
char getMark()
{
return mark;
}
string getName()
{
return name;
}
};
class Board
{
public:
Player *currentPlayer;
char **board;
int size;
static const int nWin = 2; //number of piecies to win (2,3,4..ecc)
Board(int n, Player *beginningPlayer)
{
size = n;
board = new char *[size];
for (int i = 0; i < size; i++)
board[i] = new char[size];
for (int i = 0; i<size; i++)
for (int k = 0; k<size; k++)
board[i][k] = '_';
currentPlayer = beginningPlayer;
}
void printBoard()
{
cout << " \t";
for (int i = 0; i<size; i++)
cout << i + 1 << "\t";
cout << "\n";
for (int i = 0; i<size; i++)
{
cout << i + 1 << "\t";
for (int k = 0; k<size; k++)
cout << board[i][k] << "\t";
cout << "\n\n";
}
}
bool checkPosition(int row, int col)
{
if (row >= size || row<0 || col >= size || col <0)
return false;
if (board[row][col] == '_')
return true;
else
return false;
}
bool updateBoard(int r, int c)
{
if (checkPosition(r, c))
board[r][c] = currentPlayer->getMark();
else
return false;
return true;
}
bool checkWinner()
{
int playerCount, r, c, x, y;
// Check Orizontal --OK
for (r = 0; r < size; r++)
{
playerCount = 0;
for (c = 0; c < size; c++)
{
if (board[r][c] == '_') //dopo lo metto a posto con i segni belli
playerCount = 0;
if (board[r][c] == currentPlayer->getMark())
playerCount += 1;
if (playerCount >= nWin)
return true;
}
}
//Check Vertical --OK
for (c = 0; c<size; c++)
{
playerCount = 0;
for (r = 0; r < size; r++)
{
if (board[r][c] == '_')
playerCount = 0;
if (board[r][c] == currentPlayer->getMark())
playerCount += 1;
if (playerCount >= nWin)
return true;
}
}
//check oblique 1
for (x = size, y = 0; x>0; x--, y++)
{
playerCount = 0;
for (r = y, c = 0; c<x; r++, c++)
{
if (board[r][c] == '_')
playerCount = 0;
if (board[r][c] == currentPlayer->getMark())
playerCount += 1;
if (playerCount >= nWin)
return true;
}
}
// check oblique 2
for (x = size, y = 0; x>0; x--, y++)
{
playerCount = 0;
for (r = 0, c = y; c<x; r++, c++)
{
if (board[r][c] == '_')
playerCount = 0;
if (board[r][c] == currentPlayer->getMark())
playerCount += 1;
if (playerCount >= nWin)
return true;
}
}
// If nobody won
return false;
}
};
int main()
{
int row, column;
bool control = true;
bool winner;
Player *human = new Player("MARCO", 'H');
Player *computer = new Player("KASIA", 'C');
Board *board = new Board(9, human);
cout << "PRESS A BUTTON TO CONTINUE\n";
getchar();
system("CLS");
//END WELCOME PAGE
do
{
do
{
board->printBoard();
cout << "Player: " << board->currentPlayer->getName() << "\n";
cout << "Insert row: ";
cin >> row;
cout << "Insert column: ";
cin >> column;
system("CLS");
control = board->updateBoard(row - 1, column - 1);
} while (!control);
winner = board->checkWinner();
if (!winner)
{
if (board->currentPlayer == human)
board->currentPlayer = computer;
else
board->currentPlayer = human;
}
} while (!winner);
board->printBoard();
cout << "\n\nThe winner is: " << board->currentPlayer->getName();
//problems with devcpp
cin >> row;
} |