-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiskoty.cpp
147 lines (131 loc) · 3.77 KB
/
Piskoty.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
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
#include "Piskoty.h"
#include <iostream>
#include <cstdlib> // For rand()
#include <ctime> // For time()
using namespace std;
Piskoty::Piskoty(int size) : boardSize(size), gameRunning(true), currentPlayer(0) {
board.resize(boardSize, vector<int>(boardSize, 8)); // Initialize board with empty cells (8)
}
void Piskoty::startGame() {
while (gameRunning) {
displayBoard();
if (currentPlayer == 0) {
playerMove();
}
else {
npcMove();
}
checkWin(); // Check for win or draw
currentPlayer = 1 - currentPlayer; // Switch players
}
}
void Piskoty::displayBoard() const {
cout << " ";
for (int i = 0; i < boardSize; ++i) cout << i << " "; // Column headers
cout << endl;
for (int i = 0; i < boardSize; ++i) {
cout << i << " "; // Row header
for (int j = 0; j < boardSize; ++j) {
if (board[i][j] == 8) {
cout << "- ";
}
else if (board[i][j] == 0) {
cout << "X ";
}
else {
cout << "O ";
}
}
cout << endl;
}
}
void Piskoty::playerMove() {
int row, col;
cout << "Enter your move (row and column): ";
cin >> row >> col;
if (validateInput(row, col)) {
board[row][col] = 0; // Player plays as X
}
else {
cout << "Invalid move! Try again." << endl;
playerMove(); // Retry
}
}
void Piskoty::npcMove() {
cout << "NPC is making a move..." << endl;
pair<int, int> move = npc.getMove(board);
board[move.first][move.second] = 1; // NPC plays as O
}
bool Piskoty::validateInput(int row, int col) const {
if (row < 0 || row >= boardSize || col < 0 || col >= boardSize) {
cout << "Out of bounds! Choose a valid cell." << endl;
return false;
}
if (board[row][col] != 8) {
cout << "Cell already occupied!" << endl;
return false;
}
return true;
}
void Piskoty::checkWin() {
if (checkRows() || checkCols() || checkDiagonals()) {
displayBoard();
cout << (currentPlayer == 0 ? "Player" : "NPC") << " wins!" << endl;
gameRunning = false;
}
}
bool Piskoty::checkRows() const {
for (int i = 0; i < boardSize; ++i) {
if (board[i][0] != 8) {
bool win = true;
for (int j = 1; j < boardSize; ++j) {
if (board[i][j] != board[i][j - 1]) {
win = false;
break;
}
}
if (win) return true;
}
}
return false;
}
bool Piskoty::checkCols() const {
for (int j = 0; j < boardSize; ++j) {
if (board[0][j] != 8) {
bool win = true;
for (int i = 1; i < boardSize; ++i) {
if (board[i][j] != board[i - 1][j]) {
win = false;
break;
}
}
if (win) return true;
}
}
return false;
}
bool Piskoty::checkDiagonals() const {
// Main diagonal
if (board[0][0] != 8) {
bool win = true;
for (int i = 1; i < boardSize; ++i) {
if (board[i][i] != board[i - 1][i - 1]) {
win = false;
break;
}
}
if (win) return true;
}
// Anti-diagonal
if (board[0][boardSize - 1] != 8) {
bool win = true;
for (int i = 1; i < boardSize; ++i) {
if (board[i][boardSize - i - 1] != board[i - 1][boardSize - i]) {
win = false;
break;
}
}
if (win) return true;
}
return false;
}