From 65a829fbff195e5445f35bd0c37368a2a828e4de Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sat, 29 Jan 2022 16:37:22 +0100 Subject: [PATCH 1/6] init AblageStack --- src/main/java/solitaer/AblageStack.java | 21 ++++++++++++++++ src/test/java/solitaer/AblageStackTest.java | 28 +++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/main/java/solitaer/AblageStack.java create mode 100644 src/test/java/solitaer/AblageStackTest.java diff --git a/src/main/java/solitaer/AblageStack.java b/src/main/java/solitaer/AblageStack.java new file mode 100644 index 0000000..7cb9fbe --- /dev/null +++ b/src/main/java/solitaer/AblageStack.java @@ -0,0 +1,21 @@ +package solitaer; + +public class AblageStack extends BaseStack { + + private static final long serialVersionUID = 1L; + + public AblageStack(int _x, int _y) { + super(); + super.setLocation(_x, _y); + super.setSize(IMAGE_WIDTH, STACK_HIGHT); + super.setOpaque(false); + super.setLayout(null); + } + + @Override + protected boolean cardCheck(Card _topStack, Card _playerCard) { + // TODO Auto-generated method stub + return true; + } + +} diff --git a/src/test/java/solitaer/AblageStackTest.java b/src/test/java/solitaer/AblageStackTest.java new file mode 100644 index 0000000..4a9283e --- /dev/null +++ b/src/test/java/solitaer/AblageStackTest.java @@ -0,0 +1,28 @@ +package solitaer; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class AblageStackTest { + + @ParameterizedTest + @MethodSource("testAblageStackTestDaten") + void testRulesetStartStack(String msg, Card _stapel, Card _spieler, boolean _expectedResult) { + AblageStack stack = new AblageStack(0, 0); + assertEquals(_expectedResult, stack.cardCheck(_stapel, _spieler), msg); + } + + private static Stream testAblageStackTestDaten(){ + return Stream.of( + Arguments.of("Ass-Kreuz auf Leeres Feld", + null, + new Card(1, Symbol.Kreuz, true), true) + ); + } + +} From e5fa1c365767fbf9c402e165170aba1d5d564831 Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 30 Jan 2022 16:10:00 +0100 Subject: [PATCH 2/6] Add Ruleset for AblageStack and Tests --- src/main/java/solitaer/AblageStack.java | 19 ++++++++++++--- src/test/java/solitaer/AblageStackTest.java | 26 +++++++++++++++++---- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/main/java/solitaer/AblageStack.java b/src/main/java/solitaer/AblageStack.java index 7cb9fbe..0e436b6 100644 --- a/src/main/java/solitaer/AblageStack.java +++ b/src/main/java/solitaer/AblageStack.java @@ -3,19 +3,32 @@ package solitaer; public class AblageStack extends BaseStack { private static final long serialVersionUID = 1L; + private Symbol sym; - public AblageStack(int _x, int _y) { + public AblageStack(int _x, int _y, Symbol _sym) { super(); super.setLocation(_x, _y); super.setSize(IMAGE_WIDTH, STACK_HIGHT); super.setOpaque(false); super.setLayout(null); + + this.sym = _sym; } @Override protected boolean cardCheck(Card _topStack, Card _playerCard) { - // TODO Auto-generated method stub - return true; + if(_topStack == null && _playerCard == null) { + return false; + } + if(_playerCard.getSymbol() != this.sym) { + return false; + } else if(_topStack == null) { + //nur ein Ass darf auf einen Leeren Stabel gelegt werden + return _playerCard.getNr() == 1; + } else { + return _topStack.getNr() == _playerCard.getNr() - 1; + } + } } diff --git a/src/test/java/solitaer/AblageStackTest.java b/src/test/java/solitaer/AblageStackTest.java index 4a9283e..6059ac1 100644 --- a/src/test/java/solitaer/AblageStackTest.java +++ b/src/test/java/solitaer/AblageStackTest.java @@ -12,16 +12,34 @@ class AblageStackTest { @ParameterizedTest @MethodSource("testAblageStackTestDaten") - void testRulesetStartStack(String msg, Card _stapel, Card _spieler, boolean _expectedResult) { - AblageStack stack = new AblageStack(0, 0); + void testRulesetAblageStack(String msg, Card _stapel, Card _spieler, boolean _expectedResult) { + AblageStack stack = new AblageStack(0, 0, Symbol.Kreuz); assertEquals(_expectedResult, stack.cardCheck(_stapel, _spieler), msg); } private static Stream testAblageStackTestDaten(){ return Stream.of( - Arguments.of("Ass-Kreuz auf Leeres Feld", + Arguments.of("Ass-Kreuz auf Leeres Kreuz-Feld", null, - new Card(1, Symbol.Kreuz, true), true) + null, false), + Arguments.of("Ass-Kreuz auf Leeres Kreuz-Feld", + null, + new Card(1, Symbol.Kreuz, true), true), + Arguments.of("02-Kreuz auf Leeres Kreuz-Feld", + null, + new Card(2, Symbol.Kreuz, true), false), + Arguments.of("Ass-Herz auf Leeres Kreuz-Feld", + null, + new Card(1, Symbol.Herz, true), false), + Arguments.of("05-Kreuz auf 04-Kreuz", + new Card(4, Symbol.Kreuz, true), + new Card(5, Symbol.Kreuz, true), true), + Arguments.of("05-Herz auf 04-Kreuz", + new Card(4, Symbol.Kreuz, true), + new Card(5, Symbol.Herz, true), false), + Arguments.of("Ass-Kreuz auf König-Kreuz", + new Card(13, Symbol.Kreuz, true), + new Card(1, Symbol.Kreuz, true), false) ); } From b16c5e74ad3f4673d76c414c9bddd6ac3e2d9d03 Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 30 Jan 2022 16:23:55 +0100 Subject: [PATCH 3/6] Add render for AblageStack --- src/main/java/solitaer/AblageStack.java | 29 +++++++++++++++++++ src/main/java/solitaer/SolitaerGamePanel.java | 15 ++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/main/java/solitaer/AblageStack.java b/src/main/java/solitaer/AblageStack.java index 0e436b6..56439a0 100644 --- a/src/main/java/solitaer/AblageStack.java +++ b/src/main/java/solitaer/AblageStack.java @@ -1,5 +1,9 @@ package solitaer; +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics; + public class AblageStack extends BaseStack { private static final long serialVersionUID = 1L; @@ -30,5 +34,30 @@ public class AblageStack extends BaseStack { } } + + @Override + protected void paintComponent(Graphics g) { + // TODO Auto-generated method stub + super.paintComponent(g); + if (stackIsEmpty()) { + g.setColor(Color.gray); + g.fillRect(0, 0, this.getWidth(), IMAGE_HIGHT); + + g.setColor(Color.black); + g.drawRect(0, 0, this.getWidth()-1, IMAGE_HIGHT-1); + + if (this.sym == Symbol.Herz || this.sym == Symbol.Karo) + g.setColor(Color.red); + else + g.setColor(Color.black); + + int fontSize = 20; + Font f = new Font(Font.MONOSPACED, Font.BOLD, fontSize); + g.setFont(f); + + String text = this.sym.toString(); + g.drawString(text, 5, this.IMAGE_HIGHT/2); + } + } } diff --git a/src/main/java/solitaer/SolitaerGamePanel.java b/src/main/java/solitaer/SolitaerGamePanel.java index d94b99c..a7a56f5 100644 --- a/src/main/java/solitaer/SolitaerGamePanel.java +++ b/src/main/java/solitaer/SolitaerGamePanel.java @@ -25,7 +25,7 @@ public class SolitaerGamePanel extends JPanel { - StartStack a = new StartStack(25,25); + StartStack a = new StartStack(25,125); a.addCard(deck.getDeck().get(0)); @@ -46,13 +46,13 @@ public class SolitaerGamePanel extends JPanel { this.add(a); - StartStack b = new StartStack(200,25); + StartStack b = new StartStack(200,125); deck.getDeck().get(32).setFaceUp(true); b.addCard((deck.getDeck().get(32))); this.add(b); - StartStack c = new StartStack(375,25); + StartStack c = new StartStack(375,125); deck.getDeck().get(33).setFaceUp(true); c.addCard((deck.getDeck().get(33))); @@ -62,6 +62,15 @@ public class SolitaerGamePanel extends JPanel { this.add(save); + AblageStack a1 = new AblageStack(200, 10, Symbol.Herz); + this.add(a1); + AblageStack a2 = new AblageStack(275, 10, Symbol.Kreuz); + this.add(a2); + AblageStack a3 = new AblageStack(350, 10, Symbol.Karo); + this.add(a3); + AblageStack a4 = new AblageStack(425, 10, Symbol.Pik); + this.add(a4); + } From 2cb74cadc9308531abbb85625560bd83c4f10da9 Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 30 Jan 2022 16:48:51 +0100 Subject: [PATCH 4/6] add MouseInput for AblageStack --- src/main/java/solitaer/MyMouseListener.java | 51 ++++++++++++++++--- src/main/java/solitaer/SolitaerGamePanel.java | 15 ++++-- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/main/java/solitaer/MyMouseListener.java b/src/main/java/solitaer/MyMouseListener.java index 4e9a08a..c79d440 100644 --- a/src/main/java/solitaer/MyMouseListener.java +++ b/src/main/java/solitaer/MyMouseListener.java @@ -58,26 +58,61 @@ public class MyMouseListener implements MouseInputListener { selectedStack = tempStack; } - } - //saveStack is NOT empty + } + // saveStack is NOT empty else { - if(selectedStack == null) { + if (selectedStack == null) { System.out.println("ERROR selectedStack in StartStack"); } - if(tempStack.cardCheck(tempStack.peekLastCard(), saveStack.peekLastCard())) { - while(!saveStack.stackIsEmpty()) { + if (tempStack.cardCheck(tempStack.peekLastCard(), saveStack.peekLastCard())) { + while (!saveStack.stackIsEmpty()) { tempStack.addCard(saveStack.removeLastCard()); } } else { - //Alles wieder auf den Ursprünglichen Stack packen - while(!saveStack.stackIsEmpty()) { + // Alles wieder auf den Ursprünglichen Stack packen + while (!saveStack.stackIsEmpty()) { selectedStack.addCard(saveStack.removeLastCard()); } } - + selectedStack = null; } } + // AblageStack + else if (com.getParent() instanceof AblageStack || com instanceof AblageStack) { + + System.out.println("AblageStack"); + + if (com instanceof AblageStack) { + tempStack = (AblageStack) com; + } else { + tempStack = (AblageStack) com.getParent(); + + } + + if (saveStack.stackIsEmpty()) { + // Move Top Card from AblageStack in Save + if (tempStack.stackIsEmpty() == false) { + saveStack.addCard(tempStack.removeLastCard()); + selectedStack = tempStack; + } + + } else { + // Move Card from Save in AblageStack + if (saveStack.stackSize() == 1) { + if (tempStack.cardCheck(tempStack.peekLastCard(), saveStack.peekLastCard())) { + tempStack.addCard(saveStack.removeLastCard()); + } + + } else { + // Alles wieder auf den Ursprünglichen Stack packen + while (!saveStack.stackIsEmpty()) { + selectedStack.addCard(saveStack.removeLastCard()); + } + } + } + + } } diff --git a/src/main/java/solitaer/SolitaerGamePanel.java b/src/main/java/solitaer/SolitaerGamePanel.java index a7a56f5..4c16f06 100644 --- a/src/main/java/solitaer/SolitaerGamePanel.java +++ b/src/main/java/solitaer/SolitaerGamePanel.java @@ -26,9 +26,7 @@ public class SolitaerGamePanel extends JPanel { StartStack a = new StartStack(25,125); - - a.addCard(deck.getDeck().get(0)); - + deck.getDeck().get(5).setFaceUp(true); a.addCard((deck.getDeck().get(5))); @@ -44,6 +42,9 @@ public class SolitaerGamePanel extends JPanel { deck.getDeck().get(1).setFaceUp(true); a.addCard((deck.getDeck().get(1))); + deck.getDeck().get(0).setFaceUp(true); + a.addCard(deck.getDeck().get(0)); + this.add(a); StartStack b = new StartStack(200,125); @@ -54,10 +55,14 @@ public class SolitaerGamePanel extends JPanel { StartStack c = new StartStack(375,125); - deck.getDeck().get(33).setFaceUp(true); - c.addCard((deck.getDeck().get(33))); + deck.getDeck().get(13).setFaceUp(true); + c.addCard((deck.getDeck().get(12))); this.add(c); + + StartStack d = new StartStack(475,125); + this.add(d); + //save = new SaveStack(500,250); this.add(save); From 63cdf0a8a799eea750de2171e536ea8379a756cd Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 30 Jan 2022 16:53:19 +0100 Subject: [PATCH 5/6] remove offset for AblageStack --- src/main/java/solitaer/AblageStack.java | 2 +- src/main/java/solitaer/BaseStack.java | 12 +++++++++--- src/main/java/solitaer/SaveStack.java | 2 +- src/main/java/solitaer/StartStack.java | 2 +- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/solitaer/AblageStack.java b/src/main/java/solitaer/AblageStack.java index 56439a0..c96818b 100644 --- a/src/main/java/solitaer/AblageStack.java +++ b/src/main/java/solitaer/AblageStack.java @@ -10,7 +10,7 @@ public class AblageStack extends BaseStack { private Symbol sym; public AblageStack(int _x, int _y, Symbol _sym) { - super(); + super(false); super.setLocation(_x, _y); super.setSize(IMAGE_WIDTH, STACK_HIGHT); super.setOpaque(false); diff --git a/src/main/java/solitaer/BaseStack.java b/src/main/java/solitaer/BaseStack.java index 84dfab2..a94321d 100644 --- a/src/main/java/solitaer/BaseStack.java +++ b/src/main/java/solitaer/BaseStack.java @@ -13,9 +13,15 @@ public abstract class BaseStack extends JPanel { private ArrayList stack = new ArrayList(); private final int OFFSET_VALUE = 20; int offset; + int addOffset; - public BaseStack() { + public BaseStack(boolean _offset) { // TODO Auto-generated constructor stub + if(_offset) { + addOffset = OFFSET_VALUE; + }else { + addOffset = 0; + } offset = 0; } @@ -43,13 +49,13 @@ public abstract class BaseStack extends JPanel { _card.setLocation(0, offset); stack.add(_card); RenderStackNew(); - offset += OFFSET_VALUE; + offset += addOffset; } public Card removeLastCard() { if (stack.size() != 0) { Card LastCard = stack.remove(stack.size() - 1); - offset -= OFFSET_VALUE; + offset -= addOffset; RenderStackNew(); return LastCard; } diff --git a/src/main/java/solitaer/SaveStack.java b/src/main/java/solitaer/SaveStack.java index 3f423a2..cd4cea0 100644 --- a/src/main/java/solitaer/SaveStack.java +++ b/src/main/java/solitaer/SaveStack.java @@ -9,7 +9,7 @@ public class SaveStack extends BaseStack { public SaveStack(int _x, int _y) { // TODO Auto-generated constructor stub - super(); + super(true); super.setLocation(_x, _y); super.setSize(IMAGE_WIDTH, STACK_HIGHT); super.setOpaque(false); diff --git a/src/main/java/solitaer/StartStack.java b/src/main/java/solitaer/StartStack.java index 8f14ae6..068b88f 100644 --- a/src/main/java/solitaer/StartStack.java +++ b/src/main/java/solitaer/StartStack.java @@ -9,7 +9,7 @@ public class StartStack extends BaseStack { private static final long serialVersionUID = 1L; public StartStack(int _x, int _y) { - super(); + super(true); super.setLocation(_x, _y); super.setSize(IMAGE_WIDTH, STACK_HIGHT); super.setOpaque(false); From 24f874a09d420b4b441b2c610319a6420a35b6bc Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 30 Jan 2022 17:09:23 +0100 Subject: [PATCH 6/6] Refactor MouseInput --- src/main/java/solitaer/MyMouseListener.java | 23 ++++++++++----------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/java/solitaer/MyMouseListener.java b/src/main/java/solitaer/MyMouseListener.java index c79d440..ee54111 100644 --- a/src/main/java/solitaer/MyMouseListener.java +++ b/src/main/java/solitaer/MyMouseListener.java @@ -65,14 +65,9 @@ public class MyMouseListener implements MouseInputListener { System.out.println("ERROR selectedStack in StartStack"); } if (tempStack.cardCheck(tempStack.peekLastCard(), saveStack.peekLastCard())) { - while (!saveStack.stackIsEmpty()) { - tempStack.addCard(saveStack.removeLastCard()); - } + clearSaveStack(tempStack); } else { - // Alles wieder auf den Ursprünglichen Stack packen - while (!saveStack.stackIsEmpty()) { - selectedStack.addCard(saveStack.removeLastCard()); - } + clearSaveStack(selectedStack); } selectedStack = null; @@ -87,7 +82,6 @@ public class MyMouseListener implements MouseInputListener { tempStack = (AblageStack) com; } else { tempStack = (AblageStack) com.getParent(); - } if (saveStack.stackIsEmpty()) { @@ -105,10 +99,8 @@ public class MyMouseListener implements MouseInputListener { } } else { - // Alles wieder auf den Ursprünglichen Stack packen - while (!saveStack.stackIsEmpty()) { - selectedStack.addCard(saveStack.removeLastCard()); - } + //Clear Save + clearSaveStack(selectedStack); } } @@ -116,6 +108,13 @@ public class MyMouseListener implements MouseInputListener { } + private void clearSaveStack(BaseStack _ziel) { + // Alles wieder auf den Ursprünglichen Stack packen + while (!saveStack.stackIsEmpty()) { + _ziel.addCard(saveStack.removeLastCard()); + } + } + @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub