Jenkins
3 years ago
18 changed files with 1156 additions and 0 deletions
-
22.gitignore
-
28src/main/java/hauptmenue/GameWindow.java
-
10src/main/java/hauptmenue/main.java
-
63src/main/java/solitaer/AblageStack.java
-
81src/main/java/solitaer/BaseStack.java
-
136src/main/java/solitaer/Card.java
-
42src/main/java/solitaer/CardDeck.java
-
66src/main/java/solitaer/GeberStack.java
-
199src/main/java/solitaer/MyMouseListener.java
-
38src/main/java/solitaer/SaveStack.java
-
140src/main/java/solitaer/SolitaerGamePanel.java
-
53src/main/java/solitaer/SolitaerMenue.java
-
50src/main/java/solitaer/StartStack.java
-
38src/main/java/solitaer/WasteStack.java
-
46src/test/java/solitaer/AblageStackTest.java
-
81src/test/java/solitaer/CardDeckTest.java
-
25src/test/java/solitaer/GeberStackTest.java
-
38src/test/java/solitaer/StartStackTest.java
@ -0,0 +1,28 @@ |
|||
package hauptmenue; |
|||
|
|||
import java.awt.Dimension; |
|||
|
|||
import javax.swing.JFrame; |
|||
|
|||
import solitaer.SolitaerGamePanel; |
|||
import solitaer.SolitaerMenue; |
|||
|
|||
public class GameWindow extends JFrame { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
private final SolitaerGamePanel gamePanel = new SolitaerGamePanel(); |
|||
|
|||
private SolitaerMenue smenue = new SolitaerMenue(gamePanel); |
|||
|
|||
public GameWindow() { |
|||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|||
gamePanel.setPreferredSize(new Dimension(1180, 780)); |
|||
this.setJMenuBar(smenue.getMenue()); |
|||
|
|||
add(gamePanel); |
|||
pack(); |
|||
|
|||
setVisible(true); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,10 @@ |
|||
package hauptmenue; |
|||
|
|||
public class main { |
|||
|
|||
public static void main(String[] args) { |
|||
// TODO Auto-generated method stub |
|||
new GameWindow(); |
|||
} |
|||
|
|||
} |
@ -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); |
|||
} |
|||
} |
|||
|
|||
} |
@ -0,0 +1,81 @@ |
|||
package solitaer; |
|||
|
|||
import java.util.ArrayList; |
|||
|
|||
import javax.swing.JPanel; |
|||
|
|||
public abstract class BaseStack extends JPanel { |
|||
public final int IMAGE_WIDTH = 72; |
|||
public final int IMAGE_HIGHT = 96; |
|||
public final int STACK_HIGHT = 450; |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
private ArrayList<Card> stack = new ArrayList<Card>(); |
|||
private final int OFFSET_VALUE = 20; |
|||
private int offset; |
|||
private int addOffset; |
|||
|
|||
public BaseStack(boolean _offset) { |
|||
// TODO Auto-generated constructor stub |
|||
if(_offset) { |
|||
addOffset = OFFSET_VALUE; |
|||
}else { |
|||
addOffset = 0; |
|||
} |
|||
offset = 0; |
|||
} |
|||
|
|||
public void clearStack() { |
|||
stack.clear(); |
|||
offset = 0; |
|||
RenderStackNew(); |
|||
} |
|||
|
|||
public boolean stackIsEmpty() { |
|||
return stack.isEmpty(); |
|||
} |
|||
|
|||
public Card peekLastCard() { |
|||
if(!stackIsEmpty()) { |
|||
return stack.get(stack.size()-1); |
|||
} else { |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public int indexOfCard(Card _card) { |
|||
return stack.indexOf(_card); |
|||
} |
|||
|
|||
public int stackSize() { |
|||
return stack.size(); |
|||
} |
|||
|
|||
public void addCard(Card _card) { |
|||
_card.setLocation(0, offset); |
|||
stack.add(_card); |
|||
RenderStackNew(); |
|||
offset += addOffset; |
|||
} |
|||
|
|||
public Card removeLastCard() { |
|||
if (stack.size() != 0) { |
|||
Card LastCard = stack.remove(stack.size() - 1); |
|||
offset -= addOffset; |
|||
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); |
|||
} |
@ -0,0 +1,136 @@ |
|||
package solitaer; |
|||
|
|||
import java.awt.Color; |
|||
import java.awt.Graphics; |
|||
import java.util.Objects; |
|||
|
|||
import javax.swing.JPanel; |
|||
|
|||
enum Symbol { |
|||
Herz, Karo, Kreuz, Pik |
|||
} |
|||
|
|||
public class Card extends JPanel { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
private int nr; |
|||
private Symbol symbol; |
|||
|
|||
private boolean faceUp; |
|||
|
|||
private final int IMAGE_WIDTH = 72; |
|||
private final int IMAGE_HIGHT = 96; |
|||
|
|||
public Card(int _nr, Symbol _symbol, boolean _faceUp) { |
|||
this.setSize(IMAGE_WIDTH,IMAGE_HIGHT); |
|||
this.nr = _nr; |
|||
this.symbol = _symbol; |
|||
this.faceUp = _faceUp; |
|||
} |
|||
|
|||
public boolean isFaceUp() { |
|||
return faceUp; |
|||
} |
|||
|
|||
public void setFaceUp(boolean _faceUp) { |
|||
this.faceUp = _faceUp; |
|||
} |
|||
|
|||
public int getNr() { |
|||
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() { |
|||
if (this.nr < 10) { |
|||
return "0" + this.nr; |
|||
} else { |
|||
return "" + this.nr; |
|||
} |
|||
} |
|||
|
|||
public Symbol getSymbol() { |
|||
return symbol; |
|||
} |
|||
|
|||
private String getSymboleText() { |
|||
switch (this.symbol) { |
|||
case Herz: |
|||
return "Herz"; |
|||
case Karo: |
|||
return "Karo"; |
|||
case Pik: |
|||
return "Pik"; |
|||
case Kreuz: |
|||
return "Kreuz"; |
|||
default: |
|||
return ""; |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public String toString() { |
|||
//return "Card [nr=" + nr + ", symbol=" + symbol + ", faceUp=" + faceUp + "]"; |
|||
return "Card NR="+nr+" | Symbole="+symbol+" | Face="+faceUp; |
|||
} |
|||
|
|||
@Override |
|||
public int hashCode() { |
|||
return Objects.hash(faceUp, nr, symbol); |
|||
} |
|||
|
|||
@Override |
|||
public boolean equals(Object obj) { |
|||
if (this == obj) |
|||
return true; |
|||
if (obj == null) |
|||
return false; |
|||
if (getClass() != obj.getClass()) |
|||
return false; |
|||
Card other = (Card) obj; |
|||
return faceUp == other.faceUp && nr == other.nr && symbol == other.symbol; |
|||
} |
|||
|
|||
@Override |
|||
protected void paintComponent(Graphics g) { |
|||
// TODO Auto-generated method stub |
|||
super.paintComponent(g); |
|||
paintCard(g); |
|||
|
|||
} |
|||
|
|||
|
|||
private void paintCard(Graphics g) { |
|||
if (faceUp) { |
|||
g.setColor(Color.white); |
|||
g.fillRect(0, 0, this.getWidth(), this.getHeight()); |
|||
|
|||
g.setColor(Color.black); |
|||
g.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1); |
|||
|
|||
if (this.isred()) |
|||
g.setColor(Color.red); |
|||
else |
|||
g.setColor(Color.black); |
|||
|
|||
String text = "" + this.getNrText() + " " + this.getSymboleText(); |
|||
g.drawString(text, 3, 15); |
|||
} else { |
|||
g.setColor(Color.blue); |
|||
g.fillRect(0, 0, this.getWidth(), this.getHeight()); |
|||
|
|||
g.setColor(Color.black); |
|||
g.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1); |
|||
} |
|||
} |
|||
|
|||
} |
@ -0,0 +1,42 @@ |
|||
package solitaer; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.Collections; |
|||
|
|||
public class CardDeck { |
|||
|
|||
private ArrayList<Card> deck = new ArrayList<Card>(); |
|||
|
|||
public CardDeck() { |
|||
for (int symboleZähler = 0; symboleZähler < 4; symboleZähler++) { |
|||
for (int nrZähler = 1; nrZähler <= 13; nrZähler++) { |
|||
switch (symboleZähler) { |
|||
case 0: |
|||
deck.add(new Card(nrZähler, Symbol.Herz, false)); |
|||
break; |
|||
case 1: |
|||
deck.add(new Card(nrZähler, Symbol.Karo, false)); |
|||
break; |
|||
case 2: |
|||
deck.add(new Card(nrZähler, Symbol.Kreuz, false)); |
|||
break; |
|||
case 3: |
|||
deck.add(new Card(nrZähler, Symbol.Pik, false)); |
|||
break; |
|||
} |
|||
|
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
public ArrayList<Card> getDeck() { |
|||
return deck; |
|||
} |
|||
|
|||
|
|||
public void shuffelDeck() { |
|||
Collections.shuffle(deck); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,66 @@ |
|||
package solitaer; |
|||
|
|||
import java.awt.Color; |
|||
import java.awt.Graphics; |
|||
|
|||
public class GeberStack extends BaseStack { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
private WasteStack wasteStack; |
|||
|
|||
public GeberStack(int _x, int _y, WasteStack _waste) { |
|||
super(false); |
|||
super.setLocation(_x, _y); |
|||
super.setSize(IMAGE_WIDTH, STACK_HIGHT); |
|||
super.setOpaque(false); |
|||
super.setLayout(null); |
|||
|
|||
this.wasteStack = _waste; |
|||
} |
|||
|
|||
public WasteStack getWastStack() { |
|||
return wasteStack; |
|||
} |
|||
|
|||
public void swapWasteGeber() { |
|||
while(wasteStack.stackIsEmpty() == false) { |
|||
moveCardTurnFace(wasteStack, this, false); |
|||
} |
|||
} |
|||
|
|||
public void moveToWaste() { |
|||
moveCardTurnFace(this, wasteStack, true); |
|||
} |
|||
|
|||
private void moveCardTurnFace(BaseStack _removeStack, BaseStack _addStack, boolean _face) { |
|||
Card card = _removeStack.removeLastCard(); |
|||
card.setFaceUp(_face); |
|||
_addStack.addCard(card); |
|||
|
|||
} |
|||
|
|||
|
|||
@Override |
|||
protected boolean cardCheck(Card _topStack, Card _playerCard) { |
|||
// TODO Auto-generated method stub |
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
protected void paintComponent(Graphics g) { |
|||
// TODO Auto-generated method stub |
|||
super.paintComponent(g); |
|||
if (stackIsEmpty()) { |
|||
g.setColor(Color.pink); |
|||
g.fillRect(0, 0, this.getWidth(), IMAGE_HIGHT); |
|||
|
|||
g.setColor(Color.black); |
|||
g.drawRect(0, 0, this.getWidth() - 1, IMAGE_HIGHT - 1); |
|||
|
|||
String text = "Start\nAgain"; |
|||
g.drawString(text, 5, this.IMAGE_HIGHT/2); |
|||
} |
|||
} |
|||
|
|||
} |
@ -0,0 +1,199 @@ |
|||
package solitaer; |
|||
|
|||
import java.awt.Component; |
|||
import java.awt.event.MouseEvent; |
|||
|
|||
import javax.swing.SwingUtilities; |
|||
import javax.swing.event.MouseInputListener; |
|||
|
|||
public class MyMouseListener implements MouseInputListener { |
|||
|
|||
private SaveStack saveStack; |
|||
private BaseStack tempStack; |
|||
private BaseStack selectedStack; |
|||
private Card tempCard; |
|||
|
|||
public MyMouseListener(SaveStack _save) { |
|||
// TODO Auto-generated constructor stub |
|||
this.saveStack = _save; |
|||
} |
|||
|
|||
@Override |
|||
public void mouseClicked(MouseEvent e) { |
|||
// TODO Auto-generated method stub |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void mousePressed(MouseEvent e) { |
|||
// TODO Auto-generated method stub |
|||
System.out.println("DOWN"); |
|||
Component com = SwingUtilities.getDeepestComponentAt(e.getComponent(), e.getX(), e.getY()); |
|||
|
|||
if ((com instanceof Card && com.getParent() instanceof StartStack) |
|||
|| (com instanceof StartStack && ((StartStack) com).stackIsEmpty())) { |
|||
|
|||
if (saveStack.stackIsEmpty()) { |
|||
|
|||
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(); |
|||
} |
|||
} |
|||
// 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 |
|||
else { |
|||
if(com instanceof StartStack) { |
|||
tempStack = (StartStack) com; |
|||
} else { |
|||
tempStack = (StartStack) com.getParent(); |
|||
} |
|||
|
|||
if (selectedStack == null) { |
|||
System.out.println("ERROR selectedStack in StartStack"); |
|||
} |
|||
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 { |
|||
// Clear Save |
|||
clearSaveStack(selectedStack); |
|||
} |
|||
} |
|||
|
|||
} |
|||
// GeberStack |
|||
else if (com.getParent() instanceof GeberStack || com instanceof GeberStack) { |
|||
System.out.println("GeberStack"); |
|||
if (com instanceof GeberStack) { |
|||
tempStack = (GeberStack) com; |
|||
} else { |
|||
tempStack = (GeberStack) com.getParent(); |
|||
} |
|||
|
|||
if(this.saveStack.stackIsEmpty() == false) { |
|||
clearSaveStack(selectedStack); |
|||
} |
|||
|
|||
if (tempStack.stackIsEmpty()) { |
|||
// Move all from Waste to Geber |
|||
((GeberStack) this.tempStack).swapWasteGeber(); |
|||
} else { |
|||
// Move next card to Waste |
|||
((GeberStack) this.tempStack).moveToWaste(); |
|||
} |
|||
} |
|||
// WasteStack |
|||
else if (com.getParent() instanceof WasteStack || com instanceof WasteStack) { |
|||
System.out.println("WasteStack"); |
|||
if (com instanceof WasteStack) { |
|||
tempStack = (WasteStack) com; |
|||
} else { |
|||
tempStack = (WasteStack) com.getParent(); |
|||
} |
|||
|
|||
if (saveStack.stackIsEmpty()) { |
|||
// Move Top Card from AblageStack in Save |
|||
if (tempStack.stackIsEmpty() == false) { |
|||
saveStack.addCard(tempStack.removeLastCard()); |
|||
selectedStack = tempStack; |
|||
} |
|||
|
|||
} else { |
|||
clearSaveStack(selectedStack); |
|||
} |
|||
//Clear SaveStack click on SolitaerGamePanel |
|||
}else if(com instanceof SolitaerGamePanel || |
|||
com.getParent() instanceof SolitaerGamePanel && com instanceof StartStack ){ |
|||
if(saveStack.stackIsEmpty() == false) { |
|||
clearSaveStack(selectedStack); |
|||
} |
|||
} |
|||
} |
|||
|
|||
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 |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void mouseEntered(MouseEvent e) { |
|||
// TODO Auto-generated method stub |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void mouseExited(MouseEvent e) { |
|||
// TODO Auto-generated method stub |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void mouseDragged(MouseEvent e) { |
|||
// TODO Auto-generated method stub |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void mouseMoved(MouseEvent e) { |
|||
// TODO Auto-generated method stub |
|||
|
|||
} |
|||
|
|||
} |
@ -0,0 +1,38 @@ |
|||
package solitaer; |
|||
|
|||
import java.awt.Color; |
|||
import java.awt.Graphics; |
|||
|
|||
public class SaveStack extends BaseStack { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
public SaveStack(int _x, int _y) { |
|||
// TODO Auto-generated constructor stub |
|||
super(true); |
|||
super.setLocation(_x, _y); |
|||
super.setSize(IMAGE_WIDTH, STACK_HIGHT); |
|||
super.setOpaque(false); |
|||
super.setLayout(null); |
|||
} |
|||
|
|||
@Override |
|||
protected boolean cardCheck(Card _topStack, Card _playerCard) { |
|||
// TODO Auto-generated method stub |
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
protected void paintComponent(Graphics g) { |
|||
// TODO Auto-generated method stub |
|||
super.paintComponent(g); |
|||
if (stackIsEmpty()) { |
|||
g.setColor(Color.pink); |
|||
g.fillRect(0, 0, this.getWidth(), IMAGE_HIGHT); |
|||
|
|||
g.setColor(Color.black); |
|||
g.drawRect(0, 0, this.getWidth() - 1, IMAGE_HIGHT - 1); |
|||
} |
|||
} |
|||
|
|||
} |
@ -0,0 +1,140 @@ |
|||
package solitaer; |
|||
|
|||
import java.awt.Color; |
|||
import java.awt.Graphics; |
|||
|
|||
import javax.swing.JLabel; |
|||
import javax.swing.JPanel; |
|||
|
|||
public class SolitaerGamePanel extends JPanel { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
private final int OFFSET_STACKS_X = 5; |
|||
private final int OFFSET_STACKS_Y = 15; |
|||
|
|||
public final int IMAGE_WIDTH = 72; |
|||
public final int IMAGE_HIGHT = 96; |
|||
|
|||
public final int STARTSTACK_SIZE = 7; |
|||
public final int ABLAGESTACK_SIZE = 4; |
|||
|
|||
private Color backgroundColor = new Color(0, 135, 62); |
|||
|
|||
private CardDeck deck; |
|||
private MyMouseListener mouseL; |
|||
|
|||
private SaveStack save; |
|||
private StartStack[] start = new StartStack[7]; |
|||
private AblageStack[] ablage = new AblageStack[4]; |
|||
|
|||
private WasteStack waste; |
|||
private GeberStack geber; |
|||
|
|||
public SolitaerGamePanel() { |
|||
setLayout(null); |
|||
|
|||
int Offset_x_Save = setLayoutX(STARTSTACK_SIZE + 1); |
|||
int Offset_y_Save = setLayouty(1); |
|||
|
|||
JLabel label = new JLabel(); |
|||
label.setText("selected Cards"); |
|||
label.setBounds(Offset_x_Save, Offset_y_Save - 25, 200, 25); |
|||
this.add(label); |
|||
|
|||
save = new SaveStack(Offset_x_Save, Offset_y_Save); |
|||
|
|||
mouseL = new MyMouseListener(save); |
|||
this.addMouseListener(mouseL); |
|||
|
|||
for (int i = 0; i < STARTSTACK_SIZE; i++) { |
|||
int y = setLayouty(1); |
|||
start[i] = new StartStack(setLayoutX(i), y); |
|||
} |
|||
|
|||
for (int i = 0; i < ABLAGESTACK_SIZE; i++) { |
|||
int x = setLayoutX(((STARTSTACK_SIZE - ABLAGESTACK_SIZE) + i)); |
|||
int y = setLayouty(0); |
|||
switch (i) { |
|||
case 0: |
|||
ablage[i] = new AblageStack(x, y, Symbol.Herz); |
|||
break; |
|||
case 1: |
|||
ablage[i] = new AblageStack(x, y, Symbol.Kreuz); |
|||
break; |
|||
case 2: |
|||
ablage[i] = new AblageStack(x, y, Symbol.Karo); |
|||
break; |
|||
case 3: |
|||
ablage[i] = new AblageStack(x, y, Symbol.Pik); |
|||
break; |
|||
|
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
|
|||
waste = new WasteStack(setLayoutX(1), OFFSET_STACKS_Y); |
|||
geber = new GeberStack(setLayoutX(0), OFFSET_STACKS_Y, waste); |
|||
|
|||
restartGame(); |
|||
|
|||
} |
|||
|
|||
private int setLayoutX(int _spalte) { |
|||
return (((IMAGE_WIDTH + OFFSET_STACKS_X) * _spalte) + OFFSET_STACKS_X); |
|||
} |
|||
|
|||
private int setLayouty(int _row) { |
|||
if (_row == 1) |
|||
return IMAGE_HIGHT + (OFFSET_STACKS_Y * 3); |
|||
else |
|||
return OFFSET_STACKS_Y; |
|||
} |
|||
|
|||
public void restartGame() { |
|||
deck = new CardDeck(); |
|||
deck.shuffelDeck(); |
|||
|
|||
for (int i = 0; i < STARTSTACK_SIZE; i++) { |
|||
start[i].clearStack(); |
|||
int tmp = i; |
|||
while (tmp != 0) { |
|||
start[i].addCard(deck.getDeck().remove(0)); |
|||
tmp -= 1; |
|||
} |
|||
Card card = deck.getDeck().remove(0); |
|||
card.setFaceUp(true); |
|||
start[i].addCard(card); |
|||
this.add(start[i]); |
|||
} |
|||
|
|||
for (int i = 0; i < ABLAGESTACK_SIZE; i++) { |
|||
ablage[i].clearStack(); |
|||
this.add(ablage[i]); |
|||
} |
|||
|
|||
geber.clearStack(); |
|||
waste.clearStack(); |
|||
while (deck.getDeck().size() != 1) { |
|||
geber.addCard(deck.getDeck().remove(0)); |
|||
} |
|||
Card card = deck.getDeck().remove(0); |
|||
card.setFaceUp(true); |
|||
waste.addCard(card); |
|||
|
|||
this.add(waste); |
|||
this.add(geber); |
|||
|
|||
save.clearStack(); |
|||
this.add(save); |
|||
} |
|||
|
|||
@Override |
|||
protected void paintComponent(Graphics g) { |
|||
// TODO Auto-generated method stub |
|||
super.paintComponent(g); |
|||
g.setColor(backgroundColor); |
|||
g.fillRect(0, 0, this.getWidth(), this.getHeight()); |
|||
} |
|||
} |
@ -0,0 +1,53 @@ |
|||
package solitaer; |
|||
|
|||
import java.awt.event.ActionEvent; |
|||
import java.awt.event.ActionListener; |
|||
|
|||
import javax.swing.JMenu; |
|||
import javax.swing.JMenuBar; |
|||
import javax.swing.JMenuItem; |
|||
|
|||
public class SolitaerMenue implements ActionListener{ |
|||
|
|||
SolitaerGamePanel solitaerpanel; |
|||
|
|||
JMenuBar menuBar = new JMenuBar(); |
|||
|
|||
JMenu menu = new JMenu("options"); |
|||
|
|||
JMenuItem restart = new JMenuItem("restart"); |
|||
JMenuItem quit = new JMenuItem("quit"); |
|||
|
|||
public SolitaerMenue(SolitaerGamePanel _panel) { |
|||
|
|||
solitaerpanel = _panel; |
|||
|
|||
this.restart.addActionListener(this); |
|||
this.menu.add(restart); |
|||
this.menu.addSeparator(); |
|||
this.quit.addActionListener(this); |
|||
this.menu.add(quit); |
|||
|
|||
this.menuBar.add(menu); |
|||
|
|||
} |
|||
|
|||
public JMenuBar getMenue() { |
|||
return menuBar; |
|||
} |
|||
|
|||
@Override |
|||
public void actionPerformed(ActionEvent e) { |
|||
// TODO Auto-generated method stub |
|||
if(e.getSource() == this.quit) { |
|||
System.out.println("quit"); |
|||
System.exit(0); |
|||
} |
|||
|
|||
if(e.getSource() == this.restart) { |
|||
solitaerpanel.restartGame(); |
|||
} |
|||
|
|||
} |
|||
|
|||
} |
@ -0,0 +1,50 @@ |
|||
package solitaer; |
|||
|
|||
import java.awt.Color; |
|||
import java.awt.Graphics; |
|||
|
|||
public class StartStack extends BaseStack { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
public StartStack(int _x, int _y) { |
|||
super(true); |
|||
super.setLocation(_x, _y); |
|||
super.setSize(IMAGE_WIDTH, STACK_HIGHT); |
|||
super.setOpaque(false); |
|||
super.setLayout(null); |
|||
} |
|||
|
|||
public boolean cardCheck(Card _topStack, Card _playerCard) { |
|||
if (_topStack == null && _playerCard == null) { |
|||
return false; |
|||
} |
|||
if (_topStack == null) { |
|||
// Nur der König kann auf einen leeren Stapel gelegt werden |
|||
return _playerCard.getNr() == 13; |
|||
} else if (!_topStack.isFaceUp()) { |
|||
return false; |
|||
} else { |
|||
return (// Nummer nur aufsteigend |
|||
(_topStack.getNr() == _playerCard.getNr() + 1) && |
|||
// rot auf Schwarz |
|||
((_topStack.isred() && _playerCard.isblack()) || |
|||
// schwarz auf rot |
|||
(_topStack.isblack() && _playerCard.isred()))); |
|||
} |
|||
} |
|||
|
|||
@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); |
|||
} |
|||
} |
|||
|
|||
} |
@ -0,0 +1,38 @@ |
|||
package solitaer; |
|||
|
|||
import java.awt.Color; |
|||
import java.awt.Graphics; |
|||
|
|||
public class WasteStack extends BaseStack { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
public WasteStack(int _x, int _y) { |
|||
super(false); |
|||
super.setLocation(_x, _y); |
|||
super.setSize(IMAGE_WIDTH, STACK_HIGHT); |
|||
super.setOpaque(false); |
|||
super.setLayout(null); |
|||
} |
|||
|
|||
@Override |
|||
protected boolean cardCheck(Card _topStack, Card _playerCard) { |
|||
// TODO Auto-generated method stub |
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
protected void paintComponent(Graphics g) { |
|||
// TODO Auto-generated method stub |
|||
super.paintComponent(g); |
|||
if (stackIsEmpty()) { |
|||
g.setColor(Color.lightGray); |
|||
g.fillRect(0, 0, this.getWidth(), IMAGE_HIGHT); |
|||
|
|||
g.setColor(Color.black); |
|||
g.drawRect(0, 0, this.getWidth() - 1, IMAGE_HIGHT - 1); |
|||
|
|||
} |
|||
} |
|||
|
|||
} |
@ -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) |
|||
); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,81 @@ |
|||
package solitaer; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
import java.util.stream.Stream; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
import org.junit.jupiter.params.ParameterizedTest; |
|||
import org.junit.jupiter.params.provider.Arguments; |
|||
import org.junit.jupiter.params.provider.MethodSource; |
|||
|
|||
class CardDeckTest { |
|||
|
|||
private CardDeck deck = new CardDeck(); |
|||
|
|||
@Test |
|||
void testCardDeckSize() { |
|||
int result = deck.getDeck().size(); |
|||
|
|||
assertEquals(52, result, "Kartendeck muss genau 52 Carden haben") ; |
|||
|
|||
} |
|||
|
|||
|
|||
@ParameterizedTest |
|||
@MethodSource("testSymbole") |
|||
//Jedes Symbol muss einmal vorhand sein |
|||
void testCardDeckSymbole(String _msg, Symbol _sym) { |
|||
|
|||
boolean result = false; |
|||
|
|||
for(int allCardZähler = 0; allCardZähler<deck.getDeck().size(); allCardZähler++) { |
|||
if(deck.getDeck().get(allCardZähler).getSymbol() == _sym) { |
|||
result = true; |
|||
break; |
|||
} |
|||
} |
|||
|
|||
assertTrue(result, _msg); |
|||
} |
|||
|
|||
|
|||
private static Stream<Arguments> testSymbole(){ |
|||
return Stream.of( |
|||
Arguments.of("Herz", Symbol.Herz), |
|||
Arguments.of("Karo", Symbol.Karo), |
|||
Arguments.of("Pik", Symbol.Pik), |
|||
Arguments.of("Kreuz",Symbol.Kreuz) |
|||
); |
|||
|
|||
|
|||
} |
|||
|
|||
@Test |
|||
//Jedes Symbol muss 13 mal vorrhanden sein |
|||
void testCardDeckNumber() { |
|||
|
|||
boolean result = true; |
|||
|
|||
result = CheckAllCards(Symbol.Herz) && |
|||
CheckAllCards(Symbol.Karo) && |
|||
CheckAllCards(Symbol.Kreuz) && |
|||
CheckAllCards(Symbol.Pik); |
|||
|
|||
assertTrue(result); |
|||
} |
|||
|
|||
|
|||
private boolean CheckAllCards(Symbol _sym) { |
|||
boolean result = true; |
|||
for(int nrZähler =1; nrZähler<=13; nrZähler++) { |
|||
if(deck.getDeck().contains(new Card(nrZähler, _sym, false))== false) { |
|||
result = false; |
|||
break; |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
|
|||
} |
@ -0,0 +1,25 @@ |
|||
package solitaer; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
class GeberStackTest { |
|||
|
|||
@Test |
|||
void testswapGeberWaste001() { |
|||
WasteStack waste = new WasteStack(0, 0); |
|||
GeberStack geber = new GeberStack(0, 0, waste); |
|||
|
|||
for (int i = 1; i < 14; i++) { |
|||
waste.addCard(new Card(0, Symbol.Pik, false)); |
|||
} |
|||
|
|||
int expectedResult = waste.stackSize(); |
|||
|
|||
geber.swapWasteGeber(); |
|||
|
|||
assertEquals(geber.stackSize(), expectedResult, "same Size"); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,38 @@ |
|||
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 StartStackTest { |
|||
|
|||
@ParameterizedTest |
|||
@MethodSource("testStartStackTestDaten") |
|||
//Jedes Symbol muss einmal vorhand sein |
|||
void testRulesetStartStack(String _msg, Card _stapel, Card _spieler, boolean _expectedResult) { |
|||
StartStack stack = new StartStack(10, 10); |
|||
assertEquals(_expectedResult, stack.cardCheck(_stapel, _spieler), _msg); |
|||
} |
|||
|
|||
|
|||
private static Stream<Arguments> testStartStackTestDaten(){ |
|||
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-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("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("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("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) |
|||
); |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue