-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.cpp
120 lines (100 loc) · 3.7 KB
/
ai.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
#include "ai.hpp"
#include "DEFINITIONS.hpp"
using namespace std;
namespace GameEngine
{
AI::AI(int playerPiece, GameDataRef data)
{
this->_data = data;
this->playerPiece = playerPiece;
if (O_piece_number == playerPiece)
{
aiPiece = X_piece_number;
}
else
{
aiPiece = O_piece_number;
}
checkMatchVector.push_back({ 0, 2, 1, 2, 2, 2 });
checkMatchVector.push_back({ 0, 2, 0, 1, 0, 0 });
checkMatchVector.push_back({ 0, 2, 1, 1, 2, 0 });
checkMatchVector.push_back({ 2, 2, 1, 2, 0, 2 });
checkMatchVector.push_back({ 2, 2, 2, 1, 2, 0 });
checkMatchVector.push_back({ 2, 2, 1, 1, 0, 0 });
checkMatchVector.push_back({ 0, 0, 0, 1, 0, 2 });
checkMatchVector.push_back({ 0, 0, 1, 0, 2, 0 });
checkMatchVector.push_back({ 0, 0, 1, 1, 2, 2 });
checkMatchVector.push_back({ 2, 0, 2, 1, 2, 2 });
checkMatchVector.push_back({ 2, 0, 1, 0, 0, 0 });
checkMatchVector.push_back({ 2, 0, 1, 1, 0, 2 });
checkMatchVector.push_back({ 0, 1, 1, 1, 2, 1 });
checkMatchVector.push_back({ 1, 2, 1, 1, 1, 0 });
checkMatchVector.push_back({ 2, 1, 1, 1, 0, 1 });
checkMatchVector.push_back({ 1, 0, 1, 1, 1, 2 });
checkMatchVector.push_back({ 0, 1, 2, 1, 1, 1 });
checkMatchVector.push_back({ 1, 2, 1, 0, 1, 1 });
checkMatchVector.push_back({ 0, 2, 2, 0, 1, 1 });
checkMatchVector.push_back({ 2, 2, 0, 0, 1, 1 });
checkMatchVector.push_back({ 0, 2, 2, 2, 1, 2 });
checkMatchVector.push_back({ 0, 0, 2, 0, 1, 0 });
checkMatchVector.push_back({ 0, 2, 0, 0, 0, 1 });
checkMatchVector.push_back({ 2, 2, 2, 0, 2, 1 });
}
void AI::PlacePiece(int (*gridArray)[3][3], sf::Sprite (*gridPieces)[3][3], int *gameState)
{
try
{
// check if AI can win
for (int i = 0; i < checkMatchVector.size(); i++)
{
CheckSection(checkMatchVector[i][0], checkMatchVector[i][1], checkMatchVector[i][2], checkMatchVector[i][3], checkMatchVector[i][4], checkMatchVector[i][5], AI_piece, gridArray, (*gridPieces));
}
// check if player can win
for (int i = 0; i < checkMatchVector.size(); i++)
{
CheckSection(checkMatchVector[i][0], checkMatchVector[i][1], checkMatchVector[i][2], checkMatchVector[i][3], checkMatchVector[i][4], checkMatchVector[i][5], PLAYER_PIECE, gridArray, (*gridPieces));
}
// check if center is empty
CheckIfPieceIsEmpty(1, 1, gridArray, gridPieces);
// check if a corner is empty
CheckIfPieceIsEmpty(0, 2, gridArray,gridPieces);
CheckIfPieceIsEmpty(2, 2, gridArray, gridPieces);
CheckIfPieceIsEmpty(0, 0, gridArray, gridPieces);
CheckIfPieceIsEmpty(2, 0, gridArray, gridPieces);
// check for any other empty piece
CheckIfPieceIsEmpty(1, 2, gridArray,gridPieces);
CheckIfPieceIsEmpty(0, 1, gridArray, gridPieces);
CheckIfPieceIsEmpty(2, 1, gridArray, gridPieces);
CheckIfPieceIsEmpty(1, 0, gridArray, gridPieces);
}
catch (int error)
{
}
*gameState = STATE_PLAYING;
}
void AI::CheckSection(int x1, int y1, int x2, int y2, int X, int Y, int pieceToCheck, int(*gridArray)[3][3], sf::Sprite gridPieces[3][3])
{
// check if 2 pieces match
if ((*gridArray)[x1][y1] == pieceToCheck && (*gridArray)[x2][y2] == pieceToCheck)
{
if (Empty_piece_number == (*gridArray)[X][Y])
{
(*gridArray)[X][Y] = AI_piece;
gridPieces[X][Y].setTexture(this->_data->assets.GetTexture("O Piece"));
gridPieces[X][Y].setColor(sf::Color(255, 255, 255, 255));
throw - 1;
}
}
}
void AI::CheckIfPieceIsEmpty(int X, int Y, int(*gridArray)[3][3], sf::Sprite (*gridPieces)[3][3])
{
// check if
if (Empty_piece_number == (*gridArray)[X][Y])
{
(*gridArray)[X][Y] = AI_piece;
(*gridPieces)[X][Y].setTexture(this->_data->assets.GetTexture("O Piece"));
(*gridPieces)[X][Y].setColor(sf::Color(255, 255, 255, 255));
throw - 2;
}
}
}