Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <stdlib.h>
- using namespace std;
- struct playerz{
- string name;
- int position;
- int id;
- };
- struct board{
- bool occupied;
- int pid;
- int obstacle;
- };
- int getPlayersCount();
- playerz* getPlayersNames(int);
- void shuffleDeck(int []);
- void getBoard(board*);
- void showState(int, playerz*);
- void taketurn (int, playerz*, int*, int [], board*, bool*, int);
- void switchPlaces(int, playerz*, int);
- void play (playerz*, int, int [], board*);
- int main(){
- board* Board= new board[20];
- int deck[10] = {1,1,2,2,3,3,4,4,0,-1};
- int numPlayers = getPlayersCount();
- playerz* players = getPlayersNames(numPlayers);
- shuffleDeck(deck);
- getBoard(Board);
- play (players, numPlayers, deck, Board);
- delete[] players;
- delete[] Board;
- return 0;
- }
- int getPlayersCount(){
- int num;
- cout << "How many are playing?" << endl;
- cin >> num;
- return num;
- }
- playerz* getPlayersNames(int num){
- playerz* players = new playerz[num];
- for (int i=0;i<num;i++){
- cout << "enter player " << i+1 << "'s name" << endl;
- cin >> players[i].name;
- players[i].position=0;
- players[i].id=i;
- }
- return players;
- }
- void shuffleDeck(int deck[]){
- int temp;
- int pos1;
- int pos2;
- srand (time(NULL));
- for (int i=0; i<5;i++){
- temp=0;
- pos1= rand() % 10;
- pos2= rand() % 10;
- temp=deck[pos1];
- deck[pos1]=deck[pos2];
- deck[pos2]=temp;
- }
- return;
- }
- void getBoard(board* Board){
- for (int i=0;i<20;i++){
- Board[i].obstacle=0;
- Board[i].occupied=false;
- }
- Board[3].obstacle=1;
- Board[8].obstacle=1;
- Board[12].obstacle=2;
- Board[14].obstacle=2;
- Board[17].obstacle=2;
- Board[18].obstacle=3;
- }
- void showState(int numPlayers, playerz* players){
- for (int i=0;i<numPlayers;i++){
- cout << players[i].name << " has id " << players[i].id << " and is in position " << players[i].position << endl;
- }
- return;
- }
- void switchPlaces(int player, playerz* players, int numPlayers){
- int temp;
- int switchy;
- cout << "Switching " << players[player].name << endl;
- if (player==0)
- showState (numPlayers-1, players+1);
- else
- {
- showState (player, players);
- showState (numPlayers-(player+1), players+player+1);
- }
- cout << "Which player id to switch with?" << endl;
- cin >> switchy;
- cout << "Switching " << players[player].name << " and " << players[switchy].name << endl;
- temp=players[player].position;
- players[player].position=players[switchy].position;
- players[switchy].position=temp;
- cout << players[player].name <<" is now at position " << players[player].position << endl;
- cout << players[switchy].name << " is now at position " << players[switchy].position << endl;
- return;
- }
- void taketurn (int player, playerz* players, int *card, int deck[], board* Board[], bool* gameWon, int numPlayers){
- int obstacle;
- int oldpos=players[player].position;
- int newpos;
- if (*card == 10) {
- *card = 0;
- shuffleDeck(deck);
- }
- if (deck[*card] == -1)
- switchPlaces (player, players, numPlayers);
- else {
- players[player].position += deck[*card];
- newpos=players[player].position;
- }
- Board[newpos]->occupied=true;
- Board[newpos]->pid=player;
- if (players[player].position >= 19) {
- *gameWon = true;
- cout << players[player].name << " has won" << endl;
- return;
- }
- obstacle=Board[newpos]->obstacle;
- if (obstacle!=0){
- players[player].position -= obstacle;
- newpos = players[player].position;
- cout << "The obstacle moved " << players[player].name << " back " << obstacle << " spaces" << endl;
- }
- if (Board[newpos]->occupied) {
- cout << "Spot was occupied by " << players[Board[newpos]->pid].name << ", switching places" << endl;
- players[Board[newpos]->pid].position=oldpos;
- Board[oldpos]->pid=Board[newpos]->pid;
- }
- else {
- cout << players[player].name << " drew " << deck[*card] << " and is now in position " << players[player].position << endl;
- Board[oldpos]->occupied=false;
- }
- Board[newpos]->occupied=true;
- Board[newpos]->pid=player;
- (*card)++;
- return;
- }
- void play (playerz* players, int numPlayers, int deck[], board* Board){
- int player = 0;
- int card = 0;
- int i=0;
- bool gameWon = false;
- cout << endl << endl;
- showState(numPlayers,players);
- while (!gameWon){
- cout << endl;
- if (i==numPlayers){
- i=0;
- showState(numPlayers, players);
- cout << endl;
- }
- taketurn (i, players, &card, deck, Board, &gameWon, numPlayers);
- i++;
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement