forked from comfortablejohn/threes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threes.cpp
187 lines (172 loc) · 5.46 KB
/
threes.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
/**
* Threes! player
*/
#include <threes.h>
/*
* Used for move iteration, such as user input or random move selection
* Return 0 if game ends due to full board, 1 if game ends due to end of
* tile input.
*/
int iterateMoves(Board &board,
std::vector<std::string> &move_sequence, int playType) {
// Use these maps to convert strings to Direction enums, and vice versa.
std::map<std::string, Direction> move_parse;
move_parse.insert(std::pair<std::string, Direction>("U", U));
move_parse.insert(std::pair<std::string, Direction>("D", D));
move_parse.insert(std::pair<std::string, Direction>("L", L));
move_parse.insert(std::pair<std::string, Direction>("R", R));
std::map<Direction, std::string> parse_move;
parse_move.insert(std::pair<Direction, std::string>(U, "U"));
parse_move.insert(std::pair<Direction, std::string>(D, "D"));
parse_move.insert(std::pair<Direction, std::string>(L, "L"));
parse_move.insert(std::pair<Direction, std::string>(R, "R"));
std::vector<Direction> poss_moves = getPossibleMoves(board, tile_num);
while (poss_moves.size() > 0) {
printBoard(board);
std::cout << "Possible moves: ";
for (Direction d : poss_moves)
std::cout << parse_move.find(d)->second << " ";
std::cout << "\n";
Direction m;
std::string move;
switch (playType) {
case MAN: { // user defined input determines moves
std::cin >> move;
while (move_parse.count(move) == 0) {
std::cout << "Move \"" << move
<< "\" invalid, please enter from {U, L, D, R}:\n";
std::cin >> move;
if (std::cin.eof()) {
std::cout << "Read EOF for stdin.. Ending game.\n";
return -1;
}
}
m = move_parse.find(move)->second;
break;
}
case RAND: { // random selection
int rand_move = (int)(std::rand() % poss_moves.size());
std::cout << "Chose poss[" << rand_move << "]: ";
std::cout << parse_move.find(m)->second << "\n";
m = poss_moves[rand_move];
break;
}
case AI:{
m = greedy_search(board,tile_num);
break;
}
default: {
printf("Invalid playType used. Please use those defined in threes.h\n");
exit(EXIT_FAILURE);
}
}
std::vector<Shift> shifts = makeMove(&board, m, tile_num);
std::cout << "Move #" << move_sequence.size() + 1 << " = "
<< parse_move.find(m)->second << "\n";
move_sequence.push_back(parse_move.find(m)->second);
tile_num++;
if (tile_num > inputSequence.size() - 1) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(5));
poss_moves = getPossibleMoves(board, tile_num);
}
return poss_moves.size() > 0;
}
int help() {
std::cout << "Usage: threes -i /dir/to/input_file_name [-m|-r|-a]\n";
std::cout << "\t-i: Input tile sequence file name (mandatory arg)\n";
std::cout << "\t-m: Manual moves (input U, D, L, or R for each turn)\n";
std::cout << "\t-r: Random move selection\n";
std::cout << "\t-a: AI move selection\n";
return 0;
}
/**
* Play Threes!
* Usage: threes -i /dir/to/input_file_name [-m|-r|-a]
* -i: Input tile sequence file name (mandatory arg)
* -m: Manual moves (input U, D, L, or R for each turn)
* -r: Random move selection
* -a: AI move selection
*/
int main(int argc, char *argv[]) {
time_t tstart, tend;
tstart=time(0);
Board board (BOARD_SIZE, std::vector<int>(BOARD_SIZE, EMPTY));
std::vector<std::string> move_sequence;
char *fileName;
fileName = (char *) malloc(SIZE_MAX);
int playType;
if (argc != 4) { // parse command line arguments
return help();
} else {
int c;
opterr = 0;
while ((c = getopt(argc, argv, "i:mra")) != -1) {
switch(c) {
case 'i': {
fileName = optarg;
break;
}
case 'm': {
playType = MAN;
break;
}
case 'r': {
playType = RAND;
break;
}
case 'a': {
playType = AI;
break;
}
default: {
return help();
}
}
}
}
readInFile(&board, &inputSequence, fileName);
std::srand(std::time(NULL));
int endGame = -1;
switch (playType) { // play the game how user wants to
case (MAN):
endGame = iterateMoves(board, move_sequence, MAN);
break;
case (RAND):
endGame = iterateMoves(board, move_sequence, RAND);
break;
// case (AI):
// // not implemented yet, call to AI algorithm goes here,
// // endGame = AI(); or something
// return 1;
case AI:{
endGame = iterateMoves(board, move_sequence, AI);
break;
}
default:
return help();
}
switch (endGame) {
case 0:
std::cout << "No legal moves left (board full).\n";
break;
case 1:
std::cout << "All input tiles have been used.\n";
break;
default:
std::cout << "Not sure how the game ended.. (something went wrong, sorry)\n";
break;
}
std::cout << "Printing final board...\n";
printBoard(board);
std::cout << "Game over. Score is: " << score(board) << ". Moves were: \n";
for (std::string m : move_sequence)
std::cout << m;
std::cout << "\n";
tend=time(0);
std::cout << "Time used: " << difftime(tend, tstart) << "\n";
std::cout << "Number of moves:" << move_sequence.size()+1 << "\n";
std::cout << "Moves/sec:" << (move_sequence.size()+1)/difftime(tend, tstart) << "\n";
return 0;
}