From 66b669d976c46bdd5a99cec6848afb32f6997b21 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Tue, 28 Dec 2021 16:34:35 +0100 Subject: [PATCH 01/58] 4gewinnt workingspace created --- .../java/de/tims/viergewinnt/ai/Logic.java | 5 +++++ .../de/tims/viergewinnt/ai/LogicTest.java | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/main/java/de/tims/viergewinnt/ai/Logic.java create mode 100644 src/test/java/de/tims/viergewinnt/ai/LogicTest.java diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java new file mode 100644 index 0000000..c66a520 --- /dev/null +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -0,0 +1,5 @@ +package de.tims.viergewinnt.ai; + +public class Logic { + +} diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java new file mode 100644 index 0000000..aeef208 --- /dev/null +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -0,0 +1,19 @@ +package de.tims.viergewinnt.ai; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class LogicTest { + + @BeforeEach + void setUp() throws Exception { + } + + @Test + void test() { + fail("Not yet implemented"); + } + +} From b11151562292e548e429de9848fa3ceefa337c4f Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 5 Jan 2022 13:48:29 +0100 Subject: [PATCH 02/58] 4gewinnt: testForWin emtpy Field --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 4 +++- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 12 ++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index c66a520..d5a1f4c 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -1,5 +1,7 @@ package de.tims.viergewinnt.ai; public class Logic { - + public boolean testForWin(int[][] playfield, int player) { + return true; + } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index aeef208..e56560e 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -6,14 +6,22 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class LogicTest { + + private Logic testObj; @BeforeEach void setUp() throws Exception { + testObj = new Logic(); } @Test - void test() { - fail("Not yet implemented"); + void testForWinTest() { + int[][] testField = new int[6][6]; + boolean expectedResult = false; + + boolean realResult = testObj.testForWin(testField, 1); + + assertEquals(expectedResult, realResult); } } From a0add6efc0119bffbc1e24909ba1f228bb4f1f21 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 5 Jan 2022 14:21:52 +0100 Subject: [PATCH 03/58] 4gewinnt: testForWin testmethod refactored --- .../java/de/tims/viergewinnt/ai/Logic.java | 2 +- .../de/tims/viergewinnt/ai/LogicTest.java | 20 ++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index d5a1f4c..dbb4924 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -2,6 +2,6 @@ package de.tims.viergewinnt.ai; public class Logic { public boolean testForWin(int[][] playfield, int player) { - return true; + return false; } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index e56560e..010fcd6 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -2,8 +2,13 @@ package de.tims.viergewinnt.ai; import static org.junit.jupiter.api.Assertions.*; +import java.util.stream.Stream; + import org.junit.jupiter.api.BeforeEach; 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; class LogicTest { @@ -14,14 +19,19 @@ class LogicTest { testObj = new Logic(); } - @Test - void testForWinTest() { - int[][] testField = new int[6][6]; - boolean expectedResult = false; + @ParameterizedTest(name = "[{index}] {0} Player {2} {3}") + @MethodSource("playfieldResults") + void testForWinTest(String testName, int[][] playfield, int player, boolean expectedResult) { - boolean realResult = testObj.testForWin(testField, 1); + boolean realResult = testObj.testForWin(playfield, player); assertEquals(expectedResult, realResult); } + + private static Stream playfieldResults() { + return Stream.of( + Arguments.of("emptyField", new int[6][6], 1, false) + ); + } } From bf73c5dc96b5eb7e12ecda2bb896e21cb3155f70 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 5 Jan 2022 15:01:25 +0100 Subject: [PATCH 04/58] 4gewinnt: testForWin column1 player1 check --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 9 +++++++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index dbb4924..e6ff8e0 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -2,6 +2,15 @@ package de.tims.viergewinnt.ai; public class Logic { public boolean testForWin(int[][] playfield, int player) { + int chain = 0; + for(int i = 0; i < playfield.length; i++) { + if(playfield[i][0] == player) { + chain++; + } else { + chain = 0; + } + if(chain == 4) return true; + } return false; } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 010fcd6..f6f88d8 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -29,8 +29,15 @@ class LogicTest { } private static Stream playfieldResults() { + int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {1, 0, 0, 0, 0, 0}, + {1, 0, 0, 0, 0, 0}, + {1, 0, 0, 0, 0, 0}, + {1, 0, 0, 0, 0, 0}}; return Stream.of( - Arguments.of("emptyField", new int[6][6], 1, false) + Arguments.of("emptyField", new int[6][6], 1, false), + Arguments.of("4 in 1st column player1", playfield1, 1, true) ); } From 57bc37a4d8e808887d6470c7513aaa693e9a9892 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 5 Jan 2022 15:14:30 +0100 Subject: [PATCH 05/58] 4gewinnt: testForWin win in a column --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 14 ++++++++------ .../java/de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index e6ff8e0..8c62c9f 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -3,13 +3,15 @@ package de.tims.viergewinnt.ai; public class Logic { public boolean testForWin(int[][] playfield, int player) { int chain = 0; - for(int i = 0; i < playfield.length; i++) { - if(playfield[i][0] == player) { - chain++; - } else { - chain = 0; + for(int i = 0; i < playfield[0].length; i++) { + for(int j = 0; j < playfield.length; j++) { + if(playfield[j][i] == player) { + chain++; + } else { + chain = 0; + } + if(chain == 4) return true; } - if(chain == 4) return true; } return false; } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index f6f88d8..2a861ad 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -35,9 +35,16 @@ class LogicTest { {1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0}}; + int[][] playfield2 = {{0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 1, 0, 0}, + {0, 0, 0, 1, 0, 0}, + {0, 0, 0, 1, 0, 0}, + {0, 0, 0, 1, 0, 0}}; return Stream.of( Arguments.of("emptyField", new int[6][6], 1, false), - Arguments.of("4 in 1st column player1", playfield1, 1, true) + Arguments.of("4 in column1 player1", playfield1, 1, true), + Arguments.of("4 in a column player1", playfield2, 1, true) ); } From 1cfa0df92fcd97d0036942a270f88a60366c9e0f Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 5 Jan 2022 15:54:14 +0100 Subject: [PATCH 06/58] 4gewinnt: testForWin win in a row --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 11 +++++++++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 8c62c9f..9041a0b 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -13,6 +13,17 @@ public class Logic { if(chain == 4) return true; } } + chain = 0; + for(int i = 0; i < playfield.length; i++) { + for(int j = 0; j < playfield[0].length; j++) { + if(playfield[i][j] == player) { + chain++; + } else { + chain = 0; + } + if(chain == 4) return true; + } + } return false; } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 2a861ad..11a655f 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -41,10 +41,17 @@ class LogicTest { {0, 0, 0, 1, 0, 0}, {0, 0, 0, 1, 0, 0}, {0, 0, 0, 1, 0, 0}}; + int[][] playfield3 = {{0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {1, 1, 1, 1, 0, 0}}; return Stream.of( Arguments.of("emptyField", new int[6][6], 1, false), Arguments.of("4 in column1 player1", playfield1, 1, true), - Arguments.of("4 in a column player1", playfield2, 1, true) + Arguments.of("4 in a column player1", playfield2, 1, true), + Arguments.of("4 in a row player1", playfield3, 1, true) ); } From 2639a99f673ef5299583d76e91cd0620e69d5d57 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 5 Jan 2022 17:56:00 +0100 Subject: [PATCH 07/58] 4gewinnt: testForWin diagonal right win --- .../java/de/tims/viergewinnt/ai/Logic.java | 18 ++++++++++++++++++ .../java/de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 9041a0b..eb5afa5 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -24,6 +24,24 @@ public class Logic { if(chain == 4) return true; } } + chain = 0; + for(int i = 0; i < playfield.length - 3; i++) { + for(int j = 0; j < playfield[i].length; j++) { + if(playfield[i][j] == player) { + chain++; + while(true) { + i++; j++; + if(playfield[i][j] == player) { + chain++; + if(chain == 4) return true; + } else { + chain = 0; + break; + } + } + } + } + } return false; } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 11a655f..091ff8c 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -47,11 +47,18 @@ class LogicTest { {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 0, 0}}; + int[][] playfield4 = {{0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {1, 0, 0, 0, 0, 0}, + {1, 1, 0, 0, 0, 0}, + {2, 2, 1, 0, 0, 0}, + {2, 2, 2, 1, 0, 0}}; return Stream.of( Arguments.of("emptyField", new int[6][6], 1, false), Arguments.of("4 in column1 player1", playfield1, 1, true), Arguments.of("4 in a column player1", playfield2, 1, true), - Arguments.of("4 in a row player1", playfield3, 1, true) + Arguments.of("4 in a row player1", playfield3, 1, true), + Arguments.of("4 diagonal right player1", playfield4, 1, true) ); } From 0770df9ef8a3fd0ebdbf0be948822795e36ac913 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 5 Jan 2022 19:10:57 +0100 Subject: [PATCH 08/58] 4gewinnt: testForWin diagonal left win --- .../java/de/tims/viergewinnt/ai/Logic.java | 20 ++++++++++++++++++- .../de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index eb5afa5..7dc55bc 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -26,7 +26,7 @@ public class Logic { } chain = 0; for(int i = 0; i < playfield.length - 3; i++) { - for(int j = 0; j < playfield[i].length; j++) { + for(int j = 0; j < playfield[i].length - 3; j++) { if(playfield[i][j] == player) { chain++; while(true) { @@ -42,6 +42,24 @@ public class Logic { } } } + chain = 0; + for(int i = 0; i < playfield.length - 3; i++) { + for(int j = playfield[i].length - 1; j > 2; j--) { + if(playfield[i][j] == player) { + chain++; + while(true) { + i++; j--; + if(playfield[i][j] == player) { + chain++; + if(chain == 4) return true; + } else { + chain = 0; + break; + } + } + } + } + } return false; } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 091ff8c..d6c0ad4 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -53,12 +53,19 @@ class LogicTest { {1, 1, 0, 0, 0, 0}, {2, 2, 1, 0, 0, 0}, {2, 2, 2, 1, 0, 0}}; + int[][] playfield5 = {{0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 1}, + {0, 0, 0, 0, 1, 2}, + {0, 0, 0, 1, 2, 2}, + {0, 0, 1, 1, 2, 2}}; return Stream.of( Arguments.of("emptyField", new int[6][6], 1, false), Arguments.of("4 in column1 player1", playfield1, 1, true), Arguments.of("4 in a column player1", playfield2, 1, true), Arguments.of("4 in a row player1", playfield3, 1, true), - Arguments.of("4 diagonal right player1", playfield4, 1, true) + Arguments.of("4 diagonal right player1", playfield4, 1, true), + Arguments.of("4 diagonal left player1", playfield5, 1, true) ); } From 43abd90de10313a4231e67043fd5fb4357a2cea2 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 10 Jan 2022 11:52:39 +0100 Subject: [PATCH 09/58] 4gewinnt: added gui-layout and testing methods --- .../java/de/tims/viergewinnt/ai/Logic.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 7dc55bc..c4fc3d5 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -1,6 +1,42 @@ package de.tims.viergewinnt.ai; +import javax.swing.JFrame; +import javax.swing.JPanel; + public class Logic { + + //only for testing, remove when finished + static JFrame content; + + static JPanel contentPanel; + static JPanel buttonPanel; + static JPanel playfieldPanel; + + public static JPanel create4gewinntGui() { + + //only for testing, remove when finished + content = new JFrame(); + content.setSize(200,200); + + contentPanel = new JPanel(); + + //only for testing, remove when finished + content.add(contentPanel); + + buttonPanel = new JPanel(); + playfieldPanel = new JPanel(); + contentPanel.add(buttonPanel); + contentPanel.add(playfieldPanel); + + return contentPanel; + } + + //only for testing, remove when finished + public static void main(String[] args) { + create4gewinntGui(); + content.setVisible(true); + } + public boolean testForWin(int[][] playfield, int player) { int chain = 0; for(int i = 0; i < playfield[0].length; i++) { From 973c03544bf98b7f522bb6fabd8e6778fb06b248 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 10 Jan 2022 12:32:49 +0100 Subject: [PATCH 10/58] 4gewinnt: finished gui layout --- .../java/de/tims/viergewinnt/ai/Logic.java | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index c4fc3d5..356a777 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -1,33 +1,56 @@ package de.tims.viergewinnt.ai; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.GridLayout; + +import javax.swing.BorderFactory; +import javax.swing.BoxLayout; +import javax.swing.JButton; import javax.swing.JFrame; +import javax.swing.JLabel; import javax.swing.JPanel; public class Logic { + //"static" only for testing, remove when finished //only for testing, remove when finished static JFrame content; static JPanel contentPanel; static JPanel buttonPanel; static JPanel playfieldPanel; + static JButton[] buttons = new JButton[6]; + static JLabel[] gamefield = new JLabel[36]; public static JPanel create4gewinntGui() { //only for testing, remove when finished content = new JFrame(); - content.setSize(200,200); + content.setSize(500,500); contentPanel = new JPanel(); + contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.PAGE_AXIS)); //only for testing, remove when finished content.add(contentPanel); - buttonPanel = new JPanel(); - playfieldPanel = new JPanel(); + buttonPanel = new JPanel(new GridLayout(1, 6)); + playfieldPanel = new JPanel(new GridLayout(6, 6)); contentPanel.add(buttonPanel); contentPanel.add(playfieldPanel); + for(int i = 0; i < buttons.length; i++) { + buttons[i] = new JButton(Integer.toString(i+1)); + buttonPanel.add(buttons[i]); + } + + for(int i = 0; i < gamefield.length; i++) { + gamefield[i] = new JLabel(); + gamefield[i].setBorder(BorderFactory.createLineBorder(Color.gray)); + playfieldPanel.add(gamefield[i]); + } + return contentPanel; } From 12a1010b63e20ef56f73041cbc57e69a71e26408 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 10 Jan 2022 14:24:10 +0100 Subject: [PATCH 11/58] 4gewinnt: refactored and externalized gui-testing --- .../java/de/tims/viergewinnt/ai/GuiTest.java | 21 +++++++++++ .../java/de/tims/viergewinnt/ai/Logic.java | 35 ++++--------------- 2 files changed, 28 insertions(+), 28 deletions(-) create mode 100644 src/main/java/de/tims/viergewinnt/ai/GuiTest.java diff --git a/src/main/java/de/tims/viergewinnt/ai/GuiTest.java b/src/main/java/de/tims/viergewinnt/ai/GuiTest.java new file mode 100644 index 0000000..be0aa60 --- /dev/null +++ b/src/main/java/de/tims/viergewinnt/ai/GuiTest.java @@ -0,0 +1,21 @@ +package de.tims.viergewinnt.ai; + +import javax.swing.JFrame; + +public class GuiTest { + + JFrame frame; + public GuiTest() { + frame = new JFrame(); + frame.setSize(500, 500); + Logic logic = new Logic(); + frame.add(logic.create4gewinntGui()); + frame.setVisible(true); + } + + public static void main(String[] args) { + // TODO Auto-generated method stub + GuiTest testGui = new GuiTest(); + } + +} diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 356a777..4096ce6 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -1,40 +1,25 @@ package de.tims.viergewinnt.ai; import java.awt.Color; -import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; -import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Logic { - //"static" only for testing, remove when finished - //only for testing, remove when finished - static JFrame content; + JPanel contentPanel; + JPanel buttonPanel; + JPanel playfieldPanel; + JButton[] buttons = new JButton[6]; + JLabel[] gamefield = new JLabel[36]; - static JPanel contentPanel; - static JPanel buttonPanel; - static JPanel playfieldPanel; - static JButton[] buttons = new JButton[6]; - static JLabel[] gamefield = new JLabel[36]; - - public static JPanel create4gewinntGui() { - - //only for testing, remove when finished - content = new JFrame(); - content.setSize(500,500); - + public JPanel create4gewinntGui() { contentPanel = new JPanel(); - contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.PAGE_AXIS)); - - //only for testing, remove when finished - content.add(contentPanel); - + contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.PAGE_AXIS)); buttonPanel = new JPanel(new GridLayout(1, 6)); playfieldPanel = new JPanel(new GridLayout(6, 6)); contentPanel.add(buttonPanel); @@ -54,12 +39,6 @@ public class Logic { return contentPanel; } - //only for testing, remove when finished - public static void main(String[] args) { - create4gewinntGui(); - content.setVisible(true); - } - public boolean testForWin(int[][] playfield, int player) { int chain = 0; for(int i = 0; i < playfield[0].length; i++) { From dc09cc88a9215cfcf67c9e81d6e3305fce44c259 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 10 Jan 2022 18:45:49 +0100 Subject: [PATCH 12/58] 4gewinnt: added actionListener to buttons --- .../java/de/tims/viergewinnt/ai/Logic.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 4096ce6..0f5f61c 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -2,6 +2,8 @@ package de.tims.viergewinnt.ai; import java.awt.Color; import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.BoxLayout; @@ -27,6 +29,7 @@ public class Logic { for(int i = 0; i < buttons.length; i++) { buttons[i] = new JButton(Integer.toString(i+1)); + buttons[i].addActionListener(new ButtonListener()); buttonPanel.add(buttons[i]); } @@ -39,6 +42,32 @@ public class Logic { return contentPanel; } + class ButtonListener implements ActionListener { + + @Override + public void actionPerformed(ActionEvent e) { + if(e.getSource() == buttons[0]) { + + } + if(e.getSource() == buttons[1]) { + + } + if(e.getSource() == buttons[2]) { + + } + if(e.getSource() == buttons[3]) { + + } + if(e.getSource() == buttons[4]) { + + } + if(e.getSource() == buttons[5]) { + + } + } + + } + public boolean testForWin(int[][] playfield, int player) { int chain = 0; for(int i = 0; i < playfield[0].length; i++) { From 4c9bdb2f0cd796d59a1047b41cca79f283a608b4 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 10 Jan 2022 19:06:43 +0100 Subject: [PATCH 13/58] 4gewinnt: added playChip method --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 4 ++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 0f5f61c..139df50 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -13,6 +13,10 @@ import javax.swing.JPanel; public class Logic { + public int playChip(int[][] playfield, int column, int player) { + return 0; + } + JPanel contentPanel; JPanel buttonPanel; JPanel playfieldPanel; diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index d6c0ad4..731b925 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -28,6 +28,17 @@ class LogicTest { assertEquals(expectedResult, realResult); } + @Test + void playChipTest() { + int[][] playfield = new int[6][6]; + int column = 0; + int player = 1; + int expectedResult = 0; + int realResult = testObj.playChip(playfield, column, player); + + assertEquals(expectedResult, realResult); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From 743162ef154679d194fdb4d7ffc72f40838cffa8 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 10 Jan 2022 19:22:03 +0100 Subject: [PATCH 14/58] 4gewinnt: refactored actionListener --- .../java/de/tims/viergewinnt/ai/Logic.java | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 139df50..794d610 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -13,7 +13,12 @@ import javax.swing.JPanel; public class Logic { + int[][] board; + int currentPlayer; + public int playChip(int[][] playfield, int column, int player) { + + testForWin(playfield, player); return 0; } @@ -50,23 +55,11 @@ public class Logic { @Override public void actionPerformed(ActionEvent e) { - if(e.getSource() == buttons[0]) { - - } - if(e.getSource() == buttons[1]) { - - } - if(e.getSource() == buttons[2]) { - - } - if(e.getSource() == buttons[3]) { - - } - if(e.getSource() == buttons[4]) { - - } - if(e.getSource() == buttons[5]) { - + for(int i = 0; i < buttons.length; i++) { + if(e.getSource() == buttons[i]) { + playChip(board, i, currentPlayer); + break; + } } } From 27c4b6122b1706cd7e72e05da17dc47a2a1fc02b Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 10 Jan 2022 19:54:17 +0100 Subject: [PATCH 15/58] 4gewinnt: change to player 2 after player 1 played added --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 5 ++++- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 794d610..1aef7f6 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -17,8 +17,9 @@ public class Logic { int currentPlayer; public int playChip(int[][] playfield, int column, int player) { - + //add board update testForWin(playfield, player); + currentPlayer = 2; return 0; } @@ -45,6 +46,8 @@ public class Logic { for(int i = 0; i < gamefield.length; i++) { gamefield[i] = new JLabel(); gamefield[i].setBorder(BorderFactory.createLineBorder(Color.gray)); + gamefield[i].setOpaque(true); + gamefield[i].setBackground(Color.white); playfieldPanel.add(gamefield[i]); } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 731b925..71cd919 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -39,6 +39,19 @@ class LogicTest { assertEquals(expectedResult, realResult); } + @Test + void changePlayerTo2Test() { + int[][] playfield = new int[6][6]; + int column = 0; + int player = 1; + int expectedResult = 2; + + testObj.playChip(playfield, column, player); + int realResult = testObj.currentPlayer; + + assertEquals(expectedResult, realResult); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From 6425dc9d9b503ca92d3fa8412203bcbe4406f749 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 10 Jan 2022 19:55:41 +0100 Subject: [PATCH 16/58] 4gewinnt: change to player 1 after player 2 played added --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 2 +- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 1aef7f6..56707b5 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -19,7 +19,7 @@ public class Logic { public int playChip(int[][] playfield, int column, int player) { //add board update testForWin(playfield, player); - currentPlayer = 2; + currentPlayer = (player % 2) + 1; return 0; } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 71cd919..e35cd8d 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -52,6 +52,19 @@ class LogicTest { assertEquals(expectedResult, realResult); } + @Test + void changePlayerTo1Test() { + int[][] playfield = new int[6][6]; + int column = 0; + int player = 2; + int expectedResult = 1; + + testObj.playChip(playfield, column, player); + int realResult = testObj.currentPlayer; + + assertEquals(expectedResult, realResult); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From 76a32694c0717c4b2f348f4c7cc945ca7999fc06 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 10 Jan 2022 20:03:51 +0100 Subject: [PATCH 17/58] 4gewinnt: updateGui Method created and added to playChip --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 56707b5..59104a3 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -17,7 +17,10 @@ public class Logic { int currentPlayer; public int playChip(int[][] playfield, int column, int player) { + //add board update + + //updateGui(playfield); testForWin(playfield, player); currentPlayer = (player % 2) + 1; return 0; @@ -54,6 +57,20 @@ public class Logic { return contentPanel; } + public void updateGui(int[][] playfield) { + for(int i = 0; i < playfield.length; i++) { + for(int j = 0; j < playfield[i].length; j++) { + if(playfield[i][j] == 0) { + gamefield[j + 6 * i].setBackground(Color.white); + } else if(playfield[i][j] == 1) { + gamefield[j + 6 * i].setBackground(Color.red); + } else { + gamefield[j + 6 * i].setBackground(Color.yellow); + } + } + } + } + class ButtonListener implements ActionListener { @Override From 5c0d4221b81376ba0fc02f383c5d847652af099c Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Fri, 14 Jan 2022 16:17:20 +0100 Subject: [PATCH 18/58] 4gewinnt: added calcScore method --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 4 ++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 59104a3..dfad882 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -146,4 +146,8 @@ public class Logic { } return false; } + + public int calcScore(int[][] playfield, int player) { + return 0; + } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index e35cd8d..3e344c4 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -65,6 +65,17 @@ class LogicTest { assertEquals(expectedResult, realResult); } + @Test + void calcScoreTest() { + int[][] playfield = new int[6][6]; + int player = 1; + int expectedResult = 0; + + int realResult = testObj.calcScore(playfield, player); + + assertEquals(expectedResult, realResult); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From 09ed2fe825027a13f1cb7a4beaaf17d184ba61f7 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Fri, 14 Jan 2022 16:19:53 +0100 Subject: [PATCH 19/58] 4gewinnt: refactored playChip testmethods --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 2 +- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index dfad882..db16fdc 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -20,7 +20,7 @@ public class Logic { //add board update - //updateGui(playfield); + updateGui(playfield); testForWin(playfield, player); currentPlayer = (player % 2) + 1; return 0; diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 3e344c4..2221c35 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -34,6 +34,7 @@ class LogicTest { int column = 0; int player = 1; int expectedResult = 0; + testObj.create4gewinntGui(); int realResult = testObj.playChip(playfield, column, player); assertEquals(expectedResult, realResult); @@ -46,6 +47,7 @@ class LogicTest { int player = 1; int expectedResult = 2; + testObj.create4gewinntGui(); testObj.playChip(playfield, column, player); int realResult = testObj.currentPlayer; @@ -59,6 +61,7 @@ class LogicTest { int player = 2; int expectedResult = 1; + testObj.create4gewinntGui(); testObj.playChip(playfield, column, player); int realResult = testObj.currentPlayer; From e2bd09d7137bed188bb9d3fd078e91e6f6c8762b Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Fri, 14 Jan 2022 16:45:05 +0100 Subject: [PATCH 20/58] 4gewinnt: refactored calcScore testmethods --- .../java/de/tims/viergewinnt/ai/LogicTest.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 2221c35..56c7cb9 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -68,12 +68,10 @@ class LogicTest { assertEquals(expectedResult, realResult); } - @Test - void calcScoreTest() { - int[][] playfield = new int[6][6]; - int player = 1; - int expectedResult = 0; - + @ParameterizedTest(name = "[{index}] {0} Win for Player {2} {1}") + @MethodSource("playfieldForScore") + void calcScoreTest(String testName, int[][] playfield, int player, int expectedResult) { + int realResult = testObj.calcScore(playfield, player); assertEquals(expectedResult, realResult); @@ -119,5 +117,11 @@ class LogicTest { Arguments.of("4 diagonal left player1", playfield5, 1, true) ); } + + private static Stream playfieldForScore() { + return Stream.of( + Arguments.of("emptyField", new int[6][6], 1, 0) + ); + } } From 6dd8f7a4098905b1fb8970797093cef02efa6240 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Fri, 14 Jan 2022 17:03:26 +0100 Subject: [PATCH 21/58] 4gewinnt: calcScore pointsPerChip --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 10 +++++++++- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index db16fdc..368dd08 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -148,6 +148,14 @@ public class Logic { } public int calcScore(int[][] playfield, int player) { - return 0; + int score = 1000; + for(int i = 0; i < playfield.length; i++) { + for(int j = 0; j < playfield[i].length; j++) { + if(playfield[i][j] != 0) { + score += 10; + } + } + } + return score; } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 56c7cb9..97aeda7 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -119,8 +119,15 @@ class LogicTest { } private static Stream playfieldForScore() { + int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 1, 2, 0, 0}, + {0, 0, 1, 2, 0, 0}, + {0, 0, 1, 2, 0, 0}, + {0, 0, 1, 1, 2, 0}}; return Stream.of( - Arguments.of("emptyField", new int[6][6], 1, 0) + Arguments.of("emptyField", new int[6][6], 1, 1000), + Arguments.of("9 chips played", playfield1, 1, 1090) ); } From 91075742062b400f966395d7ff08d1bc32b13ccd Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Fri, 14 Jan 2022 17:13:22 +0100 Subject: [PATCH 22/58] 4gewinnt: calcScore pointsForFirstRow added --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 4 ++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 368dd08..f1c2619 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -156,6 +156,10 @@ public class Logic { } } } + if(playfield[5][0] != 0 && playfield[5][1] != 0 && playfield[5][2] != 0 + && playfield[5][3] != 0 && playfield[5][4] != 0 && playfield[5][5] != 0) { + score += 60; + } return score; } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 97aeda7..52ed2bc 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -125,9 +125,16 @@ class LogicTest { {0, 0, 1, 2, 0, 0}, {0, 0, 1, 2, 0, 0}, {0, 0, 1, 1, 2, 0}}; + int[][] playfield2 = {{0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {1, 1, 1, 2, 2, 2}}; return Stream.of( Arguments.of("emptyField", new int[6][6], 1, 1000), - Arguments.of("9 chips played", playfield1, 1, 1090) + Arguments.of("9 chips played", playfield1, 1, 1090), + Arguments.of("first row filled", playfield2, 1, 1120) ); } From 0db7e779ba038a5fbc91aac3049330380bfa2702 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Fri, 14 Jan 2022 17:31:31 +0100 Subject: [PATCH 23/58] 4gewinnt: calcScore pointsForAnyRow --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 8 ++++---- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index f1c2619..f0cc5e6 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -150,15 +150,15 @@ public class Logic { public int calcScore(int[][] playfield, int player) { int score = 1000; for(int i = 0; i < playfield.length; i++) { + int oldScore = score; for(int j = 0; j < playfield[i].length; j++) { if(playfield[i][j] != 0) { score += 10; } } - } - if(playfield[5][0] != 0 && playfield[5][1] != 0 && playfield[5][2] != 0 - && playfield[5][3] != 0 && playfield[5][4] != 0 && playfield[5][5] != 0) { - score += 60; + if(score - 60 == oldScore) { + score += 60; + } } return score; } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 52ed2bc..e66c8c6 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -131,10 +131,17 @@ class LogicTest { {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {1, 1, 1, 2, 2, 2}}; + int[][] playfield3 = {{0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {1, 1, 2, 2, 1, 2}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {1, 1, 1, 2, 2, 2}}; return Stream.of( Arguments.of("emptyField", new int[6][6], 1, 1000), Arguments.of("9 chips played", playfield1, 1, 1090), - Arguments.of("first row filled", playfield2, 1, 1120) + Arguments.of("first row filled", playfield2, 1, 1120), + Arguments.of("2 rows filled", playfield3, 1, 1240) ); } From dcf1bde6f91a17dd780b94cdb22c6fcf92f7434d Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Fri, 14 Jan 2022 17:46:30 +0100 Subject: [PATCH 24/58] 4gewinnt: calcScore pointsForFirstColumn --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 4 ++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index f0cc5e6..63ecf74 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -160,6 +160,10 @@ public class Logic { score += 60; } } + if(playfield[0][0] != 0 && playfield[1][0] != 0 && playfield[2][0] != 0 && + playfield[3][0] != 0 && playfield[4][0] != 0 && playfield[5][0] != 0) { + score += 60; + } return score; } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index e66c8c6..49a5794 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -137,11 +137,18 @@ class LogicTest { {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {1, 1, 1, 2, 2, 2}}; + int[][] playfield4 = {{2, 0, 0, 0, 0, 0}, + {2, 0, 0, 0, 0, 0}, + {1, 0, 0, 0, 0, 0}, + {2, 0, 0, 0, 0, 0}, + {1, 0, 0, 0, 0, 0}, + {1, 0, 0, 0, 0, 0}}; return Stream.of( Arguments.of("emptyField", new int[6][6], 1, 1000), Arguments.of("9 chips played", playfield1, 1, 1090), Arguments.of("first row filled", playfield2, 1, 1120), - Arguments.of("2 rows filled", playfield3, 1, 1240) + Arguments.of("2 rows filled", playfield3, 1, 1240), + Arguments.of("first column filled", playfield4, 1, 1120) ); } From 358938422603efd9d3170687caf7ae0cb51b65fa Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Fri, 14 Jan 2022 18:02:15 +0100 Subject: [PATCH 25/58] 4gewinnt: calcScore pointsForAnyColumn --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 12 +++++++++--- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 63ecf74..6e082ba 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -160,9 +160,15 @@ public class Logic { score += 60; } } - if(playfield[0][0] != 0 && playfield[1][0] != 0 && playfield[2][0] != 0 && - playfield[3][0] != 0 && playfield[4][0] != 0 && playfield[5][0] != 0) { - score += 60; + for(int i = 0; i < playfield[0].length; i++) { + for(int j = 0; j < playfield.length; j++) { + if(playfield[j][i] == 0) { + break; + } + if(j == playfield.length - 1) { + score += 60; + } + } } return score; } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 49a5794..c6efc6d 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -143,12 +143,19 @@ class LogicTest { {2, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0}}; + int[][] playfield5 = {{2, 0, 1, 0, 0, 0}, + {2, 0, 1, 0, 0, 0}, + {1, 0, 2, 0, 0, 0}, + {2, 0, 2, 0, 0, 0}, + {1, 0, 2, 0, 0, 0}, + {1, 0, 1, 0, 0, 0}}; return Stream.of( Arguments.of("emptyField", new int[6][6], 1, 1000), Arguments.of("9 chips played", playfield1, 1, 1090), Arguments.of("first row filled", playfield2, 1, 1120), Arguments.of("2 rows filled", playfield3, 1, 1240), - Arguments.of("first column filled", playfield4, 1, 1120) + Arguments.of("first column filled", playfield4, 1, 1120), + Arguments.of("2 column filled", playfield5, 1, 1240) ); } From 08bd0b7fad3840ac3d3ea52cec1ae871b060e3f7 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Fri, 14 Jan 2022 18:25:07 +0100 Subject: [PATCH 26/58] 4gewinnt: calcScore bonusForFullField --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 3 +++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 6e082ba..e79bcf8 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -170,6 +170,9 @@ public class Logic { } } } + if(score == 2080) { + score += 500; + } return score; } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index c6efc6d..0531164 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -149,13 +149,20 @@ class LogicTest { {2, 0, 2, 0, 0, 0}, {1, 0, 2, 0, 0, 0}, {1, 0, 1, 0, 0, 0}}; + int[][] playfield6 = {{1, 1, 1, 1, 2, 2}, + {2, 1, 1, 1, 1, 2}, + {2, 1, 1, 2, 1, 2}, + {2, 1, 1, 2, 1, 2}, + {2, 1, 2, 2, 1, 2}, + {2, 1, 2, 2, 1, 2}}; return Stream.of( Arguments.of("emptyField", new int[6][6], 1, 1000), Arguments.of("9 chips played", playfield1, 1, 1090), Arguments.of("first row filled", playfield2, 1, 1120), Arguments.of("2 rows filled", playfield3, 1, 1240), Arguments.of("first column filled", playfield4, 1, 1120), - Arguments.of("2 column filled", playfield5, 1, 1240) + Arguments.of("2 column filled", playfield5, 1, 1240), + Arguments.of("fullyFilled", playfield6, 1, 2580) ); } From 9f995384062795380f29fa01598038a9e6246278 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Fri, 14 Jan 2022 18:40:53 +0100 Subject: [PATCH 27/58] 4gewinnt: calcScore refactored --- .../java/de/tims/viergewinnt/ai/Logic.java | 10 +++++----- .../de/tims/viergewinnt/ai/LogicTest.java | 20 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index e79bcf8..d4a20aa 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -147,7 +147,7 @@ public class Logic { return false; } - public int calcScore(int[][] playfield, int player) { + public int calcScore(int[][] playfield) { int score = 1000; for(int i = 0; i < playfield.length; i++) { int oldScore = score; @@ -156,8 +156,8 @@ public class Logic { score += 10; } } - if(score - 60 == oldScore) { - score += 60; + if(score - (10 * playfield[0].length) == oldScore) { + score += (10 * playfield[0].length); } } for(int i = 0; i < playfield[0].length; i++) { @@ -166,11 +166,11 @@ public class Logic { break; } if(j == playfield.length - 1) { - score += 60; + score += (10 * playfield.length); } } } - if(score == 2080) { + if(score == (1000 + 3 * (10 * playfield.length * playfield[0].length))) { score += 500; } return score; diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 0531164..d37645c 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -68,11 +68,11 @@ class LogicTest { assertEquals(expectedResult, realResult); } - @ParameterizedTest(name = "[{index}] {0} Win for Player {2} {1}") + @ParameterizedTest(name = "[{index}] {0} with {1}") @MethodSource("playfieldForScore") - void calcScoreTest(String testName, int[][] playfield, int player, int expectedResult) { + void calcScoreTest(String testName, int[][] playfield, int expectedResult) { - int realResult = testObj.calcScore(playfield, player); + int realResult = testObj.calcScore(playfield); assertEquals(expectedResult, realResult); } @@ -156,13 +156,13 @@ class LogicTest { {2, 1, 2, 2, 1, 2}, {2, 1, 2, 2, 1, 2}}; return Stream.of( - Arguments.of("emptyField", new int[6][6], 1, 1000), - Arguments.of("9 chips played", playfield1, 1, 1090), - Arguments.of("first row filled", playfield2, 1, 1120), - Arguments.of("2 rows filled", playfield3, 1, 1240), - Arguments.of("first column filled", playfield4, 1, 1120), - Arguments.of("2 column filled", playfield5, 1, 1240), - Arguments.of("fullyFilled", playfield6, 1, 2580) + Arguments.of("emptyField", new int[6][6], 1000), + Arguments.of("9 chips played", playfield1, 1090), + Arguments.of("first row filled", playfield2, 1120), + Arguments.of("2 rows filled", playfield3, 1240), + Arguments.of("first column filled", playfield4, 1120), + Arguments.of("2 column filled", playfield5, 1240), + Arguments.of("fullyFilled", playfield6, 2580) ); } From 10c67f8bcb9404852955a15fedb1e55a875983e5 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Fri, 14 Jan 2022 18:54:49 +0100 Subject: [PATCH 28/58] 4gewinnt: updated playChip --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index d4a20aa..c42c5cc 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -21,7 +21,9 @@ public class Logic { //add board update updateGui(playfield); - testForWin(playfield, player); + if(testForWin(playfield, player)) { + calcScore(playfield); + } currentPlayer = (player % 2) + 1; return 0; } From 9897836eecde598d1f9a208c1270f9f8530575d1 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Fri, 14 Jan 2022 20:25:05 +0100 Subject: [PATCH 29/58] 4gewinnt: setter for Board with player 1 --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 6 +++++- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index c42c5cc..1378bed 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -13,9 +13,13 @@ import javax.swing.JPanel; public class Logic { - int[][] board; + int[][] board = new int[6][6]; int currentPlayer; + public void setField(int player, int row, int column) { + board[row][column] = 1; + } + public int playChip(int[][] playfield, int column, int player) { //add board update diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index d37645c..7a1c79e 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -77,6 +77,17 @@ class LogicTest { assertEquals(expectedResult, realResult); } + @Test + void setFieldPlayer1Test() { + int player = 1; + int row = 0; + int column = 0; + + testObj.setField(player, row, column); + + assertEquals(1, testObj.board[row][column]); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From affdaad4b402076f3110fa9b9a440e9d1b9f320f Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Fri, 14 Jan 2022 20:54:00 +0100 Subject: [PATCH 30/58] 4gewinnt: finished setter for Board --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 2 +- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 1378bed..c26d4ee 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -17,7 +17,7 @@ public class Logic { int currentPlayer; public void setField(int player, int row, int column) { - board[row][column] = 1; + board[row][column] = player; } public int playChip(int[][] playfield, int column, int player) { diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 7a1c79e..db2a1ce 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -88,6 +88,17 @@ class LogicTest { assertEquals(1, testObj.board[row][column]); } + @Test + void setFieldPlayer2Test() { + int player = 2; + int row = 0; + int column = 0; + + testObj.setField(player, row, column); + + assertEquals(2, testObj.board[row][column]); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From e5abbc26f532406fd75c96e547e6c4b9dede1e06 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Sat, 15 Jan 2022 00:43:48 +0100 Subject: [PATCH 31/58] 4gewinnt: added getter for board --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 4 ++++ .../java/de/tims/viergewinnt/ai/LogicTest.java | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index c26d4ee..06ee5f3 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -20,6 +20,10 @@ public class Logic { board[row][column] = player; } + public int getField(int row, int column) { + return board[row][column]; + } + public int playChip(int[][] playfield, int column, int player) { //add board update diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index db2a1ce..0dd727b 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -96,7 +96,19 @@ class LogicTest { testObj.setField(player, row, column); - assertEquals(2, testObj.board[row][column]); + assertEquals(player, testObj.board[row][column]); + } + + @Test + void getFieldTest() { + int player = 2; + int row = 0; + int column = 0; + + testObj.setField(player, row, column); + int realResult = testObj.getField(row, column); + + assertEquals(player, realResult); } private static Stream playfieldResults() { From 2d40913c30e4e3283a611447c174184e65b10546 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 17 Jan 2022 17:33:43 +0100 Subject: [PATCH 32/58] 4gewinnt: added setter for currentPlayer --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 4 ++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 06ee5f3..81b03cb 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -24,6 +24,10 @@ public class Logic { return board[row][column]; } + public void setCurrentPlayer(int player) { + currentPlayer = player; + } + public int playChip(int[][] playfield, int column, int player) { //add board update diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 0dd727b..53d2061 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -111,6 +111,15 @@ class LogicTest { assertEquals(player, realResult); } + @Test + void setCurrentPlayerTest() { + int player = 1; + + testObj.setCurrentPlayer(player); + + assertEquals(player, testObj.currentPlayer); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From f03b32f0ddcbd512d322fe57c05696781d9654b7 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 17 Jan 2022 17:39:01 +0100 Subject: [PATCH 33/58] 4gewinnt: added getter for currentPlayer --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 4 ++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 81b03cb..a6373d8 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -28,6 +28,10 @@ public class Logic { currentPlayer = player; } + public int getCurrentPlayer() { + return currentPlayer; + } + public int playChip(int[][] playfield, int column, int player) { //add board update diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 53d2061..7546c0d 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -120,6 +120,16 @@ class LogicTest { assertEquals(player, testObj.currentPlayer); } + @Test + void getCurrentPlayerTest() { + int player = 2; + + testObj.setCurrentPlayer(player); + int realResult = testObj.getCurrentPlayer(); + + assertEquals(player, realResult); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From 853e57a70379dcfa32964e64b07a7a9ee079b271 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 17 Jan 2022 17:52:26 +0100 Subject: [PATCH 34/58] 4gewinnt: refactored code with setter/getter for board --- .../java/de/tims/viergewinnt/ai/Logic.java | 76 +++++++++---------- .../de/tims/viergewinnt/ai/LogicTest.java | 25 +++--- 2 files changed, 54 insertions(+), 47 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index a6373d8..dc3a04e 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -14,7 +14,7 @@ import javax.swing.JPanel; public class Logic { int[][] board = new int[6][6]; - int currentPlayer; + int currentPlayer = 1; public void setField(int player, int row, int column) { board[row][column] = player; @@ -32,13 +32,13 @@ public class Logic { return currentPlayer; } - public int playChip(int[][] playfield, int column, int player) { + public int playChip(int column, int player) { //add board update - updateGui(playfield); - if(testForWin(playfield, player)) { - calcScore(playfield); + updateGui(); + if(testForWin(player)) { + calcScore(); } currentPlayer = (player % 2) + 1; return 0; @@ -75,12 +75,12 @@ public class Logic { return contentPanel; } - public void updateGui(int[][] playfield) { - for(int i = 0; i < playfield.length; i++) { - for(int j = 0; j < playfield[i].length; j++) { - if(playfield[i][j] == 0) { + public void updateGui() { + for(int i = 0; i < board.length; i++) { + for(int j = 0; j < board[i].length; j++) { + if(getField(i,j) == 0) { gamefield[j + 6 * i].setBackground(Color.white); - } else if(playfield[i][j] == 1) { + } else if(getField(i,j) == 1) { gamefield[j + 6 * i].setBackground(Color.red); } else { gamefield[j + 6 * i].setBackground(Color.yellow); @@ -95,7 +95,7 @@ public class Logic { public void actionPerformed(ActionEvent e) { for(int i = 0; i < buttons.length; i++) { if(e.getSource() == buttons[i]) { - playChip(board, i, currentPlayer); + playChip(i, currentPlayer); break; } } @@ -103,11 +103,11 @@ public class Logic { } - public boolean testForWin(int[][] playfield, int player) { + public boolean testForWin(int player) { int chain = 0; - for(int i = 0; i < playfield[0].length; i++) { - for(int j = 0; j < playfield.length; j++) { - if(playfield[j][i] == player) { + for(int i = 0; i < board[0].length; i++) { + for(int j = 0; j < board.length; j++) { + if(getField(j,i) == player) { chain++; } else { chain = 0; @@ -116,9 +116,9 @@ public class Logic { } } chain = 0; - for(int i = 0; i < playfield.length; i++) { - for(int j = 0; j < playfield[0].length; j++) { - if(playfield[i][j] == player) { + for(int i = 0; i < board.length; i++) { + for(int j = 0; j < board[0].length; j++) { + if(getField(i,j) == player) { chain++; } else { chain = 0; @@ -127,13 +127,13 @@ public class Logic { } } chain = 0; - for(int i = 0; i < playfield.length - 3; i++) { - for(int j = 0; j < playfield[i].length - 3; j++) { - if(playfield[i][j] == player) { + for(int i = 0; i < board.length - 3; i++) { + for(int j = 0; j < board[i].length - 3; j++) { + if(getField(i,j) == player) { chain++; while(true) { i++; j++; - if(playfield[i][j] == player) { + if(getField(i,j) == player) { chain++; if(chain == 4) return true; } else { @@ -145,13 +145,13 @@ public class Logic { } } chain = 0; - for(int i = 0; i < playfield.length - 3; i++) { - for(int j = playfield[i].length - 1; j > 2; j--) { - if(playfield[i][j] == player) { + for(int i = 0; i < board.length - 3; i++) { + for(int j = board[i].length - 1; j > 2; j--) { + if(getField(i,j) == player) { chain++; while(true) { i++; j--; - if(playfield[i][j] == player) { + if(getField(i,j) == player) { chain++; if(chain == 4) return true; } else { @@ -165,30 +165,30 @@ public class Logic { return false; } - public int calcScore(int[][] playfield) { + public int calcScore() { int score = 1000; - for(int i = 0; i < playfield.length; i++) { + for(int i = 0; i < board.length; i++) { int oldScore = score; - for(int j = 0; j < playfield[i].length; j++) { - if(playfield[i][j] != 0) { + for(int j = 0; j < board[i].length; j++) { + if(getField(i,j) != 0) { score += 10; } } - if(score - (10 * playfield[0].length) == oldScore) { - score += (10 * playfield[0].length); + if(score - (10 * board[0].length) == oldScore) { + score += (10 * board[0].length); } } - for(int i = 0; i < playfield[0].length; i++) { - for(int j = 0; j < playfield.length; j++) { - if(playfield[j][i] == 0) { + for(int i = 0; i < board[0].length; i++) { + for(int j = 0; j < board.length; j++) { + if(getField(j,i) == 0) { break; } - if(j == playfield.length - 1) { - score += (10 * playfield.length); + if(j == board.length - 1) { + score += (10 * board.length); } } } - if(score == (1000 + 3 * (10 * playfield.length * playfield[0].length))) { + if(score == (1000 + 3 * (10 * board.length * board[0].length))) { score += 500; } return score; diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 7546c0d..bbb5fa9 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -22,33 +22,36 @@ class LogicTest { @ParameterizedTest(name = "[{index}] {0} Player {2} {3}") @MethodSource("playfieldResults") void testForWinTest(String testName, int[][] playfield, int player, boolean expectedResult) { + for(int i = 0; i < playfield.length; i++) { + for(int j = 0; j < playfield[i].length; j++) { + testObj.setField(playfield[i][j], i, j); + } + } - boolean realResult = testObj.testForWin(playfield, player); + boolean realResult = testObj.testForWin(player); assertEquals(expectedResult, realResult); } @Test void playChipTest() { - int[][] playfield = new int[6][6]; int column = 0; int player = 1; int expectedResult = 0; testObj.create4gewinntGui(); - int realResult = testObj.playChip(playfield, column, player); + int realResult = testObj.playChip(column, player); assertEquals(expectedResult, realResult); } @Test void changePlayerTo2Test() { - int[][] playfield = new int[6][6]; int column = 0; int player = 1; int expectedResult = 2; testObj.create4gewinntGui(); - testObj.playChip(playfield, column, player); + testObj.playChip(column, player); int realResult = testObj.currentPlayer; assertEquals(expectedResult, realResult); @@ -56,13 +59,12 @@ class LogicTest { @Test void changePlayerTo1Test() { - int[][] playfield = new int[6][6]; int column = 0; int player = 2; int expectedResult = 1; testObj.create4gewinntGui(); - testObj.playChip(playfield, column, player); + testObj.playChip(column, player); int realResult = testObj.currentPlayer; assertEquals(expectedResult, realResult); @@ -71,8 +73,13 @@ class LogicTest { @ParameterizedTest(name = "[{index}] {0} with {1}") @MethodSource("playfieldForScore") void calcScoreTest(String testName, int[][] playfield, int expectedResult) { - - int realResult = testObj.calcScore(playfield); + for(int i = 0; i < playfield.length; i++) { + for(int j = 0; j < playfield[i].length; j++) { + testObj.setField(playfield[i][j], i, j); + } + } + + int realResult = testObj.calcScore(); assertEquals(expectedResult, realResult); } From 92f33426d868e9e130a1aa7bac8bf8e6ea9c4308 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 17 Jan 2022 18:01:15 +0100 Subject: [PATCH 35/58] 4gewinnt: refactored code with setter/getter for currentPlayer --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 11 ++++++----- .../java/de/tims/viergewinnt/ai/LogicTest.java | 17 +++++++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index dc3a04e..33636f6 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -32,15 +32,15 @@ public class Logic { return currentPlayer; } - public int playChip(int column, int player) { + public int playChip(int column) { //add board update updateGui(); - if(testForWin(player)) { + if(testForWin()) { calcScore(); } - currentPlayer = (player % 2) + 1; + setCurrentPlayer((getCurrentPlayer() % 2) + 1); return 0; } @@ -95,7 +95,7 @@ public class Logic { public void actionPerformed(ActionEvent e) { for(int i = 0; i < buttons.length; i++) { if(e.getSource() == buttons[i]) { - playChip(i, currentPlayer); + playChip(i); break; } } @@ -103,7 +103,8 @@ public class Logic { } - public boolean testForWin(int player) { + public boolean testForWin() { + int player = getCurrentPlayer(); int chain = 0; for(int i = 0; i < board[0].length; i++) { for(int j = 0; j < board.length; j++) { diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index bbb5fa9..9bd71da 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -27,8 +27,9 @@ class LogicTest { testObj.setField(playfield[i][j], i, j); } } + testObj.setCurrentPlayer(player); - boolean realResult = testObj.testForWin(player); + boolean realResult = testObj.testForWin(); assertEquals(expectedResult, realResult); } @@ -39,7 +40,9 @@ class LogicTest { int player = 1; int expectedResult = 0; testObj.create4gewinntGui(); - int realResult = testObj.playChip(column, player); + testObj.setCurrentPlayer(player); + + int realResult = testObj.playChip(column); assertEquals(expectedResult, realResult); } @@ -49,9 +52,10 @@ class LogicTest { int column = 0; int player = 1; int expectedResult = 2; - testObj.create4gewinntGui(); - testObj.playChip(column, player); + testObj.setCurrentPlayer(player); + testObj.playChip(column); + int realResult = testObj.currentPlayer; assertEquals(expectedResult, realResult); @@ -62,9 +66,10 @@ class LogicTest { int column = 0; int player = 2; int expectedResult = 1; - testObj.create4gewinntGui(); - testObj.playChip(column, player); + testObj.setCurrentPlayer(player); + testObj.playChip(column); + int realResult = testObj.currentPlayer; assertEquals(expectedResult, realResult); From 12cf4f330c04e979ebec30d21781a71d1d64ef90 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 17 Jan 2022 18:35:20 +0100 Subject: [PATCH 36/58] 4gewinnt: added setter for player1Score --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 5 +++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 33636f6..dd8f643 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -15,6 +15,7 @@ public class Logic { int[][] board = new int[6][6]; int currentPlayer = 1; + int player1Score = 0; public void setField(int player, int row, int column) { board[row][column] = player; @@ -32,6 +33,10 @@ public class Logic { return currentPlayer; } + public void setPlayer1Score(int score) { + player1Score = score; + } + public int playChip(int column) { //add board update diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 9bd71da..3b5ac6c 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -125,7 +125,7 @@ class LogicTest { @Test void setCurrentPlayerTest() { - int player = 1; + int player = 2; testObj.setCurrentPlayer(player); @@ -142,6 +142,14 @@ class LogicTest { assertEquals(player, realResult); } + @Test + void setPlayer1ScoreTest() { + int expectedResult = 500; + testObj.setPlayer1Score(expectedResult); + + assertEquals(expectedResult, testObj.player1Score); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From f409fdcb75a862ce53712413af2aef5382ad2389 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 17 Jan 2022 18:50:45 +0100 Subject: [PATCH 37/58] 4gewinnt: added getter for player1Score --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 4 ++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index dd8f643..23a9a6b 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -37,6 +37,10 @@ public class Logic { player1Score = score; } + public int getPlayer1Score() { + return player1Score; + } + public int playChip(int column) { //add board update diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 3b5ac6c..c9c2bb0 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -150,6 +150,16 @@ class LogicTest { assertEquals(expectedResult, testObj.player1Score); } + @Test + void getPlayer1ScoreTest() { + int expectedResult = 500; + testObj.setPlayer1Score(expectedResult); + + int realResult = testObj.getPlayer1Score(); + + assertEquals(expectedResult, realResult); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From 97c66b727e5a7d26cf5676bcc4211b745b8b7ce0 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 17 Jan 2022 18:58:33 +0100 Subject: [PATCH 38/58] 4gewinnt: refactored attributs and test methods --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 6 +++--- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 23a9a6b..1f643c8 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -13,9 +13,9 @@ import javax.swing.JPanel; public class Logic { - int[][] board = new int[6][6]; - int currentPlayer = 1; - int player1Score = 0; + private int[][] board = new int[6][6]; + private int currentPlayer = 1; + private int player1Score = 0; public void setField(int player, int row, int column) { board[row][column] = player; diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index c9c2bb0..b70ad80 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -56,7 +56,7 @@ class LogicTest { testObj.setCurrentPlayer(player); testObj.playChip(column); - int realResult = testObj.currentPlayer; + int realResult = testObj.getCurrentPlayer(); assertEquals(expectedResult, realResult); } @@ -70,7 +70,7 @@ class LogicTest { testObj.setCurrentPlayer(player); testObj.playChip(column); - int realResult = testObj.currentPlayer; + int realResult = testObj.getCurrentPlayer(); assertEquals(expectedResult, realResult); } @@ -97,7 +97,7 @@ class LogicTest { testObj.setField(player, row, column); - assertEquals(1, testObj.board[row][column]); + assertEquals(1, testObj.getField(row, column)); } @Test @@ -108,7 +108,7 @@ class LogicTest { testObj.setField(player, row, column); - assertEquals(player, testObj.board[row][column]); + assertEquals(player, testObj.getField(row, column)); } @Test @@ -129,7 +129,7 @@ class LogicTest { testObj.setCurrentPlayer(player); - assertEquals(player, testObj.currentPlayer); + assertEquals(player, testObj.getCurrentPlayer()); } @Test @@ -147,7 +147,7 @@ class LogicTest { int expectedResult = 500; testObj.setPlayer1Score(expectedResult); - assertEquals(expectedResult, testObj.player1Score); + assertEquals(expectedResult, testObj.getPlayer1Score()); } @Test From f42de527a2f275b75985b4cd0a3f77c234a5d2d3 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Mon, 17 Jan 2022 21:41:43 +0100 Subject: [PATCH 39/58] 4gewinnt: added setter/getter for player2Score --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 1f643c8..9112de2 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -16,6 +16,7 @@ public class Logic { private int[][] board = new int[6][6]; private int currentPlayer = 1; private int player1Score = 0; + private int player2Score = 0; public void setField(int player, int row, int column) { board[row][column] = player; @@ -41,6 +42,14 @@ public class Logic { return player1Score; } + public void setPlayer2Score(int score) { + player2Score = score; + } + + public int getPlayer2Score() { + return player2Score; + } + public int playChip(int column) { //add board update From ddf26d199ad62093bf94d3e35b9c4d6986afeffa Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Tue, 18 Jan 2022 15:53:01 +0100 Subject: [PATCH 40/58] 4gewinnt: added constructor to create game logic --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 6 +++++- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 11 ++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 9112de2..a208c96 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -18,7 +18,11 @@ public class Logic { private int player1Score = 0; private int player2Score = 0; - public void setField(int player, int row, int column) { + public Logic(int size) { + // TODO Auto-generated constructor stub + } + + public void setField(int player, int row, int column) { board[row][column] = player; } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index b70ad80..cda8cbe 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -16,7 +16,7 @@ class LogicTest { @BeforeEach void setUp() throws Exception { - testObj = new Logic(); + testObj = new Logic(6); } @ParameterizedTest(name = "[{index}] {0} Player {2} {3}") @@ -160,6 +160,15 @@ class LogicTest { assertEquals(expectedResult, realResult); } + @Test + void createGameLogicTest() { + int size = 6; + Logic expectedResult = testObj; + Logic realResult = new Logic(size); + + assertEquals(expectedResult.getClass(), realResult.getClass()); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From 2530467f141b7d53620546ea197451991566653b Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Tue, 18 Jan 2022 16:02:42 +0100 Subject: [PATCH 41/58] 4gewinnt: added getter method for board size --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 4 ++++ .../java/de/tims/viergewinnt/ai/LogicTest.java | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index a208c96..883335f 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -30,6 +30,10 @@ public class Logic { return board[row][column]; } + public int getBoardSize() { + return 6; + } + public void setCurrentPlayer(int player) { currentPlayer = player; } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index cda8cbe..c53ef6b 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -162,11 +162,19 @@ class LogicTest { @Test void createGameLogicTest() { - int size = 6; - Logic expectedResult = testObj; - Logic realResult = new Logic(size); + int size = 6; + Logic expectedResult = testObj; + Logic realResult = new Logic(size); - assertEquals(expectedResult.getClass(), realResult.getClass()); + assertEquals(expectedResult.getClass(), realResult.getClass()); + } + + @Test + void getBoardSize() { + int expectedResult = 6; + int realResult = testObj.getBoardSize(); + + assertEquals(expectedResult, realResult); } private static Stream playfieldResults() { From 2d4b0be4e578ae9653173bba429e404a7d84e862 Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Tue, 18 Jan 2022 16:10:53 +0100 Subject: [PATCH 42/58] 4gewinnt: added test for board with size 0 --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 2 +- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 883335f..6151905 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -31,7 +31,7 @@ public class Logic { } public int getBoardSize() { - return 6; + return 0; } public void setCurrentPlayer(int player) { diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index c53ef6b..ab61f07 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -169,9 +169,10 @@ class LogicTest { assertEquals(expectedResult.getClass(), realResult.getClass()); } - @Test - void getBoardSize() { - int expectedResult = 6; + @ParameterizedTest(name = "[{index}] size {0}") + @MethodSource("boardSizeParameters") + void getBoardSize(String testName, int size) { + int expectedResult = size; int realResult = testObj.getBoardSize(); assertEquals(expectedResult, realResult); @@ -265,5 +266,11 @@ class LogicTest { Arguments.of("fullyFilled", playfield6, 2580) ); } + + private static Stream boardSizeParameters() { + return Stream.of( + Arguments.of("board with size 0", 0) + ); + } } From bd09c5783b9bb3e2eaef1c80ab3d75b94d0c70d9 Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Tue, 18 Jan 2022 16:31:40 +0100 Subject: [PATCH 43/58] 4gewinnt: finished board creation with individual size --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 6 +++--- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 6151905..85ec410 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -13,13 +13,13 @@ import javax.swing.JPanel; public class Logic { - private int[][] board = new int[6][6]; + private int[][] board; private int currentPlayer = 1; private int player1Score = 0; private int player2Score = 0; public Logic(int size) { - // TODO Auto-generated constructor stub + this.board = new int[size][size]; } public void setField(int player, int row, int column) { @@ -31,7 +31,7 @@ public class Logic { } public int getBoardSize() { - return 0; + return this.board.length; } public void setCurrentPlayer(int player) { diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index ab61f07..1ac3970 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -173,7 +173,7 @@ class LogicTest { @MethodSource("boardSizeParameters") void getBoardSize(String testName, int size) { int expectedResult = size; - int realResult = testObj.getBoardSize(); + int realResult = new Logic(size).getBoardSize(); assertEquals(expectedResult, realResult); } @@ -269,7 +269,8 @@ class LogicTest { private static Stream boardSizeParameters() { return Stream.of( - Arguments.of("board with size 0", 0) + Arguments.of("board with size 0", 0), + Arguments.of("board with size 6", 6) ); } From 1c7f4a513e90104d400ac107d8e882cd0f0c219f Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Tue, 18 Jan 2022 16:33:35 +0100 Subject: [PATCH 44/58] 4gewinnt: fixed GuiTest because of new parameterized constructor --- src/main/java/de/tims/viergewinnt/ai/GuiTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/GuiTest.java b/src/main/java/de/tims/viergewinnt/ai/GuiTest.java index be0aa60..c8327ee 100644 --- a/src/main/java/de/tims/viergewinnt/ai/GuiTest.java +++ b/src/main/java/de/tims/viergewinnt/ai/GuiTest.java @@ -8,7 +8,7 @@ public class GuiTest { public GuiTest() { frame = new JFrame(); frame.setSize(500, 500); - Logic logic = new Logic(); + Logic logic = new Logic(6); frame.add(logic.create4gewinntGui()); frame.setVisible(true); } From 2af74bef6b1dc90cdc93bb83affb5273a2f7498a Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Tue, 18 Jan 2022 16:36:33 +0100 Subject: [PATCH 45/58] 4gewinnt: fixed typo in getBoardSize test name --- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 1ac3970..1c9a2f5 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -169,9 +169,9 @@ class LogicTest { assertEquals(expectedResult.getClass(), realResult.getClass()); } - @ParameterizedTest(name = "[{index}] size {0}") + @ParameterizedTest(name = "[{index}] {0}") @MethodSource("boardSizeParameters") - void getBoardSize(String testName, int size) { + void getBoardSizeTest(String testName, int size) { int expectedResult = size; int realResult = new Logic(size).getBoardSize(); From ce2f8ddd8b20b42e213ff306c043efd81f662d4e Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Tue, 18 Jan 2022 17:03:59 +0100 Subject: [PATCH 46/58] 4gewinnt: added gameover method --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 4 ++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 85ec410..5135a1c 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -220,4 +220,8 @@ public class Logic { } return score; } + + public void gameover() { + setPlayer1Score(10); + } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 1c9a2f5..d423d58 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -178,6 +178,15 @@ class LogicTest { assertEquals(expectedResult, realResult); } + @Test + void gameoverTest() { + testObj.setField(1, 0, 0); + + testObj.gameover(); + + assertEquals(10, testObj.getPlayer1Score()); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From caf11d2ee21adacea011731ef1ed28408b37df4b Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Tue, 18 Jan 2022 19:59:07 +0100 Subject: [PATCH 47/58] 4gewinnt: refactored setter and getter --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 5135a1c..c557bbc 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -23,11 +23,11 @@ public class Logic { } public void setField(int player, int row, int column) { - board[row][column] = player; + this.board[row][column] = player; } public int getField(int row, int column) { - return board[row][column]; + return this.board[row][column]; } public int getBoardSize() { @@ -35,27 +35,27 @@ public class Logic { } public void setCurrentPlayer(int player) { - currentPlayer = player; + this.currentPlayer = player; } public int getCurrentPlayer() { - return currentPlayer; + return this.currentPlayer; } public void setPlayer1Score(int score) { - player1Score = score; + this.player1Score = score; } public int getPlayer1Score() { - return player1Score; + return this.player1Score; } public void setPlayer2Score(int score) { - player2Score = score; + this.player2Score = score; } public int getPlayer2Score() { - return player2Score; + return this.player2Score; } public int playChip(int column) { From 8961708346a3b876d8262ccdba13207056991e36 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 2 Feb 2022 13:35:59 +0100 Subject: [PATCH 48/58] 4gewinnt: added getFreePlace method --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 4 ++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index c557bbc..3030511 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -58,6 +58,10 @@ public class Logic { return this.player2Score; } + public int getFreePlace(int column) { + return this.getBoardSize(); + } + public int playChip(int column) { //add board update diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index d423d58..b6e55bd 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -187,6 +187,11 @@ class LogicTest { assertEquals(10, testObj.getPlayer1Score()); } + @Test + void getFreePlaceTest() { + assertEquals(testObj.getBoardSize(), testObj.getFreePlace(0)); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From 869355bddd03b940ae85228113c48fcb2633151e Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 2 Feb 2022 14:02:02 +0100 Subject: [PATCH 49/58] 4gewinnt: getFreePlaceTestWithFilledField added --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 6 +++++- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 11 +++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 3030511..a20f7ce 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -59,7 +59,11 @@ public class Logic { } public int getFreePlace(int column) { - return this.getBoardSize(); + if(getField(getBoardSize() - 1, column) == 0) { + return getBoardSize() - 1; + } else { + return getBoardSize() - 2; + } } public int playChip(int column) { diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index b6e55bd..f77859e 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -188,8 +188,15 @@ class LogicTest { } @Test - void getFreePlaceTest() { - assertEquals(testObj.getBoardSize(), testObj.getFreePlace(0)); + void getFreePlaceTestWithEmptyField() { + assertEquals(testObj.getBoardSize() - 1, testObj.getFreePlace(0)); + } + + @Test + void getFreePlaceTestWithFilledField() { + testObj.setField(1, testObj.getBoardSize() - 1, 0); + + assertEquals(testObj.getBoardSize() - 2, testObj.getFreePlace(0)); } private static Stream playfieldResults() { From caec8e73d7b57d56511e0acf9c33827ffe5c539a Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 2 Feb 2022 14:14:24 +0100 Subject: [PATCH 50/58] 4gewinnt: finished getFreePlaceMethod --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 8 ++++---- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 8 ++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index a20f7ce..fa0a461 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -59,11 +59,11 @@ public class Logic { } public int getFreePlace(int column) { - if(getField(getBoardSize() - 1, column) == 0) { - return getBoardSize() - 1; - } else { - return getBoardSize() - 2; + int size = getBoardSize() - 1; + while(getField(size, column) != 0) { + size--; } + return size; } public int playChip(int column) { diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index f77859e..6ddeefa 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -199,6 +199,14 @@ class LogicTest { assertEquals(testObj.getBoardSize() - 2, testObj.getFreePlace(0)); } + @Test + void getFreePlaceTestWithAnyField() { + testObj.setField(1, testObj.getBoardSize() - 1, 0); + testObj.setField(1, testObj.getBoardSize() - 2, 0); + + assertEquals(testObj.getBoardSize() - 3, testObj.getFreePlace(0)); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From 44ff79411eebdeaf313b0aa8fbdec589d8194a9c Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 2 Feb 2022 14:41:10 +0100 Subject: [PATCH 51/58] 4gewinnt: added clearField method --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 6 +++++- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 11 ++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index fa0a461..2b7c862 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -30,6 +30,10 @@ public class Logic { return this.board[row][column]; } + public void clearField() { + this.setField(0, 0, 0); + } + public int getBoardSize() { return this.board.length; } @@ -230,6 +234,6 @@ public class Logic { } public void gameover() { - setPlayer1Score(10); + setPlayer1Score(calcScore()); } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 6ddeefa..ca8ca7f 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -184,7 +184,16 @@ class LogicTest { testObj.gameover(); - assertEquals(10, testObj.getPlayer1Score()); + assertEquals(1010, testObj.getPlayer1Score()); + } + + @Test + void clearFieldTest() { + testObj.setField(1, 0, 0); + + testObj.clearField(); + + assertEquals(0, testObj.getField(0, 0)); } @Test From a7d3315ecbc7d7652e20a251d699cb7b70487f79 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 2 Feb 2022 14:44:17 +0100 Subject: [PATCH 52/58] 4gewinnt: finished clearField method --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 2 +- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 2b7c862..a899c8a 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -31,7 +31,7 @@ public class Logic { } public void clearField() { - this.setField(0, 0, 0); + this.board = new int[getBoardSize()][getBoardSize()]; } public int getBoardSize() { diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index ca8ca7f..51e149a 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -196,6 +196,15 @@ class LogicTest { assertEquals(0, testObj.getField(0, 0)); } + @Test + void clearFieldTestCompleteClear() { + testObj.setField(1, 5, 5); + + testObj.clearField(); + + assertEquals(0, testObj.getField(5, 5)); + } + @Test void getFreePlaceTestWithEmptyField() { assertEquals(testObj.getBoardSize() - 1, testObj.getFreePlace(0)); From 5614b7f180e72eb6d7c5a6f57e244681644cb3f7 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 2 Feb 2022 14:53:34 +0100 Subject: [PATCH 53/58] 4gewinnt: refactored and updated GameOver method and tests --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 7 ++++++- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index a899c8a..4aba921 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -234,6 +234,11 @@ public class Logic { } public void gameover() { - setPlayer1Score(calcScore()); + if(getCurrentPlayer() == 1) { + setPlayer1Score(calcScore()); + } else { + setPlayer2Score(calcScore()); + } + clearField(); } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 51e149a..02d6719 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -184,7 +184,7 @@ class LogicTest { testObj.gameover(); - assertEquals(1010, testObj.getPlayer1Score()); + assertEquals(1010 == testObj.getPlayer1Score(), 0 == testObj.getField(0,0)); } @Test From a6ed74febe5e7c294881e74b94b539c0a1a689e3 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 2 Feb 2022 15:05:40 +0100 Subject: [PATCH 54/58] 4gewinnt: updated playChip method and tests --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 6 ++---- src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 4aba921..123202a 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -71,12 +71,10 @@ public class Logic { } public int playChip(int column) { - - //add board update - + setField(getCurrentPlayer(), getFreePlace(column), column); updateGui(); if(testForWin()) { - calcScore(); + gameover(); } setCurrentPlayer((getCurrentPlayer() % 2) + 1); return 0; diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 02d6719..76ff279 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -44,7 +44,7 @@ class LogicTest { int realResult = testObj.playChip(column); - assertEquals(expectedResult, realResult); + assertEquals(expectedResult == realResult, 1 == testObj.getField(5, 0)); } @Test From 29c241de55a441deb1ee431858910b001c173756 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 2 Feb 2022 15:17:15 +0100 Subject: [PATCH 55/58] 4gewinnt: updated mutltiple methods to deactivate buttons on full columns --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 10 ++++++++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 1 + 2 files changed, 11 insertions(+) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 123202a..922f32f 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -66,12 +66,18 @@ public class Logic { int size = getBoardSize() - 1; while(getField(size, column) != 0) { size--; + if(size < 0) { + return -1; + } } return size; } public int playChip(int column) { setField(getCurrentPlayer(), getFreePlace(column), column); + if(getFreePlace(column) == -1) { + buttons[column].setEnabled(false); + } updateGui(); if(testForWin()) { gameover(); @@ -238,5 +244,9 @@ public class Logic { setPlayer2Score(calcScore()); } clearField(); + for(int i = 0; i < getBoardSize(); i++) { + buttons[i].setEnabled(true); + } + updateGui(); } } diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 76ff279..3e27f5c 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -181,6 +181,7 @@ class LogicTest { @Test void gameoverTest() { testObj.setField(1, 0, 0); + testObj.create4gewinntGui(); testObj.gameover(); From d3ec638be0ecbb16c33af687fd80d2b52860bd97 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 2 Feb 2022 15:34:46 +0100 Subject: [PATCH 56/58] 4gewinnt: added checkButtons method --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 4 ++++ src/test/java/de/tims/viergewinnt/ai/LogicTest.java | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 922f32f..5773da8 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -86,6 +86,10 @@ public class Logic { return 0; } + public boolean checkButtons() { + return true; + } + JPanel contentPanel; JPanel buttonPanel; JPanel playfieldPanel; diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index 3e27f5c..c2f308a 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -226,6 +226,16 @@ class LogicTest { assertEquals(testObj.getBoardSize() - 3, testObj.getFreePlace(0)); } + @Test + void checkButtonsTest() { + testObj.create4gewinntGui(); + boolean expectedResult = true; + + boolean realResult = testObj.checkButtons(); + + assertEquals(expectedResult, realResult); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From 645770c07c21ee5115a3c8afeb96ae4762eab46a Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 2 Feb 2022 15:37:41 +0100 Subject: [PATCH 57/58] 4gewinnt: finished checkButtons method --- src/main/java/de/tims/viergewinnt/ai/Logic.java | 6 +++++- .../java/de/tims/viergewinnt/ai/LogicTest.java | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index 5773da8..eaa9aca 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -87,7 +87,11 @@ public class Logic { } public boolean checkButtons() { - return true; + boolean res = false; + for(int i = 0; i < buttons.length; i++) { + res = buttons[i].isEnabled() | res; + } + return res; } JPanel contentPanel; diff --git a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java index c2f308a..9800775 100644 --- a/src/test/java/de/tims/viergewinnt/ai/LogicTest.java +++ b/src/test/java/de/tims/viergewinnt/ai/LogicTest.java @@ -227,7 +227,7 @@ class LogicTest { } @Test - void checkButtonsTest() { + void checkButtonsTestTrue() { testObj.create4gewinntGui(); boolean expectedResult = true; @@ -236,6 +236,19 @@ class LogicTest { assertEquals(expectedResult, realResult); } + @Test + void checkButtonsTestFalse() { + testObj.create4gewinntGui(); + boolean expectedResult = false; + for(int i = 0; i < testObj.buttons.length; i++) { + testObj.buttons[i].setEnabled(false); + } + + boolean realResult = testObj.checkButtons(); + + assertEquals(expectedResult, realResult); + } + private static Stream playfieldResults() { int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, From 6ee0ec873ca03747940a4b30bf9307c11f6b0631 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 2 Feb 2022 15:50:14 +0100 Subject: [PATCH 58/58] 4gewinnt: refactored gui elements to enable variable boardsize --- .../java/de/tims/viergewinnt/ai/Logic.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index eaa9aca..91b14c5 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -20,6 +20,8 @@ public class Logic { public Logic(int size) { this.board = new int[size][size]; + this.buttons = new JButton[getBoardSize()]; + this.gamefield = new JLabel[getBoardSize() * getBoardSize()]; } public void setField(int player, int row, int column) { @@ -81,6 +83,8 @@ public class Logic { updateGui(); if(testForWin()) { gameover(); + } else if(!checkButtons()) { + gameover(); } setCurrentPlayer((getCurrentPlayer() % 2) + 1); return 0; @@ -97,14 +101,14 @@ public class Logic { JPanel contentPanel; JPanel buttonPanel; JPanel playfieldPanel; - JButton[] buttons = new JButton[6]; - JLabel[] gamefield = new JLabel[36]; + JButton[] buttons; + JLabel[] gamefield; public JPanel create4gewinntGui() { contentPanel = new JPanel(); contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.PAGE_AXIS)); - buttonPanel = new JPanel(new GridLayout(1, 6)); - playfieldPanel = new JPanel(new GridLayout(6, 6)); + buttonPanel = new JPanel(new GridLayout(1, buttons.length)); + playfieldPanel = new JPanel(new GridLayout(buttons.length, buttons.length)); contentPanel.add(buttonPanel); contentPanel.add(playfieldPanel); @@ -129,11 +133,11 @@ public class Logic { for(int i = 0; i < board.length; i++) { for(int j = 0; j < board[i].length; j++) { if(getField(i,j) == 0) { - gamefield[j + 6 * i].setBackground(Color.white); + gamefield[j + getBoardSize() * i].setBackground(Color.white); } else if(getField(i,j) == 1) { - gamefield[j + 6 * i].setBackground(Color.red); + gamefield[j + getBoardSize() * i].setBackground(Color.red); } else { - gamefield[j + 6 * i].setBackground(Color.yellow); + gamefield[j + getBoardSize() * i].setBackground(Color.yellow); } } }