-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cc
190 lines (150 loc) · 4.97 KB
/
main.cc
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
/**
* @author Sargis S Yonan
* @date 29 September 2018
*
* @brief The main driver file for Mancala.
*/
#include "board.h"
#include "game.h"
#include "game_server.h"
#include <iostream>
#include <cstdbool>
#include <cstdint>
#include <cstdlib>
int main(void)
{
char opponent_hostname[64] = {0};
char side = '\0';
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~\n";
std::cout << " ~o~o~o Mancala o~o~o~\n";
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
std::cout << "Enter opponent's hostname or ip address: ";
std::cin >> opponent_hostname;
Mancala::Board<4u> board;
board.pretty_print();
std::cout << "Enter your side (A | B): ";
std::cin >> side;
Mancala::Side current_player_side;
Mancala::Side opponent_side;
if (side == 'A' || side == 'a')
{
current_player_side = Mancala::Side::A;
}
else if (side == 'B' || side == 'b')
{
current_player_side = Mancala::Side::B;
}
else
{
std::cout << "Error: invalid side!\n";
return 1;
}
Mancala::Game game(board);
Mancala::GameServer server(opponent_hostname, current_player_side);
uint16_t round = 0;
srand(time(NULL));
Mancala::GameState error_code = Mancala::GameState::SideA;
while(true)
{
printf("\n________________________________\n\n");
printf("Round: %u\n", game.get_rounds());
board.pretty_print();
int row_input = 0;
uint8_t row = 0;
switch (error_code)
{
case Mancala::GameState::SideA:
if (current_player_side == Mancala::Side::A)
{
printf("Your turn!\nSelect a row: ");
std::cin >> row_input;
row = static_cast<uint8_t>(row_input);
error_code = game.run_round(current_player_side, row);
if (static_cast<int8_t>(error_code) > 0)
{
server.send_move(game.get_rounds(), current_player_side, row);
}
else
{
printf("Something went wrong.\n");
}
}
else
{
printf("Waiting for opponent to move...\n");
while(!server.move_received());
server.get_move(round, opponent_side, row);
if (row < 6)
{
error_code = game.run_round(opponent_side, row);
}
else
{
printf("Error: received move with incorrect row number.");
}
error_code = game.run_round(opponent_side, row);
}
break;
case Mancala::GameState::SideB:
if (current_player_side == Mancala::Side::B)
{
printf("Your turn!\nSelect a row: ");
std::cin >> row_input;
row = static_cast<uint8_t>(row_input);
error_code = game.run_round(current_player_side, row);
if (static_cast<int8_t>(error_code) > 0)
{
server.send_move(game.get_rounds(), current_player_side, row);
}
else
{
printf("Error: code %x\n", error_code);
}
}
else
{
printf("Waiting for opponent to move...\n");
while(!server.move_received());
server.get_move(round, opponent_side, row);
if (row < 6)
{
error_code = game.run_round(opponent_side, row);
}
else
{
printf("Error: received move with incorrect row number.");
}
}
break;
case Mancala::GameState::GameOver:
printf("\nGame Over.\n\n");
board.pretty_print();
if (game.get_winner() == current_player_side)
{
printf("You win!\n");
}
else
{
printf("You lose.\n");
}
return 0;
break;
case Mancala::GameState::RoundFailure:
printf("Error: round failure.\n");
return 1;
break;
case Mancala::GameState::InvalidSide:
printf("Error: invalid side.\n");
return 1;
break;
case Mancala::GameState::EmptyHoleError:
/*
* Reselect hole.
*/
break;
default:
break;
}
}
return 0;
}