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
|
#include <iostream>
#include <string>
#include <time.h>
#include <cstdlib>
using namespace std;
void move(int, int, bool); //moving the player
int dice(); //rolling dice function
bool startGame(int);
enum PlayerColor { yellow, blue, red, green };
struct Player
{
string name;
PlayerColor color;
};
int board[50];
Player members[4];
int main()
{
bool endGame = false;
int currentPlayers = 1;
int numOfPlayers; //store the number of players
Player p0;
Player p1;
Player p2;
Player p3;
cout << "Select Players 1 - 4: ";
cin >> numOfPlayers;
if (numOfPlayers == 2) //determing how many players there will be
{ // and saving them to the array
members[0] = p0; //along with gathering names for the players.
members[1] = p1;
cin.ignore();
cout << "P1 Name: ";
getline(cin, p0.name);
cout << "P2 Name: ";
getline(cin, p1.name);
}
if (numOfPlayers == 3)
{
members[0] = p0;
members[1] = p1;
members[2] = p2;
cin.ignore();
cout << "P1 Name: ";
getline(cin, p0.name);
cout << "P2 Name: ";
getline(cin, p1.name);
cout << "P3 Name: ";
getline(cin, p2.name);
}
if (numOfPlayers == 4)
{
members[0] = p0;
members[1] = p1;
members[2] = p2;
members[3] = p3;
cin.ignore();
cout << "P1 Name: ";
getline(cin, p0.name);
cout << "P2 Name: ";
getline(cin, p1.name);
cout << "P3 Name: ";
getline(cin, p2.name);
cout << "P4 Name: ";
getline(cin, p3.name);
}
if (numOfPlayers == 1 || numOfPlayers > 4)
{
cout << "There must be atleast 2 players and no more then 4 players"; //checking for correct number of players
system("pause");
}
do
{
int roll1 = dice(); //rolling dice
int roll2 = dice();
bool doubles = false;
int total = roll1 + roll2; //save the value of the dice
if (roll1 == roll2) //test for a double
{
doubles = true; //if true
int roll1 = dice(); //dice will be rolled again
int roll2 = dice();
total = total + roll1 + roll2; //and the sum of the second roll will be added to the first roll
}
move(currentPlayers, total, doubles);
if (currentPlayers > numOfPlayers) //determining when its player 1 turn
{
currentPlayers = 1;
}
endGame = true;
} while (!endGame);
}
void move(int currentPlayer, int total, bool doubles)
{
int currentPosition = 0;
for (int i = 0; i < 50; i++)
{
if (board[i] == currentPlayer)
{
currentPosition = i;
}
}
int movePosition = currentPosition + total;
int positionOccupy = board[movePosition];
switch (total)
{
case 2: if (currentPosition == 0) //checking if player is on board and if not
{ //then the current position will be changed to 1
currentPosition = 1;
}
if (currentPosition > 0) //if they are on the board then player will move two.
{
total = 2;
}
break;
case 3:
case 5:
case 6:
case 8:
case 9:
case 10:
break;
case 4: currentPosition = currentPosition - 1; //send player back one
break;
case 7:
break;
case 11:
break;
case 12: board[currentPosition] = 0; //send players current position to zero.
}
}
bool startGame(int currentPlayer) //checks the board for the player
{
bool find = true;
for (int i = 0; i < 50; i++)
{
if (board[i] == currentPlayer)
{
find = false;
}
}
return find;
}
int dice()
{
int value;
const int sides = 6;
const int min = 1;
value = (rand() % (sides - min + 1)) + min;
return value;
}
|