Skip to content
Snippets Groups Projects
Commit 89f63f71 authored by Ricardo Müller's avatar Ricardo Müller
Browse files

Cleaned up and re-added GameBoard

fixed the win-check
parent 41dea7b0
No related branches found
No related tags found
1 merge request!9Cleaned up and re-added GameBoard
Index: src/main/java/com/example/tictactoeswtha2/Controller.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP
<+>package com.example.tictactoeswtha2;\n\nimport javafx.application.Platform;\nimport javafx.event.ActionEvent;\nimport javafx.fxml.FXML;\nimport javafx.fxml.FXMLLoader;\nimport javafx.geometry.Pos;\nimport javafx.scene.Parent;\nimport javafx.scene.Scene;\nimport javafx.scene.control.Button;\nimport javafx.scene.control.Label;\nimport javafx.scene.layout.GridPane;\nimport javafx.scene.layout.VBox;\nimport javafx.stage.Modality;\nimport javafx.stage.Stage;\n\nimport java.io.IOException;\n\n/**\n * Controller for the game-view\n * @author @ricardo.mueller\n */\npublic class Controller {\n /**\n * This char variable indicates who's turn it is\n */\n private char turnSign = 'X';\n /**\n * This grid represents the field holds all the buttons and the labels\n */\n @FXML\n private GridPane grid;\n /**\n * the buttons on the field\n */\n @FXML\n private Button b00, b01, b02, b10, b11, b12, b20, b21, b22, restartBtn;\n /**\n * the labels beneath the buttons that display the character\n */\n @FXML\n private Label l00, l01, l02, l10, l11, l12, l20, l21, l22;\n\n /**\n * Label that shows who's turn it is.\n */\n @FXML\n private Label turnLabel;\n /**\n * to start new window\n */\n private Stage stage;\n\n /**\n * gameBoard access\n */\n private GameBoard gameBoard = new GameBoard();\n\n /**\n * turns the player sign and checks the game state\n * @param row the row of the tile\n * @param column the column of the tile\n */\n private void click(int row, int column) throws IOException {\n // Todo: Check for win\n if(gameBoard.winCheck() ==1 || gameBoard.winCheck() == 2)\n {\n stage = new Stage();\n stage.initModality(Modality.APPLICATION_MODAL);\n stage.setTitle(\"neu spiel\");\n stage.setMinWidth(250);\n Label label = new Label();\n label.setText(\"willst du neu Spiel starten?\");\n Button yesBtn = new Button(\"Ja\");\n Button noBtn = new Button(\"Nein\");\n yesBtn.setOnAction(e->{\n restart();\n stage.close();\n });\n noBtn.setOnAction(e->{\n stage.close();\n });\n VBox layout = new VBox(10);\n layout.getChildren().addAll(label, yesBtn, noBtn);\n layout.setAlignment(Pos.CENTER);\n Scene scene = new Scene(layout);\n stage.setScene(scene);\n stage.showAndWait();\n }else\n {\n if (turnSign == 'X') {\n turnSign = 'O';\n turnLabel.setText(\"O's turn\");\n } else {\n turnSign = 'X';\n turnLabel.setText(\"X's turn\");\n }\n }\n\n checkWin();\n }\n\n /**\n * Checks if the game is won or tied\n */\n private void checkWin() {\n\n if (((l00.getText().length() > 0 && l01.getText().length() > 0 && l02.getText().length() > 0 && l00.getText().charAt(0) == l01.getText().charAt(0) && l01.getText().charAt(0) == l02.getText().charAt(0)) ||\n (l10.getText().length() > 0 && l11.getText().length() > 0 && l12.getText().length() > 0 && l10.getText().charAt(0) == l11.getText().charAt(0) && l11.getText().charAt(0) == l12.getText().charAt(0)) ||\n (l20.getText().length() > 0 && l21.getText().length() > 0 && l22.getText().length() > 0 && l20.getText().charAt(0) == l21.getText().charAt(0) && l21.getText().charAt(0) == l22.getText().charAt(0)) ||\n (l00.getText().length() > 0 && l10.getText().length() > 0 && l20.getText().length() > 0 && l00.getText().charAt(0) == l10.getText().charAt(0) && l10.getText().charAt(0) == l20.getText().charAt(0)) ||\n (l01.getText().length() > 0 && l11.getText().length() > 0 && l21.getText().length() > 0 && l01.getText().charAt(0) == l11.getText().charAt(0) && l11.getText().charAt(0) == l21.getText().charAt(0)) ||\n (l02.getText().length() > 0 && l12.getText().length() > 0 && l22.getText().length() > 0 && l02.getText().charAt(0) == l12.getText().charAt(0) && l12.getText().charAt(0) == l22.getText().charAt(0)) ||\n (l00.getText().length() > 0 && l11.getText().length() > 0 && l22.getText().length() > 0 && l00.getText().charAt(0) == l11.getText().charAt(0) && l11.getText().charAt(0) == l22.getText().charAt(0)) ||\n (l20.getText().length() > 0 && l11.getText().length() > 0 && l02.getText().length() > 0 && l02.getText().charAt(0) == l11.getText().charAt(0) && l11.getText().charAt(0) == l20.getText().charAt(0)))) {\n if (turnSign == 'X')\n turnLabel.setText(\"O won\");\n else\n turnLabel.setText(\"X won\");\n return;\n }\n\n\n if (l00.getText().length() > 0 && l01.getText().length() > 0 && l02.getText().length() > 0 &&\n l10.getText().length() > 0 && l11.getText().length() > 0 && l12.getText().length() > 0 &&\n l20.getText().length() > 0 && l21.getText().length() > 0 && l22.getText().length() > 0)\n turnLabel.setText(\"Tie\");\n }\n\n\n\n\n /**\n * Makes the button disappear, displays the sign and calls click to check the game state and change the sign\n * show the label\n * @param e obligatory ActionEvent\n */\n public void clickB00(ActionEvent e) throws IOException {\n int row = 0, column = 0;\n l00.setText(\"\" + turnSign);\n b00.setVisible(false);\n click(row, column);\n\n }\n /**\n * Makes the button disappear, displays the sign and calls click to check the game state and change the sign\n * show the label\n * @param e obligatory ActionEvent\n */\n public void clickB01(ActionEvent e) throws IOException {\n int row = 0, column = 1;\n l01.setText(\"\" + turnSign);\n b01.setVisible(false);\n click(row, column);\n }\n /**\n * Makes the button disappear, displays the sign and calls click to check the game state and change the sign\n * show the label\n * @param e obligatory ActionEvent\n */\n public void clickB02(ActionEvent e) throws IOException {\n int row = 0, column = 2;\n l02.setText(\"\" + turnSign);\n b02.setVisible(false);\n click(row, column);\n }\n /**\n * Makes the button disappear, displays the sign and calls click to check the game state and change the sign\n * show the label\n * @param e obligatory ActionEvent\n */\n public void clickB10(ActionEvent e) throws IOException {\n int row = 1, column = 0;\n l10.setText(\"\" + turnSign);\n b10.setVisible(false);\n click(row, column);\n }\n /**\n * Makes the button disappear, displays the sign and calls click to check the game state and change the sign\n * show the label\n * @param e obligatory ActionEvent\n */\n public void clickB11(ActionEvent e) throws IOException {\n int row = 1, column = 1;\n l11.setText(\"\" + turnSign);\n b11.setVisible(false);\n click(row, column);\n }\n /**\n * Makes the button disappear, displays the sign and calls click to check the game state and change the sign\n * show the label\n * @param e obligatory ActionEvent\n */\n public void clickB12(ActionEvent e) throws IOException {\n int row = 1, column = 2;\n l12.setText(\"\" + turnSign);\n b12.setVisible(false);\n click(row, column);\n }\n /**\n * Makes the button disappear, displays the sign and calls click to check the game state and change the sign\n * show the label\n * @param e obligatory ActionEvent\n */\n public void clickB20(ActionEvent e) throws IOException {\n int row = 2, column = 0;\n l20.setText(\"\" + turnSign);\n b20.setVisible(false);\n click(row, column);\n }\n /**\n * Makes the button disappear, displays the sign and calls click to check the game state and change the sign\n * show the label\n * @param e obligatory ActionEvent\n */\n public void clickB21(ActionEvent e) throws IOException {\n int row = 2, column = 1;\n l21.setText(\"\" + turnSign);\n b21.setVisible(false);\n click(row, column);\n }\n /**\n * Makes the button disappear, displays the sign and calls click to check the game state and change the sign\n * show the label\n * @param e obligatory ActionEvent\n */\n public void clickB22(ActionEvent e) throws IOException {\n int row = 2, column = 2;\n l22.setText(\"\" + turnSign);\n b22.setVisible(false);\n click(row, column);\n }\n\n /**\n * start new Game\n */\n public void restart(){\n b00.setVisible(true);\n b01.setVisible(true);\n b02.setVisible(true);\n b10.setVisible(true);\n b11.setVisible(true);\n b12.setVisible(true);\n b20.setVisible(true);\n b21.setVisible(true);\n b22.setVisible(true);\n\n l00.setText(\"\");\n l01.setText(\"\");\n l02.setText(\"\");\n l10.setText(\"\");\n l11.setText(\"\");\n l12.setText(\"\");\n l20.setText(\"\");\n l21.setText(\"\");\n l22.setText(\"\");\n\n turnSign = 'X';\n turnLabel.setText(\"X's turn\");\n\n }\n}
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/example/tictactoeswtha2/Controller.java b/src/main/java/com/example/tictactoeswtha2/Controller.java
--- a/src/main/java/com/example/tictactoeswtha2/Controller.java (revision 10a7c936e76dad6687de59a1a0ba755c5133328e)
+++ b/src/main/java/com/example/tictactoeswtha2/Controller.java (date 1673864064487)
@@ -62,15 +62,14 @@
* @param column the column of the tile
*/
private void click(int row, int column) throws IOException {
- // Todo: Check for win
- if(gameBoard.winCheck() ==1 || gameBoard.winCheck() == 2)
+ if(gameBoard.winCheck() == 1 || gameBoard.winCheck() == 2)
{
stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("neu spiel");
stage.setMinWidth(250);
Label label = new Label();
- label.setText("willst du neu Spiel starten?");
+ label.setText("Willst du neu Spiel starten?");
Button yesBtn = new Button("Ja");
Button noBtn = new Button("Nein");
yesBtn.setOnAction(e->{
@@ -78,7 +77,7 @@
stage.close();
});
noBtn.setOnAction(e->{
- stage.close();
+ System.exit(0);
});
VBox layout = new VBox(10);
layout.getChildren().addAll(label, yesBtn, noBtn);
@@ -96,39 +95,7 @@
turnLabel.setText("X's turn");
}
}
-
- checkWin();
}
-
- /**
- * Checks if the game is won or tied
- */
- private void checkWin() {
-
- if (((l00.getText().length() > 0 && l01.getText().length() > 0 && l02.getText().length() > 0 && l00.getText().charAt(0) == l01.getText().charAt(0) && l01.getText().charAt(0) == l02.getText().charAt(0)) ||
- (l10.getText().length() > 0 && l11.getText().length() > 0 && l12.getText().length() > 0 && l10.getText().charAt(0) == l11.getText().charAt(0) && l11.getText().charAt(0) == l12.getText().charAt(0)) ||
- (l20.getText().length() > 0 && l21.getText().length() > 0 && l22.getText().length() > 0 && l20.getText().charAt(0) == l21.getText().charAt(0) && l21.getText().charAt(0) == l22.getText().charAt(0)) ||
- (l00.getText().length() > 0 && l10.getText().length() > 0 && l20.getText().length() > 0 && l00.getText().charAt(0) == l10.getText().charAt(0) && l10.getText().charAt(0) == l20.getText().charAt(0)) ||
- (l01.getText().length() > 0 && l11.getText().length() > 0 && l21.getText().length() > 0 && l01.getText().charAt(0) == l11.getText().charAt(0) && l11.getText().charAt(0) == l21.getText().charAt(0)) ||
- (l02.getText().length() > 0 && l12.getText().length() > 0 && l22.getText().length() > 0 && l02.getText().charAt(0) == l12.getText().charAt(0) && l12.getText().charAt(0) == l22.getText().charAt(0)) ||
- (l00.getText().length() > 0 && l11.getText().length() > 0 && l22.getText().length() > 0 && l00.getText().charAt(0) == l11.getText().charAt(0) && l11.getText().charAt(0) == l22.getText().charAt(0)) ||
- (l20.getText().length() > 0 && l11.getText().length() > 0 && l02.getText().length() > 0 && l02.getText().charAt(0) == l11.getText().charAt(0) && l11.getText().charAt(0) == l20.getText().charAt(0)))) {
- if (turnSign == 'X')
- turnLabel.setText("O won");
- else
- turnLabel.setText("X won");
- return;
- }
-
-
- if (l00.getText().length() > 0 && l01.getText().length() > 0 && l02.getText().length() > 0 &&
- l10.getText().length() > 0 && l11.getText().length() > 0 && l12.getText().length() > 0 &&
- l20.getText().length() > 0 && l21.getText().length() > 0 && l22.getText().length() > 0)
- turnLabel.setText("Tie");
- }
-
-
-
/**
* Makes the button disappear, displays the sign and calls click to check the game state and change the sign
@@ -137,6 +104,13 @@
*/
public void clickB00(ActionEvent e) throws IOException {
int row = 0, column = 0;
+ int player = 0;
+ if(turnSign == 'X') {
+ player = 1;
+ } else {
+ player = 2;
+ }
+ gameBoard.addSymbol(row, column, player);
l00.setText("" + turnSign);
b00.setVisible(false);
click(row, column);
@@ -149,6 +123,13 @@
*/
public void clickB01(ActionEvent e) throws IOException {
int row = 0, column = 1;
+ int player = 0;
+ if(turnSign == 'X') {
+ player = 1;
+ } else {
+ player = 2;
+ }
+ gameBoard.addSymbol(row, column, player);
l01.setText("" + turnSign);
b01.setVisible(false);
click(row, column);
@@ -160,6 +141,13 @@
*/
public void clickB02(ActionEvent e) throws IOException {
int row = 0, column = 2;
+ int player = 0;
+ if(turnSign == 'X') {
+ player = 1;
+ } else {
+ player = 2;
+ }
+ gameBoard.addSymbol(row, column, player);
l02.setText("" + turnSign);
b02.setVisible(false);
click(row, column);
@@ -171,6 +159,13 @@
*/
public void clickB10(ActionEvent e) throws IOException {
int row = 1, column = 0;
+ int player = 0;
+ if(turnSign == 'X') {
+ player = 1;
+ } else {
+ player = 2;
+ }
+ gameBoard.addSymbol(row, column, player);
l10.setText("" + turnSign);
b10.setVisible(false);
click(row, column);
@@ -182,6 +177,13 @@
*/
public void clickB11(ActionEvent e) throws IOException {
int row = 1, column = 1;
+ int player = 0;
+ if(turnSign == 'X') {
+ player = 1;
+ } else {
+ player = 2;
+ }
+ gameBoard.addSymbol(row, column, player);
l11.setText("" + turnSign);
b11.setVisible(false);
click(row, column);
@@ -193,6 +195,13 @@
*/
public void clickB12(ActionEvent e) throws IOException {
int row = 1, column = 2;
+ int player = 0;
+ if(turnSign == 'X') {
+ player = 1;
+ } else {
+ player = 2;
+ }
+ gameBoard.addSymbol(row, column, player);
l12.setText("" + turnSign);
b12.setVisible(false);
click(row, column);
@@ -204,6 +213,13 @@
*/
public void clickB20(ActionEvent e) throws IOException {
int row = 2, column = 0;
+ int player = 0;
+ if(turnSign == 'X') {
+ player = 1;
+ } else {
+ player = 2;
+ }
+ gameBoard.addSymbol(row, column, player);
l20.setText("" + turnSign);
b20.setVisible(false);
click(row, column);
@@ -215,6 +231,13 @@
*/
public void clickB21(ActionEvent e) throws IOException {
int row = 2, column = 1;
+ int player = 0;
+ if(turnSign == 'X') {
+ player = 1;
+ } else {
+ player = 2;
+ }
+ gameBoard.addSymbol(row, column, player);
l21.setText("" + turnSign);
b21.setVisible(false);
click(row, column);
@@ -226,6 +249,13 @@
*/
public void clickB22(ActionEvent e) throws IOException {
int row = 2, column = 2;
+ int player = 0;
+ if(turnSign == 'X') {
+ player = 1;
+ } else {
+ player = 2;
+ }
+ gameBoard.addSymbol(row, column, player);
l22.setText("" + turnSign);
b22.setVisible(false);
click(row, column);
@@ -255,6 +285,8 @@
l21.setText("");
l22.setText("");
+ gameBoard = new GameBoard();
+
turnSign = 'X';
turnLabel.setText("X's turn");
<changelist name="Uncommitted_changes_before_Update_at_16_01_23,_11_19_[Changes]" date="1673864351953" recycled="false" toDelete="true">
<option name="PATH" value="$PROJECT_DIR$/.idea/shelf/Uncommitted_changes_before_Update_at_16_01_23,_11_19_[Changes]/shelved.patch" />
<option name="DESCRIPTION" value="Uncommitted changes before Update at 16.01.23, 11:19 [Changes]" />
</changelist>
\ No newline at end of file
package com.example.tictactoeswtha2;
public class GameBoard {
/**
* 3x3 game board
* 1 = player 1
* 2 = player 2
*/
private int[][] board = new int[3][3];
/**
* Initialize every position with 0
*/
public GameBoard(){
for (int y = 0; y < 3; y++){
for (int x = 0; x < 3; x++){
board[y][x] = 0;
}
}
}
/**
* Tries to place a symbol on the board
* @param row what row to place it in
* @param column what column to place it in
* @param symbol Symbol that should be placed
*/
public void addSymbol(int row, int column, int symbol){
this.board[row][column] = symbol;
}
/**
* Checks if the game is finished
* 0: game isn't finished yet
* 1: Player 1 won
* 2: Player 2 won
* 3: tie
* @return Winner or tie or unfinished
*/
public int winCheck(){
for (int i = 0; i < 3; i++){
// vertical
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] > 0)
return board[0][i];
// horizontal
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] > 0)
return board[i][0];
}
// diagonal
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] > 0)
return board[1][1];
if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] > 0)
return board[1][1];
// game isn't finished
for (int i = 0; i < 9; i++) {
int x = i % 3 + 1;
int y = i / 3;
if (board[y][x] == 0)
return 0;
}
// Tie
return 3;
}
}
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