-
Notifications
You must be signed in to change notification settings - Fork 0
/
AI.cpp
108 lines (91 loc) · 3.78 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
#include "AI.hpp"
#include "DEFINITIONS.hpp"
#include <iostream>
using namespace std;
namespace Sonar {
AI::AI(int playerPiece, GameDataRef data) {
this->_data = data;
this->playerPiece = playerPiece;
if (playerPiece == O_PIECE) {
aiPiece = X_PIECE;
}
else {
aiPiece = O_PIECE;
}
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) {
//check if AI can win
try {
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);
}
//if player can win, block that state
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);
}
//center is empty
CheckIfPieceIsEmpty(1, 1, gridArray, gridPieces);
//check corner
CheckIfPieceIsEmpty(0, 2, gridArray, gridPieces);
CheckIfPieceIsEmpty(2, 2, gridArray, gridPieces);
CheckIfPieceIsEmpty(0, 0, gridArray, gridPieces);
CheckIfPieceIsEmpty(2, 0, gridArray, gridPieces);
CheckIfPieceIsEmpty(1, 2, gridArray, gridPieces);
CheckIfPieceIsEmpty(0, 1, gridArray, gridPieces);
CheckIfPieceIsEmpty(2, 1, gridArray, gridPieces);
CheckIfPieceIsEmpty(1, 0, gridArray, gridPieces);
}
catch (int error) {
}
//AI turn over, player continues
*gameState = STATE_PLAYING;
}
//X Y -> pieces to be placed if they are placed, is the game win state?
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]) {
if ((*gridArray)[x1][y1] == pieceToCheck && (*gridArray)[x2][y2] == pieceToCheck) {
if (EMPTY_PIECE == (*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]) {
if (EMPTY_PIECE == (*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;
}
}
}