By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,251 Members | 2,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,251 IT Pros & Developers. It's quick & easy.

Help needed WRT text based adaptation of the TicTacToe game.

oedipus
P: 6
I am currently learning java at school and thus far we have oly dealt with the text-based compiler JGrasp; therefore; my attempt to code Tictactoe or "X and O's" game is written as a text based program.

I have made use of a 2 dimensional integer array to represent the 3x3 grid. Each block therefore has a co-ordinate.eg. grid[0][0].Initially all blocks in the grid are equal to zero. The computer uses ones(1) and the player uses twos(2).The user is prompted to enter the row and column number and their choice is entered.

The problemm arises when the computer has to respond by choosig a block to play in. I thought that i could solve this by generating random numbers. The glitch is that when the same set of random co-ordinates is generated it appears as though the computer hasnt played at all. This means that as yet the game is still un-playable.

Expand|Select|Wrap|Line Numbers
  1.       int row3;
  2.       int col3;
  3.       int win;
  4.        public void game()
  5.       {
  6.          display_grid();
  7.          for(int loop=4;loop>=0&&win!=1;loop--){   
  8.             System.out.println("Computers turn:");                            
  9.             int row2=(int)(Math.random()*2+0);                 
  10.             int col2=(int)(Math.random()*2+0);                 
  11.             while(grid[row2][col2]==2||grid[row2][col2]==1){//glitch is somewhere here.
  12.                row3=row2;                                      
  13.                col3=col2;                                      
  14.             }                                                  
  15.             grid[row3][col3]=1; 
  16.             display_grid();
  17.  
There are 16 possible outcomes ,all of which are catered for later in the program. I would like some suggestions/help to get the computer to stop picking the same position over and over again, and also to ensure that each block can only be used once.

Thank you.
Aug 9 '10 #1

✓ answered by Frinavale

The reason you're seeing this error is because Math.random() returns a Random Object which is not an int.

You use this Random object to get a random int (or you could use it to get a random double etc...it depends on your needs but in this case you're looking for an int)

So, you need a random integer in the range 0 to 2. The Random Object's "nextInt" method can give you this.

Make sure that you have:
Expand|Select|Wrap|Line Numbers
  1. import java.util.Random;
Or:
Expand|Select|Wrap|Line Numbers
  1.  import java.util.*;


Ok, now create a new instance Random class so that you can use this instances to call the "nextInt()" method. This method will return a random int. You can specify the range by providing the upper limit to the nextInt() method...so to return an int within the range of 0 and 2 you would provide the nextInt method with "3" (RandomInstance.nextInt(3))...


Expand|Select|Wrap|Line Numbers
  1.   Random generator = new Random();
  2.   int row;
  3.   int column;
  4.   do{
  5.     row = generator.nextInt(3);
  6.     column = generator.nextInt(3);
  7.   }while(grid[row][column]>0)
  8.  
-Frinny

Share this Question
Share on Google+
10 Replies


Frinavale
Expert Mod 5K+
P: 9,731
You need to provide a "seed" to the random method. (See Math.Random(seed) for more info).

If you don't specify a different seed each time then the same random number will be the result.

Try using the current date/time as the seed for the random method (it's always changing)
Aug 9 '10 #2

oedipus
P: 6
Frinavle-
Thank you for your time , however I think that I may have left something to be desired in the phrasing of my question. What I actually wanted to know was how to get the program to stop overwriting blocks on the grid that already have a value.
The information that you gave me was extremely interesting and I have read up on it. I have no doubt that at some later juncture it will prove invaluable. I will be extremely grateful for any help regarding the problem stated in this reply-i.e.How do I get the program to stop overwriting elements in the array that already have a value?
Aug 11 '10 #3

Frinavale
Expert Mod 5K+
P: 9,731
Initialize your array with an invalid number like -1 or 0 or something (since your users are using 1 and 2).

Now, only write a 1 or 2 in the array if that particular element doesn't have a 1 or 2 already in it (as long as it's less than 1). If there is a 1 or 2 already in the array at the element selected, tell the user that they have to choose a different spot (or if it's the computer's turn, have it generate a new random selection until it was successful)

-Frinny
Aug 11 '10 #4

oedipus
P: 6
-Frinavale
Thank you once again for your insight.I have already initialised all the elements intthe array to 0.The problem I am currently experiencing is that I cannot find the right code/syntax to do what you have suggested.
The way I see it is as follows:
-In a do-while loop(is that the correct loop?)generate the random element.
-Then check whether the value of the random element is <1.
--If it is less than 1, the loop will terminate and then after the loop, assign the value 1 to the element.
--If it is greater than 1, the loop will continue to run until an element is generated that is less than 1.

Is this what it should look like?
Also, could you please show me an example of the syntax used to seed a random integer number using seconds (I've tried it but keep getting an error.)?
Expand|Select|Wrap|Line Numbers
  1. //grid is the name of the 2d array.
  2. int row;
  3. int column;
  4. do{
  5. row=(int)(Math.random()*2+0);
  6. column=(int)(Math.random()*2+0);
  7. }while(grid[row][column]>0);
  8. grid[row][column]=1;
  9.  
Thanks-Oedipus
Aug 12 '10 #5

Frinavale
Expert Mod 5K+
P: 9,731
What is the error message that you are seeing?
Aug 12 '10 #6

oedipus
P: 6
-Frinavale
I cannot recall exactly what it was as I tried it out a while ago . It was something like "Random cannot be applied to int". I think I may have also messed up in getting the current time.
Aug 12 '10 #7

Frinavale
Expert Mod 5K+
P: 9,731
The reason you're seeing this error is because Math.random() returns a Random Object which is not an int.

You use this Random object to get a random int (or you could use it to get a random double etc...it depends on your needs but in this case you're looking for an int)

So, you need a random integer in the range 0 to 2. The Random Object's "nextInt" method can give you this.

Make sure that you have:
Expand|Select|Wrap|Line Numbers
  1. import java.util.Random;
Or:
Expand|Select|Wrap|Line Numbers
  1.  import java.util.*;


Ok, now create a new instance Random class so that you can use this instances to call the "nextInt()" method. This method will return a random int. You can specify the range by providing the upper limit to the nextInt() method...so to return an int within the range of 0 and 2 you would provide the nextInt method with "3" (RandomInstance.nextInt(3))...


Expand|Select|Wrap|Line Numbers
  1.   Random generator = new Random();
  2.   int row;
  3.   int column;
  4.   do{
  5.     row = generator.nextInt(3);
  6.     column = generator.nextInt(3);
  7.   }while(grid[row][column]>0)
  8.  
-Frinny
Aug 12 '10 #8

oedipus
P: 6
-Frinavale
Thank you,thank you and thank you.
It turns out that was all I really needed.I truly appreciate your help.
On the topic of my avatar, its actually a My Chemical Romance avatar. Perhaps the owner of the site is a fan?

Many, many thanks-Oedipus
Aug 12 '10 #9

Frinavale
Expert Mod 5K+
P: 9,731
No problem :)

You have the possibility of running into an endless loop with your current code. I hope you are checking to make sure that a move can be made before running :)

Off topic: it turns out there was some strangeness going on with my browser that made some people's avatars look like the owners...After clearing cache & refreshing the browser your real avatar was reviled to me....I still think there was a weak conspiracy to make me look crazy!
Aug 12 '10 #10

oedipus
P: 6
-Frinavale
I will check and I plan to make it a bit more aesthetically appealing before revealing it.Thanks for all the help.

Off topic:hmm.Conspiracy?Dont worry ,if there was a conspiracy, it didnt work.lol
Aug 13 '10 #11

Post your reply

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