diff --git a/src/main/java/solitaer/AblageStack.java b/src/main/java/solitaer/AblageStack.java new file mode 100644 index 0000000..c96818b --- /dev/null +++ b/src/main/java/solitaer/AblageStack.java @@ -0,0 +1,63 @@ +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; + private Symbol sym; + + public AblageStack(int _x, int _y, Symbol _sym) { + super(false); + 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) { + 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; + } + + } + + @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/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/MyMouseListener.java b/src/main/java/solitaer/MyMouseListener.java index 4e9a08a..ee54111 100644 --- a/src/main/java/solitaer/MyMouseListener.java +++ b/src/main/java/solitaer/MyMouseListener.java @@ -58,29 +58,63 @@ 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())) { + clearSaveStack(tempStack); + } else { + clearSaveStack(selectedStack); + } + + 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()); - } + //Clear Save + clearSaveStack(selectedStack); } - - selectedStack = null; } + } } + 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 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/SolitaerGamePanel.java b/src/main/java/solitaer/SolitaerGamePanel.java index d94b99c..4c16f06 100644 --- a/src/main/java/solitaer/SolitaerGamePanel.java +++ b/src/main/java/solitaer/SolitaerGamePanel.java @@ -25,10 +25,8 @@ public class SolitaerGamePanel extends JPanel { - StartStack a = new StartStack(25,25); - - a.addCard(deck.getDeck().get(0)); - + StartStack a = new StartStack(25,125); + deck.getDeck().get(5).setFaceUp(true); a.addCard((deck.getDeck().get(5))); @@ -44,24 +42,40 @@ 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,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))); + 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); + 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); + } 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); diff --git a/src/test/java/solitaer/AblageStackTest.java b/src/test/java/solitaer/AblageStackTest.java new file mode 100644 index 0000000..6059ac1 --- /dev/null +++ b/src/test/java/solitaer/AblageStackTest.java @@ -0,0 +1,46 @@ +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 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 Kreuz-Feld", + null, + 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) + ); + } + +}