From cc51b3fa681756768b2a561b4642d8b6fed0d167 Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Thu, 27 Jan 2022 22:36:28 +0100 Subject: [PATCH] Move Cards between StartStack with Mouse --- src/main/java/solitaer/BaseStack.java | 31 +++++--- src/main/java/solitaer/MyMouseListener.java | 73 +++++++++++-------- src/main/java/solitaer/SolitaerGamePanel.java | 26 ++++++- 3 files changed, 89 insertions(+), 41 deletions(-) diff --git a/src/main/java/solitaer/BaseStack.java b/src/main/java/solitaer/BaseStack.java index aabbc6f..e8064d0 100644 --- a/src/main/java/solitaer/BaseStack.java +++ b/src/main/java/solitaer/BaseStack.java @@ -1,5 +1,7 @@ package solitaer; +import java.lang.invoke.TypeDescriptor.OfField; +import java.time.OffsetDateTime; import java.util.ArrayList; import javax.swing.JPanel; @@ -25,7 +27,14 @@ public abstract class BaseStack extends JPanel { } else { return null; } - + } + + public int indexOfCard(Card _card) { + return stack.indexOf(_card); + } + + public int stackSize() { + return stack.size(); } public void addCard(Card _card) { @@ -35,23 +44,23 @@ public abstract class BaseStack extends JPanel { offset += OFFSET_VALUE; } - private void RenderStackNew() { - this.removeAll(); - // Alles neu Zeichnen - for (int i = stack.size() - 1; i >= 0; i--) { - this.add(stack.get(i)); - } - this.repaint(); - } - public Card removeLastCard() { if (stack.size() != 0) { Card LastCard = stack.remove(stack.size() - 1); + offset -= OFFSET_VALUE; RenderStackNew(); return LastCard; } return null; - + } + + private void RenderStackNew() { + this.removeAll(); + // Alles neu Zeichnen + for (int i = stack.size() - 1; i >= 0; i--) { + this.add(stack.get(i)); + } + this.repaint(); } protected abstract boolean cardCheck(Card _topStack, Card _playerCard); diff --git a/src/main/java/solitaer/MyMouseListener.java b/src/main/java/solitaer/MyMouseListener.java index 140c2a9..4ca99b8 100644 --- a/src/main/java/solitaer/MyMouseListener.java +++ b/src/main/java/solitaer/MyMouseListener.java @@ -7,11 +7,12 @@ import javax.swing.SwingUtilities; import javax.swing.event.MouseInputListener; public class MyMouseListener implements MouseInputListener { - + private SaveStack save; private BaseStack tempStack; + private BaseStack selectedStack; private Card tempCard; - + public MyMouseListener(SaveStack _save) { // TODO Auto-generated constructor stub this.save = _save; @@ -27,41 +28,55 @@ public class MyMouseListener implements MouseInputListener { public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub System.out.println("DOWN"); - Component com = SwingUtilities.getDeepestComponentAt( - e.getComponent(), e.getX(), e.getY()); -// System.out.println("########"); -// System.out.println(com.toString()); -// System.out.println(com.getParent().toString()); -// System.out.println("########"); - - //Leeres StartFeld + 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) { + // Karte, die in einem StartStack liegt + else 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(); - System.out.println("Turn"+tempCard.toString()); + + if (save.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) { + save.addCard(tempStack.removeLastCard()); + } + selectedStack = tempStack; + + } + } + //saveStack is NOT empty + else { + if(selectedStack == null) { + System.out.println("ERROR selectedStack in StartStack"); + } + if(tempStack.cardCheck(tempStack.peekLastCard(), save.peekLastCard())) { + while(!save.stackIsEmpty()) { + tempStack.addCard(save.removeLastCard()); + } + } else { + //Alles wieder auf den Ursprünglichen Stack packen + while(!save.stackIsEmpty()) { + selectedStack.addCard(save.removeLastCard()); + } } - + selectedStack = null; } - - - //System.out.println("Karte in StartStack"+com.toString()); - - - - - //save.addCard(((BaseStack)com.getParent()).removeLastCard());; } } diff --git a/src/main/java/solitaer/SolitaerGamePanel.java b/src/main/java/solitaer/SolitaerGamePanel.java index 14ab7cf..d94b99c 100644 --- a/src/main/java/solitaer/SolitaerGamePanel.java +++ b/src/main/java/solitaer/SolitaerGamePanel.java @@ -26,14 +26,38 @@ public class SolitaerGamePanel extends JPanel { StartStack a = new StartStack(25,25); + a.addCard(deck.getDeck().get(0)); - deck.getDeck().get(1).setFaceUp(false); + + deck.getDeck().get(5).setFaceUp(true); + a.addCard((deck.getDeck().get(5))); + + deck.getDeck().get(4).setFaceUp(true); + a.addCard((deck.getDeck().get(4))); + + deck.getDeck().get(3).setFaceUp(true); + a.addCard((deck.getDeck().get(3))); + + deck.getDeck().get(2).setFaceUp(true); + a.addCard((deck.getDeck().get(2))); + + deck.getDeck().get(1).setFaceUp(true); a.addCard((deck.getDeck().get(1))); + this.add(a); StartStack b = new StartStack(200,25); + + deck.getDeck().get(32).setFaceUp(true); + b.addCard((deck.getDeck().get(32))); this.add(b); + StartStack c = new StartStack(375,25); + + deck.getDeck().get(33).setFaceUp(true); + c.addCard((deck.getDeck().get(33))); + this.add(c); + //save = new SaveStack(500,250); this.add(save);