Jenkins
3 years ago
27 changed files with 1984 additions and 0 deletions
-
22.gitignore
-
143src/main/java/Minesweeper/Cell.java
-
33src/main/java/Minesweeper/MinesweeperGame.java
-
151src/main/java/Minesweeper/Playfield.java
-
60src/main/java/Minesweeper/TimerLable.java
-
60src/main/java/TicTacToe/Cell.java
-
45src/main/java/TicTacToe/Scoreboard.java
-
134src/main/java/TicTacToe/TicTacToeGame.java
-
100src/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
-
53src/test/java/Minesweeper/MinesweeperGameTest.java
-
77src/test/java/TicTacToe/TicTacToeGameTest.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,143 @@ |
|||
package Minesweeper; |
|||
|
|||
import java.awt.Color; |
|||
import java.awt.Point; |
|||
import java.awt.event.ActionEvent; |
|||
import java.awt.event.ActionListener; |
|||
import java.awt.event.MouseAdapter; |
|||
import java.awt.event.MouseEvent; |
|||
|
|||
import javax.swing.JButton; |
|||
import javax.swing.JOptionPane; |
|||
|
|||
enum CellType { |
|||
Number, Bomb |
|||
} |
|||
|
|||
public class Cell extends JButton { |
|||
|
|||
private static final Color FLAGCOLOR = Color.RED; |
|||
private static final Color FLOODEDCOLOR = Color.LIGHT_GRAY; |
|||
private static final Color HIDDENCOLOR = Color.WHITE; |
|||
private static final Color MINECOLOR = Color.BLACK; |
|||
private static final long serialVersionUID = 1L; |
|||
private Playfield playfield; |
|||
|
|||
public CellType type; |
|||
public Point cord; |
|||
public boolean flagged = false; |
|||
public int value = 0; |
|||
|
|||
public Cell(CellType _type, Playfield _playfield, Point _cord) { |
|||
type = _type; |
|||
cord = _cord; |
|||
playfield = _playfield; |
|||
|
|||
setBackground(HIDDENCOLOR); |
|||
addActionListener(new ActionListener() { |
|||
@Override |
|||
public void actionPerformed(ActionEvent e) { |
|||
OnMouseClick(); |
|||
} |
|||
}); |
|||
|
|||
addMouseListener(new MouseAdapter() { |
|||
@Override |
|||
public void mousePressed(MouseEvent e) { |
|||
// TODO Auto-generated method stub |
|||
super.mousePressed(e); |
|||
if (e.getButton() == 3) { |
|||
OnMouseRightClick(); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
|
|||
protected void OnMouseClick() { |
|||
if (!flagged) { |
|||
reveal(); |
|||
if (type != CellType.Bomb) { |
|||
flood(); |
|||
} else { |
|||
playfield.revealAllBombs(); |
|||
JOptionPane.showMessageDialog(getParent(),"KABOOM! Try again!"); |
|||
playfield.reset(); |
|||
} |
|||
|
|||
} |
|||
} |
|||
|
|||
protected void OnMouseRightClick() { |
|||
if (isEnabled()) { |
|||
if (flagged) { |
|||
flagged = false; |
|||
|
|||
setBackground(HIDDENCOLOR); |
|||
if (type == CellType.Number) { |
|||
playfield.cellDried(); |
|||
} |
|||
|
|||
} else { |
|||
flagged = true; |
|||
setBackground(FLAGCOLOR); |
|||
if (type == CellType.Number) { |
|||
playfield.cellFlooded(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
public void reveal() { |
|||
if (type == CellType.Number) { |
|||
if(value > 0) { |
|||
setText(String.valueOf(value)); |
|||
} |
|||
} else { |
|||
setBackground(MINECOLOR); |
|||
} |
|||
} |
|||
|
|||
public void flood() { |
|||
if (type == CellType.Bomb || flagged) { |
|||
return; |
|||
} |
|||
|
|||
setBackground(FLOODEDCOLOR); |
|||
setEnabled(false); |
|||
reveal(); |
|||
playfield.cellFlooded(); |
|||
|
|||
if (value == 0) { |
|||
if (cord.y > 0) { |
|||
if (playfield.cells[cord.y - 1][cord.x].type == CellType.Number |
|||
&& playfield.cells[cord.y - 1][cord.x].isEnabled()) { |
|||
playfield.cells[cord.y - 1][cord.x].flood(); |
|||
} |
|||
} |
|||
|
|||
if (cord.x < playfield.Size - 1) { |
|||
if (playfield.cells[cord.y][cord.x + 1].type == CellType.Number |
|||
&& playfield.cells[cord.y][cord.x + 1].isEnabled()) { |
|||
playfield.cells[cord.y][cord.x + 1].flood(); |
|||
} |
|||
} |
|||
|
|||
if (cord.y < playfield.Size - 1) { |
|||
if (playfield.cells[cord.y + 1][cord.x].type == CellType.Number |
|||
&& playfield.cells[cord.y + 1][cord.x].isEnabled()) { |
|||
playfield.cells[cord.y + 1][cord.x].flood(); |
|||
} |
|||
} |
|||
|
|||
if (cord.x > 0) { |
|||
if (playfield.cells[cord.y][cord.x - 1].type == CellType.Number |
|||
&& playfield.cells[cord.y][cord.x - 1].isEnabled()) { |
|||
playfield.cells[cord.y][cord.x - 1].flood(); |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
} |
|||
|
|||
} |
@ -0,0 +1,33 @@ |
|||
package Minesweeper; |
|||
|
|||
import javax.swing.JFrame; |
|||
import javax.swing.JPanel; |
|||
|
|||
public class MinesweeperGame extends JPanel { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
public static final int WIDTH = 600, HEIGTH = 600; |
|||
public Playfield playfield; |
|||
public TimerLable tl; |
|||
|
|||
public MinesweeperGame(int _playfieldSize, int _bombAmount) { |
|||
this.setSize(WIDTH, HEIGTH); |
|||
setLayout(null); |
|||
playfield = new Playfield(this, _playfieldSize, _bombAmount); |
|||
|
|||
tl = new TimerLable(); |
|||
tl.setBounds((WIDTH / 2 - 5), HEIGTH / 2 - 240, 20, 20); |
|||
add(tl); |
|||
tl.start(); |
|||
} |
|||
|
|||
public static void main(String[] args) { |
|||
JFrame f = new JFrame(); |
|||
MinesweeperGame MsG = new MinesweeperGame(8, 10); |
|||
|
|||
f.add(MsG); |
|||
f.setSize(WIDTH, HEIGTH); |
|||
f.setLayout(null); |
|||
f.setVisible(true); |
|||
} |
|||
} |
@ -0,0 +1,151 @@ |
|||
package Minesweeper; |
|||
|
|||
import java.awt.Point; |
|||
|
|||
import javax.swing.JOptionPane; |
|||
|
|||
public class Playfield { |
|||
|
|||
private static final int CELLSIZE = 50; |
|||
public int Size; |
|||
private MinesweeperGame MsG; |
|||
public Cell[][] cells; |
|||
private int bombAmount; |
|||
private int cellsFlooded = 0; |
|||
|
|||
public Playfield(MinesweeperGame _MsG, int _Size, int _bombAmount) { |
|||
MsG = _MsG; |
|||
Size = _Size; |
|||
bombAmount = _bombAmount; |
|||
generatePlayfield(); |
|||
} |
|||
|
|||
public void generatePlayfield() { |
|||
|
|||
cells = new Cell[Size][Size]; |
|||
|
|||
int[] bPlacement = new int[bombAmount]; |
|||
for (int i = 0; i < bPlacement.length; i++) { |
|||
bPlacement[i] = (int) (Math.random() * Size * Size); |
|||
|
|||
for (int j = 0; j < i; j++) { |
|||
if (bPlacement[i] == bPlacement[j]) { |
|||
i--; |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
|
|||
for (int i = 0; i < Size; i++) { |
|||
for (int j = 0; j < Size; j++) { |
|||
cells[i][j] = new Cell(CellType.Number, this, new Point(j, i)); |
|||
|
|||
cells[i][j].setBounds(j * CELLSIZE + (MsG.WIDTH / 2 - Size * CELLSIZE / 2), |
|||
i * CELLSIZE + (MsG.HEIGTH / 2 - Size * CELLSIZE / 2), CELLSIZE, CELLSIZE); |
|||
MsG.add(cells[i][j]); |
|||
for (int k = 0; k < bPlacement.length; k++) { |
|||
if (bPlacement[k] == i * Size + j) { |
|||
cells[i][j].type = CellType.Bomb; |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
for (int i = 0; i < Size; i++) { |
|||
for (int j = 0; j < Size; j++) { |
|||
if (cells[i][j].type == CellType.Number) { |
|||
calculateBombProximity(i, j); |
|||
} |
|||
} |
|||
} |
|||
MsG.repaint(); |
|||
} |
|||
|
|||
public void reset() { |
|||
cellsFlooded = 0; |
|||
for (int i = 0; i < Size; i++) { |
|||
for (int j = 0; j < Size; j++) { |
|||
MsG.remove(cells[i][j]); |
|||
} |
|||
} |
|||
MsG.tl.reset(); |
|||
generatePlayfield(); |
|||
} |
|||
|
|||
public void calculateBombProximity(int row, int column) { |
|||
|
|||
if (row > 0) { |
|||
if (column > 0) { |
|||
if (cells[row - 1][column - 1].type == CellType.Bomb) { |
|||
cells[row][column].value++; |
|||
} |
|||
} |
|||
|
|||
if (cells[row - 1][column].type == CellType.Bomb) { |
|||
cells[row][column].value++; |
|||
} |
|||
|
|||
if (column < cells.length - 1) { |
|||
if (cells[row - 1][column + 1].type == CellType.Bomb) { |
|||
cells[row][column].value++; |
|||
} |
|||
} |
|||
} |
|||
|
|||
if (row < cells.length - 1) { |
|||
if (column > 0) { |
|||
if (cells[row + 1][column - 1].type == CellType.Bomb) { |
|||
cells[row][column].value++; |
|||
} |
|||
} |
|||
|
|||
if (cells[row + 1][column].type == CellType.Bomb) { |
|||
cells[row][column].value++; |
|||
} |
|||
|
|||
if (column < cells.length - 1) { |
|||
if (cells[row + 1][column + 1].type == CellType.Bomb) { |
|||
cells[row][column].value++; |
|||
} |
|||
} |
|||
} |
|||
|
|||
if (column > 0) { |
|||
if (cells[row][column - 1].type == CellType.Bomb) { |
|||
cells[row][column].value++; |
|||
} |
|||
} |
|||
if (column < cells.length - 1) { |
|||
if (cells[row][column + 1].type == CellType.Bomb) { |
|||
cells[row][column].value++; |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
public void cellFlooded() { |
|||
cellsFlooded++; |
|||
if (cellsFlooded >= Size * Size - bombAmount) { |
|||
revealAllBombs(); |
|||
JOptionPane.showMessageDialog(MsG, "You won, congratulations!"); |
|||
reset(); |
|||
} |
|||
} |
|||
|
|||
public void revealAllBombs() { |
|||
for (int i = 0; i < Size; i++) { |
|||
for (int j = 0; j < Size; j++) { |
|||
if(cells[i][j].type == CellType.Bomb) { |
|||
cells[i][j].reveal(); |
|||
} |
|||
} |
|||
} |
|||
MsG.tl.stop(); |
|||
MsG.repaint(); |
|||
} |
|||
|
|||
public void cellDried() { |
|||
cellsFlooded--; |
|||
} |
|||
} |
@ -0,0 +1,60 @@ |
|||
package Minesweeper; |
|||
|
|||
import java.util.Timer; |
|||
import java.util.TimerTask; |
|||
|
|||
import javax.swing.JLabel; |
|||
|
|||
public class TimerLable extends JLabel { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
protected int counter = 0; |
|||
private Helper task; |
|||
|
|||
public void start() { |
|||
Timer timer = new Timer(); |
|||
task = new Helper(this); |
|||
|
|||
timer.schedule(task, 0, 1000); |
|||
} |
|||
|
|||
public void update() { |
|||
setText(String.valueOf(++counter)); |
|||
} |
|||
|
|||
public void reset() { |
|||
task.reset = true; |
|||
counter = 0; |
|||
setText(String.valueOf(counter)); |
|||
repaint(); |
|||
task.stop = false; |
|||
} |
|||
|
|||
public void stop() { |
|||
task.stop = true; |
|||
} |
|||
} |
|||
|
|||
|
|||
class Helper extends TimerTask |
|||
{ |
|||
public boolean reset; |
|||
public boolean stop; |
|||
public static int i = 0; |
|||
private TimerLable timerLable; |
|||
|
|||
public Helper(TimerLable _timerLable) { |
|||
timerLable = _timerLable; |
|||
} |
|||
public void run() |
|||
{ |
|||
if(stop) { |
|||
return; |
|||
} |
|||
if(reset) { |
|||
reset = false; |
|||
timerLable.counter = 0; |
|||
} |
|||
timerLable.update(); |
|||
} |
|||
} |
@ -0,0 +1,60 @@ |
|||
package TicTacToe; |
|||
|
|||
import java.awt.Color; |
|||
import java.awt.Graphics; |
|||
import java.awt.Graphics2D; |
|||
import java.awt.event.ActionEvent; |
|||
import java.awt.event.ActionListener; |
|||
|
|||
import javax.swing.JButton; |
|||
|
|||
public class Cell extends JButton { |
|||
|
|||
public int playerID = 0; |
|||
private TicTacToeGame ttt; |
|||
|
|||
public Cell(TicTacToeGame _ttt) { |
|||
ttt = _ttt; |
|||
setBackground(new Color(0)); |
|||
|
|||
addActionListener(new ActionListener() { |
|||
@Override |
|||
public void actionPerformed(ActionEvent e) { |
|||
OnMouseClick(); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
protected void OnMouseClick() { |
|||
if (playerID == 0) { |
|||
playerID = ttt.playerID; |
|||
setEnabled(false); |
|||
ttt.endTurn(); |
|||
repaint(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
protected void paintComponent(Graphics g) { |
|||
super.paintComponent(g); |
|||
Graphics2D g2 = (Graphics2D) g; |
|||
g2.setPaint(Color.white); |
|||
switch (playerID) { |
|||
case 1: |
|||
g2.drawLine(5, 5, 90, 90); |
|||
g2.drawLine(90, 5, 5, 90); |
|||
break; |
|||
case 2: |
|||
g2.drawOval(5, 5, 90, 90); |
|||
break; |
|||
} |
|||
} |
|||
|
|||
|
|||
protected void reset() { |
|||
playerID = 0; |
|||
setEnabled(true); |
|||
repaint(); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,45 @@ |
|||
package TicTacToe; |
|||
|
|||
import java.awt.Color; |
|||
|
|||
import javax.swing.JLabel; |
|||
|
|||
public class Scoreboard extends JLabel{ |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
public int[] playerPoints; |
|||
|
|||
public Scoreboard(int _playerCount) { |
|||
playerPoints = new int[_playerCount]; |
|||
setForeground(Color.white); |
|||
updateScores(); |
|||
} |
|||
|
|||
public void addPoint(int playerID) { |
|||
playerPoints[playerID]++; |
|||
updateScores(); |
|||
} |
|||
|
|||
public void subPoint(int playerID) { |
|||
playerPoints[playerID]--; |
|||
updateScores(); |
|||
} |
|||
|
|||
public void updateScores() { |
|||
int gamesPlayed = 0; |
|||
for(int i = 0; i < playerPoints.length; i++){ |
|||
if(i == 0) { |
|||
setText("Draws: " + playerPoints[0]); |
|||
} else { |
|||
setText(getText() + " Player " + i + ": " + playerPoints[i]); |
|||
} |
|||
|
|||
gamesPlayed += playerPoints[i]; |
|||
} |
|||
setText(getText() + " Games played: " + gamesPlayed); |
|||
|
|||
} |
|||
|
|||
|
|||
} |
@ -0,0 +1,134 @@ |
|||
package TicTacToe; |
|||
|
|||
import java.awt.Color; |
|||
import java.awt.Graphics; |
|||
import java.awt.Graphics2D; |
|||
import java.awt.geom.Line2D; |
|||
|
|||
import javax.swing.JFrame; |
|||
import javax.swing.JOptionPane; |
|||
import javax.swing.JPanel; |
|||
|
|||
|
|||
public class TicTacToeGame extends JPanel { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
public static final int width = 600, height = 600; |
|||
private static final int maxPlayers = 3; |
|||
private static final int playFieldSize = 9; |
|||
|
|||
public Cell[] field; |
|||
public int playerID = 1; |
|||
public int turns = 0; |
|||
public Scoreboard scoreboard; |
|||
|
|||
public TicTacToeGame() { |
|||
this.setSize(width, height); |
|||
setBackground(Color.black); |
|||
setLayout(null); |
|||
initField(); |
|||
initScoreboard(); |
|||
} |
|||
|
|||
|
|||
public static void main(String[] args) { |
|||
JFrame f = new JFrame(); |
|||
TicTacToeGame ttt = new TicTacToeGame(); |
|||
|
|||
f.add(ttt); |
|||
f.setSize(width,height); |
|||
f.setLayout(null); |
|||
f.setVisible(true); |
|||
} |
|||
|
|||
public void initField() { |
|||
field = new Cell[playFieldSize]; |
|||
|
|||
for(int i = 0; i < field.length; i++) { |
|||
field[i] = new Cell(this); |
|||
add(field[i]); |
|||
} |
|||
|
|||
for(int i = 0; i < field.length; i++) { |
|||
if(i < 3) { |
|||
field[i].setBounds(150 + i*100, 150 , 100, 100); |
|||
} else if (i < 6) { |
|||
field[i].setBounds(150 + i%3*100, 250 , 100, 100); |
|||
} else { |
|||
field[i].setBounds(150 + i%3*100, 350 , 100, 100); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private void initScoreboard() { |
|||
scoreboard = new Scoreboard(maxPlayers); |
|||
scoreboard.setBounds(150, 100, 300, 50); |
|||
add(scoreboard); |
|||
} |
|||
|
|||
@Override |
|||
protected void paintComponent(Graphics g) { |
|||
super.paintComponent(g); |
|||
Graphics2D g2 = (Graphics2D) g; |
|||
g2.setColor(Color.white); |
|||
|
|||
Line2D lin = new Line2D.Float(250, 150, 250, 450); |
|||
Line2D lin2 = new Line2D.Float(350, 150, 350, 450); |
|||
Line2D lin3 = new Line2D.Float(150, 250, 450, 250); |
|||
Line2D lin4 = new Line2D.Float(150, 350, 450, 350); |
|||
|
|||
g2.draw(lin); |
|||
g2.draw(lin2); |
|||
g2.draw(lin3); |
|||
g2.draw(lin4); |
|||
} |
|||
|
|||
public void endTurn() { |
|||
if(checkPlayfield() != 0) { |
|||
JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!"); |
|||
scoreboard.addPoint(playerID); |
|||
resetGame(); |
|||
} else { |
|||
turns++; |
|||
if(turns >= playFieldSize) { |
|||
JOptionPane.showMessageDialog(getParent(),"Draw!"); |
|||
scoreboard.addPoint(0); |
|||
resetGame(); |
|||
} |
|||
|
|||
playerID++; |
|||
if(playerID >= maxPlayers) { |
|||
playerID = 1; |
|||
} |
|||
} |
|||
|
|||
|
|||
} |
|||
|
|||
public int checkPlayfield() { |
|||
if ((field[0].playerID == playerID && field[1].playerID == playerID && field[2].playerID == playerID) |
|||
|| (field[0].playerID == playerID && field[3].playerID == playerID && field[6].playerID == playerID) |
|||
|| (field[8].playerID == playerID && field[5].playerID == playerID && field[2].playerID == playerID) |
|||
|| (field[8].playerID == playerID && field[7].playerID == playerID && field[6].playerID == playerID) |
|||
|| (field[0].playerID == playerID && field[4].playerID == playerID && field[8].playerID == playerID) |
|||
|| (field[0].playerID == playerID && field[4].playerID == playerID && field[8].playerID == playerID) |
|||
|| (field[2].playerID == playerID && field[4].playerID == playerID && field[6].playerID == playerID) |
|||
|| (field[3].playerID == playerID && field[4].playerID == playerID && field[5].playerID == playerID) |
|||
|| (field[1].playerID == playerID && field[4].playerID == playerID && field[7].playerID == playerID)) { |
|||
|
|||
return playerID; |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
public void resetGame() { |
|||
for (Cell c : field) { |
|||
c.reset(); |
|||
} |
|||
playerID = 1; |
|||
turns = 0; |
|||
} |
|||
|
|||
|
|||
} |
@ -0,0 +1,100 @@ |
|||
package hauptmenue; |
|||
|
|||
import java.awt.Dimension; |
|||
import java.awt.event.ActionEvent; |
|||
import java.awt.event.ActionListener; |
|||
import java.awt.event.MouseAdapter; |
|||
import java.awt.event.MouseEvent; |
|||
|
|||
import javax.swing.JButton; |
|||
import javax.swing.JFrame; |
|||
|
|||
import Minesweeper.MinesweeperGame; |
|||
import TicTacToe.TicTacToeGame; |
|||
import solitaer.SolitaerGamePanel; |
|||
import solitaer.SolitaerMenue; |
|||
|
|||
public class GameWindow extends JFrame { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
public GameWindow() { |
|||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|||
setSize(new Dimension(200, 125)); |
|||
setLayout(null); |
|||
|
|||
|
|||
|
|||
JButton soli = new JButton("Solitaer"); |
|||
JButton Tic = new JButton("TicTacToe"); |
|||
JButton Mine = new JButton("Minesweeper"); |
|||
|
|||
soli.addActionListener(new ActionListener() { |
|||
@Override |
|||
public void actionPerformed(ActionEvent e) { |
|||
soli.setVisible(false); |
|||
Tic.setVisible(false); |
|||
Mine.setVisible(false); |
|||
initSoli(); |
|||
} |
|||
}); |
|||
|
|||
Tic.addActionListener(new ActionListener() { |
|||
@Override |
|||
public void actionPerformed(ActionEvent e) { |
|||
soli.setVisible(false); |
|||
Tic.setVisible(false); |
|||
Mine.setVisible(false); |
|||
initTicTacToe(); |
|||
} |
|||
|
|||
}); |
|||
|
|||
Mine.addActionListener(new ActionListener() { |
|||
@Override |
|||
public void actionPerformed(ActionEvent e) { |
|||
soli.setVisible(false); |
|||
Tic.setVisible(false); |
|||
Mine.setVisible(false); |
|||
initMinesweeper(); |
|||
} |
|||
|
|||
|
|||
}); |
|||
soli.setBounds(0, 0, 200, 30); |
|||
Tic.setBounds(0, 30, 200, 30); |
|||
Mine.setBounds(0, 60, 200, 30); |
|||
add(soli); |
|||
add(Tic); |
|||
add(Mine); |
|||
|
|||
setVisible(true); |
|||
} |
|||
|
|||
private void initSoli() { |
|||
SolitaerGamePanel gamePanel = new SolitaerGamePanel(); |
|||
SolitaerMenue smenue = new SolitaerMenue(gamePanel); |
|||
gamePanel.setVisible(true); |
|||
gamePanel.setSize(new Dimension(1180, 780)); |
|||
setSize(new Dimension(1180, 780)); |
|||
//gamePanel.setPreferredSize(new Dimension(1180, 780)); |
|||
setJMenuBar(smenue.getMenue()); |
|||
add(gamePanel); |
|||
repaint(); |
|||
} |
|||
|
|||
public void initTicTacToe() { |
|||
TicTacToeGame ttt = new TicTacToeGame(); |
|||
setSize(ttt.width, ttt.height); |
|||
setLayout(null); |
|||
add(ttt); |
|||
} |
|||
|
|||
public void initMinesweeper() { |
|||
MinesweeperGame MsG = new MinesweeperGame(8,10); |
|||
setSize(MsG.WIDTH, MsG.HEIGTH); |
|||
setLayout(null); |
|||
add(MsG); |
|||
} |
|||
|
|||
} |
@ -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,53 @@ |
|||
package Minesweeper; |
|||
|
|||
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 MinesweeperGameTest { |
|||
|
|||
@ParameterizedTest |
|||
@MethodSource("testBombs") |
|||
void testBombPlacement(int _playfieldSize, int _bombAmount) { |
|||
MinesweeperGame m = new MinesweeperGame(_playfieldSize, _bombAmount); |
|||
|
|||
int bombCounter = 0; |
|||
|
|||
for(Cell[] row : m.playfield.cells){ |
|||
for (Cell c : row) { |
|||
if(c.type == CellType.Bomb) { |
|||
bombCounter++; |
|||
} |
|||
} |
|||
} |
|||
assertEquals(_bombAmount, bombCounter); |
|||
} |
|||
|
|||
@Test |
|||
void testProximityPoints() { |
|||
MinesweeperGame m = new MinesweeperGame(3, 0); |
|||
m.playfield.cells[0][0].type = CellType.Bomb; |
|||
m.playfield.calculateBombProximity(1, 1); |
|||
|
|||
assertEquals(1, m.playfield.cells[1][1].value); |
|||
} |
|||
|
|||
|
|||
private static Stream<Arguments> testBombs(){ |
|||
return Stream.of( |
|||
Arguments.of(8, 10), |
|||
Arguments.of(8, 0), |
|||
Arguments.of(4, 12), |
|||
Arguments.of(10, 100), |
|||
Arguments.of(5, 1) |
|||
); |
|||
} |
|||
|
|||
|
|||
|
|||
} |
@ -0,0 +1,77 @@ |
|||
package TicTacToe; |
|||
|
|||
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 TicTacToeGameTest { |
|||
|
|||
@ParameterizedTest |
|||
@MethodSource("testFieldsWinning") |
|||
void testGameEndWin(int[] _field, int _winner) { |
|||
TicTacToeGame ttt = new TicTacToeGame(); |
|||
|
|||
for(int i = 0; i < ttt.field.length; i++) { |
|||
ttt.field[i].playerID = _field[i]; |
|||
} |
|||
ttt.playerID = _winner; |
|||
int realWinner = ttt.checkPlayfield(); |
|||
|
|||
assertEquals(_winner, realWinner); |
|||
} |
|||
|
|||
@ParameterizedTest |
|||
@MethodSource("testFieldsDraw") |
|||
void testGameEndDraw(int[] _field) { |
|||
TicTacToeGame ttt = new TicTacToeGame(); |
|||
|
|||
for(int i = 0; i < ttt.field.length; i++) { |
|||
ttt.field[i].playerID = _field[i]; |
|||
} |
|||
|
|||
int noWinner = ttt.checkPlayfield(); |
|||
|
|||
assertEquals(0, noWinner); |
|||
} |
|||
|
|||
|
|||
private static Stream<Arguments> testFieldsWinning(){ |
|||
return Stream.of( |
|||
Arguments.of(new int[]{ 1,2,1, |
|||
2,2,2, |
|||
1,2,1}, 2), |
|||
Arguments.of(new int[]{ 2,1,2, |
|||
2,2,1, |
|||
1,1,1}, 1), |
|||
Arguments.of(new int[]{ 1,1,2, |
|||
1,2,2, |
|||
1,2,1}, 1), |
|||
Arguments.of(new int[]{ 2,1,1, |
|||
1,2,1, |
|||
1,1,2}, 2) |
|||
); |
|||
} |
|||
|
|||
private static Stream<Arguments> testFieldsDraw(){ |
|||
return Stream.of( |
|||
Arguments.of(new int[]{ 2,1,1, |
|||
2,2,2, |
|||
1,2,1}), |
|||
Arguments.of(new int[]{ 2,1,2, |
|||
2,2,1, |
|||
1,2,1}), |
|||
Arguments.of(new int[]{ 2,1,2, |
|||
1,2,2, |
|||
1,2,1}), |
|||
Arguments.of(new int[]{ 2,1,1, |
|||
1,2,2, |
|||
2,1,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