Skip to content

Commit

Permalink
started to link hplayer placepawn with gui
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifermliu committed Jun 7, 2018
1 parent 1bfd189 commit 646b85b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/admin/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static void main(String[] args) {
public void start(Stage stage) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(App.class.getResource("StartGame.fxml"));

BorderPane startGameView = loader.load();
Scene scene = new Scene(startGameView);
stage.setScene(scene);
Expand Down
30 changes: 12 additions & 18 deletions src/admin/StartGameController.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package tsuro.admin;

import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.effect.ImageInput;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

public class StartGameController {
public static enum Side { TOP, LEFT, RIGHT, BOTTOM }
Expand All @@ -22,48 +16,48 @@ public static enum Side { TOP, LEFT, RIGHT, BOTTOM }
private ImageView boardImageView;

@FXML
private ChoiceBox<String> sideDropdown;
private ChoiceBox<Side> sideDropdown;

@FXML
private ChoiceBox<Integer> indexDropdown;

@FXML
private Button submitButton;

private String side;
private Integer index;
public static Side startSide;
public static Integer startIndex;
private int BOARD_SIZE = 360;


public void initialize() throws FileNotFoundException {

Image boardImage = new Image(new FileInputStream("board/board.png"), BOARD_SIZE, BOARD_SIZE, false, true);
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");
});

sideDropdown.getItems().addAll("top", "down", "left", "right");
sideDropdown.getItems().addAll(Side.TOP, Side.BOTTOM, Side.LEFT, Side.RIGHT);

for(int i = 0; i < 12; i++){
indexDropdown.getItems().add(i);
}
sideDropdown.setOnMouseClicked(event -> {
System.out.println("side dropdown clicked");
System.out.println("startSide dropdown clicked");
});

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

submitButton.setOnMouseClicked(event -> {
System.out.println("submit clicked");
side = sideDropdown.getValue();
index = indexDropdown.getValue();
if(side != null && index != null){
System.out.println(side);
System.out.println(index);
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");
Expand Down
18 changes: 13 additions & 5 deletions src/main/HPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import tsuro.admin.App;
import tsuro.admin.StartGameController;
import tsuro.admin.UISuite;
import tsuro.parser.BoardParser;
import tsuro.parser.Parser;
Expand Down Expand Up @@ -39,7 +41,10 @@ public void initialize (int color, List<Integer> colors) {
}

public Token placePawn(Board b) throws Exception {
Token token = generateTokenBySideIndex(color, UISuite.startSide, UISuite.startIndex);
generateBoardImage(parser.boardParser.buildXML(b), -1, -1);


Token token = generateTokenBySideIndex(color, StartGameController.startSide, StartGameController.startIndex);
return token;
}

Expand Down Expand Up @@ -70,22 +75,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, UISuite.Side side, int index) throws Exception {
public static Token generateTokenBySideIndex(int colorIndex, StartGameController.Side side, int index) throws Exception {
if (index < 0 || index > 11) {
throw new Exception("Index is not valid");
}
int indexOnTile, x, y;
if (side == UISuite.Side.TOP) {
if (side == StartGameController.Side.TOP) {
x = index / 2;
y = -1;
indexOnTile = Tile.neighborIndex.get(index % 2);
}
else if (side == UISuite.Side.BOTTOM) {
else if (side == StartGameController.Side.BOTTOM) {
x = index / 2;
y = 6;
indexOnTile = index % 2;
}
else if (side == UISuite.Side.LEFT) {
else if (side == StartGameController.Side.LEFT) {
x = -1;
y = index / 2;
indexOnTile = index % 2 + 2;
Expand Down Expand Up @@ -122,6 +127,9 @@ private void generateImages(Board b, List<Tile> hand) throws Exception {

private void generateBoardImage(Document doc, int tileIndex, int rotationIndex) throws Exception {
String command = "./visualize -b -i image/board/" + tileIndex + "/" + rotationIndex + ".png";
if (tileIndex == -1 && rotationIndex == -1) {
command = "./visualize -b -i image/board/board.png";
}
String line;
Process p = Runtime.getRuntime().exec(command);
PrintWriter out = new PrintWriter(p.getOutputStream(), true);
Expand Down
17 changes: 9 additions & 8 deletions src/test/HPlayerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tsuro;

import org.junit.jupiter.api.Test;
import tsuro.admin.StartGameController;
import tsuro.admin.UISuite;

import static org.junit.jupiter.api.Assertions.*;
Expand All @@ -10,42 +11,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, UISuite.Side.LEFT, 2);
Token token = HPlayer.generateTokenBySideIndex(0, StartGameController.Side.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, UISuite.Side.LEFT, 7);
token = HPlayer.generateTokenBySideIndex(0, StartGameController.Side.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, UISuite.Side.RIGHT, 4);
token = HPlayer.generateTokenBySideIndex(0, StartGameController.Side.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, UISuite.Side.RIGHT, 11);
token = HPlayer.generateTokenBySideIndex(0, StartGameController.Side.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, UISuite.Side.TOP, 6);
token = HPlayer.generateTokenBySideIndex(0, StartGameController.Side.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, UISuite.Side.TOP, 9);
token = HPlayer.generateTokenBySideIndex(0, StartGameController.Side.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, UISuite.Side.BOTTOM, 8);
token = HPlayer.generateTokenBySideIndex(0, StartGameController.Side.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, UISuite.Side.BOTTOM, 5);
token = HPlayer.generateTokenBySideIndex(0, StartGameController.Side.BOTTOM, 5);
assertTrue(expected.isSameToken(token), "Generated token is not as expected.");
}

Expand Down

0 comments on commit 646b85b

Please sign in to comment.