-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
187 lines (121 loc) · 4.03 KB
/
game.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
#include<bits/stdc++.h>
#include "snake.cpp"
#include "ladder.cpp"
#include "player.cpp"
using namespace std ;
class Game{
int *cardBoard;
int noOfBlocks;
int noOfPlayers ;
Player *playersList ;
Snake *snakes;
Ladder *ladders;
public:
Game(int noOfPlayers=1 , int noOfBlocks=100){
// declare a cardBoard
this->noOfBlocks = noOfBlocks ;
cardBoard = new int[noOfBlocks+1] ;
// player count
this->noOfPlayers = noOfPlayers ;
playersList = new Player[this->noOfPlayers+1] ;
// set position of all to 0.
for(int player=1 ; player<= this->noOfPlayers ; player++){
Player playerObject(player) ;
// cout<<playerObject.getPlayerNumber()<<" "<<playerObject.getPlayerPosition()<<endl;
playersList[player] =playerObject ;
}
// ladder mapping
ladders = new Ladder() ;
// snake mapping
snakes = new Snake() ;
}
void displayRules(){
cout<<"<-- Welcome to Snake and Ladder -->"<<endl<<endl<<endl;
cout<<"***** RULES *****"<<endl<<endl;
cout<<"1. This is a commond line interface game "<<endl<<endl;
cout<<"2. Press T to throw dice"<<endl;
cout<<"3. If sanke is present on a block then you get demoted.. Sad hahhahah"<<endl;
cout<<"4. If ladder is present on a block then you will jump on ladder and ride"<<endl;
cout<<"5 You have to to reach "<<this->noOfBlocks<<" to be a WINNER. Neither less nor more. @_@"<<endl<<endl;
cout<<"--------------------------------------------"<<endl<<endl<<endl;
}
void displayCurrentState(){
for(int player=1 ; player<=this->noOfPlayers ; player++ ){
cout<<">>> player-" << playersList[player].getPlayerNumber()<<" ---> "<<playersList[player].getPlayerPosition()<<endl;
}
cout<<endl<<endl;
}
void startCustomGame(){
cout<<"Not implemented"<<endl ;
}
int getBinaryInput(){
int input ;
cin >> input ;
switch (input){
case 0 :
cout<<"You have selected [0]"<<endl;
break;
case 1 :
cout<<"You have selected [1]"<<endl;
break;
default:
cout<<"Wrong input , select 1 or 0";
input = getBinaryInput() ;
}
return input ;
}
bool isValidBlock(int blockNumber){
return blockNumber>=1 && blockNumber<=this->noOfBlocks ;
}
bool turn(Player &player){
int diceResult = player.throwDice() ;
int currentPositiion = player.getPlayerPosition();
int nextPosition = currentPositiion + diceResult ;
if(!isValidBlock(nextPosition)){
cout<<"Oops try again , "<<nextPosition<<" is not present in board"<<endl;
return false;
}
if(snakes->isSnake(nextPosition)){
cout<<"---Snake baby @_@---"<<endl<<endl;
nextPosition = snakes->snakeBite(nextPosition) ;
}
else if(ladders->isLadder(nextPosition)){
cout<<"---Ladder is here @_@---"<<endl<<endl;
nextPosition = ladders->rideLadder(nextPosition) ;
}
player.setPlayerPosition(nextPosition);
int anyWinner =false ;
if(player.isWinner(this->noOfBlocks)){
anyWinner = true ;
cout<< endl<<endl<<"Player--"<<player.getPlayerNumber()<<" is the WINNER of game"<<endl ;
}
return anyWinner;
}
void startGame(){
this->displayRules() ;
cout<<"Do you want play on Default snake and ladder setting"<<endl<<endl;
cout<<"Press 0 to yes or press 1 to enter custom setting"<<endl<<endl;
int customSetting = getBinaryInput() ;
if(customSetting){
startCustomGame() ;
return ;
}
cout<<"Setting on Default setting ..."<<endl ;
cout<<endl;
// this->diceResult();
bool searchingForWinner = false ;
while(!searchingForWinner){
for(int player =1 ;player <= noOfPlayers ; player++){
searchingForWinner= this->turn( playersList[player] ) ;
this->displayCurrentState();
if(searchingForWinner)
break;
// printf("\033[2J"); //clear full
// printf("\033[1;1H"); // point to first
}
cout<<endl;
}
cout<<" <---- Thanks ----> "<<endl;
cout<<" Developer ---Deepak Kumar--- "<<endl;
}
};