I REALLY need help. ThIs program works - except I have one fatal error that I can't seem to figure out how to fix. The players can play where other players have already played. I tried to use the switch statement at the bottom in method checkMove where if result == false, that spot is taken. But of course, anyone can see that that will not work. I have looked at other threads, but nome of them have seemed to conform to what I have here. If you can help me, PLEASE do! Thanks in advance!

import java.util.Scanner;

class Tic {

public static void main(String[] args) {
System.out.println("Welcome to Michaila's TicTacToe!");
Scanner xo = new Scanner(System.in);

char [][] tic = new char[3][3];

for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
tic[i][j] = ' ';
printMe(tic);
System.out.println("It is X's turn.");

boolean x = true;
int testing = 5;

for(int i = 1; i <= testing; i++){

if(x == true){

if(i >= 2){
System.out.println("It is X's turn. Please type a number (1-9).");
}//end if i>2
else{
System.out.println("Please type a number (1-9).");
}//end else

int numb = xo.nextInt();

if(checkMove(numb, tic) == false){
System.out.println("Taken! Please choose again (1-9).");
numb = xo.nextInt();
}//end if


while (numb >= 1 && numb <= 9){
xsmove(numb, tic, x);
break;
}//end while valid
while(numb < 1 || numb > 9){
System.out.println("That number is not valid. Please choose a number 1 - 9.");
numb = xo.nextInt();
xsmove(numb, tic, x);
}//end while not valid

printMe(tic);
System.out.println("Good move!");
x = false;

if(tic[0][0] == 'X' && tic[1][0] == 'X' && tic[2][0] == 'X' 
|| tic[0][1] == 'X' && tic[1][1] == 'X' && tic[2][1] == 'X' 
|| tic[0][2] == 'X' && tic[1][2] == 'X' && tic[2][2] == 'X' 
|| tic[0][0] == 'X' && tic[0][1] == 'X' && tic[0][2] == 'X' 
|| tic[1][0] == 'X' && tic[1][1] == 'X' && tic[1][2] == 'X' 
|| tic[2][0] == 'X' && tic[2][1] == 'X' && tic[2][2] == 'X' 
|| tic[0][0] == 'X' && tic[1][1] == 'X' && tic[2][2] == 'X'
|| tic[2][0] == 'X' && tic[1][1] == 'X' && tic[0][2] == 'X'){
System.out.println("X WINS!!! :)(: Game Over!!");	
System.exit(0);
}//end long if x
else if(i == 5){
System.out.println("Draw!");
System.exit(0);
}//end else if

}//end if xtrue

if(x == false){
System.out.println("It is O's turn. Please type a number (1-9).");
int numb = xo.nextInt();

if(checkMove(numb, tic) == false){
System.out.println("Taken! Please choose again (1-9).");
numb = xo.nextInt();
}//end if

while (numb >= 1 && numb <= 9){
xsmove(numb, tic, x);
break;
}//end while valid
while(numb < 1 || numb > 9){
System.out.println("That number is not valid. Please choose a number 1 - 9.");	
numb = xo.nextInt();
xsmove(numb, tic, x);
}//end while not valid

printMe(tic);
System.out.println("Good move!");
x = true;

if(tic[0][0] == 'O' && tic[1][0] == 'O' && tic[2][0] == 'O' 
|| tic[0][1] == 'O' && tic[1][1] == 'O' && tic[2][1] == 'O' 
|| tic[0][2] == 'O' && tic[1][2] == 'O' && tic[2][2] == 'O' 
|| tic[0][0] == 'O' && tic[0][1] == 'O' && tic[0][2] == 'O' 
|| tic[1][0] == 'O' && tic[1][1] == 'O' && tic[1][2] == 'O' 
|| tic[2][0] == 'O' && tic[2][1] == 'O' && tic[2][2] == 'O' 
|| tic[0][0] == 'O' && tic[1][1] == 'O' && tic[2][2] == 'O'
|| tic[2][0] == 'O' && tic[1][1] == 'O' && tic[0][2] == 'O'){
System.out.println("O WINS!!! :)(: Game Over!!");
System.exit(0);
}//end long if o
else if(i == 5){
System.out.println("Draw!");
System.exit(0);
}//end elseif

}//end if xfalse
}//end fortest
}//end main

private static void printMe(char [][]tic){
int numpad = 0;
System.out.println("-------\t\t-------");
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
System.out.print("|" + tic[i][j]);
}//end for j
System.out.print("|");
System.out.println("\t\t" + "|" + (numpad + 1) + "|" + (numpad + 2) + "|" + (numpad + 3) + "|");
System.out.println("-------\t\t-------");
numpad += 3;
}//end for i
}//end printMe

public static void xsmove (int numb, char[][] tic, boolean x){
switch (numb){
case 1: if(tic[0][0] == ' ' && x == true) tic[0][0] = 'X';
else tic[0][0] = 'O'; break;
case 2: if(tic[0][1] == ' ' && x == true) tic[0][1] = 'X';
else tic[0][1] = 'O'; break;
case 3: if(tic[0][2] == ' ' && x == true) tic[0][2] = 'X';
else tic[0][2] = 'O'; break;
case 4: if(tic[1][0] == ' ' && x == true) tic[1][0] = 'X';
else tic[1][0] = 'O'; break;
case 5: if(tic[1][1] == ' ' && x == true) tic[1][1] = 'X';
else tic[1][1] = 'O'; break;
case 6: if(tic[1][2] == ' ' && x == true) tic[1][2] = 'X';
else tic[1][2] = 'O'; break;
case 7: if(tic[2][0] == ' ' && x == true) tic[2][0] = 'X';
else tic[2][0] = 'O'; break;
case 8: if(tic[2][1] == ' ' && x == true) tic[2][1] = 'X';
else tic[2][1] = 'O'; break;
case 9: if(tic[2][2] == ' ' && x == true) tic[2][2] = 'X';
else tic[2][2] = 'O'; break;
}//end switch
}//end xsmove

public static boolean checkMove (int numb, char[][] tic){
boolean result = true;
switch (numb){
case 1: if (tic[0][0] != ' ') result = false; break;
case 2: if (tic[0][1] != ' ') result = false; break;
case 3: if (tic[0][2] != ' ') result = false; break;
case 4: if (tic[1][0] != ' ') result = false; break;
case 5: if (tic[1][1] != ' ') result = false; break;
case 6: if (tic[1][2] != ' ') result = false; break;
case 7: if (tic[2][0] != ' ') result = false; break;
case 8: if (tic[2][1] != ' ') result = false; break;
case 9: if (tic[2][2] != ' ') result = false; break;
}//end switch
return result;
}//end checkMove

}//end class

The players can play where other players have already played. I tried to use the switch statement at the bottom in method checkMove where if result == false, that spot is taken. But of course, anyone can see that that will not work.

Use a loop instead of just using an if statement that will check if the spot is taken every time either players input a number

How would I do this? I'm newish to java so I still haven a lot to learn. Can you explain in more detail please?

Your code assumes that the second input is OK without testing it and looping back to get a valid input if it is not.
Write a loop that When getting input from the user, stays in the loop until the user enters a good value.

Can you copy the console output and paste it here that shows the problem.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Edited 4 Years Ago by NormR1: n/a

Ok, here's the console output. I see what y'all are saying about the loop, but I have tried that. I just can't figure out what needs to go in the parameters of the loop.

Output:

Welcome to Michaila's TicTacToe!
-------
| | | |
-------
| | | |
-------
| | | |
-------

-------
|1|2|3|
-------
|4|5|6|
-------
|7|8|9|
-------

It is X's turn.
Please type a number (1-9).
1
-------
|X| | |
-------
| | | |
-------
| | | |
-------

-------
|1|2|3|
-------
|4|5|6|
-------
|7|8|9|
-------
Good move!
It is O's turn. Please type a number (1-9).
2
-------
|X|O| |
-------
| | | |
-------
| | | |
-------

-------
|1|2|3|
-------
|4|5|6|
-------
|7|8|9|
-------
Good move!
It is X's turn. Please type a number (1-9).
3
-------
|X|O|X|
-------
| | | |
-------
| | | |
-------

-------
|1|2|3|
-------
|4|5|6|
-------
|7|8|9|
-------
Good move!
It is O's turn. Please type a number (1-9).
1
Taken! Please choose again (1-9).
1
-------
|O|O|X|
-------
| | | |
-------
| | | |
-------

-------
|1|2|3|
-------
|4|5|6|
-------
|7|8|9|
-------
Good move!
It is X's turn. Please type a number (1-9).
4
-------
|O|O|X|
-------
|X| | |
-------
| | | |
-------

-------
|1|2|3|
-------
|4|5|6|
-------
|7|8|9|
-------
Good move!
It is O's turn. Please type a number (1-9).
3
Taken! Please choose again (1-9).
3
-------
|O|O|O|
-------
|X| | |
-------
| | | |
-------

-------
|1|2|3|
-------
|4|5|6|
-------
|7|8|9|
-------
Good move!
O WINS!!! :)(: Game Over!!

The loop should continue looping until it gets a good input from the user or until it or the user decides to end the program.
When you get good input, you could use the break statement to exit the loop or you could change a boolean variable that would allow the code to exit the loop.

For easier testing, I'd change the Scanner to have all the inputs with conditions that you want to test for so you would not have to type in the responses:
Something like this:
Scanner xo = new Scanner("1 2 2 2 3 4 5 6 7 8 9 \n"); //System.in);

This article has been dead for over six months. Start a new discussion instead.