From 02ad893dea6a4e153b8040d5fd1c8e402ff5949f Mon Sep 17 00:00:00 2001 From: kfkama Date: Wed, 2 Feb 2022 10:29:28 +0100 Subject: [PATCH 01/17] Init Base UI --- .gitignore | 22 +++++++++ src/main/java/TicTacToe/TicTacToeGame.java | 48 +++++++++++++++++++ .../java/TicTacToe/TicTacToeGameTest.java | 10 ++++ 3 files changed, 80 insertions(+) create mode 100644 src/main/java/TicTacToe/TicTacToeGame.java create mode 100644 src/test/java/TicTacToe/TicTacToeGameTest.java diff --git a/.gitignore b/.gitignore index 84adb3f..c837958 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,25 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +### Maven ### +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# https://github.com/takari/maven-wrapper#usage-without-binary-jar +.mvn/wrapper/maven-wrapper.jar + +# Eclipse m2e generated files +# Eclipse Core +.project +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# End of https://www.toptal.com/developers/gitignore/api/maven + diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java new file mode 100644 index 0000000..f958273 --- /dev/null +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -0,0 +1,48 @@ +package TicTacToe; + +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.geom.Line2D; + +import javax.swing.JFrame; +import javax.swing.JPanel; + + +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(); + + f.add(ttt); + f.setSize(width,height); + f.setLayout(null); + f.setVisible(true); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D) g; + + Line2D lin = new Line2D.Float(250, 150, 250, 450); + Line2D lin2 = new Line2D.Float(350, 150, 350, 450); + Line2D lin3 = new Line2D.Float(150, 250, 450, 250); + Line2D lin4 = new Line2D.Float(150, 350, 450, 350); + + g2.draw(lin); + g2.draw(lin2); + g2.draw(lin3); + g2.draw(lin4); + } + + +} \ No newline at end of file diff --git a/src/test/java/TicTacToe/TicTacToeGameTest.java b/src/test/java/TicTacToe/TicTacToeGameTest.java new file mode 100644 index 0000000..08742dd --- /dev/null +++ b/src/test/java/TicTacToe/TicTacToeGameTest.java @@ -0,0 +1,10 @@ +package TicTacToe; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +class TicTacToeGameTest { + + + +} From bb5e31ff9f826afae19ca7253647ad8147cb8f36 Mon Sep 17 00:00:00 2001 From: kfkama Date: Mon, 14 Feb 2022 23:19:33 +0100 Subject: [PATCH 02/17] 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 03/17] 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 04/17] 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 05/17] 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 06/17] 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 07/17] 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 08/17] 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)); + } + } From 30f5312440b82d75e7d2ea77dbffd062ed9c3cf1 Mon Sep 17 00:00:00 2001 From: kfkama Date: Tue, 15 Feb 2022 23:31:20 +0100 Subject: [PATCH 09/17] Add Scoreboard class with basic functions --- src/main/java/TicTacToe/Scoreboard.java | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/TicTacToe/Scoreboard.java diff --git a/src/main/java/TicTacToe/Scoreboard.java b/src/main/java/TicTacToe/Scoreboard.java new file mode 100644 index 0000000..b2f4483 --- /dev/null +++ b/src/main/java/TicTacToe/Scoreboard.java @@ -0,0 +1,36 @@ +package TicTacToe; + +import javax.swing.JLabel; + +public class Scoreboard extends JLabel{ + + private static final long serialVersionUID = 1L; + + public int[] playerPoints; + + public Scoreboard(int _playerCount) { + playerPoints = new int[_playerCount]; + } + + public void addPoint(int playerID) { + playerPoints[playerID]++; + updateScores(); + } + + public void subPoint(int playerID) { + playerPoints[playerID]--; + updateScores(); + } + + public void updateScores() { + for(int i = 0; i < playerPoints.length; i++){ + if(i == 0) { + setText("Draws: " + playerPoints[0]); + } else { + setText(getText() + " Player " + i + ": " + playerPoints[i]); + } + } + } + + +} From ca9ca4446a2bce6c5338bc4b864da1651fdfbc42 Mon Sep 17 00:00:00 2001 From: kfkama Date: Tue, 15 Feb 2022 23:38:26 +0100 Subject: [PATCH 10/17] Show Scoreboard in TicTacToe game --- src/main/java/TicTacToe/Scoreboard.java | 3 ++- src/main/java/TicTacToe/TicTacToeGame.java | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/TicTacToe/Scoreboard.java b/src/main/java/TicTacToe/Scoreboard.java index b2f4483..a9799a5 100644 --- a/src/main/java/TicTacToe/Scoreboard.java +++ b/src/main/java/TicTacToe/Scoreboard.java @@ -10,6 +10,7 @@ public class Scoreboard extends JLabel{ public Scoreboard(int _playerCount) { playerPoints = new int[_playerCount]; + updateScores(); } public void addPoint(int playerID) { @@ -27,7 +28,7 @@ public class Scoreboard extends JLabel{ if(i == 0) { setText("Draws: " + playerPoints[0]); } else { - setText(getText() + " Player " + i + ": " + playerPoints[i]); + setText(getText() + " Player " + i + ": " + playerPoints[i]); } } } diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index acef34d..6d363a9 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -19,11 +19,15 @@ public class TicTacToeGame extends JPanel { public cell[] field; public int playerID = 1; public int turns = 0; + public Scoreboard scoreboard; public TicTacToeGame() { this.setSize(width, height); setLayout(null); initField(); + scoreboard = new Scoreboard(maxPlayers); + scoreboard.setBounds(200, 100, 200, 50); + add(scoreboard); } public static void main(String[] args) { @@ -74,12 +78,14 @@ public class TicTacToeGame extends JPanel { public void endTurn() { if(checkPlayfield() != 0) { JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!"); + scoreboard.addPoint(playerID); resetGame(); } turns++; if(turns >= playFieldSize) { JOptionPane.showMessageDialog(getParent(),"Draw!"); + scoreboard.addPoint(playerID); resetGame(); } From e7468f149851e5a57e95e624880425d2086b7209 Mon Sep 17 00:00:00 2001 From: kfkama Date: Tue, 15 Feb 2022 23:45:38 +0100 Subject: [PATCH 11/17] Refactor cell, TicTacToe --- .../java/TicTacToe/{cell.java => Cell.java} | 4 ++-- src/main/java/TicTacToe/TicTacToeGame.java | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) rename src/main/java/TicTacToe/{cell.java => Cell.java} (91%) diff --git a/src/main/java/TicTacToe/cell.java b/src/main/java/TicTacToe/Cell.java similarity index 91% rename from src/main/java/TicTacToe/cell.java rename to src/main/java/TicTacToe/Cell.java index 27a3158..2c53223 100644 --- a/src/main/java/TicTacToe/cell.java +++ b/src/main/java/TicTacToe/Cell.java @@ -6,12 +6,12 @@ import java.awt.event.ActionListener; import javax.swing.JButton; -public class cell extends JButton { +public class Cell extends JButton { public int playerID = 0; private TicTacToeGame ttt; - public cell(TicTacToeGame _ttt) { + public Cell(TicTacToeGame _ttt) { ttt = _ttt; setBackground(new Color(255,255,255)); diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index 6d363a9..44aa358 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -16,7 +16,7 @@ public class TicTacToeGame extends JPanel { private static final int maxPlayers = 3; private static final int playFieldSize = 9; - public cell[] field; + public Cell[] field; public int playerID = 1; public int turns = 0; public Scoreboard scoreboard; @@ -25,10 +25,9 @@ public class TicTacToeGame extends JPanel { this.setSize(width, height); setLayout(null); initField(); - scoreboard = new Scoreboard(maxPlayers); - scoreboard.setBounds(200, 100, 200, 50); - add(scoreboard); + initScoreboard(); } + public static void main(String[] args) { JFrame f = new JFrame(); @@ -41,10 +40,10 @@ public class TicTacToeGame extends JPanel { } public void initField() { - field = new cell[playFieldSize]; + field = new Cell[playFieldSize]; for(int i = 0; i < field.length; i++) { - field[i] = new cell(this); + field[i] = new Cell(this); add(field[i]); } @@ -59,6 +58,12 @@ public class TicTacToeGame extends JPanel { } } + private void initScoreboard() { + scoreboard = new Scoreboard(maxPlayers); + scoreboard.setBounds(200, 100, 200, 50); + add(scoreboard); + } + @Override protected void paintComponent(Graphics g) { super.paintComponent(g); @@ -85,7 +90,7 @@ public class TicTacToeGame extends JPanel { turns++; if(turns >= playFieldSize) { JOptionPane.showMessageDialog(getParent(),"Draw!"); - scoreboard.addPoint(playerID); + scoreboard.addPoint(0); resetGame(); } @@ -113,7 +118,7 @@ public class TicTacToeGame extends JPanel { } public void resetGame() { - for (cell c : field) { + for (Cell c : field) { c.reset(); } playerID = 1; From 41b70d270045d59b2ac072081ad35bd9d4b7463c Mon Sep 17 00:00:00 2001 From: kfkama Date: Wed, 16 Feb 2022 10:54:30 +0100 Subject: [PATCH 12/17] Add Games played to Scoreboard --- src/main/java/TicTacToe/Scoreboard.java | 5 +++++ src/main/java/TicTacToe/TicTacToeGame.java | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/TicTacToe/Scoreboard.java b/src/main/java/TicTacToe/Scoreboard.java index a9799a5..a903129 100644 --- a/src/main/java/TicTacToe/Scoreboard.java +++ b/src/main/java/TicTacToe/Scoreboard.java @@ -24,13 +24,18 @@ public class Scoreboard extends JLabel{ } public void updateScores() { + int gamesPlayed = 0; for(int i = 0; i < playerPoints.length; i++){ if(i == 0) { setText("Draws: " + playerPoints[0]); } else { setText(getText() + " Player " + i + ": " + playerPoints[i]); } + + gamesPlayed += playerPoints[i]; } + setText(getText() + " Games played: " + gamesPlayed); + } diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index 44aa358..7b8fb5a 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -60,7 +60,7 @@ public class TicTacToeGame extends JPanel { private void initScoreboard() { scoreboard = new Scoreboard(maxPlayers); - scoreboard.setBounds(200, 100, 200, 50); + scoreboard.setBounds(150, 100, 300, 50); add(scoreboard); } From 003119fc27d64e0c94a15190cb093a64fb9887f0 Mon Sep 17 00:00:00 2001 From: kfkama Date: Wed, 16 Feb 2022 15:31:22 +0100 Subject: [PATCH 13/17] Changed Colors to cross and circle --- src/main/java/TicTacToe/Cell.java | 33 ++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/main/java/TicTacToe/Cell.java b/src/main/java/TicTacToe/Cell.java index 2c53223..a254735 100644 --- a/src/main/java/TicTacToe/Cell.java +++ b/src/main/java/TicTacToe/Cell.java @@ -1,6 +1,8 @@ package TicTacToe; import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -13,7 +15,7 @@ public class Cell extends JButton { public Cell(TicTacToeGame _ttt) { ttt = _ttt; - setBackground(new Color(255,255,255)); + setBackground(new Color(255, 255, 255)); addActionListener(new ActionListener() { @Override @@ -28,22 +30,31 @@ public class Cell extends JButton { playerID = ttt.playerID; ttt.endTurn(); setEnabled(false); + repaint(); + } + } - switch (playerID) { - case 1: - setBackground(new Color(255, 0, 0)); - break; - case 2: - setBackground(new Color(0, 0, 255)); - break; - } + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D) g; + + switch (playerID) { + case 1: + g2.drawOval(5, 5, 90, 90); + break; + case 2: + g2.drawLine(5, 5, 90, 90); + g2.drawLine(90, 5, 5, 90); + break; } } - + + protected void reset() { playerID = 0; setEnabled(true); - setBackground(new Color(255,255,255)); + repaint(); } } From 3efe0a8d3eb440f62259509427a14d646f9850cd Mon Sep 17 00:00:00 2001 From: kfkama Date: Wed, 16 Feb 2022 15:33:48 +0100 Subject: [PATCH 14/17] Bugfix unclickable cell --- src/main/java/TicTacToe/Cell.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/TicTacToe/Cell.java b/src/main/java/TicTacToe/Cell.java index a254735..01fd7ab 100644 --- a/src/main/java/TicTacToe/Cell.java +++ b/src/main/java/TicTacToe/Cell.java @@ -28,8 +28,8 @@ public class Cell extends JButton { protected void OnMouseClick() { if (playerID == 0) { playerID = ttt.playerID; - ttt.endTurn(); setEnabled(false); + ttt.endTurn(); repaint(); } } From 3ca691f2a24ba0b81b4645eaf7498e495050f0d6 Mon Sep 17 00:00:00 2001 From: kfkama Date: Wed, 16 Feb 2022 15:35:29 +0100 Subject: [PATCH 15/17] Bugfix turn 8 draw --- src/main/java/TicTacToe/TicTacToeGame.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index 7b8fb5a..56f9dc9 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -88,7 +88,7 @@ public class TicTacToeGame extends JPanel { } turns++; - if(turns >= playFieldSize) { + if(turns > playFieldSize) { JOptionPane.showMessageDialog(getParent(),"Draw!"); scoreboard.addPoint(0); resetGame(); From 899ba9bbc4087b344b6204f1c400ff9b57ebb5c5 Mon Sep 17 00:00:00 2001 From: kfkama Date: Wed, 16 Feb 2022 15:46:55 +0100 Subject: [PATCH 16/17] Requested change to darker colors --- src/main/java/TicTacToe/Cell.java | 4 ++-- src/main/java/TicTacToe/Scoreboard.java | 3 +++ src/main/java/TicTacToe/TicTacToeGame.java | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/TicTacToe/Cell.java b/src/main/java/TicTacToe/Cell.java index 01fd7ab..81b8a0f 100644 --- a/src/main/java/TicTacToe/Cell.java +++ b/src/main/java/TicTacToe/Cell.java @@ -15,7 +15,7 @@ public class Cell extends JButton { public Cell(TicTacToeGame _ttt) { ttt = _ttt; - setBackground(new Color(255, 255, 255)); + setBackground(new Color(0)); addActionListener(new ActionListener() { @Override @@ -38,7 +38,7 @@ public class Cell extends JButton { protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; - + g2.setPaint(Color.white); switch (playerID) { case 1: g2.drawOval(5, 5, 90, 90); diff --git a/src/main/java/TicTacToe/Scoreboard.java b/src/main/java/TicTacToe/Scoreboard.java index a903129..cbca4a2 100644 --- a/src/main/java/TicTacToe/Scoreboard.java +++ b/src/main/java/TicTacToe/Scoreboard.java @@ -1,5 +1,7 @@ package TicTacToe; +import java.awt.Color; + import javax.swing.JLabel; public class Scoreboard extends JLabel{ @@ -10,6 +12,7 @@ public class Scoreboard extends JLabel{ public Scoreboard(int _playerCount) { playerPoints = new int[_playerCount]; + setForeground(Color.white); updateScores(); } diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index 56f9dc9..92338f1 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -1,5 +1,6 @@ package TicTacToe; +import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; @@ -23,6 +24,7 @@ public class TicTacToeGame extends JPanel { public TicTacToeGame() { this.setSize(width, height); + setBackground(Color.black); setLayout(null); initField(); initScoreboard(); @@ -68,6 +70,7 @@ public class TicTacToeGame extends JPanel { protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; + g2.setColor(Color.white); Line2D lin = new Line2D.Float(250, 150, 250, 450); Line2D lin2 = new Line2D.Float(350, 150, 350, 450); From 848a5ba499d335d70de776f8626c65a10942e230 Mon Sep 17 00:00:00 2001 From: kfkama Date: Wed, 16 Feb 2022 15:51:57 +0100 Subject: [PATCH 17/17] Bugfix draw order --- src/main/java/TicTacToe/Cell.java | 6 +++--- src/main/java/TicTacToe/TicTacToeGame.java | 22 ++++++++++++---------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/java/TicTacToe/Cell.java b/src/main/java/TicTacToe/Cell.java index 81b8a0f..b6c4e6b 100644 --- a/src/main/java/TicTacToe/Cell.java +++ b/src/main/java/TicTacToe/Cell.java @@ -41,12 +41,12 @@ public class Cell extends JButton { g2.setPaint(Color.white); switch (playerID) { case 1: - g2.drawOval(5, 5, 90, 90); - break; - case 2: g2.drawLine(5, 5, 90, 90); g2.drawLine(90, 5, 5, 90); break; + case 2: + g2.drawOval(5, 5, 90, 90); + break; } } diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index 92338f1..642f6e5 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -88,19 +88,21 @@ public class TicTacToeGame extends JPanel { JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!"); scoreboard.addPoint(playerID); resetGame(); + } else { + turns++; + if(turns >= playFieldSize) { + JOptionPane.showMessageDialog(getParent(),"Draw!"); + scoreboard.addPoint(0); + resetGame(); + } + + playerID++; + if(playerID >= maxPlayers) { + playerID = 1; + } } - turns++; - if(turns > playFieldSize) { - JOptionPane.showMessageDialog(getParent(),"Draw!"); - scoreboard.addPoint(0); - resetGame(); - } - playerID++; - if(playerID >= maxPlayers) { - playerID = 1; - } } public int checkPlayfield() {