Skip to content

Commit

Permalink
Updated the UI, will work on displaying a winner
Browse files Browse the repository at this point in the history
  • Loading branch information
angela-zhou committed Jan 24, 2019
1 parent 961af10 commit d9b620b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 84 deletions.
162 changes: 85 additions & 77 deletions ICS4UC_RST/src/egyptianWar/EgyptianWarGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,34 @@
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class EgyptianWarGame extends Application {
// display constants
static final int GAP = 15;
static final int HEIGHT = 330;
static final int WIDTH = 100;
static final int LARGE_FONT = 28;
static final int MEDIUM_FONT = 18;
static final int SMALL_FONT = 14;
static final int FIRST_COL = 0;
static final int SECOND_COL = 6;
static final int THIRD_COL = 12;

// state constants
static final int NO_CHANCES = 0;
static final int A_CHANCES = 1;
static final int B_CHANCES = 2;

// state var
// int previousState = NO_CHANCES;
int currentState = NO_CHANCES;
int nextState;

//int numChancesA;
//int numChancesB;

Deck playingDeck;
public WarHand playerA;
public WarHand playerB;
Expand All @@ -55,8 +53,15 @@ public class EgyptianWarGame extends Application {
ArrayList<Card> playedCards;

// labels to display score
Label lblPlayerA;
Label lblPlayerB;
Label lblScoreA;
Label lblScoreB;

// labels to display who wins each round
Label lblStatus;

// listViews to display instructions and numChances
ListView<String> lstPlayerA;
ListView<String> lstPlayerB;

Stage myStage;
Stage secondStage;
Expand Down Expand Up @@ -91,60 +96,60 @@ public void start(Stage myStage) throws Exception {
*/
private void mainGame() {

// instantiate the deck and the arrayList of played cards
playingDeck = new Deck();
playedCards = new ArrayList<Card>();

// may implement feature to allow user to enter their name
playerA = new WarHand("Player A");
playerB = new WarHand("Player B");

lstPlayerA = new ListView<String>();
lstPlayerB = new ListView<String>();

// root layout
VBox root = new VBox(GAP);
GridPane root = new GridPane();
root.setHgap(GAP);
root.setVgap(GAP);
root.setPadding(new Insets(GAP, GAP, GAP, GAP));
root.setAlignment(Pos.CENTER);
root.setStyle("-fx-background-color: #008000;");

// title label
Label lblTitle = new Label("Egyptian War");
lblTitle.setFont(new Font("Broadway", LARGE_FONT));
lblTitle.setTextFill(Color.GOLD);
root.getChildren().add(lblTitle);
// player A's "hub"
// score
lblScoreA = new Label("Score: " + playerA.getScore());
lblScoreA.setFont(new Font("Candara", SMALL_FONT));
lblScoreA.setTextFill(Color.GOLD);
root.add(lblScoreA, FIRST_COL, 2, 1, 1);
// listView
lstPlayerA.getItems().add(playerA.getName() + " can press the Q key to play.");
lstPlayerA.getItems().add(playerA.getName() + " can press the S key to slap.");
root.add(lstPlayerA, FIRST_COL, 3, 1, 5);


// player B's "hub"
// score
lblScoreB = new Label("Score: " + playerB.getScore());
lblScoreB.setFont(new Font("Candara", SMALL_FONT));
lblScoreB.setTextFill(Color.GOLD);
root.add(lblScoreB, THIRD_COL, 2, 1, 1);
// listView
lstPlayerB.getItems().add(playerB.getName() + " can press the P key to play.");
lstPlayerB.getItems().add(playerB.getName() + " can press the K key to slap.");
root.add(lstPlayerB, THIRD_COL, 3, 1, 5);


// center
// status
lblStatus = new Label("Egyptian War");
lblStatus.setFont(new Font("Candara", LARGE_FONT));
lblStatus.setTextFill(Color.GOLD);
lblStatus.setAlignment(Pos.CENTER);
root.add(lblStatus, SECOND_COL, 0, 1, 1);

// cards in play image
imgCardInPlay = new ImageView(new Card().getCardImage());
root.getChildren().add(imgCardInPlay);

// display score/num of cards in hand
lblPlayerA = new Label(playerA.getName() + " has " + playerA.getScore() + " cards.");
lblPlayerA.setFont(new Font("Berlin Sans FB", SMALL_FONT));
lblPlayerA.setTextFill(Color.GOLD);
root.getChildren().add(lblPlayerA);
// play instructions
Label lblPlayA = new Label(playerA.getName() + " can press the Q key to play their next card.");
lblPlayA.setFont(new Font("Berlin Sans FB", SMALL_FONT));
lblPlayA.setTextFill(Color.GOLD);
root.getChildren().add(lblPlayA);
// slap instructions
Label lblSlapA = new Label(playerA.getName() + " can press the S key to slap.");
lblSlapA.setFont(new Font("Berlin Sans FB", SMALL_FONT));
lblSlapA.setTextFill(Color.GOLD);
root.getChildren().add(lblSlapA);

// display score/num of cards in hand
lblPlayerB = new Label(playerB.getName() + " has " + playerB.getScore() + " cards.");
lblPlayerB.setFont(new Font("Berlin Sans FB", SMALL_FONT));
lblPlayerB.setTextFill(Color.GOLD);
root.getChildren().add(lblPlayerB);
// play instructions
Label lblPlayB = new Label(playerB.getName() + " can press the P key to play their next card.");
lblPlayB.setFont(new Font("Berlin Sans FB", SMALL_FONT));
lblPlayB.setTextFill(Color.GOLD);
root.getChildren().add(lblPlayB);
// slap instructions
Label lblSlapB = new Label(playerB.getName() + " can press the K key to slap.");
lblSlapB.setFont(new Font("Berlin Sans FB", SMALL_FONT));
lblSlapB.setTextFill(Color.GOLD);
root.getChildren().add(lblSlapB);
root.add(imgCardInPlay, SECOND_COL, 1, 1, 1);

// main scene
scnMain = new Scene(root);
Expand Down Expand Up @@ -182,7 +187,12 @@ public void handleKeyPressed(KeyEvent event) {
*/
private void playCard(WarHand currentPlayer, WarHand opposingPlayer) {
// make new card from the play
Card newCard = currentPlayer.playCard();
Card newCard;
if (currentPlayer == this.playerA) {
newCard = currentPlayer.playCard(lstPlayerA);
} else {
newCard = currentPlayer.playCard(lstPlayerB);
}

// set image
imgCardInPlay.setImage(newCard.getCardImage());
Expand All @@ -201,17 +211,19 @@ private void playCard(WarHand currentPlayer, WarHand opposingPlayer) {
playerA.setCanPlay(false);
// allow player B to play (they won the round)
playerB.setCanPlay(true);
lblStatus.setText(playerB.getName() + " just won the round.");
// B gets all the cards
opposingPlayer.addAllCards(playedCards, imgCardInPlay);
opposingPlayer.addAllCards(playedCards, imgCardInPlay, lstPlayerB);
// update the score
updateScore();
} else if (currentState == B_CHANCES) {
// block player B from playing (they lost the round)
playerB.setCanPlay(false);
// allow player A to play (they won the round)
playerA.setCanPlay(true);
lblStatus.setText(playerA.getName() + " just won the round.");
// A gets all the cards
opposingPlayer.addAllCards(playedCards, imgCardInPlay);
opposingPlayer.addAllCards(playedCards, imgCardInPlay, lstPlayerA);
// update the score
updateScore();
} else {
Expand All @@ -229,18 +241,18 @@ private void playCard(WarHand currentPlayer, WarHand opposingPlayer) {
if (currentState == NO_CHANCES) {
// player A now has chances
setChances(newCard, opposingPlayer);
// line for debug purposes
System.out.println(opposingPlayer.getName() + " now has " + opposingPlayer.getNumChances() + " chances.");
// display in list View
lstPlayerA.getItems().add(opposingPlayer.getName() + " now has " + opposingPlayer.getNumChances() + " chance(s).");
} else if (currentState == B_CHANCES) {
// player A now has chances
setChances(newCard, opposingPlayer);
// line for debug purposes
System.out.println(opposingPlayer.getName() + " now has " + opposingPlayer.getNumChances() + " chances.");
// display in list view
lstPlayerA.getItems().add(opposingPlayer.getName() + " now has " + opposingPlayer.getNumChances() + " chance(s).");
// reset num chances to 0
playerB.setNumChances(0);
// line for debug purposes
System.out.println("Resetting numChancesB to 0");
System.out.println(opposingPlayer.getName() + " now has " + opposingPlayer.getNumChances() + " chances.");
// display in list View
lstPlayerB.getItems().add(playerB.getName() + " now has 0 chances.");
//lstPlayerA.getItems().add(opposingPlayer.getName() + " now has " + opposingPlayer.getNumChances() + " chances.");
}
break;
case B_CHANCES:
Expand All @@ -251,19 +263,19 @@ private void playCard(WarHand currentPlayer, WarHand opposingPlayer) {
if (currentState == NO_CHANCES) {
// player B now has chances
setChances(newCard, opposingPlayer);
// line for debug purposes
System.out.println(opposingPlayer.getName() + " now has " + opposingPlayer.getNumChances() + " chances.");
// display in list View
lstPlayerB.getItems().add(opposingPlayer.getName() + " now has " + opposingPlayer.getNumChances() + " chance(s).");
} else if (currentState == A_CHANCES) {
// player B now has chances
setChances(newCard, opposingPlayer);
// line for debug purposes
System.out.println(opposingPlayer.getName() + " now has " + opposingPlayer.getNumChances() + " chances.");
// display in list View
lstPlayerB.getItems().add(opposingPlayer.getName() + " now has " + opposingPlayer.getNumChances() + " chance(s).");
// reset num chances to 0
playerA.setNumChances(0);
// line for debug purposes
System.out.println("Resetting numChancesA to 0");
System.out.println(opposingPlayer.getName() + " now has " + opposingPlayer.getNumChances() + " chances.");
// display in list View
lstPlayerA.getItems().add(playerA.getName() + " now has 0 chances.");
//System.out.println(opposingPlayer.getName() + " now has " + opposingPlayer.getNumChances() + " chances.");

}
break;
}
Expand Down Expand Up @@ -300,7 +312,7 @@ private void runStateMachine(Card newCard, WarHand currentPlayer, WarHand opposi
if (playerA.getNumChances() > 0) {
// next state stays the same
nextState = A_CHANCES;
// and numChances is equal to zero
// and numChances is equal to zero
} else if (playerA.getNumChances() == 0) {
// next state is no chances
nextState = NO_CHANCES;
Expand All @@ -318,7 +330,7 @@ private void runStateMachine(Card newCard, WarHand currentPlayer, WarHand opposi
if (playerB.getNumChances() > 0) {
// next state stays the same
nextState = B_CHANCES;
// and numChances is equal to zero
// and numChances is equal to zero
} else if (playerB.getNumChances() == 0) {
// next state is no chances
nextState = NO_CHANCES;
Expand Down Expand Up @@ -391,14 +403,10 @@ private void startGame(Deck deck) {
* Updates the number of cards each player has
*/
private void updateScore() {
lblPlayerA.setText(playerA.getName() + " has " + playerA.getScore() + " cards.");
lblPlayerA.setFont(Font.font(SMALL_FONT));
lblPlayerB.setText(playerB.getName() + " has " + playerB.getScore() + " cards.");
lblPlayerB.setFont(Font.font(SMALL_FONT));

// line for debug purposes
// System.out.println(playerA.getName() + " has " + playerA.getNumChances() + " chances.");
// System.out.println(playerB.getName() + " has " + playerB.getNumChances() + " chances.");
lblScoreA.setText(playerA.getName() + " has " + playerA.getScore() + " cards.");
lblScoreA.setFont(Font.font(SMALL_FONT));
lblScoreB.setText(playerB.getName() + " has " + playerB.getScore() + " cards.");
lblScoreB.setFont(Font.font(SMALL_FONT));
}

/**
Expand Down Expand Up @@ -436,4 +444,4 @@ public void hideSecond() {
public static void main(String[] args) {
launch(args);
}
}
}
17 changes: 10 additions & 7 deletions ICS4UC_RST/src/egyptianWar/WarHand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
import java.util.ArrayList;

import javafx.scene.control.ListView;
import javafx.scene.image.ImageView;

public class WarHand {
Expand All @@ -21,6 +22,7 @@ public class WarHand {
private String name;
private boolean canPlay;
private int numChances;
private boolean isLoser;

public WarHand(String name) {
// create hand
Expand All @@ -43,18 +45,19 @@ public void addCard(Card card) {
score += 1;
}

public void addAllCards(ArrayList<Card> playedCards, ImageView cardsInPlay) {
public void addAllCards(ArrayList<Card> playedCards, ImageView cardsInPlay, ListView<String> list) {
// update score
score += playedCards.size();
hand.addAll(playedCards);
playedCards.clear();
//cardsInPlay.setImage(new Card().getCardImage());
// line for debug purposes
System.out.println(name + " has all the played cards.");
// display in listview
list.getItems().add(name + " has all the played cards.");
list.getItems().add(name + " can play their next card");

}

public Card playCard() {
public Card playCard(ListView<String> list) {
Card nextCard;

int cardsLeft = hand.size();
Expand All @@ -66,15 +69,15 @@ public Card playCard() {
nextCard = (hand.get(newCardIndex));

// line for debug purposes
System.out.println(name + " just played a " + hand.get(newCardIndex).toString());
//System.out.println(name + " just played a " + hand.get(newCardIndex).toString());

if (numChances > 0) {
// decrement a chance if the player has them
numChances--;
System.out.println("Decrementing the Number of Chances for " + name);
//System.out.println("Decrementing the Number of Chances for " + name);
}

System.out.println(name + " now has " + numChances + " chances.");
//list.getItems().add(name + " now has " + numChances + " chances.");

// remove it from the deck
hand.remove(newCardIndex);
Expand Down

0 comments on commit d9b620b

Please sign in to comment.