程序不会随机生成结果

时间:2015-11-21 00:52:43

标签: c++ random

这是一款使用C ++创建的棋盘游戏。为了获胜,一名球员必须能够三次到达棋盘的末端。但问题在于,它是第一个进入棋盘游戏的球员,总是赢球。我有一个功能设置,所以模拟骰子使用随机数发生器。我该如何解决这个问题?

#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;  

}

1 个答案:

答案 0 :(得分:0)

虽然你的问题很模糊,但我可以帮你一些帮助。首先,我将在骰子上显示随机因素的代码。

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;

此代码使编译器在整数范围内生成任意数字。然后将该随机数除以6,并使用余数运算符(%)查找余数。如果它是因子6,那么+1将添加到余数中,例如36.你的代码很好。问题在于它是基于时间的。电路板上的第一个可能每次都会得到相似或相同的数字,第二个也可能。这可以解释第一个总是赢的。我假设第一个每次都有类似或相同的卷,因为你对SPECIFIC问题缺乏解释。希望这有点帮助。