-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.h
148 lines (124 loc) · 4.27 KB
/
Game.h
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
#ifndef GAME_H
#define GAME_H
#include ".\Board\Board.h"
#include ".\Board\Piece.h"
#include ".\Board\Color.h"
#include ".\Board\Position.h"
#include "Global.h"
#include ".\Checkers\CheckersMatch.h"
#include ".\Checkers\CheckersPosition.h"
#include ".\Checkers\Pieces\Checker.h"
#include ".\Screen.h"
#include ".\Util.h"
#include <vector>
#include <algorithm>
#include <set>
#include <iterator>
#include <cstdlib>
using namespace std;
class Game
{
CheckersMatch* match;
Color livePlayer;
Color machinePlayer;
public :
Game(CheckersMatch* match)
{
this->match = match;
this->match->SetLivePlayer();
this->match->putPieces();
livePlayer = match->livePlayer;
machinePlayer =match->opponent(livePlayer);
}
Color playTheWholeGame()
{
Color winner;
while (!match->isMatchFinished(winner))
{
try
{
system("CLS");
Screen::printMatch(*match);
cout << endl;
vector<Position> movement;
movement = (match->currentPlayer == match->livePlayer) ? playLive() : playMachine();
match->makePlay(movement[0], movement[1]);
match->checkForLadies();
}
catch (runtime_error &e)
{
cout << e.what() << endl;
Util::ReadKey();
}
}
return winner;
}
vector<Position> playLive()
{
cout << "Origin: ";
CheckersPosition origin_ch = Screen::readCheckersPosition();
Position origin = origin_ch.toPosition();
match->validateOrigin(origin);
vector<vector<bool>> possiblePositions = match->board.piece(origin)->possibleMoves();
system("CLS");
Screen::printMatch(*match, possiblePositions);
cout << endl;
cout << "Origin: " << origin_ch << endl;
cout << "Destination: ";
Position destination = Screen::readCheckersPosition().toPosition();
match->validateDestination(origin, destination);
vector<Position> movement = {origin, destination};
return movement;
}
vector<Position> playMachine()
{
vector< pair<int, vector<Position> > > catalogueOfMoves;
vector< pair<int, vector<Position>>>::iterator it;
vector<Piece *> machinePieces;
for(int i = 0; i<match->board.piecesAtTheBoard.size(); i++)
{
if (match->board.piecesAtTheBoard[i]->getColor() == machinePlayer)
{
machinePieces.push_back(match->board.piecesAtTheBoard[i]);
}
}
for(int i=0; i< machinePieces.size(); i++)
{
vector<vector<bool>> matrix = machinePieces[i]->possibleMoves();
for(int j = 0; j < Dim; j++)
{
for(int k = 0; k < Dim; k++)
{
if(matrix[j][k])
{
vector<Position> movement = {machinePieces[i]->getPosition(), Position(j, k)};
int captured = 0;
captured += match->pieceWasCapturated(movement[0], movement[1]) ? 1 : 0;
catalogueOfMoves.push_back( make_pair(captured,movement) );
}
}
}
}
random_shuffle(catalogueOfMoves.begin(),catalogueOfMoves.end(), Util::myrandom);
vector<pair<int, vector<Position>>>::iterator bigger_catch = catalogueOfMoves.begin();
for (it = catalogueOfMoves.begin()+1; it != catalogueOfMoves.end(); it++)
{
if(bigger_catch->first < it-> first) bigger_catch = it;
}
if(bigger_catch->first == 0)
{
// bigger_catch == catalogueOfMoves.begin()
for (it = catalogueOfMoves.begin() +1; it != catalogueOfMoves.end(); it++)
{
if ((bigger_catch->second)[0].line > (it->second)[0].line)
bigger_catch = it;
}
}
vector<Position> movement = bigger_catch->second;
cout << "Origin: " << Util::toCheckersPosition(movement[0]) << endl;
cout << "Destination: " << Util::toCheckersPosition( movement[1]) << endl << endl;
Util::ReadKey();
return movement;
}
};
#endif