Commit e557515e authored by norbertdajnowski's avatar norbertdajnowski

Commit 1

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>NAC</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=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
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.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
import java.awt.Canvas;
import java.awt.Insets;
import java.awt.Label;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class main extends Application {
//on true "X" player turn on false "O" player turn
public boolean playerTurn = true;
ArrayList<Button> button = new ArrayList<Button>();
public int counter = 0;
public int internalCounter = 0;
public int horCheck = 0;
public int verCheck = 0;
public int[][] verArr = {
{0,3,6},
{1,4,7},
{2,5,8}
};
public int diaCheck = 0;
public boolean checkXwin() {
//x horizontal check
for (int z = 0; z <= 2; z++) {
for(int y = 0; y <= 2; y++){
if(button.get(internalCounter).getText() == "X") {
internalCounter = internalCounter + 1;
horCheck = horCheck + 1;
}
else {
internalCounter = internalCounter + 1;
}
if(horCheck == 3){
horCheck = 0;
return true;
}
}
horCheck = 0;
}
internalCounter = 0;
//x vertical check
for (int z = 0; z <= 2; z++) {
for(int y = 0; y <= 2; y++){
if(button.get(verArr[z][y]).getText() == "X") {
verCheck = verCheck + 1;
}
if(verCheck == 3){
verCheck = 0;
return true;
}
}
verCheck = 0;
}
internalCounter = 0;
if ((button.get(0).getText() == "X") && (button.get(4).getText() == "X") && (button.get(8).getText() == "X")) {
return true;
}
if ((button.get(2).getText() == "X") && (button.get(4).getText() == "X") && (button.get(6).getText() == "X")) {
return true;
}
return false;
}
public boolean checkOwin() {
//o horizontal check
for (int z = 0; z <= 2; z++) {
for(int y = 0; y <= 2; y++){
if(button.get(internalCounter).getText() == "O") {
internalCounter = internalCounter + 1;
horCheck = horCheck + 1;
}
else {
internalCounter = internalCounter + 1;
}
if(horCheck == 3){
horCheck = 0;
return true;
}
}
horCheck = 0;
}
internalCounter = 0;
for (int z = 0; z <= 2; z++) {
for(int y = 0; y <= 2; y++){
if(button.get(verArr[z][y]).getText() == "O") {
verCheck = verCheck + 1;
}
if(verCheck == 3){
verCheck = 0;
return true;
}
}
verCheck = 0;
}
internalCounter = 0;
if ((button.get(0).getText() == "O") && (button.get(4).getText() == "O") && (button.get(8).getText() == "O")) {
return true;
}
if ((button.get(2).getText() == "O") && (button.get(4).getText() == "O") && (button.get(6).getText() == "O")) {
return true;
}
return false;
}
public boolean checkdraw () {
for(int i = 0; i <= 8 ; i++) {
if (button.get(i).getText() == "") {
break;
}
if (i == 8) {
return true;
}
}
return false;
}
public void clearGrid() {
for (int i = 0; i <= 8 ; i ++) {
button.get(i).setText("");;
}
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Button pvpButt = new Button("Player vs Player");
pvpButt. setMinWidth (150);
pvpButt. setMinHeight (64);
pvpButt.setAlignment(Pos.CENTER);
pvpButt.setStyle("-fx-font: 15 arial; position: static;");
Button pveButt = new Button("Player vs AI");
pveButt. setMinWidth (150);
pveButt. setMinHeight (64);
pveButt.setAlignment(Pos.CENTER);
pveButt.setStyle("-fx-font: 15 arial; position: static;");
VBox rootMenu = new VBox();
Scene gameMenu = new Scene(rootMenu);
rootMenu.getChildren().addAll(pvpButt, pveButt);
Button newGame = new Button("Press to return to the main menu");
newGame.setLayoutX(80);
newGame.setLayoutY(350);
stage.setScene(gameMenu);
stage.show();
pvpButt.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
stage.close();
GridPane gridGame = new GridPane();
Group rootGame = new Group();
for (int x = 0; x <= 2; x++) {
for (int i = 0; i <= 2; i ++) {
Button tempButt = new Button();
tempButt.setMinWidth (128);
tempButt.setMinHeight (128);
tempButt.setAlignment(Pos.CENTER);
tempButt.setStyle("-fx-font: 32 arial;");
tempButt.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
//set value to O or X
if ((playerTurn == true) && (tempButt.getText() == "")) {
tempButt.setText("X");
playerTurn = false;
}
else if ((playerTurn == false) && (tempButt.getText() == "")) {
tempButt.setText("O");
playerTurn = true;
}
if (checkXwin() == true) {
Alert winAlert = new Alert(AlertType.INFORMATION);
winAlert.setContentText("Player X has won");
winAlert.setHeaderText("");
winAlert.show();
rootGame.getChildren().add(newGame);
newGame.toFront();
}
if (checkOwin() == true) {
Alert winAlert = new Alert(AlertType.INFORMATION);
winAlert.setContentText("Player O has won");
winAlert.setHeaderText("");
winAlert.show();
rootGame.getChildren().add(newGame);
newGame.toFront();
}
if (checkdraw() == true) {
Alert winAlert = new Alert(AlertType.INFORMATION);
winAlert.setContentText("The match has ended in a draw");
winAlert.setHeaderText("");
winAlert.show();
rootGame.getChildren().add(newGame);
newGame.toFront();
}
}
});
button.add(tempButt);
gridGame.add(button.get(counter), i, x);
counter = counter + 1;
}
}
counter = 0;
Stage stageGame = new Stage();
Scene sceneGame = new Scene(rootGame, Color.WHITE);
rootGame.getChildren().add(gridGame);
stageGame.setScene(sceneGame);
stageGame.show();
newGame.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
clearGrid();
stageGame.close();
rootGame.getChildren().remove(newGame);
try {
Stage emptyStage = new Stage();
rootGame.getChildren().removeAll(button);
start(emptyStage);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
});
}
}
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