-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherr_min_player.cpp
More file actions
122 lines (105 loc) · 3.59 KB
/
Copy patherr_min_player.cpp
File metadata and controls
122 lines (105 loc) · 3.59 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
120
121
122
#include <iostream>
#include <string>
#include <vector>
#include "PlayerGame.hpp"
#include "err_min.hpp"
Game::wall_type lwall;
bool initialized = false;
/** Pretty print a game wall */
void print_wall(const Game::wall_type& s, bool highlight = false)
{
printf(" ||");
for (unsigned i = 0; i < s[0].size(); ++i) printf(" %2d |", i);
printf("|\n%s\n", std::string(4+5*s[0].size()+1,'=').c_str());
for (unsigned i = s.size()-1; i > 0; --i) {
printf("%c ||", 'A' + i);
int prev = -1;
for (unsigned j = 0; j < s[i].size(); ++j) {
if(highlight && s[i][j] != lwall[i][j]) {
printf(" \033[1;31m%2d\033[0m |", s[i][j]);
prev = lwall[i][j];
} else {
printf(" %2d |", s[i][j]);
}
}
if (prev == -1)
printf("|\n");
else
printf("| %d\n", prev);
printf("--||");
for (unsigned j = 0; j < s[i].size(); ++j) printf("----|");
printf("|\n");
}
printf("%c ||", 'A');
for (unsigned j = 0; j < s[0].size(); ++j) printf(" %2d |", s[0][j]);
printf("|\n%s\n", std::string(4+5*s[0].size()+1,'=').c_str());
}
/** Choose the known brick in the discard or a unknown brick in the pile.
* Input:
* wall: A 2D array denoting our game wall.
* owall: A 2D array denoting the opponent's game wall.
* discard_brick: The integer value of the known brick in the discard.
* Output:
* 'd': Accept the known brick in the discard.
* 'p': Reject the known brick in the discard and draw from the pile.
*/
std::string choose_discard_or_pile(const Game::wall_type& wall,
const Game::wall_type& owall,
int discard_brick)
{
std::cout << "\nOpponent:" << std::endl;
print_wall(owall, initialized);
std::cout << "\nMy Wall:" << std::endl;
print_wall(wall);
lwall = owall;
initialized = true;
return pd;
}
/** Convert numeric (row, col) to alphanumeric "XY" */
std::string rowcol2coord(int row, int col)
{
return std::string(1, 'A' + row) + std::string(1, '0' + col);
}
/** Choose the coordinate to place the chosen brick into our wall.
* Input:
* wall: A 2D array denoting our wall.
* owall: A 2D array denoting the opponent's wall.
* brick: The integer value of the chosen brick.
* Output:
* 'XY' where X is a char from 'A' to 'D' and Y is an char from '0' to '3'
* Consider using rowcol2coord to produce valid output.
*/
std::string choose_coord(const Game::wall_type& wall,
const Game::wall_type& owall,
int brick)
{
std::cout << brick << std::endl;
std::cout << "Coord: ";
std::string move;
std::cin >> move;
return move;
}
int main(int argc, char** argv)
{
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " GAMEID" << std::endl;
std::cout << "\tGAMEID = 0 creates a new game" << std::endl;
std::cout << "\tGAMEID = WXYZ connect to a specific game" << std::endl;
exit(0);
}
// Connect to a FoosGame with id from the command line
Game game(argv[1]);
while (true) {
// On our turn, we get the brick on the pile
int pile = game.get_discard();
// Choose 'p' to get the pile brick or 'd' to get a random brick
std::string pd_move = choose_discard_or_pile(game.wall, game.owall, pile);
// Get the brick we chose (either the pile brick or a random brick)
int brick = game.get_brick(pd_move);
// Determine where to place this brick with row-col coords "A0", "B3", etc
std::string co_move = choose_coord(game.wall, game.owall, brick);
// Make the move -- informs the opponent and updates the wall
game.make_move(co_move);
}
return 0;
}