-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
244 lines (214 loc) · 5.91 KB
/
Copy pathgame.cpp
File metadata and controls
244 lines (214 loc) · 5.91 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdlib.h>
#include <time.h>
namespace constants
{
const int MAX_LADDER = 8;
const int MAX_SNAKE = 8;
const int MAX_POSITION = 100;
const int INITIAL = 0;
const int FINAL = 1;
const int OFFSET = 11;
}
cv::Mat goti1 = cv::imread("red_piece.png", CV_LOAD_IMAGE_COLOR);
cv::Mat goti2 = cv::imread("blue_piece.png", CV_LOAD_IMAGE_COLOR);
cv::Mat board = cv::imread("board.jpg", CV_LOAD_IMAGE_COLOR);
cv::Mat game=board;
const int ladder[constants::MAX_LADDER][2] =
{{1, 38}, {4, 14},
{9, 31}, {21, 42},
{28, 84}, {51, 67},
{71, 91}, {80, 100}};
const int snake[constants::MAX_SNAKE][2] =
{{17, 7}, {54, 34},
{62, 19}, {64, 60},
{87, 24}, {93, 73},
{95, 75}, {98, 79}};
int roll_the_dice (double fraction)
{
int random =1+ static_cast<int>(6*(rand()*fraction));
//std::cout<<random<<std::endl;
return random;
}
void toggle_player (bool &player_index)
{
player_index = !player_index;
}
bool stepped_on_ladder(int position, bool is_enabled)
{
int ladder_index;
using namespace constants;
if(is_enabled)
for(ladder_index = 0 ; ladder_index < MAX_LADDER ; ladder_index++)
{
if(ladder[ladder_index][INITIAL]==position)
{
std::cout<<"climbed ladder of Index"<<ladder_index<<std::endl;
return true;
}
}
return false;
}
int get_other_end_of_ladder (int position)
{
int ladder_index;
using namespace constants;
for(ladder_index = 0 ; ladder_index < MAX_LADDER ; ladder_index++)
{
if(ladder[ladder_index][INITIAL]==position)
return ladder[ladder_index][FINAL];
}
}
bool bitten_by_snake(int position)
{
int snake_index;
using namespace constants;
for(snake_index = 0 ; snake_index < MAX_SNAKE ; snake_index++)
{
if(snake[snake_index][INITIAL]==position)
{
std::cout<<"Bitten by snake of Index "<<snake_index<<std::endl;
return true;
}
}
return false;
}
int get_tail_of_snake (int position)
{
int snake_index;
using namespace constants;
for(snake_index = 0 ; snake_index < constants::MAX_LADDER ; snake_index++)
{
if(snake[snake_index][INITIAL]==position)
return snake[snake_index][FINAL];
}
}
int get_x_coordinate(int position)
{
int x_coordinate = (((position-1)/10)%2)?(board.rows-constants::OFFSET-87*((position-1)%10)-87):(constants::OFFSET+87*((position-1)%10));
return x_coordinate;
}
int get_y_coordinate(int position)
{
int y_coordinate = board.cols-constants::OFFSET-87*((position-1)/10);
return y_coordinate;
}
void show_game(int x1, int y1, int x2, int y2)
{
int row, col;
using namespace cv;
game=imread("board.jpg", CV_LOAD_IMAGE_COLOR);
for(row=0;row<86;row++)
for(col=0;col<86;col++)
{
Vec3b & temp = game.at<Vec3b>(y1-col, x1+row);
Vec3b goti = goti1.at<Vec3b>(col, row);
if(!(goti[1]>100&&goti[2]>100&&goti[0]>100))
{
temp[0]=goti[0];
temp[1]=goti[1];
temp[2]=goti[2];
}
Vec3b & temp_ = game.at<Vec3b>(y2-col, x2+row);
Vec3b goti_ = goti2.at<Vec3b>(col, row);
if(!(goti_[1]>100&&goti_[2]>100&&goti_[0]>100))
{
temp_[0]=goti_[0];
temp_[1]=goti_[1];
temp_[2]=goti_[2];
}
}
}
void move(int destination, bool player_index, int &initial, bool &is_enabled, int other_player_position)
{
if(!is_enabled)
{
if((destination-initial)<6)
{
std::cout<<"Still trapped!"<<std::endl;
return;
}
else
{
std::cout<<"set free!"<<std::endl;
is_enabled = !is_enabled;
destination-=7;
}
}
{
if(destination>100)
{
std::cout<<"Going beyond 100 is not allowed"<<std::endl;
return;
}
if(!player_index)
{
show_game(get_x_coordinate(destination), get_y_coordinate(destination),
get_x_coordinate(other_player_position), get_y_coordinate(other_player_position));
}
else
{
show_game(get_x_coordinate(other_player_position), get_y_coordinate(other_player_position),
get_x_coordinate(destination), get_y_coordinate(destination));
}
initial=destination;
}
}
void check_for_win(int position, bool ¬_finished, bool player_index)
{
if(position==100)
{
not_finished = false;
std::cout<<((player_index)?"CPU won!":"You won!")<<std::endl;
}
return;
}
int main()
{
bool not_finished=true, is_enabled[2]={false, false}, player_index=false;
int step_count=0, position[2]={1, 1};
cv::namedWindow("SnakeAndLadders", CV_WINDOW_NORMAL);
imshow("SnakeAndLadders", game);
srand(time(NULL));
const double fraction = 1.0 / (static_cast<double>(RAND_MAX) + 1.0);
show_game(get_x_coordinate(1), get_y_coordinate(1), get_x_coordinate(1), get_y_coordinate(1));
while(not_finished)
{
step_count = roll_the_dice(fraction);
if(step_count==6)
{
std::cout<<((player_index)?"CPU got ":"Yay! You got ")<<"6. "<<((player_index)?"CPU get ":"You get ")<<"another chance"<<std::endl;
cv::waitKey(0);
step_count += roll_the_dice(fraction);
if(step_count==12)
{
std::cout<<((player_index)?"CPU got ":"Yay! You got ")<<"6 again. "<<((player_index)?"CPU get ":"You get ")<<"another chance" << std::endl;
cv::waitKey(0);
step_count +=roll_the_dice(fraction);
if(step_count==18)
{
std::cout<<"Alas! Triple 6s"<<std::endl;
toggle_player(player_index);
cv::waitKey(0);
continue;
}
}
}
std::cout<<((player_index)?"CPU got ":"You got ")<<step_count<<std::endl;
move(step_count+position[player_index], player_index, position[player_index], is_enabled[player_index], position[!player_index]);
if(stepped_on_ladder(position[player_index], is_enabled[player_index]))
{
move(get_other_end_of_ladder(position[player_index]), player_index, position[player_index], is_enabled[player_index], position[!player_index]);
}
if(bitten_by_snake(position[player_index]))
{
move(get_tail_of_snake(position[player_index]), player_index, position[player_index], is_enabled[player_index], position[!player_index]);
}
imshow("SnakeAndLadders", game);
cv::waitKey(0);
check_for_win(position[player_index], not_finished, player_index);
toggle_player(player_index);
}
}