I have been doing an assignment for my my first year software course, in which we have to make a simple TicTacToe program in java.
For the final 30% mark, we have to add a GUI in. Apart fro a tutorial only teaching me the basics (how to make boxes and buttons), I am up a creek without a paddle.
My code:
import java.util.Random;
import javax.swing.JOptionPane;
public class tictac
{
public static void main (String[] args) {
while (true){
char computer = 'o';
char player = 'x';
char a[] = {'A','B','C','D','E','F','G','H','I'};
char s;
int x;
int w = 0;
Random rand = new Random();
// grid at the start
System.out.println(" "+a[0]+" | "+a[1]+" | "+a[2]+" ");
System.out.println(" "+a[3]+" | "+a[4]+" | "+a[5]+" ");
System.out.println(" "+a[6]+" | "+a[7]+" | "+a[8]+" ");
if(JOptionPane.showConfirmDialog(null," Go first?",null,JOptionPane.YES_NO_OPTION)==0){ //asks player to go first or not
computer = 'o'; //declares symbol for player
player = 'x';
}
else{
a[4]=computer; //computer automatically goes for the middle
System.out.println(" "+a[0]+" | "+a[1]+" | "+a[2]+" ");
System.out.println(" "+a[3]+" | "+a[4]+" | "+a[5]+" ");
System.out.println(" "+a[6]+" | "+a[7]+" | "+a[8]+" ");
}
while(true){ //while continuing to play is possible
s = new Character(JOptionPane.showInputDialog("Please choose a location").charAt(0));
switch (s){
case ('A'):
case ('a'):
if (a[0]!= computer&&a[0]!=player)
{
a[0]=player;
}
else {
w=1;
}
break;
case ('B'):
case ('b'):
if (a[1]!= computer&&a[1]!=player){
a[1]=player;
}
else {
w=1;
}
break;
case ('C'):
case ('c'):
if (a[2]!= computer&&a[2]!=player){
a[2]=player;
}
else {
w=1;
}
break;
case ('D'):
case ('d'):
if (a[3]!= computer&&a[3]!=player){
a[3]=player;
}
else {
w=1;
}
break;
case ('E'):
case ('e'):
if (a[4]!= computer&&a[4]!=player){
a[4]=player;
}
else {
w=1;
}
break;
case ('F'):
case ('f'):
if (a[5]!= computer&&a[5]!=player){
a[5]=player;
}
else {
w=1;
}
break;
case ('G'):
case ('g'):
if (a[6]!= computer&&a[6]!=player){
a[6]=player;
}
else {
w=1;
}
break;
case ('H'):
case ('h'):
if (a[7]!= computer&&a[7]!=player){
a[7]=player;
}
else {
w=1;
}
break;
case ('I'):
case ('i'):
if (a[8]!= computer&&a[8]!=player){
a[8]=player;
}
else {
w=1;
}
break;
default:
w=1;
break;
} //bring back the grid with updated array
System.out.println(" ");
System.out.println(" "+a[0]+" | "+a[1]+" | "+a[2]+" ");
System.out.println("----+-----+----");
System.out.println(" "+a[3]+" | "+a[4]+" | "+a[5]+" ");
System.out.println("----+-----+----");
System.out.println(" "+a[6]+" | "+a[7]+" | "+a[8]+" ");
if (w==1)
{
JOptionPane.showMessageDialog(null, "Invalid Selection");
w=0;
continue;
}
if (a[4]!=player&&a[4]!=computer) //human win conditions
{
a[4]=computer;
}
//computer movement conditions
else if (a[0]==player&&a[1]==player&&a[2]!=player&&a[2]!=computer)
a[2]=computer;
else if (a[0]==player&&a[2]==player&&a[1]!=player&&a[1]!=computer)
a[1]=computer;
else if (a[2]==player&&a[1]==player&&a[0]!=player&&a[0]!=computer)
a[0]=computer;
else if (a[3]==player&&a[4]==player&&a[5]!=player&&a[5]!=computer)
a[5]=computer;
else if (a[4]==player&&a[5]==player&&a[3]!=player&&a[3]!=computer)
a[3]=computer;
else if (a[6]==player&&a[7]==player&&a[8]!=player&&a[8]!=computer)
a[8]=computer;
else if (a[6]==player&&a[8]==player&&a[7]!=player&&a[7]!=computer)
a[7]=computer;
else if (a[7]==player&&a[8]==player&&a[6]!=player&&a[6]!=computer)
a[6]=computer;
else if (a[0]==player&&a[3]==player&&a[6]!=player&&a[6]!=computer)
a[6]=computer;
else if (a[0]==player&&a[6]==player&&a[3]!=player&&a[3]!=computer)
a[3]=computer;
else if (a[3]==player&&a[6]==player&&a[0]!=player&&a[0]!=computer)
a[0]=computer;
else if (a[1]==player&&a[4]==player&&a[7]!=player&&a[7]!=computer)
a[7]=computer;
else if (a[4]==player&&a[7]==player&&a[1]!=player&&a[1]!=computer)
a[1]=computer;
else if (a[2]==player&&a[5]==player&&a[8]!=player&&a[8]!=computer)
a[8]=computer;
else if (a[2]==player&&a[8]==player&&a[5]!=player&&a[5]!=computer)
a[5]=computer;
else if (a[5]==player&&a[8]==player&&a[2]!=player&&a[2]!=computer)
a[2]=computer;
else if (a[0]==player&&a[4]==player&&a[8]!=player&&a[8]!=computer)
a[8]=computer;
else if (a[8]==player&&a[4]==player&&a[0]!=player&&a[0]!=computer)
a[0]=computer;
else if (a[2]==player&&a[4]==player&&a[6]!=player&&a[6]!=computer)
a[6]=computer;
else if (a[6]==player&&a[4]==player&&a[2]!=player&&a[2]!=computer)
a[2]=computer;
else if (a[0]==computer&&a[1]==computer&&a[2]!=player&&a[2]!=computer)
a[2]=computer;
else if (a[0]==computer&&a[2]==computer&&a[1]!=player&&a[1]!=computer)
a[1]=computer;
else if (a[2]==computer&&a[1]==computer&&a[0]!=player&&a[0]!=computer)
a[0]=computer;
else if (a[3]==computer&&a[4]==computer&&a[5]!=player&&a[5]!=computer)
a[5]=computer;
else if (a[4]==computer&&a[5]==computer&&a[3]!=player&&a[3]!=computer)
a[3]=computer;
else if (a[6]==computer&&a[7]==computer&&a[8]!=player&&a[8]!=computer)
a[8]=computer;
else if (a[6]==computer&&a[8]==computer&&a[7]!=player&&a[7]!=computer)
a[7]=computer;
else if (a[7]==computer&&a[8]==computer&&a[6]!=player&&a[6]!=computer)
a[6]=computer;
else if (a[0]==computer&&a[3]==computer&&a[6]!=player&&a[6]!=computer)
a[6]=computer;
else if (a[0]==computer&&a[6]==computer&&a[3]!=player&&a[3]!=computer)
a[3]=computer;
else if (a[3]==computer&&a[6]==computer&&a[0]!=player&&a[0]!=computer)
a[0]=computer;
else if (a[1]==computer&&a[4]==computer&&a[7]!=player&&a[7]!=computer)
a[7]=computer;
else if (a[4]==computer&&a[7]==computer&&a[1]!=player&&a[1]!=computer)
a[1]=computer;
else if (a[2]==computer&&a[5]==computer&&a[8]!=player&&a[8]!=computer)
a[8]=computer;
else if (a[2]==computer&&a[8]==computer&&a[5]!=player&&a[5]!=computer)
a[5]=computer;
else if (a[5]==computer&&a[8]==computer&&a[2]!=player&&a[2]!=computer)
a[2]=computer;
else if (a[0]==computer&&a[4]==computer&&a[8]!=player&&a[8]!=computer)
a[8]=computer;
else if (a[8]==computer&&a[4]==computer&&a[0]!=player&&a[0]!=computer)
a[0]=computer;
else if (a[2]==computer&&a[4]==computer&&a[6]!=player&&a[6]!=computer)
a[6]=computer;
else if (a[6]==computer&&a[4]==computer&&a[2]!=player&&a[2]!=computer)
a[2]=computer;
else if ((a[0]!=player&&a[0]!=computer)||(a[2]!=player&&a[2]!=computer)||(a[6]!=player&&a[6]!=computer)||(a[8]!=player&&a[8]!=computer))
{
while (true)
{
final int[] Choices = {0,2,6,8};
x = Choices[rand.nextInt(Choices.length)];
if (a[x]!=player&&a[x]!=computer)
{
a[x]=computer;
break;
}
}
}
else if ((a[1]!=player&&a[1]!=computer)||(a[3]!=player&&a[3]!=computer)||(a[5]!=player&&a[5]!=computer)||(a[7]!=player&&a[7]!=computer))
{
while(true)
{
final int[] Choices = {1,3,5,7};
x = Choices[rand.nextInt(Choices.length)];
if (a[x]!=player&&a[x]!=computer)
{
a[x]=computer;
break;
}
}
}
System.out.println(" ");
System.out.println(" "+a[0]+" | "+a[1]+" | "+a[2]+" ");
System.out.println(" "+a[3]+" | "+a[4]+" | "+a[5]+" ");
System.out.println(" "+a[6]+" | "+a[7]+" | "+a[8]+" ");
if ((a[0]==a[1]&&a[1]==a[2]&&a[2]==player)||(a[3]==a[4]&&a[4]==a[5]&&a[5]==player)||(a[6]==a[7]&&a[7]==a[8]&&a[8]==player)||(a[0]==a[4]&&a[4]==a[8]&&a[8]==player)||(a[2]==a[4]&&a[4]==a[6]&&a[6]==player)||(a[0]==a[3]&&a[3]==a[6]&&a[6]==player)||(a[1]==a[4]&&a[4]==a[7]&&a[7]==player)||(a[2]==a[5]&a[5]==a[8]&&a[8]==player))
{
System.out.print("You win!");
break;
}
else if ((a[0]==a[1]&&a[1]==a[2]&&a[2]==computer)||(a[3]==a[4]&&a[4]==a[5]&&a[5]==computer)||(a[6]==a[7]&&a[7]==a[8]&&a[8]==computer)||(a[0]==a[4]&&a[4]==a[8]&&a[8]==computer)||(a[2]==a[4]&&a[4]==a[6]&&a[6]==computer)||(a[0]==a[3]&&a[3]==a[6]&&a[6]==computer)||(a[1]==a[4]&&a[4]==a[7]&&a[7]==computer)||(a[2]==a[5]&&a[5]==a[8]&&a[8]==computer))
{
System.out.print("Computer wins!");
break;
}
//draw conditions
else if((a[0]==player||a[0]==computer)&&(a[1]==player||a[1]==computer)&&(a[2]==player||a[2]==computer)&&(a[3]==player||a[3]==computer)&&(a[4]==player||a[4]==computer)&&(a[5]==player||a[5]==computer)&&(a[6]==player||a[6]==computer)&&(a[7]==player||a[7]==computer)&&(a[8]==player||a[8]==computer))
{
System.out.print("Draw!!");
break;
}
}
if (JOptionPane.showConfirmDialog(null, "Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION)==1)
break;//ask if he wants to play again
}
}
}
Yes, I know the code could be better. My problem is I dont exactly know where to start. Do I make a seperate class for the GUI or change the actual code?