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.
Here's my main routine:
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):
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; }