My code:
import java.util.Scanner;
public class TikTakToe {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String space1 = "1"; //these are just for visual coordinated. These will always be on the screen along with any Xs and Os.
String space2 = "2";
String space3 = "3";
String space4 = "4";
String space5 = "5";
String space6 = "6";
String space7 = "7";
String space8 = "8";
String space9 = "9";
String board1 = "X"; //these actually keep track of the Xs and Os, they are printed along side of the char spaces that I have above. So
String board2 = "O"; //for example, the grid will look like this when game is going on
String board3 = "X"; //1 X 2 X 3 O
String board4 = "O"; //4 O 5 X 6 O
String board5 = "X"; //7 O 8 X 9 (as you can see, X has won, and space nine is empty. That is what I'm trying to achieve
String board6 = "O";
String board7 = " ";
String board8 = " ";
String board9 = "O";
boolean x = false; //I don't know what I am doing with these, but I was thinking maybe
boolean o = false; //to see who wins.
String input = null;
while //loop all this till all spaces are filled. Then to find who wins, I get more specific later.
((board1 == " ") || (board2 == " ") || (board3 == " ") || (board4 == " ") || (board5 == " ")
|| (board6 == " ") || (board7 == " ") || (board8 == " ") || (board9 == " ")){
System.out.println(space1 +"|"+ board1+"\t"+space2 +"|"+ board2+"\t"+space3+"|"+board3);//I want to print out the board here and have it updated each time a user picks a space
System.out.println(space4 +"|"+ board4+"\t"+space5 +"|"+ board5+"\t"+space6+"|"+board6);
System.out.println(space7 +"|"+ board7+"\t"+space8 +"|"+ board8+"\t"+space9+"|"+board9);
//everytime it goes through the loop, it will print the updated board to the screen.
//I don't know if I should create a separate method for that (which I have tried, but ran into problems)
//check the input in this loop
do{
System.out.print("Enter the number for the space you want to choose: ");
input = scanner.nextLine();
if (input.equals("1"))
board1 = "X";
if (input.equals("2"))
board2 = "X";
if (input.equals("3"))
board3 = "X";
if (input.equals("4"))
board4 = "X";
if (input.equals("5"))
board5 = "X";
if (input.equals("6"))
board6 = "X";
if (input.equals("7"))
board7 = "X";
if (input.equals("8"))
board8 = "X";
if (input.equals("9"))
board9 = "X";
else{System.out.print("The input is not 1-9, you just lost a turn");} //I don't wanna add any extra work to make my program robust for
//idiots who can't read or count.
System.out.println(space1 +"|"+ board1+"\t"+space2 +"|"+ board2+"\t"+space3+"|"+board3);//I want to print out the board here and have it updated each time a user picks a space
System.out.println(space4 +"|"+ board4+"\t"+space5 +"|"+ board5+"\t"+space6+"|"+board6);
System.out.println(space7 +"|"+ board7+"\t"+space8 +"|"+ board8+"\t"+space9+"|"+board9);
} while(x = false);
}
}
}
Edited by An Alien, 07 January 2011 - 02:56 PM.


Sign In
Create Account


Back to top









