mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 21:00:11 +01:00
flytta ut move logik till metod
This commit is contained in:
parent
f52f3e7d0d
commit
be59f829c5
@ -9,7 +9,6 @@ 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.DefaultListModel;
|
||||||
@ -27,13 +26,12 @@ 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> moveList;
|
||||||
private DefaultListModel<Move> listModel;
|
|
||||||
|
|
||||||
public Board(DefaultListModel<Move> listModel) 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;
|
this.moveList = listModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -111,24 +109,8 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void makeMove(Move move) {
|
||||||
public void mousePressed(MouseEvent mouseEvent) {
|
move.movedPiece.move(pieces, move.to);
|
||||||
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 på en pjäs och sedan ska gå 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);
|
|
||||||
turnCount++;
|
turnCount++;
|
||||||
whitesTurn = !whitesTurn;
|
whitesTurn = !whitesTurn;
|
||||||
|
|
||||||
@ -153,6 +135,25 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
default:
|
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 på en pjäs och sedan ska gå 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 {
|
} else {
|
||||||
previouslyClickedPoint = new Point(clickedCoordinate);
|
previouslyClickedPoint = new Point(clickedCoordinate);
|
||||||
validMovesToDraw = new ArrayList<>(); // Snabbare än .clear
|
validMovesToDraw = new ArrayList<>(); // Snabbare än .clear
|
||||||
|
@ -8,7 +8,6 @@ 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 {
|
||||||
|
|
||||||
|
@ -5,14 +5,10 @@ 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.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.swing.DefaultListModel;
|
import javax.swing.DefaultListModel;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JLabel;
|
|
||||||
import javax.swing.JList;
|
import javax.swing.JList;
|
||||||
import javax.swing.JMenu;
|
import javax.swing.JMenu;
|
||||||
import javax.swing.JMenuBar;
|
import javax.swing.JMenuBar;
|
||||||
@ -20,16 +16,12 @@ import javax.swing.JMenuItem;
|
|||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
import javax.swing.JScrollPane;
|
import javax.swing.JScrollPane;
|
||||||
import javax.swing.JSplitPane;
|
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.Move;
|
||||||
import com.billenius.schack.MoveLogging.PieceRenderer;
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user