flytta ut move logik till metod

This commit is contained in:
Love 2022-12-04 18:15:26 +01:00
parent f52f3e7d0d
commit be59f829c5
No known key found for this signature in database
GPG Key ID: A3C10DC241C8FA9F
3 changed files with 33 additions and 41 deletions

View File

@ -9,7 +9,6 @@ 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;
@ -27,13 +26,12 @@ 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;
private DefaultListModel<Move> moveList;
public Board(DefaultListModel<Move> listModel) throws IOException {
this.pieces = getPieces();
setPreferredSize(new Dimension(800, 800));
this.listModel = listModel;
this.moveList = listModel;
}
/**
@ -111,24 +109,8 @@ public class Board extends JPanel implements MouseListener {
}
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
final int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE);
final int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE);
final Point clickedCoordinate = new Point(mouseCoordinateX, mouseCoordinateY);
// Ifall vi har tryckt en pjäs och sedan ska dit
if (validMovesToDraw.contains(clickedCoordinate)) {
Piece selectedPiece = pieces[previouslyClickedPoint.x][previouslyClickedPoint.y];
if (selectedPiece == null) {
validMovesToDraw.clear();
return;
}
Move m = new Move(selectedPiece, selectedPiece.position, clickedCoordinate);
moveLog.add(m);
listModel.addElement(m);
selectedPiece.move(pieces, clickedCoordinate);
private void makeMove(Move move) {
move.movedPiece.move(pieces, move.to);
turnCount++;
whitesTurn = !whitesTurn;
@ -153,6 +135,25 @@ public class Board extends JPanel implements MouseListener {
default:
}
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
final int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE);
final int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE);
final Point clickedCoordinate = new Point(mouseCoordinateX, mouseCoordinateY);
// Ifall vi har tryckt en pjäs och sedan ska dit
if (validMovesToDraw.contains(clickedCoordinate)) {
Piece selectedPiece = pieces[previouslyClickedPoint.x][previouslyClickedPoint.y];
if (selectedPiece == null) {
validMovesToDraw.clear();
return;
}
Move move = new Move(selectedPiece, selectedPiece.position, clickedCoordinate);
moveList.addElement(move);
makeMove(move);
} else {
previouslyClickedPoint = new Point(clickedCoordinate);
validMovesToDraw = new ArrayList<>(); // Snabbare än .clear

View File

@ -8,7 +8,6 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public abstract class Piece {

View File

@ -5,14 +5,10 @@ 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;
@ -20,16 +16,12 @@ 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