mirror of
				https://github.com/lov3b/Schack.git
				synced 2025-11-03 22:50:24 +01:00 
			
		
		
		
	Logger working
This commit is contained in:
		@@ -9,10 +9,15 @@ import java.awt.event.MouseEvent;
 | 
			
		||||
import java.awt.event.MouseListener;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.LinkedList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import javax.swing.DefaultListModel;
 | 
			
		||||
import javax.swing.JOptionPane;
 | 
			
		||||
import javax.swing.JPanel;
 | 
			
		||||
 | 
			
		||||
import com.billenius.schack.MoveLogging.Move;
 | 
			
		||||
 | 
			
		||||
public class Board extends JPanel implements MouseListener {
 | 
			
		||||
 | 
			
		||||
    public static final int SIZE_OF_TILE = 100;
 | 
			
		||||
@@ -22,10 +27,13 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
    private final Color moveableColor = new Color(255, 191, 0);
 | 
			
		||||
    short turnCount = 0;
 | 
			
		||||
    private boolean whitesTurn = true;
 | 
			
		||||
    private List<Move> moveLog = new LinkedList<>();
 | 
			
		||||
    private DefaultListModel<Move> listModel;
 | 
			
		||||
 | 
			
		||||
    public Board() throws IOException {
 | 
			
		||||
    public Board(DefaultListModel<Move> listModel) throws IOException {
 | 
			
		||||
        this.pieces = getPieces();
 | 
			
		||||
        setPreferredSize(new Dimension(800, 800));
 | 
			
		||||
        this.listModel = listModel;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -83,7 +91,7 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Måla alla pjäser
 | 
			
		||||
        for (Piece[] pieceArr : pieces) 
 | 
			
		||||
        for (Piece[] pieceArr : pieces)
 | 
			
		||||
            for (Piece piece : pieceArr) {
 | 
			
		||||
                if (piece == null) {
 | 
			
		||||
                    continue;
 | 
			
		||||
@@ -116,6 +124,10 @@ public class Board extends JPanel implements MouseListener {
 | 
			
		||||
                validMovesToDraw.clear();
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            Move m = new Move(selectedPiece, selectedPiece.position, clickedCoordinate);
 | 
			
		||||
            moveLog.add(m);
 | 
			
		||||
            listModel.addElement(m);
 | 
			
		||||
            selectedPiece.move(pieces, clickedCoordinate);
 | 
			
		||||
            turnCount++;
 | 
			
		||||
            whitesTurn = !whitesTurn;
 | 
			
		||||
 
 | 
			
		||||
@@ -78,9 +78,9 @@ public class Pawn extends Piece {
 | 
			
		||||
    private List<Point> addAttackMovesIfCan(Point pos, Piece[][] pieces) {
 | 
			
		||||
        List<Point> movable = new ArrayList<>();
 | 
			
		||||
        // Se till att vi inte är utanför brädet
 | 
			
		||||
        if (pos.x >= pieces.length || pos.x < 0 || pos.y >= pieces[0].length || pos.y < 0) {
 | 
			
		||||
        if (pos.x >= pieces.length || pos.x < 0 || pos.y >= pieces[0].length || pos.y < 0) 
 | 
			
		||||
            return movable;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        final Piece potentialEnemy = pieces[pos.x][pos.y];
 | 
			
		||||
        // Ifall det är tomt här, gör ingenting
 | 
			
		||||
        if (potentialEnemy != null && potentialEnemy.isWhite() != this.isWhite()) {
 | 
			
		||||
@@ -93,9 +93,8 @@ public class Pawn extends Piece {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected boolean addMovesIfCan(Point pos, List<Point> movable, Piece[][] pieces, boolean allowedToRecurse) {
 | 
			
		||||
        if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) {
 | 
			
		||||
        if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) 
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Piece pieceToCheck = pieces[pos.x][pos.y];
 | 
			
		||||
        if (pieceToCheck == null) {
 | 
			
		||||
@@ -114,9 +113,8 @@ public class Pawn extends Piece {
 | 
			
		||||
 | 
			
		||||
        // Check if the pawn has moved to the end and should be transformed
 | 
			
		||||
        if (this.position.y == 0 && this.isWhite()
 | 
			
		||||
                || this.position.y == 7 && !this.isWhite()) {
 | 
			
		||||
                || this.position.y == 7 && !this.isWhite()) 
 | 
			
		||||
            transform(pieces);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void transform(Piece[][] pieces) throws HeadlessException {
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@ import java.io.InputStream;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import javax.imageio.ImageIO;
 | 
			
		||||
import javax.swing.ImageIcon;
 | 
			
		||||
 | 
			
		||||
public abstract class Piece {
 | 
			
		||||
 | 
			
		||||
@@ -197,9 +198,9 @@ public abstract class Piece {
 | 
			
		||||
        // Kollar ifall kungen står i schack just nu
 | 
			
		||||
        for (Point enemyAttack : enemyAttacks) {
 | 
			
		||||
            Piece attackedPiece = pieces[enemyAttack.x][enemyAttack.y];
 | 
			
		||||
            if (attackedPiece != null && attackedPiece.supremeRuler) 
 | 
			
		||||
            if (attackedPiece != null && attackedPiece.supremeRuler)
 | 
			
		||||
                return true;
 | 
			
		||||
            
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
@@ -226,4 +227,8 @@ public abstract class Piece {
 | 
			
		||||
        return moved;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public BufferedImage getIcon() {
 | 
			
		||||
        return this.icon;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,20 +1,35 @@
 | 
			
		||||
package com.billenius.schack;
 | 
			
		||||
 | 
			
		||||
import java.awt.Dimension;
 | 
			
		||||
import java.awt.HeadlessException;
 | 
			
		||||
import java.awt.event.ActionEvent;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.Inet4Address;
 | 
			
		||||
import java.net.NetworkInterface;
 | 
			
		||||
import java.net.UnknownHostException;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import javax.swing.DefaultListModel;
 | 
			
		||||
import javax.swing.JFrame;
 | 
			
		||||
import javax.swing.JLabel;
 | 
			
		||||
import javax.swing.JList;
 | 
			
		||||
import javax.swing.JMenu;
 | 
			
		||||
import javax.swing.JMenuBar;
 | 
			
		||||
import javax.swing.JMenuItem;
 | 
			
		||||
import javax.swing.JOptionPane;
 | 
			
		||||
import javax.swing.JScrollPane;
 | 
			
		||||
import javax.swing.JSplitPane;
 | 
			
		||||
import javax.swing.ListModel;
 | 
			
		||||
import javax.swing.SpinnerDateModel;
 | 
			
		||||
import javax.swing.UIManager;
 | 
			
		||||
 | 
			
		||||
import com.billenius.schack.MoveLogging.Move;
 | 
			
		||||
import com.billenius.schack.MoveLogging.PieceRenderer;
 | 
			
		||||
import com.formdev.flatlaf.FlatLightLaf;
 | 
			
		||||
 | 
			
		||||
import com.billenius.schack.MoveLogging.PieceType;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *
 | 
			
		||||
 * @author Love Billenius & Simon Hansson
 | 
			
		||||
@@ -37,11 +52,25 @@ public class Schack {
 | 
			
		||||
        frame = new JFrame();
 | 
			
		||||
        frame.setTitle("Schack");
 | 
			
		||||
        frame.setAlwaysOnTop(false);
 | 
			
		||||
        frame.setResizable(false);
 | 
			
		||||
        frame.setResizable(true);
 | 
			
		||||
 | 
			
		||||
        DefaultListModel<Move> listModel = new DefaultListModel<>();
 | 
			
		||||
 | 
			
		||||
        // Might throw an IOException if the icon of the Pieces isn't embedded correctly
 | 
			
		||||
        final Board board = new Board();
 | 
			
		||||
        frame.setContentPane(board);
 | 
			
		||||
        final Board board = new Board(listModel);
 | 
			
		||||
 | 
			
		||||
        final JList<Move> jlist = new JList<>(listModel);
 | 
			
		||||
        jlist.setVisible(true);
 | 
			
		||||
        jlist.setCellRenderer(new PieceRenderer());
 | 
			
		||||
 | 
			
		||||
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
 | 
			
		||||
        splitPane.setDividerLocation(800);
 | 
			
		||||
        board.setMinimumSize(new Dimension(800, 800));
 | 
			
		||||
        jlist.setMinimumSize(new Dimension(200, 800));
 | 
			
		||||
        splitPane.setLeftComponent(board);
 | 
			
		||||
        splitPane.setRightComponent(new JScrollPane(jlist));
 | 
			
		||||
 | 
			
		||||
        frame.setContentPane(splitPane);
 | 
			
		||||
        frame.getContentPane().addMouseListener(board);
 | 
			
		||||
 | 
			
		||||
        // Create menu
 | 
			
		||||
@@ -88,7 +117,6 @@ public class Schack {
 | 
			
		||||
        connectToOpponent.addActionListener((ActionEvent ae) -> {
 | 
			
		||||
            String opponentIP = JOptionPane.showInputDialog(null, "What's your opponents IP?");
 | 
			
		||||
            System.out.println("opponents ip: " + opponentIP);
 | 
			
		||||
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        // Add the menu stuff
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user