Skip to content

Commit

Permalink
add local play
Browse files Browse the repository at this point in the history
  • Loading branch information
randomtestfive committed Apr 12, 2018
1 parent 11ce2da commit d8159d1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/randomtestfive/chess3d/graphics/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Optional;

import randomtestfive.chess3d.core.Player;
import randomtestfive.chess3d.core.Position3D;
import randomtestfive.chess3d.core.pieces.ChessPiece;
import randomtestfive.chess3d.network.Chess3DClient;

Expand Down Expand Up @@ -56,4 +57,12 @@ public static void done() {
public static boolean getReady() {
return ready;
}

public static void commitMove(Position3D s, Position3D e) {
if(client!=null) {
client.commitMove(s, e);
} else {
player=Player.values()[(player.ordinal()+1)%2];
}
}
}
14 changes: 12 additions & 2 deletions src/randomtestfive/chess3d/graphics/Startup.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@
import javax.swing.JTextField;

import randomtestfive.chess3d.core.Board;
import randomtestfive.chess3d.core.Player;
import randomtestfive.chess3d.core.StartPieces;
import randomtestfive.chess3d.network.Chess3DClient;
import randomtestfive.chess3d.network.Chess3DServer;

public class Startup implements ActionListener {
private JFrame frame;
JButton server;
JButton server, local;
JTextField text;

public Startup() {
frame = new JFrame("3D Chess");
local = new JButton("Start Local");
local.addActionListener(this);
server = new JButton("Start Server");
server.addActionListener(this);
JButton client = new JButton("Start Client");
Expand All @@ -35,6 +38,7 @@ public Startup() {
// TODO Auto-generated catch block
e.printStackTrace();
}
frame.add(local);
frame.add(server);
frame.add(client);
text = new JTextField(5);
Expand All @@ -53,9 +57,15 @@ 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);
} else {
Globals.setPlayer(Player.WHITE);
Globals.ready();
}

new Chess3DClient(text.getText(), b, new MainDisplay(b).boards::repaint);
frame.dispose();
}
}
2 changes: 1 addition & 1 deletion src/randomtestfive/chess3d/graphics/SubboardDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void mouseClicked(MouseEvent e) {
.filter(p->p.getX()==gridx)
.filter(p->p.getY()==gridy)
.findFirst().ifPresent((p)->{
Globals.getClient().commitMove(piece.getPosition(), p);
Globals.commitMove(piece.getPosition(), p);
Globals.nextTurn();
piece.moveTo(p,board.getPieces());
});
Expand Down
7 changes: 2 additions & 5 deletions src/randomtestfive/chess3d/network/Chess3DClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.function.Consumer;

import randomtestfive.chess3d.core.Board;
import randomtestfive.chess3d.core.Player;
Expand All @@ -25,21 +24,19 @@ public Chess3DClient(String ip, Board b, Runnable c) {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Globals.setPlayer(Player.values()[Integer.parseInt(in.readLine().substring(1))]);
Globals.setClient(this);
new Thread(new ClientThread(out, in, b, c)).start();
new Thread(new ClientThread(in, b, c)).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private static class ClientThread implements Runnable {
private PrintWriter out;
private BufferedReader in;
private Board board;
private Runnable callback;

public ClientThread(PrintWriter o, BufferedReader i, Board b, Runnable c) {
out = o;
public ClientThread(BufferedReader i, Board b, Runnable c) {
in = i;
board = b;
callback = c;
Expand Down

0 comments on commit d8159d1

Please sign in to comment.