Forum Topic: Where to put rand( ) and srand( )?

(177 views • 8 replies)

This topic is 1 page long.

<< < > >>
None

fallensoul289

Reply To Post Reply & Quote

Posted at: 10/13/09 12:42 PM

fallensoul289 EVIL LEVEL 18

Sign-Up: 09/11/07

Posts: 1,083

I have a problem with this project I am doing. I made a win32 file that is a game of tic tac toe. It works fine but now instead of having it be able to be played with 2 people I need to make it 1 player where the computer plays against the human player. I know I have to put rand( ) and srand( ) functions in the code but I have no clue where to. To help you guys out there is a total of 8 moves and you choose a move by picking a number 1-8 and hitting enter. Can anyone help me out? Here is my code.

// Include the libraries

#include <iostream>

#include <string>

 

//Use the standard namespace

using namespace std;

 

// Declare global variables

char Board[9];

 

// Declare functions

void showBoard ( );

bool moveIsValid (int m);

int whoWon ( ); //Returns 0 if no one has won, 1 if player 1 has won, and 2 if computer has won

 

void main ( )

{

   // Declare local variables

   string Player_1_Name;

   string Computer_Name;

   int Whose_Turn = 1; // 1 means it's player 1's turn, 2 means it's the computer's turn

   int Move; // Stores where the player wants to move

   int Total_Moves = 0;

 

   //Assign values to the playing board

   Board[0] = '0';

   Board[1] = '1';

   Board[2] = '2';

   Board[3] = '3';

   Board[4] = '4';

   Board[5] = '5';

   Board[6] = '6';

   Board[7] = '7';

   Board[8] = '8';

 

   // Get player names

   cout << "Player 1: Please enter your name." << endl;

   cin >> Player_1_Name;

   cout << "Please enter the computer's name." << endl;

   cin >> Computer_Name;

 

   while (whoWon ( ) == 0 && Total_Moves < 9)

   {

      // Do this until the player chooses a valid move

      do

      {

         // Show the board

         showBoard ( );

 

         // Tell which player to move

         if (Whose_Turn == 1)

         {

            cout << Player_1_Name << ": It's your turn." << endl;

         }

         else

         {

            cout << Player_2_Name << ": It's the computers turn." << endl;

         }

         // Get the move

         cout << "Enter the number of the spot where you'd like to move." << endl;

         cin >> Move;

      } while (moveIsValid (Move) != true);

 

      // Add 1 to Total_Moves

      Total_Moves++;

 

      // Change whose turn it is

      switch (Whose_Turn)

      {

      case (1):

         {

            Board[Move] = 'x';

            Whose_Turn = 2;

            break;

         }

      case (2):

         {

            Board[Move] = 'o';

            Whose_Turn = 1;

         }

      }

   }

   // Show the board

   showBoard ( );

 

   if (whoWon ( ) == 1)

   {

      cout << Player_1_Name << " has won the game!" << endl;

   }

   else if (whoWon ( ) == 2)

   {

      cout << Computers_Name << " has won the game!" << endl;

   }

   else

   {

      cout << "It's a tie game!" << endl;

   }

 

   system ("PAUSE");

}

 

void showBoard ( )

{

   cout << endl;

   cout << Board[0] << " | " << Board[1] << " | " << Board[2] << endl;

   cout << "--+---+--" << endl;

   cout << Board[3] << " | " << Board[4] << " | " << Board[5] << endl;

   cout << "--+---+--" << endl;

   cout << Board[6] << " | " << Board[7] << " | " << Board[8] << endl;

   cout << endl;

}

 

bool moveIsValid (int m)

{

   if (Board[m] != 'x' && Board[m] != 'o')

   {

      return true;

   }

   else

   {

      return false;

   }

}

 

int whoWon ( )

{

   if (Board[0] == Board[1] && Board[1] == Board[2])

   {

      if (Board[0] == 'x')

      {

         return 1;

      }

      else

      {

         return 2;

      }

   }

   if (Board[3] == Board[4] && Board[4] == Board[5])

   {

      if (Board[3] == 'x')

      {

         return 1;

      }

      else

      {

         return 2;

      }

   }

   if (Board[6] == Board[7] && Board[7] == Board[8])

   {

      if (Board[6] == 'x')

      {

         return 1;

      }

      else

      {

         return 2;

      }

   }

   if (Board[0] == Board[3] && Board[3] == Board[6])

   {

      if (Board[0] == 'x')

      {

         return 1;

      }

      else

      {

         return 2;

      }

   }

   if (Board[1] == Board[4] && Board[4] == Board[7])

   {

      if (Board[1] == 'x')

      {

         return 1;

      }

      else

      {

         return 2;

      }

   }

   if (Board[2] == Board[5] && Board[5] == Board[8])

   {

      if (Board[2] == 'x')

      {

         return 1;

      }

      else

      {

         return 2;

      }

   }

   if (Board[0] == Board[4] && Board[4] == Board[8])

   {

      if (Board[0] == 'x')

      {

         return 1;

      }

      else

      {

         return 2;

      }

   }

   if (Board[2] == Board[4] && Board[4] == Board[6])

   {

      if (Board[2] == 'x')

      {

         return 1;

      }

      else

      {

         return 2;

      }

   }

   return 0;

}

|Add me on XBL| My flash's|Thanks to SupraAddict for the awesome sig!

BBS Signature

None

Cinjection

Reply To Post Reply & Quote

Posted at: 10/13/09 02:04 PM

Cinjection LIGHT LEVEL 18

Sign-Up: 04/06/04

Posts: 2,490

rand() will generate a random number between 0 and a predefined constant. You need a number between 1 and 8, so you'll need to do some math. Specifically, reduce the random number mod 8 and shift up by one. More specifically,

int computer_move = rand() % 8 + 1;

This will generate the random number. You will need to generate a new number every time the computer wants to make a move.

The seed (srand()) only needs to happen once, at the beginning of your program. That is, you should put this function call at the top of main.

The best part about going on Newgrounds is that no one know that you're naked!
[My Coder Profile]

BBS Signature

None

henke37

Reply To Post Reply & Quote

Posted at: 10/14/09 09:08 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,647

Just remember to use something that's actually random as the seed.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

Cinjection

Reply To Post Reply & Quote

Posted at: 10/14/09 12:32 PM

Cinjection LIGHT LEVEL 18

Sign-Up: 04/06/04

Posts: 2,490

At 10/14/09 09:08 AM, henke37 wrote: Just remember to use something that's actually random as the seed.

Right. For example, you could seed the random number using the current time:

#include <ctime>
#include <cstdlib>

srand( time(0) );

The best part about going on Newgrounds is that no one know that you're naked!
[My Coder Profile]

BBS Signature

None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 10/14/09 10:08 PM

Glaiel-Gamer NEUTRAL LEVEL 28

Sign-Up: 12/28/04

Posts: 8,070

FYI, 0 to 8 is 9 moves total, not 8


None

Cinjection

Reply To Post Reply & Quote

Posted at: 10/14/09 10:53 PM

Cinjection LIGHT LEVEL 18

Sign-Up: 04/06/04

Posts: 2,490

At 10/14/09 10:08 PM, Glaiel-Gamer wrote: FYI, 0 to 8 is 9 moves total, not 8

I believe that the move numbers are 1 to 8, not 0 to 8.

The best part about going on Newgrounds is that no one know that you're naked!
[My Coder Profile]

BBS Signature

None

henke37

Reply To Post Reply & Quote

Posted at: 10/15/09 01:23 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,647

Except that the current time is not random at all.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

Cinjection

Reply To Post Reply & Quote

Posted at: 10/15/09 11:31 AM

Cinjection LIGHT LEVEL 18

Sign-Up: 04/06/04

Posts: 2,490

At 10/15/09 01:23 AM, henke37 wrote: Except that the current time is not random at all.

What would be a better seed? I think in this scenario, the current time would make an adequate seed.

The best part about going on Newgrounds is that no one know that you're naked!
[My Coder Profile]

BBS Signature

None

ParoXoN

Reply To Post Reply & Quote

Posted at: 10/15/09 06:26 PM

ParoXoN LIGHT LEVEL 01

Sign-Up: 10/28/07

Posts: 3

At 10/15/09 01:23 AM, henke37 wrote: Except that the current time is not random at all.

No, but it's a unique seed value for the PRNG.

@OP:
srand() means "seed (pseudo) random number generator". Computers are very bad at generating truly random numbers, so they use tricks to make number that appear random. The value you pass to srand() will determine the set of numbers rand() will return.

That means if you use the same value for srand() over and over again, the same set of numbers will be returned by rand() and your program will do the same thing everytime.

By using time(0) as your seed you guarantee that the sequence is unique every time.


All times are Eastern Standard Time (GMT -5) | Current Time: 06:11 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!