Skip to content

Commit

Permalink
Finished place pawn ui
Browse files Browse the repository at this point in the history
  • Loading branch information
ginowangsh4 committed Jun 7, 2018
1 parent a277ac1 commit 7dce9ca
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 65 deletions.
6 changes: 3 additions & 3 deletions src/admin/AdminSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import java.net.Socket;

public class AdminSocket {
Socket socket;
BufferedReader in;
PrintWriter out;
private Socket socket;
private BufferedReader in;
private PrintWriter out;

public AdminSocket(String hostName, int postNum) throws IOException {
this.socket = new Socket(hostName, postNum);
Expand Down
14 changes: 14 additions & 0 deletions src/admin/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
Expand All @@ -25,6 +26,19 @@ public void start(Stage stage) throws IOException {
stage.setTitle("Tsuro Game");
stage.show();
}

public static void changeScene(Node node, String source) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(App.class.getResource(source));
BorderPane newView = null;
try {
newView = loader.load();
} catch (IOException e) {
e.printStackTrace();
}
Stage stage = (Stage) node.getScene().getWindow();
stage.setScene(new Scene(newView));
}
}


Expand Down
55 changes: 18 additions & 37 deletions src/admin/PlacePawnController.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package tsuro.admin;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
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 PlacePawnController {
public static enum Side { TOP, LEFT, RIGHT, BOTTOM }
Expand All @@ -34,13 +30,14 @@ public static enum Side { TOP, LEFT, RIGHT, BOTTOM }
private int BOARD_SIZE = 360;



public void initialize() throws FileNotFoundException {

Image boardImage = new Image(new FileInputStream("image/board/board.png"), BOARD_SIZE, BOARD_SIZE, false, true);
boardImageView.setImage(boardImage);

boardImageView.setOnMouseClicked(event -> {
System.out.println("image clicked");
System.out.println("Image clicked");
});

sideDropdown.getItems().addAll(Side.TOP, Side.BOTTOM, Side.LEFT, Side.RIGHT);
Expand All @@ -49,46 +46,30 @@ public void initialize() throws FileNotFoundException {
indexDropdown.getItems().add(i);
}
sideDropdown.setOnMouseClicked(event -> {
System.out.println("startSide dropdown clicked");
System.out.println("StartSide dropdown clicked");
});

indexDropdown.setOnMouseClicked(event -> {
System.out.println("startIndex dropdown clicked");
System.out.println("StartIndex dropdown clicked");
});

submitButton.setOnMouseClicked(event -> {
System.out.println("submit clicked");
System.out.println("Submit button clicked");
startSide = sideDropdown.getValue();
startIndex = indexDropdown.getValue();
if(startSide != null && startIndex != null){
System.out.println(startSide);
System.out.println(startIndex);
}
else{
System.out.println("should not submit now");
if (startSide != null && startIndex != null) {
System.out.println("Selected side = " + startSide);
System.out.println("Selected index = " + startIndex);

App.socket.writeOutputToServer(startSide.toString() + "," + startIndex.toString());

App.changeScene(submitButton, "PlayTurn.fxml");
} else {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Tsuro Game Dialog");
alert.setContentText("Shouldn't submit now. Please choose both side and index!");
alert.showAndWait();
}
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");
}
}
13 changes: 4 additions & 9 deletions src/admin/PlayTurnController.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public void initialize() throws Exception {
tile2Image.setImage(new Image(new FileInputStream("image/hand/tile.png")));
tile3Image.setImage(new Image(new FileInputStream("image/hand/tile.png")));

rotateTileButton.setDisable(true);
commitMoveButton.setDisable(true);

chooseTile1Button.setOnAction(event -> {
currTileButton = chooseTile1Button;
});
Expand All @@ -71,15 +74,7 @@ public void initialize() throws Exception {
});

commitMoveButton.setOnMouseClicked(event -> {
try {
startServer();
} catch (IOException e) {
e.printStackTrace();
}
App.socket.writeOutputToServer("Testing sending back stuff from play turn controller");
});
}

private void startServer() throws IOException {
App.socket.writeOutputToServer("Testing sending back stuff from play turn controller");
}
}
18 changes: 10 additions & 8 deletions src/main/HPlayer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tsuro;

import javafx.application.Application;
import org.w3c.dom.Document;
import tsuro.admin.App;
import tsuro.admin.PlacePawnController;
import tsuro.parser.Parser;

Expand Down Expand Up @@ -35,7 +37,6 @@ public HPlayer(String name) throws Exception {
}

public String getName() throws IOException {
System.out.println(in.readLine());
return name;
}

Expand All @@ -46,8 +47,8 @@ public void initialize (int color, List<Integer> colors) {

public Token placePawn(Board b) throws Exception {
generateBoardImage(parser.boardParser.buildXML(b), -1, -1);
System.out.println(in.readLine());
return null;
String[] sideIndex= in.readLine().split(",");
return buildTokenFromSideAndIndex(color, sideIndex[0], Integer.parseInt(sideIndex[1]));
}

public Tile playTurn(Board b, List<Tile> hand, int tilesLeft) throws Exception {
Expand Down Expand Up @@ -75,22 +76,22 @@ public void endGame(Board b, List<Integer> colors) throws Exception {
* @param index index of location clicked
* @return token game object
*/
public static Token generateTokenBySideIndex(int colorIndex, PlacePawnController.Side side, int index) throws Exception {
public static Token buildTokenFromSideAndIndex(int colorIndex, String side, int index) throws Exception {
if (index < 0 || index > 11) {
throw new Exception("Index is not valid");
}
int indexOnTile, x, y;
if (side == PlacePawnController.Side.TOP) {
if (side == "TOP") {
x = index / 2;
y = -1;
indexOnTile = Tile.neighborIndex.get(index % 2);
}
else if (side == PlacePawnController.Side.BOTTOM) {
else if (side == "BOTTOM") {
x = index / 2;
y = 6;
indexOnTile = index % 2;
}
else if (side == PlacePawnController.Side.LEFT) {
else if (side == "LEFT") {
x = -1;
y = index / 2;
indexOnTile = index % 2 + 2;
Expand Down Expand Up @@ -157,6 +158,7 @@ private void generateTileImage(Document doc, int tileIndex) throws Exception {
public static void main(String[] args) throws Exception {
HPlayer p = new HPlayer("Jeff");
p.getName();
p.placePawn(new Board());
Token t = p.placePawn(new Board());
System.out.println();
}
}
16 changes: 8 additions & 8 deletions src/test/HPlayerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,42 @@ class HPlayerTest {
void generateTokenBySideIndex() throws Exception {
// left and even index
Token expected = new Token(0, new int[]{-1,1}, 2);
Token token = HPlayer.generateTokenBySideIndex(0, PlacePawnController.Side.LEFT, 2);
Token token = HPlayer.buildTokenFromSideAndIndex(0, "LEFT", 2);
assertTrue(expected.isSameToken(token), "Generated token is not as expected.");

// left and odd index
expected = new Token(0, new int[]{-1,3}, 3);
token = HPlayer.generateTokenBySideIndex(0, PlacePawnController.Side.LEFT, 7);
token = HPlayer.buildTokenFromSideAndIndex(0, "LEFT", 7);
assertTrue(expected.isSameToken(token), "Generated token is not as expected.");

// right and even index
expected = new Token(0, new int[]{6,2}, 7);
token = HPlayer.generateTokenBySideIndex(0, PlacePawnController.Side.RIGHT, 4);
token = HPlayer.buildTokenFromSideAndIndex(0, "RIGHT", 4);
assertTrue(expected.isSameToken(token), "Generated token is not as expected.");

// right and odd index
expected = new Token(0, new int[]{6,5}, 6);
token = HPlayer.generateTokenBySideIndex(0, PlacePawnController.Side.RIGHT, 11);
token = HPlayer.buildTokenFromSideAndIndex(0, "RIGHT", 11);
assertTrue(expected.isSameToken(token), "Generated token is not as expected.");

// top and even index
expected = new Token(0, new int[]{3,-1}, 5);
token = HPlayer.generateTokenBySideIndex(0, PlacePawnController.Side.TOP, 6);
token = HPlayer.buildTokenFromSideAndIndex(0, "TOP", 6);
assertTrue(expected.isSameToken(token), "Generated token is not as expected.");

// top and odd index
expected = new Token(0, new int[]{4,-1}, 4);
token = HPlayer.generateTokenBySideIndex(0, PlacePawnController.Side.TOP, 9);
token = HPlayer.buildTokenFromSideAndIndex(0, "TOP", 9);
assertTrue(expected.isSameToken(token), "Generated token is not as expected.");

// bottom and even index
expected = new Token(0, new int[]{4,6}, 0);
token = HPlayer.generateTokenBySideIndex(0, PlacePawnController.Side.BOTTOM, 8);
token = HPlayer.buildTokenFromSideAndIndex(0, "BOTTOM", 8);
assertTrue(expected.isSameToken(token), "Generated token is not as expected.");

// bottom and odd index
expected = new Token(0, new int[]{2,6}, 1);
token = HPlayer.generateTokenBySideIndex(0, PlacePawnController.Side.BOTTOM, 5);
token = HPlayer.buildTokenFromSideAndIndex(0, "BOTTOM", 5);
assertTrue(expected.isSameToken(token), "Generated token is not as expected.");
}

Expand Down

0 comments on commit 7dce9ca

Please sign in to comment.