Commit e21e43e9 authored by jordan.dalby's avatar jordan.dalby

+ Added restart button

parent b60e53bf
...@@ -21,7 +21,6 @@ public class Main extends Application ...@@ -21,7 +21,6 @@ public class Main extends Application
private Turn currentTurn = Turn.CROSSES; private Turn currentTurn = Turn.CROSSES;
private boolean gameOver = false; private boolean gameOver = false;
private boolean firstTurn = true;
private Random random = new Random(); private Random random = new Random();
...@@ -84,6 +83,20 @@ public class Main extends Application ...@@ -84,6 +83,20 @@ public class Main extends Application
label.setPrefHeight(128); label.setPrefHeight(128);
label.setPrefWidth(256); label.setPrefWidth(256);
Button button = new Button();
button.setText("Restart");
button.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent arg0)
{
restart();
}
});
pane.add(button, 128*5, 128*4);
winLabel = label; winLabel = label;
pane.add(label, 128*4, 128*4); pane.add(label, 128*4, 128*4);
...@@ -94,6 +107,17 @@ public class Main extends Application ...@@ -94,6 +107,17 @@ public class Main extends Application
s.show(); s.show();
} }
public void restart()
{
gameOver = false;
currentTurn = Turn.CROSSES;
for (Button b : buttons)
{
b.setStyle("-fx-font-size:40");
b.setText("");
}
}
public void updateButton(Button button) public void updateButton(Button button)
{ {
if (gameOver) if (gameOver)
...@@ -172,88 +196,71 @@ public class Main extends Application ...@@ -172,88 +196,71 @@ public class Main extends Application
{ {
// AI PRIORITISES AGGRESSION BUT CAN ALSO BE VERY DEFENSIVE // AI PRIORITISES AGGRESSION BUT CAN ALSO BE VERY DEFENSIVE
if (firstTurn) String turnCheck = "";
String opponentCheck = "";
switch (currentTurn)
{ {
firstTurn = false; case CROSSES:
int i = -1; turnCheck = "X";
int count = 0; opponentCheck = "O";
while (!isSlotAvailable(i) && count < 100000) break;
{ case NOUGHTS:
count ++; turnCheck = "O";
i = random.nextInt(buttons.size()); opponentCheck = "X";
} break;
default:
updateButton(buttons.get(i)); break;
} }
else
{
String turnCheck = "";
String opponentCheck = "";
switch (currentTurn)
{
case CROSSES:
turnCheck = "X";
opponentCheck = "O";
break;
case NOUGHTS:
turnCheck = "O";
opponentCheck = "X";
break;
default:
break;
}
System.out.println("CHECKING IF IT CAN WIN"); System.out.println("CHECKING IF IT CAN WIN");
for (String s : checks) // AI checks for winning positions for itself for (String s : checks) // AI checks for winning positions for itself
{ {
String[] split = s.split(","); String[] split = s.split(",");
int x = Integer.parseInt(split[0]); int x = Integer.parseInt(split[0]);
int y = Integer.parseInt(split[1]); int y = Integer.parseInt(split[1]);
int z = Integer.parseInt(split[2]); int z = Integer.parseInt(split[2]);
int i = willBeWin(x,y,z,turnCheck); int i = willBeWin(x,y,z,turnCheck);
if (i != -1) if (i != -1)
{
updateButton(buttons.get(i));
return;
}
}
System.out.println("IT CAN'T");
System.out.println("CHECKING IF IT CAN BLOCK");
for (String s : checks) // AI checks for winning positions for opponent
{ {
String[] split = s.split(","); updateButton(buttons.get(i));
int x = Integer.parseInt(split[0]); return;
int y = Integer.parseInt(split[1]);
int z = Integer.parseInt(split[2]);
int i = willBeWin(x,y,z,opponentCheck);
if (i != -1)
{
updateButton(buttons.get(i));
return;
}
} }
}
System.out.println("IT CAN'T"); System.out.println("IT CAN'T");
System.out.println("CHECKING IF IT CAN BLOCK");
// AI last resort, can't figure anything else out
int i = -1; for (String s : checks) // AI checks for winning positions for opponent
int count = 0; {
while (!isSlotAvailable(i) && count < 100000) String[] split = s.split(",");
int x = Integer.parseInt(split[0]);
int y = Integer.parseInt(split[1]);
int z = Integer.parseInt(split[2]);
int i = willBeWin(x,y,z,opponentCheck);
if (i != -1)
{ {
count ++; updateButton(buttons.get(i));
i = random.nextInt(buttons.size()); return;
} }
}
updateButton(buttons.get(i)); System.out.println("IT CAN'T");
// AI last resort, can't figure anything else out
int i = -1;
int count = 0;
while (!isSlotAvailable(i) && count < 100000)
{
count ++;
i = random.nextInt(buttons.size());
} }
updateButton(buttons.get(i));
} }
public void checkForWin() public void checkForWin()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment