-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameLogic.cpp
More file actions
119 lines (108 loc) · 2.08 KB
/
GameLogic.cpp
File metadata and controls
119 lines (108 loc) · 2.08 KB
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
#include "GameLogic.h"
GameLogic::GameLogic(int col, int row, int p, string mode,int order)
{
this->col = col;
this->row = row;
this->p = p;
this->mode = mode;
this->order = order;
this->aiList = new vector<AI*>();
}
void GameLogic::Manual()
{
int player = 1;
int winPlayer = 0;
bool init = true;
Move move;
Board board(col, row, p);
board.initializeGame();
board.showBoard();
while (true)
{
move = ((*aiList)[player - 1])->GetMove(move);
cout << move.toString() << endl;
try
{
board.makeMove(move, player);
}
catch (InvalidMoveError)
{
cerr << "Invalid Move!" << endl;
winPlayer = player == 1 ? 2 : 1;
break;
}
winPlayer = board.isWin(player);
board.showBoard();
if (winPlayer != 0)
{
break;
}
player = player == 1 ? 2 : 1;
}
if (winPlayer == -1)
{
cout<< "Tie"<<endl;
}
else
{
cout << "Player " << winPlayer << " wins!" << endl;
}
}
void GameLogic::TournamentInterface()
{
StudentAI ai(col, row, p);
while (true)
{
string instr;
cin >> instr;
Move result = ai.GetMove(Move(instr));
cout << result.toString()<< endl;
}
}
void GameLogic::Run()
{
if (mode == "m" or mode == "manual")
{
AI* studentai = new StudentAI(col, row, p);
AI* manualai = new ManualAI(col, row, p);
if (order == 1)
{
aiList->push_back(manualai);
aiList->push_back(studentai);
}
else
{
aiList->push_back(studentai);
aiList->push_back(manualai);
}
Manual();
}
else if (mode == "s" or mode == "self")
{
AI* studentai = new StudentAI(col, row, p);
AI* manualai = new StudentAI(col, row, p);
if (order == 1)
{
aiList->push_back(manualai);
aiList->push_back(studentai);
}
else
{
aiList->push_back(studentai);
aiList->push_back(manualai);
}
Manual();
}
else if (mode == "t")
{
TournamentInterface();
}
}
GameLogic::~GameLogic()
{
while(!aiList->empty())
{
delete aiList->back();
aiList->pop_back();
}
}