TicTacToe program - Space taken?
Hi everyone, I apologize for making a new thread but for I wanted to specifically ask "how I should go about coding my program if a space is taken?" Originally it wasn't boolean but after looking at some other codes, I decided to change it into a boolean. I'm almost done with this program finally but this is the only issue. I tried thinking about a do/while loop but couldn't quite get it to work.
I was thinking about doing this but x isn't put into a variable.
Code java:
do
{
System.out.println("Your Turn");
System.out.print("Please make a move (0-8): ");
int x = Integer.parseInt(keybd.readLine());
User(a,x);
drawBoard(a);
} while (SpaceTaken(a,x) == true);
Here's my main routine:
Code java:
import java.io.*;
public class Test
{
public static void main(String args[]) throws IOException
{
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
String name;
System.out.println("Hello! Welcome to a one-player version of Tic-Tac-Toe!");
System.out.print("Please enter your name: ");
name = keybd.readLine();
int[] a = new int[9];
int nummoves = 0;
while(true)
{
ExampleBoard();
for (int i = 0; i <= 8; i++)
{
System.out.println("Your Turn");
System.out.print("Please make a move (0-8): ");
int x = Integer.parseInt(keybd.readLine());
User(a,x);
drawBoard(a);
nummoves++;
if (Winner(a) == true)
{
System.out.println("Congratulations " + name + "! You win!");
break;
}
else if (nummoves == 9)
{
System.out.println("Cat's game!");
break;
}
else
System.out.println();
System.out.println("Computer's Turn");
AI(a,x);
drawBoard(a);
System.out.println();
nummoves++;
if (Loser(a) == true)
{
System.out.println("Sorry " + name + "! You Lose!");
break;
}
}
break;
}
}
Sub-routine if the space is taken (only for the user, so far):
Code java:
public static boolean SpaceTaken(int[] a, int x)
{
if (x == 0 && a[0] != 0)
{
System.out.println("Sorry, space is taken!");
return true;
}
else if (x == 1 && a[1] != 0)
{
System.out.println("Sorry, space is taken!");
return true;
}
else if (x == 2 && a[2] != 0)
{
System.out.println("Sorry, space is taken!");
return true;
}
else if (x == 3 && a[3] != 0)
{
System.out.println("Sorry, space is taken!");
return true;
}
else if (x == 4 && a[4] != 0)
{
System.out.println("Sorry, space is taken!");
return true;
}
else if (x == 5 && a[5] != 0)
{
System.out.println("Sorry, space is taken!");
return true;
}
else if (x == 6 && a[6] != 0)
{
System.out.println("Sorry, space is taken!");
return true;
}
else if (x == 7 && a[7] != 0)
{
System.out.println("Sorry, space is taken!");
return true;
}
else if (x == 8 && a[8] != 0)
{
System.out.println("Sorry, space is taken!");
return true;
}
else
return false;
}
Re: TicTacToe program - Space taken?
Wow, I would strongly recommend changing your spaceTaken method. You can directly put x into the index of a, therefore reducing your code down to one if-else statement.
I'm somewhat confused, what is your question? Are you asking where should you put the spaceTaken call in your main method? It should be in a 1.5 loop (or a do while loop) surrounding where you're asking the user for their next move.
Re: TicTacToe program - Space taken?
Quote:
Originally Posted by
helloworld922
Wow, I would strongly recommend changing your spaceTaken method. You can directly put x into the index of a, therefore reducing your code down to one if-else statement.
I'm somewhat confused, what is your question? Are you asking where should you put the spaceTaken call in your main method? It should be in a 1.5 loop (or a do while loop) surrounding where you're asking the user for their next move.
Hi helloworld922, thanks for replying. Basically I was just frustrated with how to get my spaceTaken method to work like it should: loop the user input if the spot is taken already. I already knew where to put it but I wasn't quite sure how to code a working method. I tried a do while loop using the code I had already (for testing purposes) but it ended up looping just the user input. This may have been because I placed it in the wrong spot but I'll take what you've said in mind and work on it tomorrow since it's pretty late.
*edit* So I wasn't really sure what you meant by being able to "directly put x into the index of a" but I changed my method to the following.
Code java:
public static boolean SpaceTaken(int[] a, int i)
{
if (a[i] != 0)
{
System.out.println("Sorry, space is taken!");
return true;
}
else
{
return false;
}
}
Although the code is shorter, it still pretty much does what the other one did before (I guess since I haven't changed much except shortened it).
I also added the do while loops for each of the inputs but I got confused about the true/false statements, so I apologize if they're flip-flopped. Here's the main with the added do whiles:
Code java:
public static void main(String args[]) throws IOException
{
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Hello! Welcome to a one-player version of Tic-Tac-Toe!");
System.out.print("Please enter your name: ");
String name = keybd.readLine();
int[] a = new int[9]; // Creates an array to store 'X' and 'O'
int nummoves = 0; // Initializes the # of turns
while(true)
{
ExampleBoard(); // Creates initial board for user to refer to
for (int i = 0; i <= 8; i++)
{
do
{
System.out.println("Your Turn");
System.out.print("Please make a move (0-8): ");
int x = Integer.parseInt(keybd.readLine());
User(a,x);
drawBoard(a);
} while (SpaceTaken(a,i) == false);
nummoves++;
if (Winner(a) == true) // Checks winning conditions
{
System.out.println("Congratulations " + name + "! You win!");
break;
}
else if (nummoves == 9) // Checks for a tie
{
System.out.println("Cat's game!");
break;
}
else
{
System.out.println();
System.out.println("Computer's Turn");
do
{
AI(a);
drawBoard(a);
System.out.println();
} while (SpaceTaken(a,i) == false);
nummoves++;
if (Loser(a) == true) // Checks for losing conditions
{
System.out.println("Sorry " + name + "! You Lose!");
break;
}
}
}
break;
}
}
Re: TicTacToe program - Space taken?
Well... I've changed my subroutine once again but I still haven't gotten it to work. I've tried just about every variable I have and I still can't seem to get it right.
At the moment, it is the following but it basically did what I had before. Also, in my main, I changed the while condition to true since the ai won't work otherwise.
I honestly don't know what to do at this point. I've been googling, trying to find something like this but without much luck. I get the feeling it's really easy to do but for some reason I'm just not getting it.
Code java:
public static boolean SpaceTaken(int[] a, int i)
{
if (a[i] == 'X' || a[i] == 'O')
{
System.out.println("Sorry, space is taken!");
return true;
}
else
{
return false;
}
}
Re: TicTacToe program - Space taken?
1. Don't use an int array if you're trying to store characters. There's conveniently a character data type.
2. Are you sure you're populating the array a with 'X' and 'O' (or even at all)? Java is case sensitive. Please post the code for User(a,x);. It's difficult to determine what's going on without it. And while you're at it, why don't you just post the whole code in it's entirety? It's really difficult to debug code which calls mysterious black boxes (aka methods with no given code).
Re: TicTacToe program - Space taken?
I apologize with the confusing code. I've been trying to figure this out for awhile now and at this point, I've just been trying random stuff to see if there are any changes. Here's the full code:
Code java:
import java.io.*;
public class Test
{
public static void main(String args[]) throws IOException
{
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Hello! Welcome to a one-player version of Tic-Tac-Toe!");
System.out.print("Please enter your name: ");
String name = keybd.readLine();
int[] a = new int[9]; // Creates an array to store 'X' and 'O'
int nummoves = 0; // Initializes the # of turns
while(true)
{
ExampleBoard(); // Creates initial board for user to refer to
for (int i = 0; i <= 8; i++)
{
do
{
System.out.println("Your Turn");
System.out.print("Please make a move (0-8): ");
int x = Integer.parseInt(keybd.readLine());
User(a,x);
drawBoard(a);
} while (SpaceTaken(a,i) == true);
nummoves++;
if (Winner(a) == true) // Checks winning conditions
{
System.out.println("Congratulations " + name + "! You win!");
break;
}
else if (nummoves == 9) // Checks for a tie
{
System.out.println("Cat's game!");
break;
}
else
{
System.out.println();
System.out.println("Computer's Turn");
do
{
AI(a);
drawBoard(a);
System.out.println();
} while (SpaceTaken(a,i) == true);
nummoves++;
if (Loser(a) == true) // Checks for losing conditions
{
System.out.println("Sorry " + name + "! You Lose!");
break;
}
}
}
break;
}
}
public static void drawBoard(int[] a)
{
String[] s = new String[9]; // since moves were tracked with int array a
for (int i=0; i<=8; i++) // instead of String array a, we must now
{ // create extra array with X's and O's
if (a[i]==0) // instead of 1's and 2's, for nicer output.
s[i]=" "; // (Instead, you could have tracked moves
else if (a[i]==1) // with String array a, instead of int array a.
s[i]="X"; // But, that would make the XWins and OWins
else // routines much more complex.)
s[i]="O";
}
System.out.println(" "+s[0]+" | "+s[1]+" | "+s[2]);
System.out.println("---+---+---");
System.out.println(" "+s[3]+" | "+s[4]+" | "+s[5]);
System.out.println("---+---+---");
System.out.println(" "+s[6]+" | "+s[7]+" | "+s[8]);
}
public static void User(int[] a, int x) // Sets 'X' in the array based on user input
{
if (x == 0 && a[0] == 0)
{
a[0] = 1;
}
else if (x == 1 && a[1] == 0)
{
a[1] = 1;
}
else if (x == 2 && a[2] == 0)
{
a[2] = 1;
}
else if (x == 3 && a[3] == 0)
{
a[3] = 1;
}
else if (x == 4 && a[4] == 0)
{
a[4] = 1;
}
else if (x == 5 && a[5] == 0)
{
a[5] = 1;
}
else if (x == 6 && a[6] == 0)
{
a[6] = 1;
}
else if (x == 7 && a[7] == 0)
{
a[7] = 1;
}
else if (x == 8 && a[8] == 0)
{
a[8] = 1;
}
}
public static void AI(int[] a) // Sets 'O' in the array based on randomly generated
{ // numbers
int r = (int)(Math.random() * 9);
if (r == 0 && a[0] == 0)
{
a[0] = 2;
}
else if (r == 1 && a[1] == 0)
{
a[1] = 2;
}
else if (r == 2 && a[2] == 0)
{
a[2] = 2;
}
else if (r == 3 && a[3] == 0)
{
a[3] = 2;
}
else if (r == 4 && a[4] == 0)
{
a[4] = 2;
}
else if (r == 5 && a[5] == 0)
{
a[5] = 2;
}
else if (r == 6 && a[6] == 0)
{
a[6] = 2;
}
else if (r == 7 && a[7] == 0)
{
a[7] = 2;
}
else if (r == 8 && a[8] == 0)
{
a[8] = 2;
}
}
public static boolean Winner(int[] a) // Contains all of the winning combinations
{
if (a[0] == 1 && a[1] == 1 && a[2] == 1)
{
return true;
}
else if (a[3] == 1 && a[4] == 1 && a[5] == 1)
{
return true;
}
else if (a[6] == 1 && a[7] == 1 && a[8] == 1)
{
return true;
}
else if (a[0] == 1 && a[3] == 1 && a[6] == 1)
{
return true;
}
else if (a[1] == 1 && a[4] == 1 && a[7] == 1)
{
return true;
}
else if (a[2] == 1 && a[5] == 1 && a[8] == 1)
{
return true;
}
else if (a[0] == 1 && a[4] == 1 && a[8] == 1)
{
return true;
}
else if (a[2] == 1 && a[4] == 1 && a[6] == 1)
{
return true;
}
else
{
return false;
}
}
public static boolean Loser(int[] a) // Contains all of the losing combinations
{
if (a[0] == 2 && a[1] == 2 && a[2] == 2)
{
return true;
}
else if (a[3] == 2 && a[4] == 2 && a[5] == 2)
{
return true;
}
else if (a[6] == 2 && a[7] == 2 && a[8] == 2)
{
return true;
}
else if (a[0] == 2 && a[3] == 2 && a[6] == 2)
{
return true;
}
else if (a[1] == 2 && a[4] == 2 && a[7] == 2)
{
return true;
}
else if (a[2] == 2 && a[5] == 2 && a[8] == 2)
{
return true;
}
else if (a[0] == 2 && a[4] == 2 && a[8] == 2)
{
return true;
}
else if (a[2] == 2 && a[4] == 2 && a[6] == 2)
{
return true;
}
else
{
return false;
}
}
public static boolean SpaceTaken(int[] a, int i)
{
if (a[i] == 'X' || a[i] == 'O')
{
System.out.println("Sorry, space is taken!");
return true;
}
else
{
return false;
}
}
public static void ExampleBoard() // Visual example of corresponding options
{
System.out.println(" " + "0" + " | " + "1" + " | " + "2");
System.out.println("---+---+---");
System.out.println(" " + "3" + " | " + "4" + " | " + "5");
System.out.println("---+---+---");
System.out.println(" " + "6" + " | " + "7" + " | " + "8");
}
}
I'm sorry if it's a bit lengthy, unorganized, or even unnecessary. I probably ended up making a lot of useless commands but I planned to go through it and shorten it after I got it to work. As for the int array, I originally had and still have the array storing ints. Like I said, I just random tried code after things weren't working. Anyways, thanks for taking the time to reply to my problem once again helloworld922.
Re: TicTacToe program - Space taken?
Ok, here are a few things:
1. Remember what you did to SpaceTaken? Try to do that with your other methods (i.e. get rid of all those unecessary if/else's. Only 1 pair is needed, 0 if you make some smart assumptions and code design). Note that the code in each of the different methods will be slightly different, but conceptually will be very similar.
2. You're AI and User methods don't set the tile to 'X' or 'O', but rather to 1 or 2. Rather than look up the unicode equivalent of 'X' or 'O', simply change your AI and User methods to directly set a[i] to 'X' or 'O'
3. Your AI method isn't quite right. It only tries 1 guess. Instead, it should keep guessing until it gets a valid position to put something into (hint: use a while loop).
4. I didn't look through your logic in the main method, but try running the code by hand (i.e. get out a pencil and paper). See if you can figure out what it is your code is doing. Is what it's doing what you expect to happen? If not, how could you change it? Something I can see right away is that you have an unnecessary while(true) statement (and the corresponding break statement at the bottom).