Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey Guys, I am making a TicTacToe Game in Java and I am having someone difficulty figuring out what I am doing Wrong.

1. I made a tic tic toe board which look something like this.
Java
System.out.println("          |          |          );
System.out.println(    0      |    1     |     2    );
System.out.println(           |          |          );
System.out.println(  -------------------------------);
System.out.println(           |          |          );
System.out.println(    3      |    4     |     5    );
System.out.println(           |          |          );
System.out.println(  -------------------------------);
System.out.println(           |          |          );
System.out.println(    6      |    7     |     8    );
System.out.println(           |          |          );


2. I asked the user for a number between 0-8 and the number they choose will be Replaced by X.

Say they choose number 5. This is how it will look like.


Java
System.out.println("          |          |          );
System.out.println(    0      |    1     |     2    );
System.out.println(           |          |          );
System.out.println(  -------------------------------);
System.out.println(           |          |          );
System.out.println(    3      |    4     |     X    );
System.out.println(           |          |          );
System.out.println(  -------------------------------);
System.out.println(           |          |          );
System.out.println(    6      |    7     |     8    );
System.out.println(           |          |          );


3. I don't know how to create a loop which will keep asking the user for a number until it reaches to all the 8 numbers. Also, how do I check if the number is taken or not.


IT WILL BE GREAT IF YOU CAN HELP ME.

COde from Comment:
Java
import java.util.Scanner;

public class TicTacToeArray {

public static void main(String[] args){

Scanner in = new Scanner(System.in);

int [] A1 = {0,1,2};
int [] A2 = {3,4,5};
int [] A3 = {6,7,8}; 

for (int i = 0; i < 8; i++){
in.nextInt();
}

System.out.println("Player #1: Please enter a number between 0-8"); 
int playerOne = in.nextInt(); 


if (playerOne == A1 [0]){
System.out.println(" | | ");
System.out.println(" 0 | 1 | 2 ");
System.out.println(" | | "); 
System.out.println(" -------------------------------"); 
System.out.println(" | | ");
System.out.println(" 3 | 4 | 5 ");
System.out.println(" | | "); 
System.out.println(" -------------------------------"); 
System.out.println(" | | ");
System.out.println(" 6 | 7 | 8 ");
System.out.println(" | | ");


}else if (playerOne ==A1 [1]){
System.out.println(" | | ");
System.out.println(" 0 | X | 2 ");
System.out.println(" | | "); 
System.out.println(" -------------------------------"); 
System.out.println(" | | ");
System.out.println(" 3 | 4 | 5 ");
System.out.println(" | | "); 
System.out.println(" -------------------------------"); 
System.out.println(" | | ");
System.out.println(" 6 | 7 | 8 ");
System.out.println(" | | ");


}else if (playerOne ==A1 [2]){
System.out.println(" | | ");
System.out.println(" 0 | 1 | X ");
System.out.println(" | | "); 
System.out.println(" -------------------------------"); 
System.out.println(" | | ");
System.out.println(" 3 | 4 | 5 ");
System.out.println(" | | "); 
System.out.println(" -------------------------------"); 
System.out.println(" | | ");
System.out.println(" 6 | 7 | 8 ");
System.out.println(" | | ");


}else if (playerOne ==A2 [0]){
System.out.println(" | | ");
System.out.println(" 0 | 1 | 2 ");
System.out.println(" | | "); 
System.out.println(" -------------------------------"); 
System.out.println(" | | ");
System.out.println(" X | 4 | 5 ");
System.out.println(" | | "); 
System.out.println(" -------------------------------"); 
System.out.println(" | | ");
System.out.println(" 6 | 7 | 8 ");
System.out.println(" | | ");
Posted
Updated 12-May-15 20:40pm
v3
Comments
CHill60 12-May-15 7:40am    
Share code that prompts the user for the number
Jainam Patel 12-May-15 7:53am    
This is the code that I used. But it only asks the user for One time. Please help me.



import java.util.Scanner;

public class TicTacToeArray {

public static void main(String[] args){

Scanner in = new Scanner(System.in);

int [] A1 = {0,1,2};
int [] A2 = {3,4,5};
int [] A3 = {6,7,8};

for (int i = 0; i < 8; i++){
in.nextInt();
}

System.out.println("Player #1: Please enter a number between 0-8");
int playerOne = in.nextInt();


if (playerOne == A1 [0]){
System.out.println(" | | ");
System.out.println(" 0 | 1 | 2 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 3 | 4 | 5 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 6 | 7 | 8 ");
System.out.println(" | | ");


}else if (playerOne ==A1 [1]){
System.out.println(" | | ");
System.out.println(" 0 | X | 2 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 3 | 4 | 5 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 6 | 7 | 8 ");
System.out.println(" | | ");


}else if (playerOne ==A1 [2]){
System.out.println(" | | ");
System.out.println(" 0 | 1 | X ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 3 | 4 | 5 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 6 | 7 | 8 ");
System.out.println(" | | ");


}else if (playerOne ==A2 [0]){
System.out.println(" | | ");
System.out.println(" 0 | 1 | 2 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" X | 4 | 5 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 6 | 7 | 8 ");
System.out.println(" | | ");


// And the Rest If statement Goes Here.


1 solution

The trick behind this is that you are going to replace everything at all. Since you are using Console you won't be able to customize any one graphic node. So, chances are that you are going to write everything to re-write the output.

Note: I do not know the code to clear the screen as in C++ clrscr(), so you can add that code yourself to remove previous characters.

Since you have the code to write everything, do it this way,

Java
// Store variables inside the class as members
char zero = '0';
char one = '1';
char two = '2';
char three = '3';
char four = '4';
char five = '5';
char six = '6';
char seven = '7';
char eight = '8';

// Create the function now
private void render() {
   System.out.println("          |          |          ");
   System.out.println("   " + zero + "      |    " + one + "     |     " + two + "    ");
   System.out.println("          |          |          ");
   System.out.println(" -------------------------------");
   System.out.println("          |          |          ");
   System.out.println("   " + three + "      |    " + four + "     |     " + five + "    ");
   System.out.println("          |          |          ");
   System.out.println(" -------------------------------");
   System.out.println("          |          |          ");
   System.out.println("   " + six + "      |    " + seven + "     |     " + eight + "    ");
   System.out.println("          |          |          ");
}


Working...

The above program would in the first case render the block that you've shown first. Then you can get the input from user and update the variables, something like this,

Java
// Upate the variable if the input was 1.
MyClass.one = 'X';


You will be using a switch statement or an if else condition to check what was the input. Then for every key input you will also trigger the render function.

Java
render();


Now when the function would be called again, it would render the new input for that previous number (which would now be "x").
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-May-15 9:00am    
Voted 4. You could better use enum instead of your zero, one, two, etc., which are not even constants (could be "static final char").
—SA
Afzaal Ahmad Zeeshan 12-May-15 9:46am    
But that would cause a problem while updating the variable; from numeric to X character.
Sergey Alexandrovich Kryukov 12-May-15 10:13am    
You would print X character where it's needed without modifying the constants. :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900