-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.cpp
executable file
·225 lines (209 loc) · 6.15 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "Game.h"
#include "Move.h"
#include "Exceptions.h"
#include "computerS.h"
#include "Board.h"
#include "Player.h"
#include "Tile.h"
#include "Util.h"
#include "Bag.h"
#include <iostream>
#include <set>
#include <string>
#include <stdexcept>
#include <cctype>
#include <vector>
using namespace std;
//thins function checks if it is an AI
string checkForAI(string nameofplayer);
//convert input to uppercase word
string refineupper(string input);
int main(int argc, char const *argv[])
{
//check input
if(argc<2 || argc>2)
{
cout << "Invalid arguement"<<endl;
}
try {
//start game
Game* g = new Game(argv[1]);
computerS * ai_S;
computerS * ai_L;
//asking number of player
int number_player=0;
cout << "\nEnter number of players(1-8)"<< ": ";
cin >> number_player;
//enter their name and add player
string player_name;
string player_name_upper;
string * names = new string[number_player];
for (int i=0; i<number_player; i++)
{
cout << "\nPlease enter the name of player " << i+1 <<": ";
cin >> player_name;
player_name_upper = refineupper(player_name);
names[i]=player_name_upper;
cin.ignore();
//check if it is AI
if(checkForAI(names[i]) == "CPUS")
{
//add as AI load AIs
ai_S = new computerS(names[i]);
ai_S->initialize(g->getDictionary());
g->addPlayer(names[i]);
}
else if(checkForAI(names[i]) == "CPUL")
{
//add as AI load AIs
ai_L = new computerS(names[i]);
ai_L->initialize(g->getDictionary());
g->addPlayer(names[i]);
}
else {
//add as normal player
g->addPlayer(names[i]);
}
}
cout << endl;
//create Move object
// Move * m = new Move;
//start the game loop
while(!g->isFinished())
{
//display board
cout << g->getBoard()->getDisplay();
//displayPLA
Player * p = g->getCurrentPlayer();
string playername = p->getName();
cout << playername << " ,it's your turn" << endl;
//TILES
cout << "Your current tiles "<< endl;
string tiles_player = p->getHand();
cout << tiles_player << endl;
cout << "current scores :" << endl;
vector<pair<string,int> > scores = g->getScores();
for(unsigned int j=0; j<scores.size(); j++)
{
cout << scores[j].first << ": "<<scores[j].second << endl;
}
//detect AI
if(checkForAI(playername) == "CPUS" )
{
//int scoreForOneTurn=0;
cout << "CPUS" << endl;
Move ais_move=ai_S->getMove(*g->getBoard(),*p,g->initialTileCount());
try {
g->makeMove(ais_move);
g->finalizeMove();
}
catch(MoveException & o)
{
cout << o.getMessage() << endl;
}
}
else if (checkForAI(playername) == "CPUL")
{
cout << "CPUL"<<endl;
Move ail_move=ai_L->getMove(*g->getBoard(),*p,g->initialTileCount());
// g->makeMove(*m);
// g->finalizeMove();
try {
g->makeMove(ail_move);
g->finalizeMove();
}
catch(MoveException & o)
{
cout << o.getMessage() << endl;
}
}
else {
//type command
cout << "TYPE command" << endl;
cout << "1.EXCHANGE <string of tiles> \n2.PASS \n3.PLACE <dir> <row> <column> <string of tiles> \n";
string command;
getline(cin,command);
Move player_move(command,*p);
try
{
g->makeMove(player_move);
//retrive the most recent score add to player
cout << "points : " << g->getRecentScore()<< endl;
//p->addPoints(g->getRecentScore());
g->finalizeMove();
}
catch(MoveException & o)
{
cout << o.getMessage() << endl;
}
}
}
//finalsub
//get winner
//game done
cout << "Game over" << endl;
vector<pair<string,int> > finalsocre = g->finalSubtraction();
for(unsigned int j=0; j<finalsocre.size(); j++)
{
cout << finalsocre[j].first << ": "<<finalsocre[j].second << endl;
}
cout << "winner is : " <<endl;
vector<string> winner = g->getWinners();
for(unsigned int i=0; i<winner.size(); i++)
{
cout << winner[i] << endl;
}
}
//EXCEPETION
catch(ConfigParameterException & o)
{
cout << o.getMessage() << endl;
}
catch(FileException & o)
{
cout << o.getMessage() << endl;
}
catch(MoveException & o)
{
cout << o.getMessage() << endl;
}
return 1;
}
string checkForAI(string s)
{
string cpus = "CPUS";
string cpul = "CPUL";
string notAI = "NOTAI";
//grab the first 4 letters
string tocheck = refineupper(s);
//check length
if(s.length() >= 4)
{
string first_four="";
for(int i=0; i<4; i++)
{
char toadd = tocheck[i];
first_four+=toadd;
}
if(first_four == "CPUS")
{
return cpus;
}
else if (first_four == "CPUL")
{
return cpul;
}
}
return notAI;
}
string refineupper (string torefine)
{
string input;
char in;
for(unsigned int i=0; i<torefine.length(); i++)
{
in = (char)toupper(torefine[i]);
input = input + in;
}
return input;
}