#include <iostream>
#include <string>
#include <time.h>
#include <cstdlib>
using namespace std;
struct Players {
string firstName;
string lastName;
int token_space;
};
void displayRules();
void play(int board [], int cards [], Players & player1, Players & player2);
void takeTurn(Players & player1, Players & player2, int cards [], int board [], int &available, Players& player);
void showState(Players & player1, Players & player2);
void switchPlaces(Players & player1, Players & player2, int board [], Players& player);
bool gameOver (Players & player1, Players & player2);
void shuffleCards (int cards[], int n);
void getPlayerInformation(Players& player1, Players& player2);
void findFirstPlayer(Players& player1, Players& player2, Players& firstPlayer, Players& secondPlayer);
bool gameNotOver(Players& firstPlayer, Players& secondPlayer, int board [], int cards [], int &available, Players& player);
bool gameIsNotOver(Players& player1, Players& player2, int board [], int cards [], int &available, Players& player);
int main () {
srand(time(0));
int available;
int cards [10] = {1,1,2,2,3,3,4,4,7,8};
int board[20];
for(int i=0; i<20; i++)
board[i] = 0;
board[3] = 2;
board[10] = 1;
board[15] = 3;
board[13] = 1;
board[8] = 2;
board[6] = 1;
Players player1, player2;
displayRules();
play(board, cards, player1, player2);
return 0;
}
void displayRules() {
cout << "Players take turn in order until one player wins.\n";
cout << "A turn consists of picking a card.\n";
cout << "If the card has a number, the player moves that number of spaces.\n";
cout << "If the card says lose turn, the player does nothing, and the the game continues with the next player.\n";
cout << "If the card says switch places, the player must switch places with a player of its choice.\n";
cout << "If the player lands on a space with a number, the player moves back that number of spaces.\n" << endl;
}
void play(int board [], int cards [], Players & player1, Players & player2) {
getPlayerInformation(player1, player2);
Players firstPlayer, secondPlayer;
findFirstPlayer(player1, player2, firstPlayer, secondPlayer);
showState(player1, player2);
}
void switchPlaces(Players & player1, Players & player2) {
int temp = player1.token_space;
player1.token_space = player2.token_space;
player2.token_space = temp;
}
bool gameOver(Players & player1, Players & player2) {
if(player1.token_space >= 19){
cout << "Player 1 WON !!!" << endl;
cout << "His name was: " << player1.firstName << " " << player1.lastName << endl;
return true;
}
if(player2.token_space >= 19){
cout << "Player 2 WON !!!" << endl;
cout << "His name was: " << player2.firstName << " " << player2.lastName << endl;
return true;
}
return false;
}
void takeTurn(Players & player1, Players & player2, int cards [], int board [], int &available, int player) {
if (available==0) {
shuffleCards(cards, 10);
available=10;
}
int turn = cards[available-1];
if(turn > 0 && turn < 5) {
if (player == 1)
player1.token_space += turn;
else
player2.token_space += turn;
}
if (turn == 8) {
switchPlaces(player1, player2);
}
if (turn == 7) {
; // lose a turn
}
}
void showState(Players & player1, Players & player2) {
for(int i=0; i<20; i++){
cout << "cell " << i << ": ";
if(player1.token_space == i)
cout << "player 1 ";
else
cout << '\t';
if(player2.token_space == i)
cout << "player 2 ";
else
cout << '\t';
cout << endl;
if(i == 19)
cout << "H O M E" << endl << endl;
}
cout << "Press enter to make a move";
cin.get();
}
void shuffleCards (int cards [], int n) {
int temp;
for (int i=0; i<n; i++) {
temp=cards[i];
int randomIndex=rand()%10;
cards[i]=cards[randomIndex];
cards[randomIndex]=temp;
}
}
void getPlayerInformation(Players& player1, Players& player2) {
cout << "Enter your first and last name, player1" << endl;
cin >> player1.firstName >> player1.lastName;
cout << "Enter your first and last name, player2 " << endl;
cin >> player2.firstName >> player2.lastName;
cout<<endl;
player1.token_space = 0;
player2.token_space = 0;
}
void findFirstPlayer (Players& player1, Players& player2, Players& firstPlayer, Players& secondPlayer) {
int player = 1;
if (player1.firstName.length() <= player2.firstName.length())
player = 1;
else
player = 2;
}
bool gameNotOver(Players& firstPlayer, Players& secondPlayer, int board [], int cards [], int &available, Players& player) {
int count = 0;
while(gameNotOver(firstPlayer, secondPlayer, board, cards, available, player)) {
if(count%2==0)
takeTurn(firstPlayer, secondPlayer, board, cards, available, player);
else
takeTurn(secondPlayer, firstPlayer, board, cards, available, player);
count++;
}
}
bool gameIsNotOver(Players & player1, Players & player2, int board [], int cards [], int &available, Players& player) {
while (gameIsNotOver(player1, player2, board, cards, available, player)) {
takeTurn(player1, player2, board, cards, available, player);
showState(player1, player2);
if(player = 1)
player = 2;
else
player = 1;
available--;
if(available)
available--;
else
available = 10;
}
}
Comments
Post a Comment