mirror of
				https://github.com/lov3b/Schack.git
				synced 2025-10-31 21:30:20 +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.awt.event.MouseListener; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
|  | import java.util.LinkedList; | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  |  | ||||||
|  | import javax.swing.DefaultListModel; | ||||||
| import javax.swing.JOptionPane; | import javax.swing.JOptionPane; | ||||||
| import javax.swing.JPanel; | import javax.swing.JPanel; | ||||||
|  |  | ||||||
|  | import com.billenius.schack.MoveLogging.Move; | ||||||
|  |  | ||||||
| public class Board extends JPanel implements MouseListener { | public class Board extends JPanel implements MouseListener { | ||||||
|  |  | ||||||
|     public static final int SIZE_OF_TILE = 100; |     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); |     private final Color moveableColor = new Color(255, 191, 0); | ||||||
|     short turnCount = 0; |     short turnCount = 0; | ||||||
|     private boolean whitesTurn = true; |     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(); |         this.pieces = getPieces(); | ||||||
|         setPreferredSize(new Dimension(800, 800)); |         setPreferredSize(new Dimension(800, 800)); | ||||||
|  |         this.listModel = listModel; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
| @@ -116,6 +124,10 @@ public class Board extends JPanel implements MouseListener { | |||||||
|                 validMovesToDraw.clear(); |                 validMovesToDraw.clear(); | ||||||
|                 return; |                 return; | ||||||
|             } |             } | ||||||
|  |  | ||||||
|  |             Move m = new Move(selectedPiece, selectedPiece.position, clickedCoordinate); | ||||||
|  |             moveLog.add(m); | ||||||
|  |             listModel.addElement(m); | ||||||
|             selectedPiece.move(pieces, clickedCoordinate); |             selectedPiece.move(pieces, clickedCoordinate); | ||||||
|             turnCount++; |             turnCount++; | ||||||
|             whitesTurn = !whitesTurn; |             whitesTurn = !whitesTurn; | ||||||
|   | |||||||
| @@ -78,9 +78,9 @@ public class Pawn extends Piece { | |||||||
|     private List<Point> addAttackMovesIfCan(Point pos, Piece[][] pieces) { |     private List<Point> addAttackMovesIfCan(Point pos, Piece[][] pieces) { | ||||||
|         List<Point> movable = new ArrayList<>(); |         List<Point> movable = new ArrayList<>(); | ||||||
|         // Se till att vi inte är utanför brädet |         // 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; |             return movable; | ||||||
|         } |          | ||||||
|         final Piece potentialEnemy = pieces[pos.x][pos.y]; |         final Piece potentialEnemy = pieces[pos.x][pos.y]; | ||||||
|         // Ifall det är tomt här, gör ingenting |         // Ifall det är tomt här, gör ingenting | ||||||
|         if (potentialEnemy != null && potentialEnemy.isWhite() != this.isWhite()) { |         if (potentialEnemy != null && potentialEnemy.isWhite() != this.isWhite()) { | ||||||
| @@ -93,9 +93,8 @@ public class Pawn extends Piece { | |||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     protected boolean addMovesIfCan(Point pos, List<Point> movable, Piece[][] pieces, boolean allowedToRecurse) { |     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; |             return false; | ||||||
|         } |  | ||||||
|  |  | ||||||
|         Piece pieceToCheck = pieces[pos.x][pos.y]; |         Piece pieceToCheck = pieces[pos.x][pos.y]; | ||||||
|         if (pieceToCheck == null) { |         if (pieceToCheck == null) { | ||||||
| @@ -114,10 +113,9 @@ public class Pawn extends Piece { | |||||||
|  |  | ||||||
|         // Check if the pawn has moved to the end and should be transformed |         // Check if the pawn has moved to the end and should be transformed | ||||||
|         if (this.position.y == 0 && this.isWhite() |         if (this.position.y == 0 && this.isWhite() | ||||||
|                 || this.position.y == 7 && !this.isWhite()) { |                 || this.position.y == 7 && !this.isWhite())  | ||||||
|             transform(pieces); |             transform(pieces); | ||||||
|     } |     } | ||||||
|     } |  | ||||||
|  |  | ||||||
|     private void transform(Piece[][] pieces) throws HeadlessException { |     private void transform(Piece[][] pieces) throws HeadlessException { | ||||||
|         String[] transformations = { "Queen", "Rook", "Bishop", "Horse" }; |         String[] transformations = { "Queen", "Rook", "Bishop", "Horse" }; | ||||||
|   | |||||||
| @@ -8,6 +8,7 @@ import java.io.InputStream; | |||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import javax.imageio.ImageIO; | import javax.imageio.ImageIO; | ||||||
|  | import javax.swing.ImageIcon; | ||||||
|  |  | ||||||
| public abstract class Piece { | public abstract class Piece { | ||||||
|  |  | ||||||
| @@ -226,4 +227,8 @@ public abstract class Piece { | |||||||
|         return moved; |         return moved; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public BufferedImage getIcon() { | ||||||
|  |         return this.icon; | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,20 +1,35 @@ | |||||||
| package com.billenius.schack; | package com.billenius.schack; | ||||||
|  |  | ||||||
|  | import java.awt.Dimension; | ||||||
| import java.awt.HeadlessException; | import java.awt.HeadlessException; | ||||||
| import java.awt.event.ActionEvent; | import java.awt.event.ActionEvent; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| import java.net.Inet4Address; | import java.net.Inet4Address; | ||||||
| import java.net.NetworkInterface; | import java.net.NetworkInterface; | ||||||
| import java.net.UnknownHostException; | import java.net.UnknownHostException; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.List; | ||||||
|  |  | ||||||
|  | import javax.swing.DefaultListModel; | ||||||
| import javax.swing.JFrame; | import javax.swing.JFrame; | ||||||
|  | import javax.swing.JLabel; | ||||||
|  | import javax.swing.JList; | ||||||
| import javax.swing.JMenu; | import javax.swing.JMenu; | ||||||
| import javax.swing.JMenuBar; | import javax.swing.JMenuBar; | ||||||
| import javax.swing.JMenuItem; | import javax.swing.JMenuItem; | ||||||
| import javax.swing.JOptionPane; | 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 javax.swing.UIManager; | ||||||
|  |  | ||||||
|  | import com.billenius.schack.MoveLogging.Move; | ||||||
|  | import com.billenius.schack.MoveLogging.PieceRenderer; | ||||||
| import com.formdev.flatlaf.FlatLightLaf; | import com.formdev.flatlaf.FlatLightLaf; | ||||||
|  |  | ||||||
|  | import com.billenius.schack.MoveLogging.PieceType; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * |  * | ||||||
|  * @author Love Billenius & Simon Hansson |  * @author Love Billenius & Simon Hansson | ||||||
| @@ -37,11 +52,25 @@ public class Schack { | |||||||
|         frame = new JFrame(); |         frame = new JFrame(); | ||||||
|         frame.setTitle("Schack"); |         frame.setTitle("Schack"); | ||||||
|         frame.setAlwaysOnTop(false); |         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 |         // Might throw an IOException if the icon of the Pieces isn't embedded correctly | ||||||
|         final Board board = new Board(); |         final Board board = new Board(listModel); | ||||||
|         frame.setContentPane(board); |  | ||||||
|  |         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); |         frame.getContentPane().addMouseListener(board); | ||||||
|  |  | ||||||
|         // Create menu |         // Create menu | ||||||
| @@ -88,7 +117,6 @@ public class Schack { | |||||||
|         connectToOpponent.addActionListener((ActionEvent ae) -> { |         connectToOpponent.addActionListener((ActionEvent ae) -> { | ||||||
|             String opponentIP = JOptionPane.showInputDialog(null, "What's your opponents IP?"); |             String opponentIP = JOptionPane.showInputDialog(null, "What's your opponents IP?"); | ||||||
|             System.out.println("opponents ip: " + opponentIP); |             System.out.println("opponents ip: " + opponentIP); | ||||||
|  |  | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         // Add the menu stuff |         // Add the menu stuff | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user