Tic-Tac-Toe program *editted*
I've been working on this but still haven't gotten much progress. I haven't done much changes compared to what it was before this. Basically I just put the if/else control statements into subroutines to help the main routine look a bit cleaner but I still haven't found a way to shorten the code up (although that can wait until I actually get the program to actually work). I still haven't found a way to enact the winner/loser subroutines, I was thinking about putting them into an if/else and break the program if one of them become true.
Something like this:
Code java:
if (nummoves == 9)
{
System.out.println("Cat's game!");
break;
}
else if (winner(a) = true)
{
System.out.println("Congratulations! ......");
break;
}
else if // Same thing with the loser(a) statement
Anyways, I also still haven't figured out a way to program the loop to loop again if the space is taken.
Updated Code:
Code java:
import java.io.*;
public class HW7
{
public static void main(String args[]) throws IOException
{
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
import java.io.*;
public class Test
{
public static void main(String args[]) throws IOException
{
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
int[] a = new int[9];
int nummoves = 0;
while(true)
{
for (int i = 0; i <= 8; i++)
{
System.out.println("Your Turn");
System.out.println("Enter: ");
int x = Integer.parseInt(keybd.readLine());
SpaceTaken(a,x);
User(a,x);
drawBoard(a);
System.out.println();
System.out.println("Computer's Turn:");
AI(a,x);
drawBoard(a);
System.out.println();
}
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)
{
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, int x)
{
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)
{
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)
{
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 void SpaceTaken(int[] a, int x)
{
if (x == 0 && (a[0] == 1 || a[0] == 2))
{
System.out.println("Sorry, space is taken!");
return;
}
else if (x == 1 && (a[1] == 1 || a[1] == 2))
{
System.out.println("Sorry, space is taken!");
return;
}
else if (x == 2 && (a[2] == 1 || a[2] == 2))
{
System.out.println("Sorry, space is taken!");
return;
}
else if (x == 3 && (a[3] == 1 || a[3] == 2))
{
System.out.println("Sorry, space is taken!");
return;
}
else if (x == 4 && (a[4] == 1 || a[4] == 2))
{
System.out.println("Sorry, space is taken!");
return;
}
else if (x == 5 && (a[5] == 1 || a[5] == 2))
{
System.out.println("Sorry, space is taken!");
return;
}
else if (x == 6 && (a[6] == 1 || a[6] == 2))
{
System.out.println("Sorry, space is taken!");
return;
}
else if (x == 7 && (a[7] == 1 || a[7] == 2))
{
System.out.println("Sorry, space is taken!");
return;
}
else if (x == 8 && a[8] != 0)
{
System.out.println("Sorry, space is taken!");
return;
}
}
}
At this point in the program, it prints out the following (with overlaps, since I haven't put in any code for it yet. Also the subroutines aren't working as I wanted but I think it may be because I haven't done anything telling the program to break once it turns true.)
Code java:
Hello! Welcome to a one-player version of Tic-Tac-Toe!
Please enter your name: k
| |
---+---+---
| |
---+---+---
| |
Enter:
0
X | |
---+---+---
| O |
---+---+---
| |
Enter:
1
X | X |
---+---+---
| O |
---+---+---
O | |
Enter:
2
X | X | X
---+---+---
| O |
---+---+---
O | O |
Enter:
3
X | X | X
---+---+---
X | O |
---+---+---
O | O |
Enter:
4
Cat's game!