Skip to content

Commit

Permalink
UI network finally worked
Browse files Browse the repository at this point in the history
  • Loading branch information
ginowangsh4 committed Jun 7, 2018
1 parent 646b85b commit a277ac1
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 145 deletions.
Binary file modified image/board/board.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 23 additions & 26 deletions src/admin/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,25 @@
import tsuro.MPlayer;
import tsuro.Tile;
import tsuro.Token;
import tsuro.parser.BoardParser;
import tsuro.parser.Parser;
import tsuro.parser.TileParser;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.util.List;

public class Admin {
// public static final int PORT_NUM = 12345;
private static AdminSocket socket;
private static Parser parser;
private static MPlayer mPlayer;

// Commend line arguments as "PORT_NUMBER PLAYER_NAME STRATEGY(R/MS/LS)"
// If there is not argument for strategy, the default is to use a Random strategy
public static void main(String[] args) throws Exception {
// set up connection to local host
String hostname = "127.0.0.1";
int port = Integer.parseInt(args[0]);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// set up connection to local host
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
parser = new Parser(db);
AdminSocket socket = new AdminSocket(hostname, port, db);
socket = new AdminSocket("127.0.0.1", Integer.parseInt(args[0]));
switch (args[2]) {
case "R":
mPlayer = new MPlayer(MPlayer.Strategy.R, args[1]);
Expand All @@ -42,40 +39,40 @@ public static void main(String[] args) throws Exception {
mPlayer = new MPlayer(MPlayer.Strategy.R, args[1]);
}
while (socket.connectionEstablished()) {
String res = socket.readInputFromClient();
String res = socket.readInputFromServer();
// server has closed the connection
if (res == null) break;
Document doc = parser.stringToDocument(res);
Node node = doc.getFirstChild();
switch (node.getNodeName()) {
case "get-name":
processGetName(db, socket);
processGetName(db);
break;
case "initialize":
processInitialize(db, socket, node);
processInitialize(db, node);
break;
case "place-pawn":
processPlacePawn(db, socket, node);
processPlacePawn(db, node);
break;
case "play-turn":
processPlayTurn(db, socket, node);
processPlayTurn(db, node);
break;
case "end-game":
processEndGame(db, socket, node);
processEndGame(db, node);
break;
default:
throw new IllegalArgumentException("Admin: Invalid method call over network");
}
}
}

public static void processGetName(DocumentBuilder db, AdminSocket socket) throws Exception {
public static void processGetName(DocumentBuilder db) throws Exception {
String playerName = mPlayer.getName();
Document getNameResXML = parser.buildPlayerNameXML(playerName);
sendXMLToClient(socket, getNameResXML, "Admin: get-name complete");
sendXMLToClient(getNameResXML, "Admin: get-name complete");
}

public static void processInitialize(DocumentBuilder db, AdminSocket socket, Node node) throws Exception {
public static void processInitialize(DocumentBuilder db, Node node) throws Exception {
Node colorNode = node.getFirstChild();

int color = Token.getColorInt(colorNode.getTextContent());
Expand All @@ -86,20 +83,20 @@ public static void processInitialize(DocumentBuilder db, AdminSocket socket, Nod

mPlayer.initialize(color, colors);
Document voidXML = parser.buildVoidXML();
sendXMLToClient(socket, voidXML, "Admin: initialize complete ");
sendXMLToClient(voidXML, "Admin: initialize complete ");
}

public static void processPlacePawn(DocumentBuilder db, AdminSocket socket, Node node) throws Exception {
public static void processPlacePawn(DocumentBuilder db, Node node) throws Exception {
Node boardNode = node.getFirstChild();
Document boardDoc = Parser.fromNodeToDoc(boardNode, db);
Board board = parser.boardParser.fromXML(boardDoc);

Token token = mPlayer.placePawn(board);
Document pawnLocXML = parser.buildPawnLocXML(token.getPosition(), token.getIndex());
sendXMLToClient(socket, pawnLocXML, "Admin: place-pawn complete");
sendXMLToClient(pawnLocXML, "Admin: place-pawn complete");
}

public static void processPlayTurn(DocumentBuilder db, AdminSocket socket, Node node) throws Exception {
public static void processPlayTurn(DocumentBuilder db, Node node) throws Exception {
Node boardNode = node.getFirstChild();
Document boardDoc = Parser.fromNodeToDoc(boardNode, db);
Board board = parser.boardParser.fromXML(boardDoc);
Expand All @@ -113,10 +110,10 @@ public static void processPlayTurn(DocumentBuilder db, AdminSocket socket, Node

Tile tile = mPlayer.playTurn(board, hand, tilesLeft);
Document tileXML = parser.tileParser.buildXML(tile);
sendXMLToClient(socket, tileXML, "Admin: play-turn complete");
sendXMLToClient(tileXML, "Admin: play-turn complete");
}

public static void processEndGame(DocumentBuilder db, AdminSocket socket, Node node) throws Exception {
public static void processEndGame(DocumentBuilder db, Node node) throws Exception {
Node boardNode = node.getFirstChild();
Document boardDoc = Parser.fromNodeToDoc(boardNode, db);
Board board = parser.boardParser.fromXML(boardDoc);
Expand All @@ -127,12 +124,12 @@ public static void processEndGame(DocumentBuilder db, AdminSocket socket, Node n

mPlayer.endGame(board, colors);
Document voidXML = parser.buildVoidXML();
sendXMLToClient(socket, voidXML, "Admin: end-game complete");
sendXMLToClient(voidXML, "Admin: end-game complete");
}

public static void sendXMLToClient(AdminSocket socket, Document doc, String printMessage) throws Exception {
public static void sendXMLToClient(Document doc, String printMessage) throws Exception {
String s = parser.documentToString(doc);
System.out.println(printMessage + s);
socket.writeOutputToClient(s);
socket.writeOutputToServer(s);
}
}
24 changes: 13 additions & 11 deletions src/admin/AdminSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,29 @@

public class AdminSocket {
Socket socket;
DocumentBuilder db;
BufferedReader bufferedReader;
PrintWriter printWriter;
BufferedReader in;
PrintWriter out;

public AdminSocket(String hostName, int postNum, DocumentBuilder db) throws IOException {
public AdminSocket(String hostName, int postNum) throws IOException {
this.socket = new Socket(hostName, postNum);
this.bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.printWriter = new PrintWriter(socket.getOutputStream(), true);
this.db = db;
this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.out = new PrintWriter(socket.getOutputStream(), true);
}

public boolean connectionEstablished() {
return socket.isConnected();
}

public String readInputFromClient() throws IOException {
String input = bufferedReader.readLine();
public String readInputFromServer() throws IOException {
String input = in.readLine();
return input;
}

public void writeOutputToClient(String output) {
printWriter.println(output);
public void writeOutputToServer(String output) {
out.println(output);
}

public void closeConnection() throws IOException {
socket.close();
}
}
6 changes: 4 additions & 2 deletions src/admin/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
import javafx.stage.Stage;

public class App extends Application {
public static AdminSocket socket;

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage stage) throws IOException {
socket = new AdminSocket("127.0.0.1", 9000);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(App.class.getResource("StartGame.fxml"));

loader.setLocation(App.class.getResource("PlacePawn.fxml"));
BorderPane startGameView = loader.load();
Scene scene = new Scene(startGameView);
stage.setScene(scene);
Expand Down
2 changes: 1 addition & 1 deletion src/admin/StartGame.fxml → src/admin/PlacePawn.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="560.0" prefWidth="480.0" xmlns="http://javafx.com/javafx/9.0.4" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tsuro.admin.StartGameController">
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="560.0" prefWidth="480.0" xmlns="http://javafx.com/javafx/9.0.4" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tsuro.admin.PlacePawnController">
<center>
<ImageView fx:id="boardImageView" fitHeight="360.0" fitWidth="360.0" pickOnBounds="true" BorderPane.alignment="CENTER" />
</center>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package tsuro.admin;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class StartGameController {
public class PlacePawnController {
public static enum Side { TOP, LEFT, RIGHT, BOTTOM }

@FXML
Expand Down Expand Up @@ -62,6 +67,28 @@ public void initialize() throws FileNotFoundException {
else{
System.out.println("should not submit now");
}
try {
startServer();
} catch (IOException e) {
e.printStackTrace();
}

// now switch to play turn scene
FXMLLoader loader = new FXMLLoader();
loader.setLocation(App.class.getResource("PlayTurn.fxml"));
BorderPane playTurnView = null;
try {
playTurnView = loader.load();
} catch (IOException e) {
e.printStackTrace();
}
Stage stage = (Stage) submitButton.getScene().getWindow();
stage.setScene(new Scene(playTurnView));
});
}

private void startServer() throws IOException {
AdminSocket socket = new AdminSocket("127.0.0.1", 9000);
App.socket.writeOutputToServer("Testing sending back stuff from start game controller");
}
}
29 changes: 6 additions & 23 deletions src/admin/PlayTurn.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
Expand All @@ -13,14 +12,10 @@

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="560.0" prefWidth="480.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tsuro.admin.PlayTurnController">
<center>
<ImageView fx:id="boardImage" fitHeight="360.0" fitWidth="360.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER">
<image>
<Image url="@../../image/boardafter.png" />
</image>
</ImageView>
<ImageView fx:id="boardImage" fitHeight="360.0" fitWidth="360.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER" />
</center>
<top>
<VBox prefHeight="200.0" prefWidth="480.0" BorderPane.alignment="CENTER">
<VBox prefHeight="160.0" prefWidth="480.0" style="-fx-border-color: black;" BorderPane.alignment="CENTER">
<children>
<Label alignment="CENTER" prefHeight="20.0" prefWidth="480.0" text="It's your turn to play, please choose a tile." />
<GridPane prefHeight="140.0" prefWidth="480.0">
Expand All @@ -40,21 +35,9 @@
<Button fx:id="chooseTile1Button" mnemonicParsing="false" text="Choose Tile 1" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
<Button fx:id="chooseTile2Button" mnemonicParsing="false" text="Choose Tile 2" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
<Button fx:id="chooseTile3Button" mnemonicParsing="false" text="Choose Tile 3" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
<ImageView fx:id="tile1Image" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
<image>
<Image url="@../../image/tile.png" />
</image>
</ImageView>
<ImageView fx:id="tile2Image" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
<image>
<Image url="@../../image/tile.png" />
</image>
</ImageView>
<ImageView fx:id="tile3Image" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
<image>
<Image url="@../../image/tile.png" />
</image>
</ImageView>
<ImageView fx:id="tile1Image" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
<ImageView fx:id="tile2Image" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
<ImageView fx:id="tile3Image" fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
</children>
</GridPane>
</children></VBox>
Expand All @@ -66,6 +49,6 @@
<VBox prefHeight="360.0" prefWidth="60.0" BorderPane.alignment="CENTER" />
</right>
<bottom>
<HBox prefHeight="60.0" prefWidth="480.0" BorderPane.alignment="CENTER" />
<HBox prefHeight="30.0" prefWidth="480.0" BorderPane.alignment="CENTER" />
</bottom>
</BorderPane>
36 changes: 22 additions & 14 deletions src/admin/PlayTurnController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package tsuro.admin;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import tsuro.parser.Parser;
Expand All @@ -14,8 +18,8 @@
import java.util.HashMap;
import java.util.Map;

// WORK IN PROGRESS
public class PlayTurnController {
private Parser parser;
@FXML
private ImageView boardImage;
@FXML
Expand All @@ -35,15 +39,16 @@ public class PlayTurnController {
@FXML
private Button commitMoveButton;

private Map tileMap;

private Button currTileButton;

@FXML
public void initialize() {
boardImage.setOnMouseClicked(event -> {
public void initialize() throws Exception {
Image image = new Image(new FileInputStream("image/board/board.png"));
boardImage.setImage(image);

});
tile1Image.setImage(new Image(new FileInputStream("image/hand/tile.png")));
tile2Image.setImage(new Image(new FileInputStream("image/hand/tile.png")));
tile3Image.setImage(new Image(new FileInputStream("image/hand/tile.png")));

chooseTile1Button.setOnAction(event -> {
currTileButton = chooseTile1Button;
Expand All @@ -59,19 +64,22 @@ public void initialize() {

rotateTileButton.setOnAction(event -> {
System.out.println("Rotating tile [" + currTileButton.getText() + "]");
});

Image image = null;
commitMoveButton.setOnAction(event -> {

});

commitMoveButton.setOnMouseClicked(event -> {
try {
image = new Image(new FileInputStream("image/uiboard.png"));
} catch (FileNotFoundException e) {
startServer();
} catch (IOException e) {
e.printStackTrace();
}
boardImage.setImage(image);
System.out.println("Board image updated!");
});
}

commitMoveButton.setOnAction(event -> {

});
private void startServer() throws IOException {
App.socket.writeOutputToServer("Testing sending back stuff from play turn controller");
}
}
Loading

0 comments on commit a277ac1

Please sign in to comment.