Bryt ut till getSchackState

This commit is contained in:
lov3b 2022-05-15 20:24:46 +02:00
parent 452d7b1c4c
commit d8bdacdb80
2 changed files with 61 additions and 30 deletions

View File

@ -4,6 +4,7 @@ import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.Point; import java.awt.Point;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
@ -115,38 +116,30 @@ public class Board extends JPanel implements MouseListener {
turnCount++; turnCount++;
whitesTurn = !whitesTurn; whitesTurn = !whitesTurn;
final ArrayList<Point> allValidMoves = getMoves(whitesTurn); SchackState state = getSchackState();
final ArrayList<Point> opposingAttacks = getAttacks(!whitesTurn); switch (state) {
case SCHACK:
final boolean weCanMove = !allValidMoves.isEmpty();
boolean inSchack = false;
for (Point attack : opposingAttacks) {
final Piece attacked = pieces[attack.x][attack.y];
if (attacked == null) {
continue;
}
if (attacked.supremeRuler) {
validMovesToDraw.clear();
getParent().repaint();
// Kolla ifall vi är i schackmatt
if (weCanMove) {
JOptionPane.showMessageDialog(this, "Du står i schack"); JOptionPane.showMessageDialog(this, "Du står i schack");
} else { break;
final int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?"); case SCHACKMATT:
if (choise == JOptionPane.YES_OPTION) { if (JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?") == JOptionPane.YES_OPTION) {
try { try {
restartGame(); restartGame();
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex);
} }
} }
} break;
inSchack = true; case PATT:
if (JOptionPane.showConfirmDialog(this, "Patt\nVill du starta om?") == JOptionPane.YES_OPTION) {
try {
restartGame();
} catch (IOException ex) {
Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex);
} }
} }
if (!inSchack && !weCanMove) { break;
JOptionPane.showMessageDialog(this, "Patt"); default:
} }
} else { } else {
@ -170,6 +163,34 @@ public class Board extends JPanel implements MouseListener {
getParent().repaint(); getParent().repaint();
} }
private SchackState getSchackState() {
final ArrayList<Point> allValidMoves = getMoves(whitesTurn);
final ArrayList<Point> opposingAttacks = getAttacks(!whitesTurn);
final boolean weCanMove = !allValidMoves.isEmpty();
boolean inSchack = false;
for (Point attack : opposingAttacks) {
final Piece attacked = pieces[attack.x][attack.y];
if (attacked == null) {
continue;
}
if (attacked.supremeRuler) {
inSchack = true;
validMovesToDraw.clear();
getParent().repaint();
if (weCanMove) {
return SchackState.SCHACK;
} else {
return SchackState.SCHACKMATT;
}
}
}
if (!inSchack && !weCanMove) {
return SchackState.PATT;
}
return SchackState.NORMAL;
}
private ArrayList<Point> getMoves(boolean whiteMovesAreWanted) { private ArrayList<Point> getMoves(boolean whiteMovesAreWanted) {
final ArrayList<Point> allValidMoves = new ArrayList<>(); final ArrayList<Point> allValidMoves = new ArrayList<>();
for (Piece[] pieceArr : pieces) { for (Piece[] pieceArr : pieces) {

View File

@ -0,0 +1,10 @@
package schack;
/**
*
* @author love
*/
public enum SchackState {
SCHACK, SCHACKMATT, PATT, NORMAL, REMI
}