Browse Source

Merge commit '24f874a09d420b4b441b2c610319a6420a35b6bc' into HEAD

solitaer_Game
Jenkins 3 years ago
parent
commit
fed92c15c3
  1. 63
      src/main/java/solitaer/AblageStack.java
  2. 12
      src/main/java/solitaer/BaseStack.java
  3. 54
      src/main/java/solitaer/MyMouseListener.java
  4. 2
      src/main/java/solitaer/SaveStack.java
  5. 28
      src/main/java/solitaer/SolitaerGamePanel.java
  6. 2
      src/main/java/solitaer/StartStack.java
  7. 46
      src/test/java/solitaer/AblageStackTest.java

63
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);
}
}
}

12
src/main/java/solitaer/BaseStack.java

@ -13,9 +13,15 @@ public abstract class BaseStack extends JPanel {
private ArrayList<Card> stack = new ArrayList<Card>(); private ArrayList<Card> stack = new ArrayList<Card>();
private final int OFFSET_VALUE = 20; private final int OFFSET_VALUE = 20;
int offset; int offset;
int addOffset;
public BaseStack() {
public BaseStack(boolean _offset) {
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
if(_offset) {
addOffset = OFFSET_VALUE;
}else {
addOffset = 0;
}
offset = 0; offset = 0;
} }
@ -43,13 +49,13 @@ public abstract class BaseStack extends JPanel {
_card.setLocation(0, offset); _card.setLocation(0, offset);
stack.add(_card); stack.add(_card);
RenderStackNew(); RenderStackNew();
offset += OFFSET_VALUE;
offset += addOffset;
} }
public Card removeLastCard() { public Card removeLastCard() {
if (stack.size() != 0) { if (stack.size() != 0) {
Card LastCard = stack.remove(stack.size() - 1); Card LastCard = stack.remove(stack.size() - 1);
offset -= OFFSET_VALUE;
offset -= addOffset;
RenderStackNew(); RenderStackNew();
return LastCard; return LastCard;
} }

54
src/main/java/solitaer/MyMouseListener.java

@ -59,28 +59,62 @@ public class MyMouseListener implements MouseInputListener {
} }
} }
//saveStack is NOT empty
// saveStack is NOT empty
else { else {
if(selectedStack == null) {
if (selectedStack == null) {
System.out.println("ERROR selectedStack in StartStack"); System.out.println("ERROR selectedStack in StartStack");
} }
if(tempStack.cardCheck(tempStack.peekLastCard(), saveStack.peekLastCard())) {
while(!saveStack.stackIsEmpty()) {
tempStack.addCard(saveStack.removeLastCard());
}
if (tempStack.cardCheck(tempStack.peekLastCard(), saveStack.peekLastCard())) {
clearSaveStack(tempStack);
} else { } else {
//Alles wieder auf den Ursprünglichen Stack packen
while(!saveStack.stackIsEmpty()) {
selectedStack.addCard(saveStack.removeLastCard());
}
clearSaveStack(selectedStack);
} }
selectedStack = null; 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 {
//Clear Save
clearSaveStack(selectedStack);
}
}
}
} }
private void clearSaveStack(BaseStack _ziel) {
// Alles wieder auf den Ursprünglichen Stack packen
while (!saveStack.stackIsEmpty()) {
_ziel.addCard(saveStack.removeLastCard());
}
}
@Override @Override
public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub // TODO Auto-generated method stub

2
src/main/java/solitaer/SaveStack.java

@ -9,7 +9,7 @@ public class SaveStack extends BaseStack {
public SaveStack(int _x, int _y) { public SaveStack(int _x, int _y) {
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
super();
super(true);
super.setLocation(_x, _y); super.setLocation(_x, _y);
super.setSize(IMAGE_WIDTH, STACK_HIGHT); super.setSize(IMAGE_WIDTH, STACK_HIGHT);
super.setOpaque(false); super.setOpaque(false);

28
src/main/java/solitaer/SolitaerGamePanel.java

@ -25,9 +25,7 @@ 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); deck.getDeck().get(5).setFaceUp(true);
a.addCard((deck.getDeck().get(5))); a.addCard((deck.getDeck().get(5)));
@ -44,24 +42,40 @@ public class SolitaerGamePanel extends JPanel {
deck.getDeck().get(1).setFaceUp(true); deck.getDeck().get(1).setFaceUp(true);
a.addCard((deck.getDeck().get(1))); a.addCard((deck.getDeck().get(1)));
deck.getDeck().get(0).setFaceUp(true);
a.addCard(deck.getDeck().get(0));
this.add(a); this.add(a);
StartStack b = new StartStack(200,25);
StartStack b = new StartStack(200,125);
deck.getDeck().get(32).setFaceUp(true); deck.getDeck().get(32).setFaceUp(true);
b.addCard((deck.getDeck().get(32))); b.addCard((deck.getDeck().get(32)));
this.add(b); 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); this.add(c);
StartStack d = new StartStack(475,125);
this.add(d);
//save = new SaveStack(500,250); //save = new SaveStack(500,250);
this.add(save); 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);
} }

2
src/main/java/solitaer/StartStack.java

@ -9,7 +9,7 @@ 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();
super(true);
super.setLocation(_x, _y); super.setLocation(_x, _y);
super.setSize(IMAGE_WIDTH, STACK_HIGHT); super.setSize(IMAGE_WIDTH, STACK_HIGHT);
super.setOpaque(false); super.setOpaque(false);

46
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<Arguments> 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)
);
}
}
Loading…
Cancel
Save