-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
189 lines (161 loc) · 4.91 KB
/
main.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
188
/** @file main.cpp
@author William Brege
@date 05/16/2017
This is a recreation of the children's game "Snakes and Ladders", with some creative additions
*/
#include <iostream>
#include "snakesAndHeaders.h"
#include <time.h>
void manualConstruct(board&);
void proceduralGen(board&);
int rollDice();
int main() {
srand(time(nullptr)); //Set a time based seed
//Prepare the game board
board gameBoard;
//manualConstruct(gameBoard);
proceduralGen(gameBoard);
gameBoard.connectTiles();
char response;
std::cout << "How many people are playing (1/2)? " << std::endl;
std::cin >> response;
gameBoard.printBoard();
//Prepare the players
bool AI = true;
player humanPlayer = gameBoard.start(); //Represented by '1' on the board
player AIPlayer = gameBoard.start(); //Represented by '2' on the board
if(response == '2'){
AI = false;
}
else{
AIPlayer.setAI(true);
}
//Begin the game!
while(*humanPlayer != 'E' && *AIPlayer != 'E'){
//////// HUMAN TURN ////////
std::cout << std::endl << std::endl << "Player 1 turn!" << std::endl << "Roll the dice (y/n)? ";
std::cin >> response;
if(response == 'n'){
std::cout << "Player 1 forfeits!" << std::endl;
return 0;
}
//Move the player
int playerRoll = rollDice();
for(int i = 0; i < playerRoll; ++i){
++humanPlayer;
}
std::cout << "Player 1 rolled a " << playerRoll << "!" << std::endl;
//Check for special tiles
humanPlayer.checkTile();
///////// AI TURN //////////
std::cout << std::endl << "Player 2 turn!" << std::endl << std::endl;
//If playing with a human:
if(AI == false){
std::cout << "Roll the dice (y/n)? ";
std::cin >> response;
if(response == 'n'){
std::cout << "Player 2 forfeits!" << std::endl;
return 0;
}
}
//Move the AI
int AIRoll = rollDice();
for(int i = 0; i < AIRoll; ++i){
++AIPlayer;
}
std::cout << "Player 2 rolled a " << AIRoll << "!" << std::endl;
//Check for special tiles
AIPlayer.checkTile();
///////// PRINT THE BOARD //////////
char tempPlayer = *humanPlayer;
*humanPlayer = '1';
char tempAI = *AIPlayer;
*AIPlayer = '2';
gameBoard.printBoard();
*AIPlayer = tempAI;
*humanPlayer = tempPlayer; //In this order it shouldn't matter if both players are on the same tile
}
//Check who is the winner
if(*humanPlayer == 'E'){
std::cout << std::endl << "Congratulations Player 1! You Win!!!" << std::endl;
return 0;
}
else{
std::cout << std::endl << "Congratulations Player 2! You Win!!!" << std::endl;
return 0;
}
return 0;
}
/** Manual board construction, to be replaced by a procedural generation algorithm later
*/
void manualConstruct(board& manualBoard){
manualBoard.push_back('#');
for(int i = 0; i < 5; ++i){
manualBoard.push_back(' ');
}
manualBoard.push_back('#');
for(int i = 0; i < 12; ++i){
manualBoard.push_back(' ');
}
manualBoard.push_back('#');
for(int i = 0; i < 8; ++i){
manualBoard.push_back(' ');
}
manualBoard.push_back('$');
for(int i = 0; i < 15; ++i){
manualBoard.push_back(' ');
}
manualBoard.push_back('#');
for(int i = 0; i < 9; ++i){
manualBoard.push_back(' ');
}
manualBoard.push_back('$');
for(int i = 0; i < 13; ++i){
manualBoard.push_back(' ');
}
manualBoard.push_back('$');
for(int i = 0; i < 10; ++i){
manualBoard.push_back(' ');
}
manualBoard.push_back('$');
for(int i = 0; i < 18; ++i){
manualBoard.push_back(' ');
}
}
/** Rolls two "6-sided dice" and returns the result
*/
int rollDice(){
int die1 = rand()%5 + 1;
int die2 = rand()%5 + 1;
return (die1 + die2);
}
/** Procedurally generates a 100 tile board
*/
void proceduralGen(board& procBoard){
int tileCounter = 2;
while(tileCounter < 85){
int randNum = rand()%14 + 1;
for(int i = 0; i < randNum; ++i){
procBoard.push_back(' ');
}
int randSpec = rand()%4;
if(randSpec == 0){
procBoard.push_back('$');
}
else if(randSpec == 1){
procBoard.push_back('#');
}
else if(randSpec == 2){
procBoard.push_back('?');
}
else if(randSpec == 3){
procBoard.push_back('T');
}
tileCounter += (randNum + 1);
}
int remaining = 100 - tileCounter;
for(int i = 0; i < remaining; ++i){
procBoard.push_back(' ');
}
return;
}