Skip to content
Snippets Groups Projects
Commit 8ff907a6 authored by qingyuw2's avatar qingyuw2
Browse files

add main function

parent 9a51c95a
Branches assignment1.2
No related tags found
No related merge requests found
Showing
with 212 additions and 7 deletions
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -2,10 +2,24 @@ package Card;
import Card.Card;
import java.util.HashMap;
public class ActionCard extends Card {
// 0:skip 1:reverse 2:draw two cards 3:wild 4:wild draw four
// 0:skip 1:reverse 2:draw two cards 3:wild 4:wild draw four 5:switch 6:one more round
private int action;
private static HashMap<Integer, String> actionSet;
static {
actionSet = new HashMap<Integer, String>();
actionSet.put(0, "Skip");
actionSet.put(1, "Reverse");
actionSet.put(2, "Draw Two");
actionSet.put(3, "");
actionSet.put(4, "Draw four");
actionSet.put(5, "Switch");
actionSet.put(6, "One More Round");
}
@Override
public boolean equals(Object o){
if(o instanceof ActionCard){
......@@ -18,6 +32,11 @@ public class ActionCard extends Card {
return false;
}
@Override
public String toString() {
return String.format("%s %s", coloSet.get(this.getColor()), actionSet.get(action));
}
/**
* Constructor for ActionCard object.
* @param action action of the card
......
package Card;
import java.util.HashMap;
public abstract class Card {
// represents type of card true:action false:number
private boolean type;
// represents colors 0:red 1:yellow 2:green 3:blue 4:wild
private int color;
public static HashMap<Integer, String> coloSet;
static {
coloSet = new HashMap<Integer, String>();
coloSet.put(0, "red");
coloSet.put(1, "yellow");
coloSet.put(2, "green");
coloSet.put(3, "blue");
coloSet.put(4, "wild");
}
/**
* Setter for type variable.
* @param type type of card
......
......@@ -16,6 +16,11 @@ public class NumberCard extends Card {
return false;
}
@Override
public String toString() {
return String.format("%s %s", coloSet.get(this.getColor()), Integer.toString(number));
}
/**
* Constructor for NumberCard object.
* @param number action of the card
......
......@@ -5,7 +5,12 @@ import Deck.Deck;
import Discard.Discard;
import Player.Player;
import java.awt.*;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Game {
private Player players[];
......@@ -40,9 +45,76 @@ public class Game {
assignCardToDiscard();
}
public final static void clearConsole() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
/**
* Print the current game state. Information includes number of cards in deck, order of the game, card on the discard top.
* It also prints the cards a player has and legal cards a player could play.
*/
public void printCurrentGameState() {
clearConsole();
String strOrder = order == 1 ? "clockwise" : "counter-clockwise";
String deckNum = Integer.toString(deck.getNumOfCards());
String discardCard = discard.getTop().toString();
System.out.format("You are Player No.%d and the order is %s. There are %s cards in deck right now. \n" +
"Top card on the discard pile is %s.\n", turn, strOrder, deckNum, discardCard);
printPlayerCard(players[turn]);
}
/**
* helper function for printCurrentGameState. It prints the cards a player has and legal cards a player could paly.
* @param player current player
*/
private void printPlayerCard(Player player) {
String playerCard = "";
HashMap<Integer, ArrayList<Card>> hand = player.getHand();
for (Map.Entry mapElement : hand.entrySet()) {
for (Card card : (ArrayList<Card>)mapElement.getValue()) {
playerCard += "[" + card.toString() + "] ";
}
}
String legalStr = "";
ArrayList<Card> legalCards = player.returnIfContainsLegal(discard.getTop());
for (Card card : legalCards) {
legalStr += "[" + card.toString() + "] ";
}
System.out.println("You have " + playerCard + "in your hands");
if (legalCards.size() == 0) {
System.out.println("You don't have a card to play. Draw a card.");
} else {
System.out.println("And you could play " + legalStr);
System.out.println("Or do you want to draw a card?");
}
}
/**
* Print game ending information.
*/
public void printGameEnd() {
System.out.format("Player No.%d win! Congrats!\n", turn);
}
/**
* Print discard pile reshuffle information.
*/
public void printReshuffle() {
System.out.println("Deck is empty! Reshuffle discard pile and use them as new deck.");
System.out.format("There are %d cards in the deck now.\n", deck.getNumOfCards());
}
/**
* Print wild card selection for player who just play a wild card.
*/
public void printWildSelection() {
System.out.println("You just played a wild card. You could set your color for next turn(red, yellow, green, blue)");
}
/**
* Get all the players
* @return array of players
* eturn array of players
*/
public Player[] getPlayers() {
return players;
......@@ -88,6 +160,13 @@ public class Game {
return turn;
}
/**
* Setter for turn variable. Used for testing only.
*/
public void setTurn(int turn) {
this.turn = turn;
}
/**
* Initialization function for Game object.
*/
......@@ -108,7 +187,7 @@ public class Game {
* @return index of next player
*/
public int nextTurn() {
// Mod operation that gives positive number only
// Md operation that gives positive number only
return (((turn + order) % numOfPlayers) + numOfPlayers) % numOfPlayers;
}
......@@ -191,6 +270,25 @@ public class Game {
wild.setColor(color);
}
/**
* Handle double skip card.
*/
public void handleDoubleSkip() {
if (numOfPlayers <= 3) {
handleOneMoreRound();
} else {
handleSkip();
handleSkip();
}
}
/**
* Handle one more round card.
*/
public void handleOneMoreRound() {
turn -= order;
}
/**
* Deal card on top of deck to one player.
* @param player player to be dealt card to
......@@ -219,6 +317,4 @@ public class Game {
discard.discardCard(temp);
} while(temp.getType());
}
}
......@@ -21,6 +21,10 @@ public class Player {
}
}
public HashMap<Integer, ArrayList<Card>> getHand() {
return hand;
}
/**
* Getter for number of cards in player.
* @return number of cards a player has
......
......@@ -67,6 +67,39 @@ public class GameTest {
assertEquals(11, players[1].getNumOfCards());
}
@Test
public void switchTest() {
game = new Game(2);
int turn = game.getTurn();
game.handleDoubleSkip();
game.handleSkip();
int next = game.getTurn();
assertEquals(turn, next);
game = new Game(3);
turn = game.getTurn();
game.handleDoubleSkip();
game.handleSkip();
next = game.getTurn();
assertEquals(turn, next);
game = new Game(5);
game.handleDoubleSkip();
game.handleSkip();
next = game.getTurn();
assertEquals(3, next);
}
@Test
public void oneMoreRoundTest() {
game = new Game(5);
int turn = game.getTurn();
game.handleOneMoreRound();
game.handleSkip();
int oneMore = game.getTurn();
assertEquals(turn, oneMore);
}
@Test
public void gameEndTest() {
game = new Game(2);
......
package Test;public class UITest {
package Test;
import Game.Game;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
public class UITest {
@Test
public void simpleTest() throws InterruptedException {
Game game = new Game(3);
game.printCurrentGameState();
game.setTurn(1);
game.printCurrentGameState();
game.setTurn(2);
game.printCurrentGameState();
game.printWildSelection();
game.printReshuffle();
game.printGameEnd();
}
}
package PACKAGE_NAME;public class startGame {
import Game.*;
import java.util.concurrent.TimeUnit;
public class startGame {
public static void main(String[] args) throws InterruptedException {
Game game = new Game(3);
game.printCurrentGameState();
TimeUnit.SECONDS.sleep(5);
game.setTurn(1);
game.printCurrentGameState();
TimeUnit.SECONDS.sleep(5);
game.setTurn(2);
game.printCurrentGameState();
game.printWildSelection();
game.printReshuffle();
game.printGameEnd();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment