Skip to content

Commit

Permalink
added turn numbers and turn advance button
Browse files Browse the repository at this point in the history
  • Loading branch information
baz committed Aug 29, 2014
1 parent 8570c8a commit 2d7aeee
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 6 deletions.
10 changes: 9 additions & 1 deletion cardshifter-core/src/main/java/com/cardshifter/core/Game.java
Expand Up @@ -19,7 +19,7 @@ public class Game {
private final Random random;
public final LuaValue data;
private boolean gameOver = false;

private int turnNumber;
private Player currentPlayer;

public Game(InputStream file, Random random) {
Expand All @@ -33,11 +33,17 @@ public Game(InputStream file, Random random) {
this.players.add(new Player(this, "Player1"));
this.players.add(new Player(this, "Player2"));
this.random = random;

this.turnNumber = 0;
}

public Game(InputStream file) {
this(file, new Random());
}

public int getTurnNumber() {
return this.turnNumber;
}

public Player getCurrentPlayer() {
return currentPlayer;
Expand Down Expand Up @@ -94,6 +100,8 @@ public void nextTurn() {
this.currentPlayer = currentPlayer == null ? players.get(0) : currentPlayer.getNextPlayer();

this.events.callEvent(Events.TURN_START, CoerceJavaToLua.coerce(this.currentPlayer), null);

turnNumber++;
}

public int randomInt(int count) {
Expand Down
2 changes: 2 additions & 0 deletions cardshifter-fx/pom.xml
Expand Up @@ -13,6 +13,8 @@
<url>https://github.com/Cardshifter/Cardshifter</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
Expand Down
Expand Up @@ -2,12 +2,22 @@

import com.cardshifter.core.CommandLineOptions;
import com.cardshifter.core.Game;
import com.cardshifter.core.Player;
import com.cardshifter.core.TargetAction;
import com.cardshifter.core.Targetable;
import com.cardshifter.core.UsableAction;
import com.cardshifter.core.Zone;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.Scanner;
import java.util.stream.Collectors;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
Expand All @@ -18,6 +28,9 @@ public class FXMLDocumentController implements Initializable {
//need a forward declaration so that this is global to the class
Game game;

//hack to make the buttons work properly
private boolean gameHasStarted = false;

//I think this is a public constructor, this code initializes the Game
public FXMLDocumentController() throws Exception {
CommandLineOptions options = new CommandLineOptions();
Expand All @@ -31,13 +44,126 @@ public FXMLDocumentController() throws Exception {
@FXML
private void handleButtonAction(ActionEvent event) {
//System.out.println("You clicked me!");
label.setText("Starting Game");
game.getEvents().startGame(game);
if (!gameHasStarted) {
label.setText("Starting Game");
game.getEvents().startGame(game);
gameHasStarted = true;
}
}

@FXML
private Label turnLabel;

@FXML
private void handleTurnButtonAction(ActionEvent event) {
if (gameHasStarted) {
game.nextTurn();
turnLabel.setText(String.format("Turn Number %d", game.getTurnNumber()));
}
}

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}


public void play() {
Scanner input = new Scanner(System.in);
while (!game.isGameOver()) {
outputGameState();
List<UsableAction> actions = game.getAllActions().stream().filter(action -> action.isAllowed()).collect(Collectors.toList());
outputList(actions);

String in = input.nextLine();
if (in.equals("exit")) {
break;
}

handleActionInput(actions, in, input);
}
//print("--------------------------------------------");
outputGameState();
//print("Game over!");
}

private void handleActionInput(final List<UsableAction> actions, final String in, Scanner input) {
Objects.requireNonNull(actions, "actions");
Objects.requireNonNull(in, "in");
//print("Choose an action:");

try {
int value = Integer.parseInt(in);
if (value < 0 || value >= actions.size()) {
//print("Action index out of range: " + value);
return;
}

UsableAction action = actions.get(value);
//print("Action " + action);
if (action.isAllowed()) {
if (action instanceof TargetAction) {
TargetAction targetAction = (TargetAction) action;
List<Targetable> targets = targetAction.findTargets();
if (targets.isEmpty()) {
//print("No available targets for action");
return;
}

outputList(targets);
//print("Enter target index:");
int targetIndex = Integer.parseInt(input.nextLine());
if (value < 0 || value >= actions.size()) {
//print("Target index out of range: " + value);
return;
}

Targetable target = targets.get(targetIndex);
targetAction.perform(target);
}
else action.perform();
//print("Action performed");
}
else {
//print("Action is not allowed");
}
}
catch (NumberFormatException ex) {
//print("Illegal action index: " + in);
}
}

private void outputList(final List<?> actions) {
Objects.requireNonNull(actions, "actions");
//print("------------------");
ListIterator<?> it = actions.listIterator();
while (it.hasNext()) {
//print(it.nextIndex() + ": " + it.next());
}
}

private void outputGameState() {
//print("------------------");
//print(this.game);
for (Player player : game.getPlayers()) {
//print(player);
//player.getActions().values().forEach(action -> print(4, "Action: " + action));
//printLua(4, player.data); // TODO: Some LuaData should probably be hidden from other players, or even from self.
}

for (Zone zone : game.getZones()) {
//print(zone);
if (zone.isKnownToPlayer(game.getCurrentPlayer())) {
zone.getCards().forEach(card -> {
//print(4, card);
//card.getActions().values().forEach(action -> print(8, "Action: " + action));
//printLua(8, card.data);
});
}
}
}




}
Expand Up @@ -6,9 +6,21 @@
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="406.0" prefWidth="467.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.cardshifter.fx.FXMLDocumentController">
<AnchorPane id="AnchorPane" maxHeight="600" maxWidth="1200" minHeight="600" minWidth="1200" prefHeight="600.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.cardshifter.fx.FXMLDocumentController">
<children>
<Button fx:id="button" layoutX="192.0" layoutY="177.0" onAction="#handleButtonAction" text="Click Me!" />
<Label fx:id="label" layoutX="199.0" layoutY="236.0" minHeight="16" minWidth="69" />
<Button fx:id="button" layoutX="1098.0" layoutY="530.0" onAction="#handleButtonAction" text="Start Game" />
<Label fx:id="label" layoutX="1098.0" layoutY="564.0" minHeight="16" minWidth="69" />
<Button layoutX="1093.0" layoutY="367.0" mnemonicParsing="false" onAction="#handleTurnButtonAction" text="Next Turn" />
<Label fx:id="turnLabel" layoutX="1091.0" layoutY="412.0" text="Turn Number" />
<Pane layoutX="120.0" layoutY="393.0" prefHeight="200.0" prefWidth="960.0">
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</Pane>
<Pane layoutX="120.0" layoutY="14.0" prefHeight="200.0" prefWidth="960.0">
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</Pane>
</children>
</AnchorPane>

0 comments on commit 2d7aeee

Please sign in to comment.