mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 12:50:10 +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.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,6 +109,34 @@ public class Board extends JPanel implements MouseListener {
|
||||
}
|
||||
}
|
||||
|
||||
private void makeMove(Move move) {
|
||||
move.movedPiece.move(pieces, move.to);
|
||||
turnCount++;
|
||||
whitesTurn = !whitesTurn;
|
||||
|
||||
SchackState state = getSchackState();
|
||||
switch (state) {
|
||||
case SCHACK:
|
||||
JOptionPane.showMessageDialog(this, "Du står i schack");
|
||||
break;
|
||||
case SCHACKMATT:
|
||||
case PATT:
|
||||
String stateStr = state.toString();
|
||||
String msg = stateStr.charAt(0) + stateStr.substring(1, stateStr.length()).toLowerCase();
|
||||
int choice = JOptionPane.showConfirmDialog(this, msg + "\nVill du starta om?");
|
||||
|
||||
if (choice == JOptionPane.YES_OPTION)
|
||||
try {
|
||||
restartGame();
|
||||
} catch (IOException ex) {
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent mouseEvent) {
|
||||
final int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE);
|
||||
@ -125,34 +151,9 @@ public class Board extends JPanel implements MouseListener {
|
||||
return;
|
||||
}
|
||||
|
||||
Move m = new Move(selectedPiece, selectedPiece.position, clickedCoordinate);
|
||||
moveLog.add(m);
|
||||
listModel.addElement(m);
|
||||
selectedPiece.move(pieces, clickedCoordinate);
|
||||
turnCount++;
|
||||
whitesTurn = !whitesTurn;
|
||||
|
||||
SchackState state = getSchackState();
|
||||
switch (state) {
|
||||
case SCHACK:
|
||||
JOptionPane.showMessageDialog(this, "Du står i schack");
|
||||
break;
|
||||
case SCHACKMATT:
|
||||
case PATT:
|
||||
String stateStr = state.toString();
|
||||
String msg = stateStr.charAt(0) + stateStr.substring(1, stateStr.length()).toLowerCase();
|
||||
int choice = JOptionPane.showConfirmDialog(this, msg + "\nVill du starta om?");
|
||||
|
||||
if (choice == JOptionPane.YES_OPTION)
|
||||
try {
|
||||
restartGame();
|
||||
} catch (IOException ex) {
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
Move move = new Move(selectedPiece, selectedPiece.position, clickedCoordinate);
|
||||
moveList.addElement(move);
|
||||
makeMove(move);
|
||||
} else {
|
||||
previouslyClickedPoint = new Point(clickedCoordinate);
|
||||
validMovesToDraw = new ArrayList<>(); // Snabbare än .clear
|
||||
|
@ -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 {
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user