From 5f6b3bbfffe27232e42c450887e493bbe355087c Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 30 Jan 2022 22:26:22 +0100 Subject: [PATCH 1/3] Fix MouseInput StartStack --- src/main/java/solitaer/MyMouseListener.java | 58 +++++++++++-------- src/main/java/solitaer/SolitaerGamePanel.java | 4 +- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/main/java/solitaer/MyMouseListener.java b/src/main/java/solitaer/MyMouseListener.java index 4067231..70aec81 100644 --- a/src/main/java/solitaer/MyMouseListener.java +++ b/src/main/java/solitaer/MyMouseListener.java @@ -30,37 +30,49 @@ public class MyMouseListener implements MouseInputListener { System.out.println("DOWN"); Component com = SwingUtilities.getDeepestComponentAt(e.getComponent(), e.getX(), e.getY()); - // Leeres StartFeld - if (com instanceof StartStack) { - System.out.println("Leer Start"); - - } - // Karte, die in einem StartStack liegt - else if (com instanceof Card && com.getParent() instanceof StartStack) { - tempCard = (Card) com; - tempStack = (StartStack) com.getParent(); +// // Leeres StartFeld +// if (com instanceof StartStack) { +// System.out.println("Leer Start"); +// +// } +// // Karte, die in einem StartStack liegt +// else + if ((com instanceof Card && com.getParent() instanceof StartStack) + || (com instanceof StartStack && ((StartStack) com).stackIsEmpty())) { if (saveStack.stackIsEmpty()) { - // Wenn faceDown, schau ob es die oberste Carde ist und dreh diese um - if (!tempCard.isFaceUp()) { - if (tempStack.peekLastCard().equals(tempCard)) { - tempCard.setFaceUp(true); - tempCard.repaint(); - } - } - // Card faceUp - else { - // move all Cards below the selected Card to Save - int index = tempStack.indexOfCard(tempCard); - while (tempStack.stackSize() - 1 >= index) { - saveStack.addCard(tempStack.removeLastCard()); + + if (com instanceof Card && com.getParent() instanceof StartStack) { + tempCard = (Card) com; + tempStack = (StartStack) com.getParent(); + + // Wenn faceDown, schau ob es die oberste Carde ist und dreh diese um + if (!tempCard.isFaceUp()) { + if (tempStack.peekLastCard().equals(tempCard)) { + tempCard.setFaceUp(true); + tempCard.repaint(); + } } - selectedStack = tempStack; + // Card faceUp + else { + // move all Cards below the selected Card to Save + int index = tempStack.indexOfCard(tempCard); + while (tempStack.stackSize() - 1 >= index) { + saveStack.addCard(tempStack.removeLastCard()); + } + selectedStack = tempStack; + } } } // saveStack is NOT empty else { + if(com instanceof StartStack) { + tempStack = (StartStack) com; + } else { + tempStack = (StartStack) com.getParent(); + } + if (selectedStack == null) { System.out.println("ERROR selectedStack in StartStack"); } diff --git a/src/main/java/solitaer/SolitaerGamePanel.java b/src/main/java/solitaer/SolitaerGamePanel.java index f3f757d..7eaf634 100644 --- a/src/main/java/solitaer/SolitaerGamePanel.java +++ b/src/main/java/solitaer/SolitaerGamePanel.java @@ -55,8 +55,8 @@ public class SolitaerGamePanel extends JPanel { StartStack c = new StartStack(375,125); - deck.getDeck().get(10).setFaceUp(true); - c.addCard((deck.getDeck().get(10))); + //deck.getDeck().get(20).setFaceUp(true); + c.addCard((deck.getDeck().get(20))); this.add(c); From d89b2a0c02bfbc0b999b84158c4e419b2980d944 Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 30 Jan 2022 22:35:24 +0100 Subject: [PATCH 2/3] Update check and test from StartStack --- src/main/java/solitaer/StartStack.java | 36 +++++++++------------- src/test/java/solitaer/StartStackTest.java | 5 +-- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/main/java/solitaer/StartStack.java b/src/main/java/solitaer/StartStack.java index 068b88f..0dd2f8f 100644 --- a/src/main/java/solitaer/StartStack.java +++ b/src/main/java/solitaer/StartStack.java @@ -3,11 +3,10 @@ package solitaer; import java.awt.Color; import java.awt.Graphics; - public class StartStack extends BaseStack { private static final long serialVersionUID = 1L; - + public StartStack(int _x, int _y) { super(true); super.setLocation(_x, _y); @@ -17,30 +16,27 @@ public class StartStack extends BaseStack { } public boolean cardCheck(Card _topStack, Card _playerCard) { - if(_topStack == null && _playerCard == null) { + if (_topStack == null && _playerCard == null) { return false; - } + } if (_topStack == null) { // Nur der König kann auf einen leeren Stapel gelegt werden return _playerCard.getNr() == 13; - }else { + } else if (!_topStack.isFaceUp()) { + return false; + } else { return (// Nummer nur aufsteigend - _topStack.getNr() == _playerCard.getNr() + 1) - && - // rot auf Schwarz - ( - ((_topStack.getSymbol() == Symbol.Herz || _topStack.getSymbol() == Symbol.Karo) - && (_playerCard.getSymbol() == Symbol.Pik || _playerCard.getSymbol() == Symbol.Kreuz)) - || + _topStack.getNr() == _playerCard.getNr() + 1) && + // rot auf Schwarz + (((_topStack.getSymbol() == Symbol.Herz || _topStack.getSymbol() == Symbol.Karo) + && (_playerCard.getSymbol() == Symbol.Pik || _playerCard.getSymbol() == Symbol.Kreuz)) || // schwarz auf rot - ((_topStack.getSymbol() == Symbol.Pik || _topStack.getSymbol() == Symbol.Kreuz) - && (_playerCard.getSymbol() == Symbol.Herz || _playerCard.getSymbol() == Symbol.Karo)) - ); + ((_topStack.getSymbol() == Symbol.Pik || _topStack.getSymbol() == Symbol.Kreuz) + && (_playerCard.getSymbol() == Symbol.Herz + || _playerCard.getSymbol() == Symbol.Karo))); } } - - @Override protected void paintComponent(Graphics g) { // TODO Auto-generated method stub @@ -48,12 +44,10 @@ public class StartStack extends BaseStack { 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); + g.drawRect(0, 0, this.getWidth() - 1, IMAGE_HIGHT - 1); } } - - } diff --git a/src/test/java/solitaer/StartStackTest.java b/src/test/java/solitaer/StartStackTest.java index e9515e8..d033e45 100644 --- a/src/test/java/solitaer/StartStackTest.java +++ b/src/test/java/solitaer/StartStackTest.java @@ -29,8 +29,9 @@ class StartStackTest { Arguments.of("5-Pik auf 6-Karo", new Card(6, Symbol.Karo, true), new Card(5, Symbol.Pik, true), true), Arguments.of("König-Karo auf Ass-Kreuz", new Card(1, Symbol.Kreuz, true), new Card(13, Symbol.Karo, true), false), Arguments.of("5-Karo auf 7-Kreuz", new Card(7, Symbol.Kreuz, true), new Card(5, Symbol.Karo, true), false), - Arguments.of("2-Herz auf Ass-Kreuz", new Card(2, Symbol.Herz, true), new Card(1, Symbol.Kreuz, true), true), - Arguments.of("null auf null", null, null, false) + Arguments.of("Ass-Herz auf 2-Kreuz", new Card(2, Symbol.Herz, true), new Card(1, Symbol.Kreuz, true), true), + Arguments.of("null auf null", null, null, false), + Arguments.of("Ass-Herz auf face-Down-2-Kreuz", new Card(2, Symbol.Herz, false), new Card(1, Symbol.Kreuz, true), false) ); } From 3ad5083c354c4999ba1c9b2ae4595f7c2a45d9f3 Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 30 Jan 2022 22:43:43 +0100 Subject: [PATCH 3/3] Refactor Card and StartStack --- src/main/java/solitaer/Card.java | 12 +++++++++++- src/main/java/solitaer/StartStack.java | 9 +++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/solitaer/Card.java b/src/main/java/solitaer/Card.java index af55cfc..5cad36d 100644 --- a/src/main/java/solitaer/Card.java +++ b/src/main/java/solitaer/Card.java @@ -39,6 +39,16 @@ public class Card extends JPanel { public int getNr() { return nr; } + + public boolean isred() { + return this.symbol == Symbol.Herz + || this.symbol == Symbol.Karo; + } + + public boolean isblack() { + return this.symbol == Symbol.Kreuz + || this.symbol == Symbol.Pik; + } private String getNrText() { if (this.nr < 10) { @@ -107,7 +117,7 @@ public class Card extends JPanel { g.setColor(Color.black); g.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1); - if (this.symbol == Symbol.Herz || this.symbol == Symbol.Karo) + if (this.isred()) g.setColor(Color.red); else g.setColor(Color.black); diff --git a/src/main/java/solitaer/StartStack.java b/src/main/java/solitaer/StartStack.java index 0dd2f8f..9c66b1d 100644 --- a/src/main/java/solitaer/StartStack.java +++ b/src/main/java/solitaer/StartStack.java @@ -26,14 +26,11 @@ public class StartStack extends BaseStack { return false; } else { return (// Nummer nur aufsteigend - _topStack.getNr() == _playerCard.getNr() + 1) && + (_topStack.getNr() == _playerCard.getNr() + 1) && // rot auf Schwarz - (((_topStack.getSymbol() == Symbol.Herz || _topStack.getSymbol() == Symbol.Karo) - && (_playerCard.getSymbol() == Symbol.Pik || _playerCard.getSymbol() == Symbol.Kreuz)) || + ((_topStack.isred() && _playerCard.isblack()) || // schwarz auf rot - ((_topStack.getSymbol() == Symbol.Pik || _topStack.getSymbol() == Symbol.Kreuz) - && (_playerCard.getSymbol() == Symbol.Herz - || _playerCard.getSymbol() == Symbol.Karo))); + (_topStack.isblack() && _playerCard.isred()))); } }