Solved

TIC TAC TOE

Posted on 1998-04-19
7
507 Views
Last Modified: 2010-04-15
I have a program that involves the design of a tic tac toe game. How do you use a random generator to make the computer respond to the players move?
0
Comment
Question by:TOA
  • 3
  • 3
7 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 1250037
Assuming that the players move is a number from 1..N (say 1..9 for tic-tac-toe) then you use
  int move = int(N*rand()/double(RAND_MAX+1)+1);
rand() returns an int 0 .. RAND_MAX -- the multiplication and division gives you 0..N-1, then add 1 to get 1..N


0
 

Expert Comment

by:justcky
ID: 1250038
Assume that the user/player enter a move (1 - N) for the tic-tac-toe game.
declare  a few varibles and constant:

#define FALSE 0
#define TRUE 1

int userMove[9], comMove[9], success, index;
/*for the user move and the position of the computer and success is for checking if the ramdom is same as the user move*/

do
{
           comMove[index] = random (N) + 1;
 /*a for loop should be run here to check all the user move against the     com mover */
          if (comMove[index] != UserMove[index])
                    success = TRUE;
          else
                    success = FALSE;

}while (success == FALSE);

the code above is for the first move if you will like to use it for the next few move than use this as a function and molified the if statement to run a loop to check all the user moves.


0
 

Expert Comment

by:justcky
ID: 1250039
Assume that the user/player enter a move (1 - N) for the tic-tac-toe game.
declare  a few varibles and constant:

#define FALSE 0
#define TRUE 1

int userMove[9], comMove[9], success, index;
/*for the user move and the position of the computer and success is for checking if the ramdom is same as the user move*/

do
{
           comMove[index] = random (N) + 1;
 /*a for loop should be run here to check all the user move against the     com mover */
          if (comMove[index] != UserMove[index])
                    success = TRUE;
          else
                    success = FALSE;

}while (success == FALSE);

the code above is for the first move if you will like to use it for the next few move than use this as a function and molified the if statement to run a loop to check all the user moves.


0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

by:justcky
ID: 1250040
Assume that the user/player enter a move (1 - N) for the tic-tac-toe game.
declare  a few varibles and constant:

#define FALSE 0
#define TRUE 1

int userMove[9], comMove[9], success, index;
/*for the user move and the position of the computer and success is for checking if the ramdom is same as the user move*/

do
{
           comMove[index] = random (N) + 1;
 /*a for loop should be run here to check all the user move against the     com mover */
          if (comMove[index] != UserMove[index])
                    success = TRUE;
          else
                    success = FALSE;

}while (success == FALSE);

the code above is for the first move if you will like to use it for the next few move than use this as a function and molified the if statement to run a loop to check all the user moves.


0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1250041
There is some bad advice in the above...

* random(N) is not standard C and so may well be missing in some implementations.

* the code
  if (comMove[index] != UserMove[index])
    success = TRUE;
  else
    success = FALSE;
is not really good.  better is
  success = comMove[index] != userMove[index];

* similaraly
  } while (success == FALSE);
would be better as
  ) while (! success)

* and the whole logic is flawed anyway.

how about this..

char board[9], move[9];
int i;
int userturn =1 ;
char userpiece='o';
char comppiece='x';
for (i=0;i<9;i++) board[i] = ' '; /* no o or x */
for (i=0;i<9;i++) move[i] = -1; /* no move */
for (i=0;i<9;i++) {
  int which;
  char piece;
  if (i%2 == userturn) {
    piece = userpiece;
    /*
    ask user for which square
    */
  } else {
    do {
      which = int(9*rand()/double(RAND_MAX+1)+1);
    } while (board[i] != ' ');
  }
  move[i] = which;
  board[i] = piece;
  if (i > 4) {
    /*
    check for win
    */
  }
}

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1250042
BTW change
  which = int(9*rand()/double(RAND_MAX+1)+1);
into
  which = int(9*rand()/double(RAND_MAX+1));
as in the code above, move numbers are 0..8 rather than 1..9

0
 

Author Comment

by:TOA
ID: 1250043
I'm sorry guys but I finished the whole program two days ago.

Thanks for your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now