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

+ first commit

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="lib" path="/Users/Shared/javafx-sdk-11.0.2/lib/javafx-swt.jar">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="lib" path="/Users/Shared/javafx-sdk-11.0.2/lib/javafx.base.jar">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="lib" path="/Users/Shared/javafx-sdk-11.0.2/lib/javafx.controls.jar">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="lib" path="/Users/Shared/javafx-sdk-11.0.2/lib/javafx.fxml.jar">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="lib" path="/Users/Shared/javafx-sdk-11.0.2/lib/javafx.graphics.jar">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="lib" path="/Users/Shared/javafx-sdk-11.0.2/lib/javafx.media.jar">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="lib" path="/Users/Shared/javafx-sdk-11.0.2/lib/javafx.swing.jar">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="lib" path="/Users/Shared/javafx-sdk-11.0.2/lib/javafx.web.jar">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>NoughtsAndCrosses</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=12
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=12
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=12
package fx;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application
{
enum Turn {NOUGHTS, CROSSES};
private Turn currentTurn = Turn.CROSSES;
private boolean gameOver = false;
private boolean firstTurn = true;
private Random random = new Random();
private List<String> checks = new ArrayList<String>();
private Label winLabel;
List<Button> buttons = new ArrayList<Button>();
@Override
public void start(Stage s)
{
checks.add("0,1,2");
checks.add("3,4,5");
checks.add("6,7,8");
checks.add("0,3,6");
checks.add("1,4,7");
checks.add("2,5,8");
checks.add("0,4,8");
checks.add("2,4,6");
s.setWidth(800);
s.setHeight(600);
GridPane pane = new GridPane();
draw();
System.out.println(buttons.size());
int x = 0;
int y = 0;
int row = 0;
for (Button button : buttons)
{
pane.add((button), x, y);
x+=128;
if (x >= 128*3)
{
x=0;
row++;
y = 128*row;
}
button.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent arg0)
{
updateButton(button);
}
});
}
Label label = new Label();
label.setPrefHeight(128);
label.setPrefWidth(256);
winLabel = label;
pane.add(label, 128*4, 128*4);
Scene scene = new Scene(pane);
s.setScene(scene);
s.show();
}
public void updateButton(Button button)
{
if (gameOver)
return;
if (!button.getText().equals(""))
return;
switch (currentTurn)
{
case CROSSES:
button.setText("X");
checkForWin();
currentTurn = Turn.NOUGHTS;
aiPicksPosition();
break;
case NOUGHTS:
button.setText("O");
checkForWin();
currentTurn = Turn.CROSSES;
break;
default:
break;
}
}
public boolean isWin(int x, int y, int z, String val)
{
boolean value = buttons.get(x).getText().equals(val) && buttons.get(y).getText().equals(val) && buttons.get(z).getText().equals(val);
if (value)
{
buttons.get(x).setStyle("-fx-font-size:40; -fx-text-fill: green");
buttons.get(y).setStyle("-fx-font-size:40; -fx-text-fill: green");
buttons.get(z).setStyle("-fx-font-size:40; -fx-text-fill: green");
}
return value;
}
public boolean checkBoard()
{
boolean spaceLeft = false;
for (Button b : buttons)
if (b.getText().equals(""))
spaceLeft = true;
return spaceLeft;
}
public int willBeWin(int x, int y, int z, String val)
{
boolean value1 = buttons.get(x).getText().equals(val) && buttons.get(y).getText().equals(val); // xy
boolean value2 = buttons.get(x).getText().equals(val) && buttons.get(z).getText().equals(val); // xz
boolean value3 = buttons.get(y).getText().equals(val) && buttons.get(z).getText().equals(val); // yz
if (value1)
if (buttons.get(z).getText().equals(""))
return z;
if (value2)
if (buttons.get(y).getText().equals(""))
return y;
if (value3)
if (buttons.get(x).getText().equals(""))
return x;
return -1;
}
public boolean isSlotAvailable(int i)
{
if (i == -1)
return false;
return buttons.get(i).getText().equals("");
}
public void aiPicksPosition()
{
// AI PRIORITISES AGGRESSION BUT CAN ALSO BE VERY DEFENSIVE
if (firstTurn)
{
firstTurn = false;
int i = -1;
int count = 0;
while (!isSlotAvailable(i) && count < 100000)
{
count ++;
i = random.nextInt(buttons.size());
}
updateButton(buttons.get(i));
}
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");
for (String s : checks) // AI checks for winning positions for itself
{
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,turnCheck);
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(",");
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)
{
updateButton(buttons.get(i));
return;
}
}
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()
{
String turnCheck = "";
switch (currentTurn)
{
case CROSSES:
turnCheck = "X";
break;
case NOUGHTS:
turnCheck = "O";
break;
default:
break;
}
if (turnCheck.equals(""))
return;
for (String s : checks)
{
String[] split = s.split(",");
int x = Integer.parseInt(split[0]);
int y = Integer.parseInt(split[1]);
int z = Integer.parseInt(split[2]);
if (isWin(x,y,z,turnCheck))
win(turnCheck);
}
if (!checkBoard())
matchDraw();
}
public void matchDraw()
{
gameOver = true;
winLabel.setText("No one wins this time!");
}
public void win(String winnerName)
{
gameOver = true;
winLabel.setText(winnerName + " wins!");
}
public static void main(String passes[])
{
launch(passes);
}
void draw()
{
for (int i = 0; i < 9; i++)
{
Button b = new Button();
b.setMinWidth(128);
b.setMinHeight(128);
b.setStyle("-fx-font-size:40"); // https://stackoverflow.com/questions/50305533/javafx-button-setfont
buttons.add(b);
}
}
}
\ No newline at end of file
module Test {
requires javafx.base;
requires javafx.controls;
requires javafx.fxml;
requires javafx.graphics;
requires javafx.media;
requires javafx.swing;
requires javafx.swt;
requires javafx.web;
exports fx;
}
\ No newline at end of file
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