mirror of
				https://github.com/lov3b/Schack.git
				synced 2025-11-03 22:50:24 +01:00 
			
		
		
		
	Lägg till remi + ge upp + små fix
This commit is contained in:
		@@ -19,19 +19,30 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
    private Piece[][] pieces = new Piece[8][8];
 | 
			
		||||
    private ArrayList<Point> validMovesToDraw = new ArrayList<>();
 | 
			
		||||
    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;
 | 
			
		||||
    short turnCount = 0;
 | 
			
		||||
    boolean whitesTurn = true;
 | 
			
		||||
    private boolean whitesTurn = true;
 | 
			
		||||
 | 
			
		||||
    public Board() throws IOException {
 | 
			
		||||
 | 
			
		||||
        this.pieces = initPieces();
 | 
			
		||||
        this.pieces = getPieces();
 | 
			
		||||
        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 = {
 | 
			
		||||
            {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) {
 | 
			
		||||
        int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE);
 | 
			
		||||
        int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE);
 | 
			
		||||
 | 
			
		||||
        Point clicked = new Point(mouseCoordinateX, mouseCoordinateY);
 | 
			
		||||
 | 
			
		||||
        // Ifall vi har tryckt på en pjäs och sedan ska gå dit
 | 
			
		||||
@@ -127,7 +137,7 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
 | 
			
		||||
                ArrayList<Point> opposingAttacks = checkAttacks(!whitesTurn);
 | 
			
		||||
 | 
			
		||||
                boolean weCanMove = allValidMoves.size() > 0;
 | 
			
		||||
                boolean weCanMove = !allValidMoves.isEmpty();
 | 
			
		||||
                boolean inSchack = false;
 | 
			
		||||
 | 
			
		||||
                for (Point attack : opposingAttacks) {
 | 
			
		||||
@@ -140,10 +150,11 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
                        if (weCanMove) {
 | 
			
		||||
                            JOptionPane.showMessageDialog(this, "Du står i schack");
 | 
			
		||||
                        } else {
 | 
			
		||||
                            validMovesToDraw = new ArrayList<>();
 | 
			
		||||
                            getParent().repaint();
 | 
			
		||||
                            int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?");
 | 
			
		||||
                            if (choise == JOptionPane.YES_OPTION) {
 | 
			
		||||
                                this.pieces = initPieces();
 | 
			
		||||
                                whitesTurn = true;
 | 
			
		||||
                                restartGame();
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                        inSchack = true;
 | 
			
		||||
@@ -205,8 +216,7 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
            validMovesToDraw.clear();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        getParent()
 | 
			
		||||
                .repaint();
 | 
			
		||||
        getParent().repaint();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public ArrayList<Point> checkAttacks(boolean preferedColor) {
 | 
			
		||||
@@ -236,4 +246,8 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
    @Override
 | 
			
		||||
    public void mouseExited(MouseEvent e) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean isWhitesTurn() {
 | 
			
		||||
        return whitesTurn;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,12 @@
 | 
			
		||||
package schack;
 | 
			
		||||
 | 
			
		||||
import com.formdev.flatlaf.FlatLightLaf;
 | 
			
		||||
import java.awt.event.ActionEvent;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.InetAddress;
 | 
			
		||||
import java.net.UnknownHostException;
 | 
			
		||||
import java.util.logging.Level;
 | 
			
		||||
import java.util.logging.Logger;
 | 
			
		||||
import javax.swing.JFrame;
 | 
			
		||||
import javax.swing.JMenu;
 | 
			
		||||
import javax.swing.JMenuBar;
 | 
			
		||||
@@ -51,7 +54,6 @@ public class Schack {
 | 
			
		||||
        JMenuItem showLocalIP = new JMenuItem("Show IP");
 | 
			
		||||
        JMenuItem askForRemi = new JMenuItem("Ask for remi");
 | 
			
		||||
        JMenuItem surrender = new JMenuItem("Surrender");
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        // Actions
 | 
			
		||||
        connectToOpponent.addActionListener((ActionEvent ae) -> {
 | 
			
		||||
@@ -66,12 +68,30 @@ public class Schack {
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        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) -> {
 | 
			
		||||
            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
 | 
			
		||||
        frame.setJMenuBar(menuBar);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user