Browse Source

Merge commit '3ad5083c354c4999ba1c9b2ae4595f7c2a45d9f3' into HEAD

solitaer_Game
Jenkins 3 years ago
parent
commit
cd2e0ac545
  1. 12
      src/main/java/solitaer/Card.java
  2. 58
      src/main/java/solitaer/MyMouseListener.java
  3. 4
      src/main/java/solitaer/SolitaerGamePanel.java
  4. 33
      src/main/java/solitaer/StartStack.java
  5. 5
      src/test/java/solitaer/StartStackTest.java

12
src/main/java/solitaer/Card.java

@ -39,6 +39,16 @@ public class Card extends JPanel {
public int getNr() { public int getNr() {
return nr; 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() { private String getNrText() {
if (this.nr < 10) { if (this.nr < 10) {
@ -107,7 +117,7 @@ public class Card extends JPanel {
g.setColor(Color.black); g.setColor(Color.black);
g.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1); 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); g.setColor(Color.red);
else else
g.setColor(Color.black); g.setColor(Color.black);

58
src/main/java/solitaer/MyMouseListener.java

@ -30,37 +30,49 @@ public class MyMouseListener implements MouseInputListener {
System.out.println("DOWN"); System.out.println("DOWN");
Component com = SwingUtilities.getDeepestComponentAt(e.getComponent(), e.getX(), e.getY()); 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()) { 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 // saveStack is NOT empty
else { else {
if(com instanceof StartStack) {
tempStack = (StartStack) com;
} else {
tempStack = (StartStack) com.getParent();
}
if (selectedStack == null) { if (selectedStack == null) {
System.out.println("ERROR selectedStack in StartStack"); System.out.println("ERROR selectedStack in StartStack");
} }

4
src/main/java/solitaer/SolitaerGamePanel.java

@ -55,8 +55,8 @@ public class SolitaerGamePanel extends JPanel {
StartStack c = new StartStack(375,125); 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); this.add(c);

33
src/main/java/solitaer/StartStack.java

@ -3,11 +3,10 @@ package solitaer;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
public class StartStack extends BaseStack { public class StartStack extends BaseStack {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public StartStack(int _x, int _y) { public StartStack(int _x, int _y) {
super(true); super(true);
super.setLocation(_x, _y); super.setLocation(_x, _y);
@ -17,30 +16,24 @@ public class StartStack extends BaseStack {
} }
public boolean cardCheck(Card _topStack, Card _playerCard) { public boolean cardCheck(Card _topStack, Card _playerCard) {
if(_topStack == null && _playerCard == null) {
if (_topStack == null && _playerCard == null) {
return false; return false;
}
}
if (_topStack == null) { if (_topStack == null) {
// Nur der König kann auf einen leeren Stapel gelegt werden // Nur der König kann auf einen leeren Stapel gelegt werden
return _playerCard.getNr() == 13; return _playerCard.getNr() == 13;
}else {
} else if (!_topStack.isFaceUp()) {
return false;
} else {
return (// Nummer nur aufsteigend 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.isred() && _playerCard.isblack()) ||
// schwarz auf rot // schwarz auf rot
((_topStack.getSymbol() == Symbol.Pik || _topStack.getSymbol() == Symbol.Kreuz)
&& (_playerCard.getSymbol() == Symbol.Herz || _playerCard.getSymbol() == Symbol.Karo))
);
(_topStack.isblack() && _playerCard.isred())));
} }
} }
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
@ -48,12 +41,10 @@ public class StartStack extends BaseStack {
if (stackIsEmpty()) { if (stackIsEmpty()) {
g.setColor(Color.gray); g.setColor(Color.gray);
g.fillRect(0, 0, this.getWidth(), IMAGE_HIGHT); g.fillRect(0, 0, this.getWidth(), IMAGE_HIGHT);
g.setColor(Color.black); g.setColor(Color.black);
g.drawRect(0, 0, this.getWidth()-1, IMAGE_HIGHT-1);
g.drawRect(0, 0, this.getWidth() - 1, IMAGE_HIGHT - 1);
} }
} }
} }

5
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("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("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("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)
); );
} }

Loading…
Cancel
Save