From bb5e31ff9f826afae19ca7253647ad8147cb8f36 Mon Sep 17 00:00:00 2001 From: kfkama Date: Mon, 14 Feb 2022 23:19:33 +0100 Subject: [PATCH 1/7] Add cell class --- src/main/java/TicTacToe/TicTacToeGame.java | 5 +++-- src/main/java/TicTacToe/cell.java | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 src/main/java/TicTacToe/cell.java diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index f958273..db769ff 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -4,6 +4,7 @@ import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; +import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; @@ -12,13 +13,13 @@ public class TicTacToeGame extends JPanel { private static final long serialVersionUID = 1L; private static int width = 600, height = 600; - + + public TicTacToeGame() { this.setSize(width, height); } public static void main(String[] args) { - JFrame f = new JFrame(); TicTacToeGame ttt = new TicTacToeGame(); diff --git a/src/main/java/TicTacToe/cell.java b/src/main/java/TicTacToe/cell.java new file mode 100644 index 0000000..ea76245 --- /dev/null +++ b/src/main/java/TicTacToe/cell.java @@ -0,0 +1,13 @@ +package TicTacToe; +import javax.swing.JButton; + +public class cell extends JButton { + + int value = 0; + + public cell() { + + } + + +} From 597dffcc6067df7ffd60e54fc5068a368b2e618f Mon Sep 17 00:00:00 2001 From: kfkama Date: Tue, 15 Feb 2022 00:05:50 +0100 Subject: [PATCH 2/7] Made cell change color on click/Player turns --- src/main/java/TicTacToe/TicTacToeGame.java | 35 ++++++++++++++++++++-- src/main/java/TicTacToe/cell.java | 31 +++++++++++++++++-- 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index db769ff..89fa969 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -4,7 +4,6 @@ import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; -import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; @@ -13,22 +12,44 @@ public class TicTacToeGame extends JPanel { private static final long serialVersionUID = 1L; private static int width = 600, height = 600; - + public cell[] field; + public int player = 1; public TicTacToeGame() { this.setSize(width, height); + setLayout(null); + initField(); } public static void main(String[] args) { JFrame f = new JFrame(); TicTacToeGame ttt = new TicTacToeGame(); - + f.add(ttt); f.setSize(width,height); f.setLayout(null); f.setVisible(true); } + public void initField() { + field = new cell[9]; + + for(int i = 0; i < field.length; i++) { + field[i] = new cell(this); + add(field[i]); + } + + for(int i = 0; i < field.length; i++) { + if(i < 3) { + field[i].setBounds(150 + i*100, 150 , 100, 100); + } else if (i < 6) { + field[i].setBounds(150 + i%3*100, 250 , 100, 100); + } else { + field[i].setBounds(150 + i%3*100, 350 , 100, 100); + } + } + } + @Override protected void paintComponent(Graphics g) { super.paintComponent(g); @@ -45,5 +66,13 @@ public class TicTacToeGame extends JPanel { g2.draw(lin4); } + public void endTurn() { + player++; + if(player >= 3) { + player = 1; + } + + } + } \ No newline at end of file diff --git a/src/main/java/TicTacToe/cell.java b/src/main/java/TicTacToe/cell.java index ea76245..b8c4351 100644 --- a/src/main/java/TicTacToe/cell.java +++ b/src/main/java/TicTacToe/cell.java @@ -1,13 +1,40 @@ package TicTacToe; +import java.awt.Color; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + import javax.swing.JButton; public class cell extends JButton { int value = 0; + private TicTacToeGame ttt; - public cell() { + public cell(TicTacToeGame _ttt) { + ttt = _ttt; + addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + OnMouseClick(); + } + }); } - + protected void OnMouseClick() { + value = ttt.player; + ttt.endTurn(); + switch (value) { + case 1: setBackground(new Color(255,0,0)); + break; + case 2: setBackground(new Color(0,0,255)); + break; + default: ; + break; + } + } + + + + } From e684a87d66335b9cdf1ce814f8721d1fca056ca6 Mon Sep 17 00:00:00 2001 From: kfkama Date: Tue, 15 Feb 2022 12:49:03 +0100 Subject: [PATCH 3/7] Expanded cell click logic --- src/main/java/TicTacToe/cell.java | 22 +++++++++++-------- .../java/TicTacToe/TicTacToeGameTest.java | 2 -- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/TicTacToe/cell.java b/src/main/java/TicTacToe/cell.java index b8c4351..65e0db5 100644 --- a/src/main/java/TicTacToe/cell.java +++ b/src/main/java/TicTacToe/cell.java @@ -22,15 +22,19 @@ public class cell extends JButton { } protected void OnMouseClick() { - value = ttt.player; - ttt.endTurn(); - switch (value) { - case 1: setBackground(new Color(255,0,0)); - break; - case 2: setBackground(new Color(0,0,255)); - break; - default: ; - break; + if(value == 0) { + value = ttt.player; + ttt.endTurn(); + setEnabled(false); + + switch (value) { + case 1: setBackground(new Color(255,0,0)); + break; + case 2: setBackground(new Color(0,0,255)); + break; + default: ; + break; + } } } diff --git a/src/test/java/TicTacToe/TicTacToeGameTest.java b/src/test/java/TicTacToe/TicTacToeGameTest.java index 08742dd..a02accb 100644 --- a/src/test/java/TicTacToe/TicTacToeGameTest.java +++ b/src/test/java/TicTacToe/TicTacToeGameTest.java @@ -4,7 +4,5 @@ import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; class TicTacToeGameTest { - - } From 1b5b0777046741e0b6c76cbe5e074ae4647a5faf Mon Sep 17 00:00:00 2001 From: kfkama Date: Tue, 15 Feb 2022 13:52:50 +0100 Subject: [PATCH 4/7] 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) + ); + + + } } From aad8ef8cf9d610da47396bb73f2cdcbed9825b72 Mon Sep 17 00:00:00 2001 From: kfkama Date: Tue, 15 Feb 2022 14:04:25 +0100 Subject: [PATCH 5/7] Refactore: TicTacToe, cell --- src/main/java/TicTacToe/TicTacToeGame.java | 38 ++++++++++--------- src/main/java/TicTacToe/cell.java | 8 ++-- .../java/TicTacToe/TicTacToeGameTest.java | 7 +--- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index 3cde289..9784f8b 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -12,9 +12,12 @@ import javax.swing.JPanel; public class TicTacToeGame extends JPanel { private static final long serialVersionUID = 1L; - private static int width = 600, height = 600; + private static final int width = 600, height = 600; + private static final int maxPlayers = 3; + private static final int playFieldSize = 9; + public cell[] field; - public int player = 1; + public int playerID = 1; public TicTacToeGame() { this.setSize(width, height); @@ -33,7 +36,7 @@ public class TicTacToeGame extends JPanel { } public void initField() { - field = new cell[9]; + field = new cell[playFieldSize]; for(int i = 0; i < field.length; i++) { field[i] = new cell(this); @@ -68,30 +71,29 @@ public class TicTacToeGame extends JPanel { } public void endTurn() { - if(checkPlayfield() != 0) { - JOptionPane.showMessageDialog(getParent(),"Player: " + player + " Wins!"); + JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!"); System.exit(0); } - player++; - if(player >= 3) { - player = 1; + playerID++; + if(playerID >= maxPlayers) { + playerID = 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)) { + if ((field[0].playerID == playerID && field[1].playerID == playerID && field[2].playerID == playerID) + || (field[0].playerID == playerID && field[3].playerID == playerID && field[6].playerID == playerID) + || (field[8].playerID == playerID && field[5].playerID == playerID && field[2].playerID == playerID) + || (field[8].playerID == playerID && field[7].playerID == playerID && field[6].playerID == playerID) + || (field[0].playerID == playerID && field[4].playerID == playerID && field[8].playerID == playerID) + || (field[0].playerID == playerID && field[4].playerID == playerID && field[8].playerID == playerID) + || (field[2].playerID == playerID && field[4].playerID == playerID && field[6].playerID == playerID) + || (field[3].playerID == playerID && field[4].playerID == playerID && field[5].playerID == playerID) + || (field[1].playerID == playerID && field[4].playerID == playerID && field[7].playerID == playerID)) { - return player; + return playerID; } return 0; diff --git a/src/main/java/TicTacToe/cell.java b/src/main/java/TicTacToe/cell.java index b4cff42..249ff4b 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 { - public int value = 0; + public int playerID = 0; private TicTacToeGame ttt; public cell(TicTacToeGame _ttt) { @@ -22,12 +22,12 @@ public class cell extends JButton { } protected void OnMouseClick() { - if(value == 0) { - value = ttt.player; + if(playerID == 0) { + playerID = ttt.playerID; ttt.endTurn(); setEnabled(false); - switch (value) { + switch (playerID) { case 1: setBackground(new Color(255,0,0)); break; case 2: setBackground(new Color(0,0,255)); diff --git a/src/test/java/TicTacToe/TicTacToeGameTest.java b/src/test/java/TicTacToe/TicTacToeGameTest.java index 4111603..1211ca3 100644 --- a/src/test/java/TicTacToe/TicTacToeGameTest.java +++ b/src/test/java/TicTacToe/TicTacToeGameTest.java @@ -1,15 +1,12 @@ 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 { @@ -19,9 +16,9 @@ class TicTacToeGameTest { TicTacToeGame ttt = new TicTacToeGame(); for(int i = 0; i < ttt.field.length; i++) { - ttt.field[i].value = _field[i]; + ttt.field[i].playerID = _field[i]; } - ttt.player = _winner; + ttt.playerID = _winner; int realWinner = ttt.checkPlayfield(); assertEquals(_winner, realWinner); From 072610ab1b7612ce6f0317bcceb2339697874fff Mon Sep 17 00:00:00 2001 From: kfkama Date: Tue, 15 Feb 2022 14:12:52 +0100 Subject: [PATCH 6/7] Game end: Draw, with Test --- src/main/java/TicTacToe/TicTacToeGame.java | 9 +++++- .../java/TicTacToe/TicTacToeGameTest.java | 32 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index 9784f8b..c551abf 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -18,6 +18,7 @@ public class TicTacToeGame extends JPanel { public cell[] field; public int playerID = 1; + public int turns = 0; public TicTacToeGame() { this.setSize(width, height); @@ -75,7 +76,13 @@ public class TicTacToeGame extends JPanel { JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!"); System.exit(0); } - + + turns++; + if(turns >= playFieldSize) { + JOptionPane.showMessageDialog(getParent(),"Draw!"); + System.exit(0); + } + playerID++; if(playerID >= maxPlayers) { playerID = 1; diff --git a/src/test/java/TicTacToe/TicTacToeGameTest.java b/src/test/java/TicTacToe/TicTacToeGameTest.java index 1211ca3..beaabde 100644 --- a/src/test/java/TicTacToe/TicTacToeGameTest.java +++ b/src/test/java/TicTacToe/TicTacToeGameTest.java @@ -24,6 +24,21 @@ class TicTacToeGameTest { assertEquals(_winner, realWinner); } + @ParameterizedTest + @MethodSource("testFieldsDraw") + void testGameEndDraw(int[] _field) { + TicTacToeGame ttt = new TicTacToeGame(); + + for(int i = 0; i < ttt.field.length; i++) { + ttt.field[i].playerID = _field[i]; + } + + int noWinner = ttt.checkPlayfield(); + + assertEquals(0, noWinner); + } + + private static Stream testFieldsWinning(){ return Stream.of( Arguments.of(new int[]{ 1,2,1, @@ -39,6 +54,23 @@ class TicTacToeGameTest { 1,2,1, 1,1,2}, 2) ); + } + + private static Stream testFieldsDraw(){ + return Stream.of( + Arguments.of(new int[]{ 2,1,1, + 2,2,2, + 1,2,1}), + Arguments.of(new int[]{ 2,1,2, + 2,2,1, + 1,2,1}), + Arguments.of(new int[]{ 2,1,2, + 1,2,2, + 1,2,1}), + Arguments.of(new int[]{ 2,1,1, + 1,2,2, + 2,1,1}) + ); } From e5690a005d319fc71ce5b192ffa44440c4f0be1f Mon Sep 17 00:00:00 2001 From: kfkama Date: Tue, 15 Feb 2022 14:24:20 +0100 Subject: [PATCH 7/7] Reset game on game end --- src/main/java/TicTacToe/TicTacToeGame.java | 12 ++++++-- src/main/java/TicTacToe/cell.java | 35 ++++++++++++---------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index c551abf..acef34d 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -74,13 +74,13 @@ public class TicTacToeGame extends JPanel { public void endTurn() { if(checkPlayfield() != 0) { JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!"); - System.exit(0); + resetGame(); } turns++; if(turns >= playFieldSize) { JOptionPane.showMessageDialog(getParent(),"Draw!"); - System.exit(0); + resetGame(); } playerID++; @@ -105,6 +105,14 @@ public class TicTacToeGame extends JPanel { return 0; } + + public void resetGame() { + for (cell c : field) { + c.reset(); + } + playerID = 1; + turns = 0; + } } \ No newline at end of file diff --git a/src/main/java/TicTacToe/cell.java b/src/main/java/TicTacToe/cell.java index 249ff4b..27a3158 100644 --- a/src/main/java/TicTacToe/cell.java +++ b/src/main/java/TicTacToe/cell.java @@ -1,4 +1,5 @@ package TicTacToe; + import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -9,10 +10,11 @@ public class cell extends JButton { public int playerID = 0; private TicTacToeGame ttt; - + public cell(TicTacToeGame _ttt) { ttt = _ttt; - + setBackground(new Color(255,255,255)); + addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -22,23 +24,26 @@ public class cell extends JButton { } protected void OnMouseClick() { - if(playerID == 0) { - playerID = ttt.playerID; + if (playerID == 0) { + playerID = ttt.playerID; ttt.endTurn(); setEnabled(false); - + switch (playerID) { - case 1: setBackground(new Color(255,0,0)); - break; - case 2: setBackground(new Color(0,0,255)); - break; - default: ; - break; + case 1: + setBackground(new Color(255, 0, 0)); + break; + case 2: + setBackground(new Color(0, 0, 255)); + break; + } } - } } - - - + protected void reset() { + playerID = 0; + setEnabled(true); + setBackground(new Color(255,255,255)); + } + }