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.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,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 @Override
public void mousePressed(MouseEvent mouseEvent) { public void mousePressed(MouseEvent mouseEvent) {
final int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE); final int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE);
@ -125,34 +151,9 @@ public class Board extends JPanel implements MouseListener {
return; return;
} }
Move m = new Move(selectedPiece, selectedPiece.position, clickedCoordinate); Move move = new Move(selectedPiece, selectedPiece.position, clickedCoordinate);
moveLog.add(m); moveList.addElement(move);
listModel.addElement(m); makeMove(move);
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:
}
} else { } else {
previouslyClickedPoint = new Point(clickedCoordinate); previouslyClickedPoint = new Point(clickedCoordinate);
validMovesToDraw = new ArrayList<>(); // Snabbare än .clear validMovesToDraw = new ArrayList<>(); // Snabbare än .clear

View File

@ -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 {

View File

@ -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