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: -
void computerturn()
-
{
-
char p1 = 'O';
-
char p2 = 'X';
-
int randnum;
-
-
srand(time(0));
-
randnum = rand()%9;
-
-
while ((board[randnum] != p1) && (board[randnum] != p2))
-
{
-
srand(time(0));
-
randnum = rand()%10;
-
-
if ((board[randnum] != p1) && (board[randnum] != p2))
-
board[randnum] = p2;
-
}
-
}
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
5 2500
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: -
void computerturn()
-
{
-
char p1 = 'O';
-
char p2 = 'X';
-
int randnum;
-
-
srand(time(0));
-
randnum = rand()%9;
-
-
while ((board[randnum] != p1) && (board[randnum] != p2))
-
{
-
srand(time(0));
-
randnum = rand()%10;
-
-
if ((board[randnum] != p1) && (board[randnum] != p2))
-
board[randnum] = p2;
-
}
-
}
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
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.
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
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: - void computerturn()
-
{
-
char p1 = 'O';
-
char p2 = 'X';
-
int randnum;
-
-
srand(time(0));
-
randnum = rand()%9;
-
-
if (board[randnum] == p1)
-
{
-
while (board[randnum] == p1)
-
{
-
srand(time(0));
-
randnum = rand()%10;
-
-
if (board[randnum] != p1)
-
board[randnum] = p2;
-
}
-
}
-
else if (board[randnum] == p2)
-
{
-
while (board[randnum] == p2)
-
{
-
srand(time(0));
-
randnum = rand()%10;
-
-
if (board[randnum] != p2)
-
board[randnum] = p2;
-
}
-
}
-
else
-
board[randnum] = p2;
-
}
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...
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: - void computerturn()
-
{
-
char p1 = 'O';
-
char p2 = 'X';
-
int randnum;
-
-
srand(time(0));
-
randnum = rand()%9;
-
-
if (board[randnum] == p1)
-
{
-
while (board[randnum] == p1)
-
{
-
srand(time(0));
-
randnum = rand()%10;
-
-
if (board[randnum] != p1)
-
board[randnum] = p2;
-
}
-
}
-
else if (board[randnum] == p2)
-
{
-
while (board[randnum] == p2)
-
{
-
srand(time(0));
-
randnum = rand()%10;
-
-
if (board[randnum] != p2)
-
board[randnum] = p2;
-
}
-
}
-
else
-
board[randnum] = p2;
-
}
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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 -...
|
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....
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |