Lägg till remi + ge upp + små fix

This commit is contained in:
lov3b 2022-05-11 21:12:44 +02:00
parent cb3ccecade
commit 0f665c5829
2 changed files with 48 additions and 14 deletions

View File

@ -19,19 +19,30 @@ public class Board extends JPanel implements MouseListener {
private Piece[][] pieces = new Piece[8][8]; private Piece[][] pieces = new Piece[8][8];
private ArrayList<Point> validMovesToDraw = new ArrayList<>(); private ArrayList<Point> validMovesToDraw = new ArrayList<>();
private Point selectedPiece = new Point(); private Point selectedPiece = new Point();
private Color moveableColor = new Color(255, 191, 0); private final Color moveableColor = new Color(255, 191, 0);
public boolean developerMode = false; public boolean developerMode = false;
short turnCount = 0; short turnCount = 0;
boolean whitesTurn = true; private boolean whitesTurn = true;
public Board() throws IOException { public Board() throws IOException {
this.pieces = initPieces(); this.pieces = getPieces();
setPreferredSize(new Dimension(800, 800)); setPreferredSize(new Dimension(800, 800));
} }
private Piece[][] initPieces() throws IOException { /**
* Nollställ brädet
*
* @throws IOException
*/
public void restartGame() throws IOException {
this.pieces = getPieces();
this.whitesTurn = true;
getParent().repaint();
}
private Piece[][] getPieces() throws IOException {
Piece[][] piecesRet = { Piece[][] piecesRet = {
{new Rook(false, new Point(0, 0)), null, null, null, null, null, null, new Rook(true, new Point(0, 7))}, {new Rook(false, new Point(0, 0)), null, null, null, null, null, null, new Rook(true, new Point(0, 7))},
@ -102,7 +113,6 @@ public class Board extends JPanel implements MouseListener {
public void mousePressed(MouseEvent mouseEvent) { public void mousePressed(MouseEvent mouseEvent) {
int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE); int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE);
int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE); int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE);
Point clicked = new Point(mouseCoordinateX, mouseCoordinateY); Point clicked = new Point(mouseCoordinateX, mouseCoordinateY);
// Ifall vi har tryckt en pjäs och sedan ska dit // Ifall vi har tryckt en pjäs och sedan ska dit
@ -127,7 +137,7 @@ public class Board extends JPanel implements MouseListener {
ArrayList<Point> opposingAttacks = checkAttacks(!whitesTurn); ArrayList<Point> opposingAttacks = checkAttacks(!whitesTurn);
boolean weCanMove = allValidMoves.size() > 0; boolean weCanMove = !allValidMoves.isEmpty();
boolean inSchack = false; boolean inSchack = false;
for (Point attack : opposingAttacks) { for (Point attack : opposingAttacks) {
@ -140,10 +150,11 @@ public class Board extends JPanel implements MouseListener {
if (weCanMove) { if (weCanMove) {
JOptionPane.showMessageDialog(this, "Du står i schack"); JOptionPane.showMessageDialog(this, "Du står i schack");
} else { } else {
validMovesToDraw = new ArrayList<>();
getParent().repaint();
int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?"); int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?");
if (choise == JOptionPane.YES_OPTION) { if (choise == JOptionPane.YES_OPTION) {
this.pieces = initPieces(); restartGame();
whitesTurn = true;
} }
} }
inSchack = true; inSchack = true;
@ -205,8 +216,7 @@ public class Board extends JPanel implements MouseListener {
validMovesToDraw.clear(); validMovesToDraw.clear();
} }
getParent() getParent().repaint();
.repaint();
} }
public ArrayList<Point> checkAttacks(boolean preferedColor) { public ArrayList<Point> checkAttacks(boolean preferedColor) {
@ -236,4 +246,8 @@ public class Board extends JPanel implements MouseListener {
@Override @Override
public void mouseExited(MouseEvent e) { public void mouseExited(MouseEvent e) {
} }
public boolean isWhitesTurn() {
return whitesTurn;
}
} }

View File

@ -1,9 +1,12 @@
package schack; package schack;
import com.formdev.flatlaf.FlatLightLaf;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JMenu; import javax.swing.JMenu;
import javax.swing.JMenuBar; import javax.swing.JMenuBar;
@ -52,7 +55,6 @@ public class Schack {
JMenuItem askForRemi = new JMenuItem("Ask for remi"); JMenuItem askForRemi = new JMenuItem("Ask for remi");
JMenuItem surrender = new JMenuItem("Surrender"); JMenuItem surrender = new JMenuItem("Surrender");
// Actions // Actions
connectToOpponent.addActionListener((ActionEvent ae) -> { connectToOpponent.addActionListener((ActionEvent ae) -> {
System.out.println("Connecting (TODO)"); System.out.println("Connecting (TODO)");
@ -66,13 +68,31 @@ public class Schack {
} }
}); });
askForRemi.addActionListener((ActionEvent ae) -> { askForRemi.addActionListener((ActionEvent ae) -> {
System.out.println("I BEG FOR LE MERCY! (TODO)"); String whosWantingRemi = board.isWhitesTurn() ? "Vit" : "Svart";
int choice = JOptionPane.showConfirmDialog(board, whosWantingRemi + " erbjuder remi\nAccepterar du?");
if (choice == JOptionPane.YES_OPTION) {
choice = JOptionPane.showConfirmDialog(board, "Lika\nStarta om?");
if (choice == JOptionPane.YES_OPTION) {
try {
board.restartGame();
} catch (IOException ex) {
Logger.getLogger(Schack.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}); });
surrender.addActionListener((ActionEvent ae) -> { surrender.addActionListener((ActionEvent ae) -> {
System.out.println("I'M FRENCH! (TODO)"); String whosGivingUp = board.isWhitesTurn() ? "Vit" : "Svart";
int choice = JOptionPane.showConfirmDialog(board, whosGivingUp + " har gett upp\nStarta om?");
if (choice == JOptionPane.YES_OPTION) {
try {
board.restartGame();
} catch (IOException ex) {
Logger.getLogger(Schack.class.getName()).log(Level.SEVERE, null, ex);
}
}
}); });
// Add the menu stuff // Add the menu stuff
frame.setJMenuBar(menuBar); frame.setJMenuBar(menuBar);
menuBar.add(gameMenu); menuBar.add(gameMenu);