473,394 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

While loop error in C++

Reika
5
Hey there, I posted once before with help and everyone did a good job guiding me.

I come now with another C++ question, using CodeWarrior 5
I'm making a tic tac toe game. The stage I am at now (there are three) is to make a human player vs. a computer player that moves randomly. (The next stage is with strategy)

However, I can't seem to figure out why it doesn't heed my loop to not place a random number over top a character that is already filled in. Here is the code:

Expand|Select|Wrap|Line Numbers
  1. void computerturn()
  2. {
  3.     char p1 = 'O';
  4.     char p2 = 'X';
  5.     int randnum;
  6.  
  7.     srand(time(0));
  8.     randnum = rand()%9;
  9.  
  10.     while ((board[randnum] != p1) && (board[randnum] != p2))
  11.     {
  12.         srand(time(0));
  13.         randnum = rand()%10;
  14.  
  15.         if ((board[randnum] != p1) && (board[randnum] != p2))
  16.             board[randnum] = p2;
  17.     }
  18. }
So the focus is the function for the computer's turn. Is the problem in my loop conditions? What am I missing?

Thanks in advance!
~Reika
May 16 '07 #1
5 2500
AdrianH
1,251 Expert 1GB
Hey there, I posted once before with help and everyone did a good job guiding me.

I come now with another C++ question, using CodeWarrior 5
I'm making a tic tac toe game. The stage I am at now (there are three) is to make a human player vs. a computer player that moves randomly. (The next stage is with strategy)

However, I can't seem to figure out why it doesn't heed my loop to not place a random number over top a character that is already filled in. Here is the code:

Expand|Select|Wrap|Line Numbers
  1. void computerturn()
  2. {
  3.     char p1 = 'O';
  4.     char p2 = 'X';
  5.     int randnum;
  6.  
  7.     srand(time(0));
  8.     randnum = rand()%9;
  9.  
  10.     while ((board[randnum] != p1) && (board[randnum] != p2))
  11.     {
  12.         srand(time(0));
  13.         randnum = rand()%10;
  14.  
  15.         if ((board[randnum] != p1) && (board[randnum] != p2))
  16.             board[randnum] = p2;
  17.     }
  18. }
So the focus is the function for the computer's turn. Is the problem in my loop conditions? What am I missing?

Thanks in advance!
~Reika
There a few things I can comment on here.
  • you don’t need to reseed every time you want to get a random number. You can, but I think (at least in this case) it is unnecessary.
  • Your loop will never terminate.

    Tell me, what happens at line 10.

    Ok, now tell me what happens at line15-16 in that same iteration.

    Now tell me what happens when execution goes back to 10 again.

  • line 13 doesn’t look right :nudge: :nudge: ;) ;) :cough: :cough: :hint: :hint:
Good luck,


Adrian
May 16 '07 #2
Reika
5
Hmmm.
Line 10 was meant to keep the loop running while the random number corresponded to a board space already taken by an 'X' or 'O'
I see now that it makes no sense.

Line 15-16 was meant to change the space the random number generated to an 'X' if it was not already occupied by an 'X' or an 'O'

I see now that these things overlap and are not really solving the problem.
I'll try and rework this out.
May 17 '07 #3
AdrianH
1,251 Expert 1GB
Hmmm.
Line 10 was meant to keep the loop running while the random number corresponded to a board space already taken by an 'X' or 'O'
I see now that it makes no sense.

Line 15-16 was meant to change the space the random number generated to an 'X' if it was not already occupied by an 'X' or an 'O'

I see now that these things overlap and are not really solving the problem.
I'll try and rework this out.
Personally, I'd skip the random value and put it to some single value that is a marker for 'not x or y'. It is easier to determine if nothing is occupying that spot.


Adrian
May 17 '07 #4
Reika
5
Personally, I'd skip the random value and put it to some single value that is a marker for 'not x or y'. It is easier to determine if nothing is occupying that spot.


Adrian
I need to have the random value, as the point of the project is to have the computer randomly select a spot to move to. The random number is that spot, but I need to make it not write over a space already there.

I changed my code to this:
Expand|Select|Wrap|Line Numbers
  1. void computerturn()
  2. {
  3.     char p1 = 'O';
  4.     char p2 = 'X';
  5.     int randnum;
  6.  
  7.     srand(time(0));
  8.     randnum = rand()%9;
  9.  
  10.     if (board[randnum] == p1)
  11.     {
  12.         while (board[randnum] == p1)
  13.         {
  14.             srand(time(0));
  15.             randnum = rand()%10;
  16.  
  17.             if (board[randnum] != p1)
  18.                 board[randnum] = p2;
  19.         }
  20.     }
  21.     else if (board[randnum] == p2)
  22.     {
  23.         while (board[randnum] == p2)
  24.         {
  25.             srand(time(0));
  26.             randnum = rand()%10;
  27.  
  28.             if (board[randnum] != p2)
  29.                 board[randnum] = p2;
  30.         }
  31.     }
  32.     else
  33.         board[randnum] = p2;
  34. }
I had some very wierd problems with the random numbers, but now it works.... Only for a short while though. It'll stop the entire program randomly in the game before a winning condition is met (computer wins, tie, or human wins).

No error messages or anything, and I can't figure out the problem.
It may be that it has an error when the space it wants to move to is occupied...
May 17 '07 #5
AdrianH
1,251 Expert 1GB
I need to have the random value, as the point of the project is to have the computer randomly select a spot to move to. The random number is that spot, but I need to make it not write over a space already there.
If you have three values, X, Y and Unoccupied, it will not affect the chance of you overwriting that spot, and it is eaiser to determine if a spot is Unoccupied (only one test is preformed instead of two).
I changed my code to this:
Expand|Select|Wrap|Line Numbers
  1. void computerturn()
  2. {
  3.     char p1 = 'O';
  4.     char p2 = 'X';
  5.     int randnum;
  6.  
  7.     srand(time(0));
  8.     randnum = rand()%9;
  9.  
  10.     if (board[randnum] == p1)
  11.     {
  12.         while (board[randnum] == p1)
  13.         {
  14.             srand(time(0));
  15.             randnum = rand()%10;
  16.  
  17.             if (board[randnum] != p1)
  18.                 board[randnum] = p2;
  19.         }
  20.     }
  21.     else if (board[randnum] == p2)
  22.     {
  23.         while (board[randnum] == p2)
  24.         {
  25.             srand(time(0));
  26.             randnum = rand()%10;
  27.  
  28.             if (board[randnum] != p2)
  29.                 board[randnum] = p2;
  30.         }
  31.     }
  32.     else
  33.         board[randnum] = p2;
  34. }
I had some very wierd problems with the random numbers, but now it works.... Only for a short while though. It'll stop the entire program randomly in the game before a winning condition is met (computer wins, tie, or human wins).

No error messages or anything, and I can't figure out the problem.
It may be that it has an error when the space it wants to move to is occupied...
*cough* *cough* line 26 & 15 *cough* *cough* ;)


Adrian
May 17 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
4
by: muser | last post by:
I have a logical error in my program, I have submitted the program and my tutor hasn't listed the other problems with the code, but said that the program won't run because of a while statement....
4
by: James E Koehler | last post by:
I can't get the WHILE statement to work in MySQL. The version of MySQL that I am using is: Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32) running on Windows MX. Here is the relevant...
52
by: Rick | last post by:
Hi, For portability, can an issue arise if we use while(1) for an infinite loop in C? If so, should we then use for(;;)? Thanks, Rick
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
2
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with...
5
by: Shawn Minisall | last post by:
I just learned about while statements and get why you place them around inputs for validation, but I'm a little lost on exactly where to place it with what condition in this program where the...
5
by: =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?= | last post by:
Good evening all, I am trying to write a process that uses a while loop to cycle multiple files from an array throught the StreamReader Process. The whole thing works using: Dim...
2
by: cmb3587 | last post by:
I am having a problem with the validation of the account number and password. The beginning of the program asks for users account # then pwd. The program is then supposed to go to a checkID...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.