lundi 23 novembre 2015

Program not randomly generating results

This is a board game created using C++. In order to win, one player must be able to reach the end of the board three times. But the problem is that it is the the first player who is entered into the board game that is always winning. I have a function setup so the that the simulated dice uses a random number-generator. How can I fix this issue?

#include <iostream>
using namespace std;
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <string>
#define SIZE 20 //EDITED: SIZE is Defined at 20 for the number of squares  wanted for the board game. Makes is easier for change


   //NOTE TO SELF: This is the revamped version. "EDITED" marks addition/change from 'assignment2_fixed.cpp

 /**********************
 * FUNCTION PROTOTYPES *
 ***********************/       

void displayRules();
void action();
void firstRoll();
void showState();
int spinDie();
bool play();
bool winner();

int square[SIZE]; //For each square on the board, SIZE is Defined
const int loseTurn=1, switchPlace=2, goAgain=3, moveBack=4; //CONSTANT!  Labels each action location for each square
int *location, *lastsq, *losTurn; //EDITED: Declared for dynamic allocation for location on board, last square of board, and lost turn
string *names; //EDITED: This is to hold the names of the players 
int numPlayers = 0; //Holds the number of players 
int turn; //EDITED: First players turn

/************
* FUNCTIONS *
*************/

   void displayRules(){ //Displays the rules to the player once the game starts
    cout << "Welcome to 3X" << endl;
    cout << "The first player to go around the board three times wins!" <<endl;
    cout << "The player with the lowest number goes first" << endl;
    cout << "The players take turns until one player wins" << endl;
    cout << "The players must move forward the number of spaces the die displays" << endl;
    cout << "If the player lands on a square with an action, the player must perform that action, otherwise the next player goes" <<endl;
    cout << endl << endl; 
   }

void action(){ //EDITED: This lists actions for each square
square[2] = goAgain;
square[4] = switchPlace;
square[6] = goAgain;
square[8] = loseTurn;
square[11] = goAgain;
square[14] = moveBack;
square[16] = switchPlace;
square[18] = loseTurn;
 }

void firstRoll(){ //EDITED: Initializes for the first die roll

numPlayers = spinDie();

for(int i=0; i<numPlayers; i++)
    location[i];
for(int i=0; i<numPlayers; i++)
    lastsq[i];
for(int i=0; i<numPlayers; i++)
    losTurn[i];

 }  

void showState(){
for(int i=0; i<numPlayers; i++){
    cout << "Player " << names[i] << " is at location " << location[i] << endl;
}
for(int i=0; i<numPlayers; i++){
    cout << names[i] << " lands on last space " << lastsq[i] << " times " << endl;
}

 }

int spinDie(){ //For the dice. Number is generated randomly based on time 
    int x = rand()%6 +1 ;
    cout<< " a "<< x<< " was rolled"<<endl;
    return x;
 }

bool play(){ //EDITED: Play function through squares
srand(time(0));
while(true){
    if(losTurn[turn]){
        losTurn[turn] = 0;//EDITED: Finds if player lost turn based on the value of 'turn'
        turn = (turn) % numPlayers;
        continue;
    }

    cout << "Turn: "<< names[turn] << endl;

    int spot = spinDie(); //EDITED: Players spot on the board based on random number generator
    cout << "Number on die : " << spot << endl;

    location[turn] += spot;
    if(location[turn] >= SIZE-1){
        location[turn] -= SIZE;
        lastsq[turn]++;
        cout << names[turn] << " passes End!"<<endl;
        if(winner())
            return true;
    }

    switch(square[location[turn]]){
        case goAgain:
            cout<<"Action: Go again.\n";
            showState();
            break;
        case loseTurn:
            cout<<"Action: Lose turn.\n";
            losTurn[turn] += 1;
            break;
        case switchPlace:
            cout<<"Action: Switch places\n";
            break;
        case moveBack:
            cout<<"Action: Move back 2 places.\n";
            location[turn] -= 2;
            break;
        default:
            cout<<"Action: None\n";
    }

    showState();
    turn = (turn) % numPlayers;
}

return false;
   }

 bool winner(){ //Finds the winner who hits the end square three times
if(lastsq[turn] == 3){
    cout<<"\n"<<names[turn]<<" WIN!\n";//Prints name of player with their turn
    return true;
  }

return false;
  }



/***************
* MAIN PROGRAM *
****************/


int main(){
srand(time(0)); //Makes time based number in die random
displayRules(); //Diplays the rules first

cout << "Please type in the number of players between 1-6 : " << endl; 
cin >> numPlayers;

while ((numPlayers < 1) || (numPlayers > 6)) //EDITED: Guard against any values outside of 1-6
{
   cout << "ERROR: Enter a value from 1 - 6: ";
   cin  >> numPlayers;
   }

names = new string[numPlayers]; //EDITED: Dynamically allocates array of string to hold names of players
location = new int[numPlayers]; //EDITED: Dynamically allocates array of int to hold locations of players
losTurn = new int[numPlayers]; //EDITED: Dynamically allocates array of int to hold lost turn of players
lastsq = new int[numPlayers]; //EDITED: Dynamically allocates array of int to hold count of each time player reach end square

for(int i=0; i<numPlayers; i++){
    cout << "Enter name of player " << i+1 << " " << endl; //EDITED: To get name of each player starting with Player 1 (i+1)
    cin >> names[i]; //EDITED: Gets names of players into the dynamically allocated strings
    }


while(true){//EDITED: Continues the loop
    if(play())//EDITED: If the "Play" function is true
        break;//EDITED: Breaks once the winner is found
  }



 return 0;  

}




Aucun commentaire:

Enregistrer un commentaire