Skip to content
Snippets Groups Projects
Commit 0a257ae9 authored by Timon Harries's avatar Timon Harries
Browse files

Merge branch 'dev' of gitlab.uni-hannover.de:nick130625/ta2_swt into dev

parents f919a279 9cacd9e1
No related branches found
No related tags found
1 merge request!1Verision 1.0 finished
......@@ -13,7 +13,7 @@ public class SceneController {
private Actor player;
private Actor enemy;
public boolean playerTurn = true;
public int playerTurn = 1;
public void initData(Board board, Actor player, Actor enemy) {
this.board = board;
......@@ -22,7 +22,7 @@ public class SceneController {
}
public void playerTurn(){
this.playerTurn = true;
this.playerTurn = 1;
turnlabel.setText("Your Turn");
}
......@@ -53,12 +53,30 @@ public class SceneController {
buttonList.add(button9);
}
public void aiTurn(int col, int row){
public void aiTurn(){
// AI's Turn
playerTurn = 0;
turnlabel.setText("AI's Turn");
int[] coordinates = enemy.makeMove(board);
// DEBUG
System.out.println("Enemy: " + coordinates[0] + ", " + coordinates[1]);
initialize();
buttonList.get(col + row).setText("o");
buttonList.get(coordinates[0] + coordinates[1]).setText("o");
playerTurn();
}
public void over(){
if(board.checkForWin()){
System.out.println("Game is over!");
if(playerTurn == 0){
turnlabel.setText("You win!");
}else if(playerTurn == 1){
turnlabel.setText("You lose!");
}
playerTurn = -1;
}
}
// Diese Funktion wird beim Button-Click aufgerufen
@FXML
......@@ -71,15 +89,16 @@ public class SceneController {
int row = GridPane.getRowIndex(pressedButton); // Zeilen-Koordinate
int col = GridPane.getColumnIndex(pressedButton); // Spalten-Koordinate
if(buttonText.equals(" ") && playerTurn) {
over();
if(buttonText.equals(" ") && playerTurn == 1) {
// Setze X
pressedButton.setText("X");
player.makeMove(board, col, row);
over();
// AI's Turn
playerTurn = false;
turnlabel.setText("AI's Turn");
aiTurn();
}
}
......
......@@ -42,26 +42,6 @@ public class TicTacToe extends Application {
launch(args);
while (!board.checkForWin()){
if (sceneController.playerTurn) {
// TODO Spieler ist am Zug
continue;
} else {
// TODO Gegner ist am Zug
}
// TODO Spielablauf
}
// TODO Spiel beenden
// TODO Gewinner verkünden
// TODO Option zum Neustart
}
}
......@@ -8,4 +8,20 @@ public interface Actor {
public char getSymbol();
public void setSymbol(char symbol);
// Methode für Spieler mit Koordinaten
default boolean makeMove(Board board, int x, int y) {
return board.placeToken(x, y, symbol);
}
// Methode für Gegner (Zufallsgenerator)
default int[] makeMove(Board board) {
Random random = new Random();
int x, y;
do {
x = random.nextInt(3);
y = random.nextInt(3);
} while (!board.placeToken(x, y, symbol));
int[] coordinates = {x, y};
return coordinates;
}
}
package logic;
import java.util.Random;
public class Enemy implements Actor {
public char symbol;
public Enemy() {
......@@ -18,16 +16,4 @@ public class Enemy implements Actor {
this.symbol = symbol;
}
// Methode, um den nächsten Zug des Akteurs zu machen
public boolean makeMove(Board board) {
Random random = new Random();
int x;
int y;
do {
x = random.nextInt(3);
y = random.nextInt(3);
} while (!board.placeToken(x, y, symbol));
return true;
}
}
......@@ -16,10 +16,4 @@ public class Player implements Actor {
this.symbol = symbol;
}
// Methode, um den nächsten Zug des Akteurs zu machen
public boolean makeMove(Board board, int x, int y) {
return board.placeToken(x, y, symbol);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment