Skip to content

Commit

Permalink
make turn counter and win state more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
randomtestfive committed Apr 16, 2018
1 parent d8159d1 commit 0394752
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
72 changes: 43 additions & 29 deletions src/randomtestfive/chess3d/graphics/MainDisplay.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package randomtestfive.chess3d.graphics;


import java.awt.Graphics;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -16,50 +17,63 @@
import randomtestfive.chess3d.core.pieces.ChessPiece;
import randomtestfive.chess3d.core.pieces.King;

public class MainDisplay {
public JFrame frame;
JPanel boards;
public class MainDisplay extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel boards;
private Board b;

public MainDisplay(Board b) {
frame = new JFrame("3D Chess (NOT READY)");
super("3D Chess (NOT READY)");
this.b = b;
boards = new JPanel();
boards.setLayout(new BoxLayout(boards, BoxLayout.X_AXIS));
boards.add(new SubboardDisplay(false, b, 4));
boards.add(new SubboardDisplay(false, b, 4, this::repaint));
boards.add(Box.createHorizontalStrut(10));
boards.add(new SubboardDisplay(true, b, 3));
boards.add(new SubboardDisplay(true, b, 3, this::repaint));
boards.add(Box.createHorizontalStrut(10));
boards.add(new SubboardDisplay(false, b, 2));
boards.add(new SubboardDisplay(false, b, 2, this::repaint));
boards.add(Box.createHorizontalStrut(10));
boards.add(new SubboardDisplay(true, b, 1));
boards.add(new SubboardDisplay(true, b, 1, this::repaint));
boards.add(Box.createHorizontalStrut(10));
boards.add(new SubboardDisplay(false, b, 0));
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
JLabel turn = new JLabel("Turn: 1 - WHITE");
boards.add(new SubboardDisplay(false, b, 0, this::repaint));
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
JLabel turn = new JLabel("Turn: 1 - WHITE") {
private static final long serialVersionUID = 1L;

@Override
public void paint(Graphics g) {
setText("Turn: "+Globals.getTurn()+" - "+Player.values()[Globals.getTurn()%2]);
super.paint(g);
}
};
turn.setHorizontalAlignment(JLabel.LEFT);
turn.setAlignmentX(-1);
boards.setAlignmentX(-1);
frame.add(turn);
frame.add(boards);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
add(turn);
add(boards);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
new Thread(()->{
while(!Globals.getReady()) {
Thread.yield();
}
frame.setTitle("3D Chess - "+Globals.getPlayer());
while(true) {
turn.setText("Turn: "+Globals.getTurn()+" - "+Player.values()[Globals.getTurn()%2]);
Set<ChessPiece> kings = b.getPieces().stream()
.filter(p->p instanceof King)
.collect(Collectors.toSet());
if(kings.size()<2) {
Globals.done();
JOptionPane.showMessageDialog(frame, kings.stream().findFirst().get().getOwner()+" WINS");
System.exit(0);
}
Thread.yield();
}
setTitle("3D Chess - "+Globals.getPlayer());
}).start();
}

@Override
public void paint(Graphics g) {
super.paint(g);
if(b!=null) {
Set<ChessPiece> kings = b.getPieces().stream()
.filter(p->p instanceof King)
.collect(Collectors.toSet());
if(kings.size()<2) {
Globals.done();
JOptionPane.showMessageDialog(this, kings.stream().findFirst().get().getOwner()+" WINS");
System.exit(0);
}
}
}
}
3 changes: 2 additions & 1 deletion src/randomtestfive/chess3d/graphics/Startup.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ public void actionPerformed(ActionEvent arg0) {
}
Board b = new Board();
b.getPieces().addAll(StartPieces.getStartPieces());

MainDisplay md = new MainDisplay(b);

if(!arg0.getSource().equals(local)) {
new Chess3DClient(text.getText(), b, md.boards::repaint);
new Chess3DClient(text.getText(), b, md::repaint);
} else {
Globals.setPlayer(Player.WHITE);
Globals.ready();
Expand Down
6 changes: 5 additions & 1 deletion src/randomtestfive/chess3d/graphics/SubboardDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public class SubboardDisplay extends JPanel implements MouseListener, MouseMotio
private Board board;
private int layer;
private static Map<String, BufferedImage[]> sprites;
private final Runnable callback;

public SubboardDisplay(boolean invert, Board b, int layer) {
public SubboardDisplay(boolean invert, Board b, int layer, Runnable c) {
super();
//setSize(100, 100);
this.invert = invert;
Expand Down Expand Up @@ -60,6 +61,8 @@ public SubboardDisplay(boolean invert, Board b, int layer) {
addMouseListener(this);
addMouseMotionListener(this);
setPreferredSize(new Dimension(320, 320));

callback = c;
}

@Override
Expand Down Expand Up @@ -107,6 +110,7 @@ public void mouseClicked(MouseEvent e) {
Globals.commitMove(piece.getPosition(), p);
Globals.nextTurn();
piece.moveTo(p,board.getPieces());
callback.run();
});
Globals.setSelection(null);
} else {
Expand Down

0 comments on commit 0394752

Please sign in to comment.