Browse Source

Refactor Card and StartStack

feature_solitaer_StartStack
Alexander Hartung 3 years ago
parent
commit
67d9ada932
  1. 10
      src/main/java/solitaer/Card.java
  2. 12
      src/main/java/solitaer/StartStack.java
  3. 9
      src/test/java/solitaer/StartStackTest.java

10
src/main/java/solitaer/Card.java

@ -14,9 +14,6 @@ enum Symbol {
public class Card extends JPanel { public class Card extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private int nr; private int nr;
private Symbol symbol; private Symbol symbol;
@ -26,8 +23,11 @@ public class Card extends JPanel {
private Image Up; private Image Up;
private Image back; private Image back;
private final int IMAGE_WIDTH = 72;
private final int IMAGE_HIGHT = 96;
public Card(int nr, Symbol symbol, boolean faceUp) { public Card(int nr, Symbol symbol, boolean faceUp) {
this.setSize(72,96);
this.setSize(IMAGE_WIDTH,IMAGE_HIGHT);
this.nr = nr; this.nr = nr;
this.symbol = symbol; this.symbol = symbol;
this.faceUp = faceUp; this.faceUp = faceUp;
@ -75,7 +75,7 @@ public class Card extends JPanel {
case Kreuz: case Kreuz:
return "Kreuz"; return "Kreuz";
default: default:
return null;
return "";
} }
} }

12
src/main/java/solitaer/StartStack.java

@ -12,12 +12,16 @@ public class StartStack extends JPanel {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private Image im; private Image im;
private ArrayList<Card> stack = new ArrayList<Card>(); private ArrayList<Card> stack = new ArrayList<Card>();
private final int OFFSET_VALUE = 20;
private final int IMAGE_WIDTH = 72;
private final int IMAGE_HIGHT = 450;
private int offset; private int offset;
public StartStack(int _x, int _y) { public StartStack(int _x, int _y) {
super.setLocation(_x, _y); super.setLocation(_x, _y);
super.setSize(72, 450);
super.setSize(IMAGE_WIDTH, IMAGE_HIGHT);
super.setOpaque(false); super.setOpaque(false);
super.setLayout(null); super.setLayout(null);
@ -51,12 +55,12 @@ public class StartStack extends JPanel {
_card.setLocation(0, offset); _card.setLocation(0, offset);
stack.add(_card); stack.add(_card);
addtoPanel(); addtoPanel();
offset += 20;
offset += OFFSET_VALUE;
} }
private void addtoPanel() { private void addtoPanel() {
this.removeAll(); this.removeAll();
//Alles neu Zeichnen
for (int i = stack.size() - 1; i >= 0; i--) { for (int i = stack.size() - 1; i >= 0; i--) {
this.add(stack.get(i)); this.add(stack.get(i));
} }
@ -67,9 +71,7 @@ public class StartStack extends JPanel {
// TODO Auto-generated method stub // TODO Auto-generated method stub
super.paintComponent(g); super.paintComponent(g);
if (stack.size() == 0) { if (stack.size() == 0) {
//Arcade64\src\main\java\solitaer\images
ImageIcon ii = new ImageIcon(getClass().getResource("images/empty.png")); ImageIcon ii = new ImageIcon(getClass().getResource("images/empty.png"));
//ImageIcon ii = new ImageIcon("C:/Users/Alexander/eclipse-workspace/Arcade64/src/main/java/solitaer/images/empty.png");
im = ii.getImage(); im = ii.getImage();
g.drawImage(im, 0, 0, this.getWidth(), 96, this); g.drawImage(im, 0, 0, this.getWidth(), 96, this);
} }

9
src/test/java/solitaer/StartStackTest.java

@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.*;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
@ -16,8 +15,6 @@ class StartStackTest {
//Jedes Symbol muss einmal vorhand sein //Jedes Symbol muss einmal vorhand sein
void testRulesetStartStack(String msg, Card _stapel, Card _spieler, boolean _expectedResult) { void testRulesetStartStack(String msg, Card _stapel, Card _spieler, boolean _expectedResult) {
StartStack stack = new StartStack(10, 10); StartStack stack = new StartStack(10, 10);
assertEquals(_expectedResult, stack.cardCheck(_stapel, _spieler), msg); assertEquals(_expectedResult, stack.cardCheck(_stapel, _spieler), msg);
} }
@ -25,7 +22,8 @@ class StartStackTest {
private static Stream<Arguments> testStartStackTestDaten(){ private static Stream<Arguments> testStartStackTestDaten(){
return Stream.of( return Stream.of(
Arguments.of("Ass-Kreuz auf 2-Herz", new Card(2, Symbol.Herz, true), new Card(1, Symbol.Kreuz, true), true), Arguments.of("Ass-Kreuz auf 2-Herz", new Card(2, Symbol.Herz, true), new Card(1, Symbol.Kreuz, true), true),
Arguments.of("Ass-Herz auf 2Herz", new Card(1, Symbol.Herz, true), new Card(2, Symbol.Herz, true), false),
Arguments.of("Ass-Herz auf 2-Herz", new Card(2, Symbol.Herz, true), new Card(1, Symbol.Herz, true), false),
Arguments.of("Ass-Herz auf 2-Herz", new Card(1, Symbol.Herz, true), new Card(2, Symbol.Herz, true), false),
Arguments.of("Ass-Kreuz auf leeres Feld", null, new Card(1, Symbol.Kreuz, true), false), Arguments.of("Ass-Kreuz auf leeres Feld", null, new Card(1, Symbol.Kreuz, true), false),
Arguments.of("König auf leeres Feld", null, new Card(13, Symbol.Kreuz, true), true), Arguments.of("König auf leeres Feld", null, new Card(13, Symbol.Kreuz, true), true),
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),
@ -33,10 +31,7 @@ class StartStackTest {
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("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("null auf null", null, null, false)
); );
} }
} }
Loading…
Cancel
Save