From 1b5b0777046741e0b6c76cbe5e074ae4647a5faf Mon Sep 17 00:00:00 2001 From: kfkama Date: Tue, 15 Feb 2022 13:52:50 +0100 Subject: [PATCH] Game end: Player wins, with Test --- src/main/java/TicTacToe/TicTacToeGame.java | 25 +++++++++++- src/main/java/TicTacToe/cell.java | 2 +- .../java/TicTacToe/TicTacToeGameTest.java | 40 +++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index 89fa969..3cde289 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -5,6 +5,7 @@ import java.awt.Graphics2D; import java.awt.geom.Line2D; import javax.swing.JFrame; +import javax.swing.JOptionPane; import javax.swing.JPanel; @@ -67,11 +68,33 @@ public class TicTacToeGame extends JPanel { } public void endTurn() { + + if(checkPlayfield() != 0) { + JOptionPane.showMessageDialog(getParent(),"Player: " + player + " Wins!"); + System.exit(0); + } + player++; if(player >= 3) { player = 1; } - + } + + public int checkPlayfield() { + if ((field[0].value == player && field[1].value == player && field[2].value == player) + || (field[0].value == player && field[3].value == player && field[6].value == player) + || (field[8].value == player && field[5].value == player && field[2].value == player) + || (field[8].value == player && field[7].value == player && field[6].value == player) + || (field[0].value == player && field[4].value == player && field[8].value == player) + || (field[0].value == player && field[4].value == player && field[8].value == player) + || (field[2].value == player && field[4].value == player && field[6].value == player) + || (field[3].value == player && field[4].value == player && field[5].value == player) + || (field[1].value == player && field[4].value == player && field[7].value == player)) { + + return player; + } + + return 0; } diff --git a/src/main/java/TicTacToe/cell.java b/src/main/java/TicTacToe/cell.java index 65e0db5..b4cff42 100644 --- a/src/main/java/TicTacToe/cell.java +++ b/src/main/java/TicTacToe/cell.java @@ -7,7 +7,7 @@ import javax.swing.JButton; public class cell extends JButton { - int value = 0; + public int value = 0; private TicTacToeGame ttt; public cell(TicTacToeGame _ttt) { diff --git a/src/test/java/TicTacToe/TicTacToeGameTest.java b/src/test/java/TicTacToe/TicTacToeGameTest.java index a02accb..4111603 100644 --- a/src/test/java/TicTacToe/TicTacToeGameTest.java +++ b/src/test/java/TicTacToe/TicTacToeGameTest.java @@ -1,8 +1,48 @@ package TicTacToe; import static org.junit.jupiter.api.Assertions.*; + +import java.util.stream.Stream; + import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import Arcade64.Arcade64.Code; class TicTacToeGameTest { + @ParameterizedTest + @MethodSource("testFieldsWinning") + void testGameEndWin(int[] _field, int _winner) { + TicTacToeGame ttt = new TicTacToeGame(); + + for(int i = 0; i < ttt.field.length; i++) { + ttt.field[i].value = _field[i]; + } + ttt.player = _winner; + int realWinner = ttt.checkPlayfield(); + + assertEquals(_winner, realWinner); + } + + private static Stream testFieldsWinning(){ + return Stream.of( + Arguments.of(new int[]{ 1,2,1, + 2,2,2, + 1,2,1}, 2), + Arguments.of(new int[]{ 2,1,2, + 2,2,1, + 1,1,1}, 1), + Arguments.of(new int[]{ 1,1,2, + 1,2,2, + 1,2,1}, 1), + Arguments.of(new int[]{ 2,1,1, + 1,2,1, + 1,1,2}, 2) + ); + + + } }