Created
February 28, 2016 19:24
-
-
Save umuturan/69b0eb0d7ad4eb278dbc to your computer and use it in GitHub Desktop.
CardGame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cardgame; | |
| /** | |
| * Card - a single playing card | |
| * | |
| * @author Umut Turan | |
| * @version 16/02/2016 | |
| */ | |
| public class Card { | |
| final String[] SUITS = { "Hearts", "Diamonds", "Spades", "Clubs" }; | |
| final String[] FACES = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; | |
| final int NOOFCARDSINSUIT = 13; | |
| // properties | |
| int cardNo; | |
| // constructors | |
| public Card(int faceValue, int suit) { | |
| cardNo = faceValue + suit * NOOFCARDSINSUIT; | |
| } | |
| public Card(int cardNumber) { | |
| cardNo = cardNumber; | |
| } | |
| public int getFaceValue() { | |
| return cardNo % NOOFCARDSINSUIT; | |
| } | |
| public int getSuit() { | |
| return cardNo / NOOFCARDSINSUIT; | |
| } | |
| public String toString() { | |
| return FACES[getFaceValue()] + " of " + SUITS[getSuit()]; | |
| } | |
| public boolean equals(Card c) { | |
| boolean res; | |
| if (getFaceValue() == c.getFaceValue() && getSuit() == c.getSuit()) { | |
| res = true; | |
| } else | |
| res = false; | |
| return res; | |
| } | |
| public int compareTo(Card c) { | |
| if(getFaceValue()==c.getFaceValue()) | |
| return 0; | |
| else if (getFaceValue()>c.getFaceValue()) | |
| return 1; | |
| else | |
| return 2; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cardgame; | |
| import java.util.ArrayList; | |
| // Cardgame | |
| // author:Umut turan | |
| // date:16/02/16 | |
| public class CardGame { | |
| // properties | |
| Cards fullPack; | |
| ArrayList<Player> players; | |
| ScoreCard scoreCard; | |
| Cards[] cardsOnTable; | |
| int roundNo; | |
| int turnOfPlayer; | |
| // constructors | |
| public CardGame(Player p1, Player p2, Player p3, Player p4) { | |
| players = new ArrayList<Player>(); | |
| players.add(p1); | |
| players.add(p2); | |
| players.add(p3); | |
| players.add(p4); | |
| } | |
| // methods | |
| public boolean playTurn(Player p, Card c) { | |
| /* | |
| * Todo | |
| */ | |
| if (!isTurnOf(p) || isGameOver()) { | |
| return false; | |
| } else { | |
| cardsOnTable[getTurnOfPlayerNo() - 1].addTopCard(c); | |
| if (roundNo % 4 == 3) { | |
| updateScores(); | |
| } | |
| if (roundNo < 51) | |
| roundNo++; | |
| return true; | |
| } | |
| } | |
| private void updateScores() { | |
| Card max = new Card(0); | |
| for (int i = 0; i < scoreCard.scores.length - 1; i++) { | |
| if (cardsOnTable[i].cards[getRoundNo() - 1].getFaceValue() >= max.getFaceValue()) { | |
| max = cardsOnTable[i].cards[getRoundNo() - 1]; | |
| } | |
| } | |
| for (int j = 0; j < scoreCard.scores.length; j++) { | |
| if (cardsOnTable[j].cards[getRoundNo() - 1].getFaceValue() == max.getFaceValue()) { | |
| scoreCard.update(j, 1); | |
| } | |
| } | |
| /* | |
| * if there is more than max cards with the same face values and | |
| * different suite all of them gets the point for that round | |
| * | |
| */ | |
| } | |
| public boolean isTurnOf(Player p) { | |
| if (players.get(roundNo % 4).getName().equals(p.getName())) | |
| return true; | |
| else | |
| return false; | |
| /* | |
| * todo | |
| */ | |
| } | |
| public boolean isGameOver() { | |
| /* | |
| * ToDo | |
| */ | |
| if (getRoundNo() == 13 && cardsOnTable[3].cards.length == 13) | |
| return true; | |
| else | |
| return false; | |
| } | |
| public int getScore(int playerNumber) { | |
| /* | |
| * ToDo | |
| */ | |
| return scoreCard.scores[playerNumber - 1]; | |
| } | |
| public String getName(int playerNumber) { | |
| return players.get(playerNumber - 1).getName(); | |
| } | |
| public int getRoundNo() { | |
| if (roundNo < 52) | |
| return (roundNo / 4) + 1; | |
| else | |
| return roundNo / 4; | |
| } | |
| public int getTurnOfPlayerNo() { | |
| /* | |
| * ToDo | |
| */ | |
| int i; | |
| for (i = 0; i < players.size() && !isTurnOf(players.get(i)); i++) { | |
| } | |
| return i + 1; | |
| } | |
| public Player[] getWinners() { | |
| Player[] winners = new Player[scoreCard.getWinners().length]; | |
| for (int i = 0; i < winners.length; i++) { | |
| winners[i] = players.get(scoreCard.getWinners()[i]); | |
| } | |
| return winners; | |
| } | |
| public void initGame() { | |
| if (players.size() == 4) { | |
| fullPack = new Cards(true); | |
| fullPack.shuffle(); | |
| for (int i = 0; i < 52; i++) { | |
| players.get(i % 4).hand.addTopCard(fullPack.getTopCard()); | |
| } | |
| scoreCard = new ScoreCard(4); | |
| cardsOnTable = new Cards[4]; | |
| for (int i = 0; i < 4; i++) { | |
| cardsOnTable[i] = new Cards(false); | |
| } | |
| roundNo = 0; | |
| turnOfPlayer = 0; | |
| } | |
| } | |
| public String showScoreCard() { | |
| return scoreCard.toString(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cardgame; | |
| import java.util.Scanner; | |
| import cardgame.*; | |
| // CardGameTest | |
| // author:Umut Turan | |
| // date:16/02/16 | |
| public class CardGameTest | |
| { | |
| public static void main( String[] args) | |
| { | |
| Scanner scan = new Scanner( System.in); | |
| System.out.println( "Start of CardGameTest\n"); | |
| // CONSTANTS | |
| // VARIABLES | |
| Card c; | |
| Cards cards; | |
| ScoreCard scores; | |
| Player p; | |
| CardGame game; | |
| // PROGRAM CODE | |
| // test Card class | |
| c = new Card( 1); | |
| System.out.println( c); | |
| System.out.println(); | |
| // test Cards class | |
| cards = new Cards( true); | |
| cards.addTopCard( c); | |
| //cards.testOnlyPrint(); // remove method after testing! | |
| // test ScoreCard class | |
| scores = new ScoreCard( 4); | |
| scores.update( 3, 1); | |
| scores.update( 1, 2); | |
| System.out.println( "\n" + scores ); | |
| // test Player class | |
| p= new Player("umut"); | |
| p.add(c); | |
| System.out.println(p.getName().toUpperCase()); | |
| System.out.println(p.playCard()); | |
| // test CardGame class too? | |
| // Todo | |
| // Once you have all the bits working, complete the MyCardGame program | |
| // that provides a menu allowing any of the players to play their card, | |
| // an option to see the score card, and one to quit the game at any time. | |
| // When the game is over it should print out the winners. | |
| System.out.println( "\nEnd of CardGameTest\n" ); | |
| } | |
| } // end of class CardGameTest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cardgame; | |
| import java.util.Random; | |
| // Cards - Maintains a collection of zero or more playing cards. | |
| // Provides facilities to create a full pack of 52 cards | |
| // and to shuffle the cards. | |
| // author:Umut Turan | |
| // date:16/02/16 | |
| public class Cards { | |
| final int NOOFCARDSINFULLPACK = 52; | |
| // properties | |
| Card[] cards; | |
| int valid; // number of cards currently in collection | |
| // constructors | |
| public Cards(boolean fullPack) { | |
| cards = new Card[NOOFCARDSINFULLPACK]; | |
| valid = 0; | |
| if (fullPack) | |
| createFullPackOfCards(); | |
| } | |
| // methods | |
| public Card getTopCard() { | |
| Card tmp; | |
| if (valid <= 0) | |
| return null; | |
| else { | |
| valid--; | |
| tmp = cards[valid]; | |
| cards[valid] = null; | |
| return tmp; | |
| } | |
| } | |
| public boolean addTopCard(Card c) { | |
| if (valid < cards.length) { | |
| cards[valid] = c; // SHOULD THİS BE CLONED?? | |
| valid++; | |
| return true; | |
| } | |
| return false; | |
| } | |
| private void createFullPackOfCards() { | |
| /* | |
| * Todo | |
| */ | |
| for(int i = 0; i<52;i++){ | |
| addTopCard(new Card(i)); | |
| } | |
| } | |
| public void shuffle() { | |
| Random rGen = new Random(); // Random number generator | |
| for (int i=0; i<cards.length; i++) { | |
| int randomPosition = rGen.nextInt(cards.length); | |
| Card temp = cards[i]; | |
| cards[i] = cards[randomPosition]; | |
| cards[randomPosition] = temp; | |
| } | |
| /* | |
| * Todo | |
| */ | |
| } | |
| // For testOnly... remove from production version! | |
| public void testOnlyPrint() { | |
| for (int i = 0; i < valid; i++) { | |
| System.out.println(cards[i]); | |
| } | |
| } | |
| } // end class Cards |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cardgame; | |
| import java.util.Scanner; | |
| import cardgame.*; | |
| // MyCardGame - provides a menu allowing any of the players to play their card, | |
| // an option to see the score card, and one to quit the game at any time. | |
| // When the game is over it dislays the winners. | |
| // author:Umut Turan | |
| // date:16/02/16 | |
| public class MyCardGame | |
| { | |
| public static void main( String[] args) | |
| { | |
| Scanner scan = new Scanner( System.in); | |
| System.out.println( "Start of MyCardGame\n"); | |
| // CONSTANTS | |
| final int MENU_EXIT = 0; | |
| final int MENU_PLAY_P1 = 1; | |
| final int MENU_PLAY_P2 = 2; | |
| final int MENU_PLAY_P3 = 3; | |
| final int MENU_PLAY_P4 = 4; | |
| final int MENU_SCORES = 5; | |
| // VARIABLES | |
| Player p1, p2, p3, p4; | |
| CardGame game; | |
| int selection; | |
| // PROGRAM CODE | |
| // create players... | |
| p1 = new Player( "p1"); | |
| p2 = new Player( "p2"); | |
| p3 = new Player( "p3"); | |
| p4 = new Player( "p4"); | |
| // create game with the 4 players... | |
| game = new CardGame( p1, p2, p3, p4); | |
| game.initGame(); | |
| // display menu, get and process selection, until exit | |
| do | |
| { | |
| // display menu | |
| System.out.println(); | |
| System.out.println( "MyCardGame Round: " + game.getRoundNo() | |
| + "\t TurnOfPlayer: " + game.getTurnOfPlayerNo() ); | |
| System.out.println(); | |
| System.out.println( MENU_PLAY_P1 + " - Player " + MENU_PLAY_P1 + " plays" ); | |
| System.out.println( MENU_PLAY_P2 + " - Player " + MENU_PLAY_P2 + " plays" ); | |
| System.out.println( MENU_PLAY_P3 + " - Player " + MENU_PLAY_P3 + " plays" ); | |
| System.out.println( MENU_PLAY_P4 + " - Player " + MENU_PLAY_P4 + " plays" ); | |
| System.out.println( MENU_SCORES + " - Show scores" ); | |
| // ask for and get selection | |
| System.out.println(); | |
| System.out.println( "Selection (" + MENU_EXIT + " to exit): "); | |
| selection = scan.nextInt(); | |
| // process selection | |
| if ( selection == MENU_PLAY_P1 ) | |
| play( p1, game); | |
| else if ( selection == MENU_PLAY_P2 ) | |
| play( p2, game); | |
| else if ( selection == MENU_PLAY_P3 ) | |
| play( p3, game); | |
| else if ( selection == MENU_PLAY_P4 ) | |
| play( p4, game); | |
| else if ( selection == MENU_SCORES ){ | |
| System.out.println( game.showScoreCard() ); | |
| System.out.println( "ToDo..." ); | |
| } | |
| else if ( selection != MENU_EXIT) | |
| System.out.println( "Invalid selection! \n" ); | |
| } while ( selection != MENU_EXIT); | |
| // display winners... | |
| System.out.println("Winner"); | |
| for(int i=0; i<game.getWinners().length;i++){ | |
| System.out.println( game.getWinners()[0] ); | |
| } | |
| System.out.println( "\nEnd of MyCardGame\n" ); | |
| } | |
| // ToDo... | |
| // get the card, c, that player p wants to play | |
| // pass c to the game, see if it accepted c from p | |
| // if game didn't accept the card, give c back to the player! | |
| // return accepted. | |
| static boolean play( Player p, CardGame game) | |
| { | |
| Card c; | |
| boolean accepted; | |
| c = p.playCard(); | |
| accepted = game.playTurn(p, c); | |
| if(accepted == false){ | |
| p.hand.addTopCard(c); | |
| } | |
| /* | |
| * ToDo... | |
| */ | |
| return accepted; | |
| } | |
| } // end class MyCardGame |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cardgame; | |
| // Player - Simple card game player with name and hand of cards | |
| // author:Umut Turan | |
| // date:16/02/16 | |
| public class Player { | |
| // properties | |
| String name; | |
| Cards hand; | |
| // constructors | |
| public Player(String name) { | |
| this.name=name; | |
| hand = new Cards(false); | |
| } | |
| // methods | |
| public String getName() { | |
| return name; | |
| } | |
| public void add(Card c) { | |
| hand.addTopCard(c); | |
| } | |
| public Card playCard() { | |
| /* | |
| * ToDo | |
| */ | |
| return hand.getTopCard(); | |
| } | |
| public String toString(){ | |
| return getName(); | |
| } | |
| } // end class Player |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cardgame; | |
| import java.util.ArrayList; | |
| // ScoreCard - Maintains one integer score per player, for any number of players | |
| // Caution: invalid playernumbers result in run-time exception! | |
| // author:Umut Turan | |
| // date:16/02/16 | |
| public class ScoreCard { | |
| // properties | |
| int[] scores; | |
| // constructors | |
| public ScoreCard(int noOfPlayers) { | |
| scores = new int[noOfPlayers]; | |
| // init all scores to zero | |
| for (int i = 0; i < scores.length; i++) | |
| scores[i] = 0; | |
| } | |
| // methods | |
| public int getScore(int playerNo) { | |
| return scores[playerNo]; | |
| } | |
| public void update(int playerNo, int amount) { | |
| scores[playerNo] += amount; | |
| } | |
| public String toString() { | |
| String s; | |
| s = "\n" + "_____________\n" + "\nPlayer\tScore\n" + "_____________\n"; | |
| for (int playerNo = 0; playerNo < scores.length; playerNo++) { | |
| s = s + (playerNo+1) + "\t" + scores[playerNo] + "\n"; | |
| } | |
| s += "_____________\n"; | |
| return s; | |
| } | |
| public int[] getWinners() { | |
| /* | |
| * ToDo | |
| */ | |
| int maxScore = 0; | |
| int[] winners; | |
| ArrayList<Integer> win = new ArrayList<Integer>(); | |
| for (int i = 0; i < scores.length; i++) { | |
| if (scores[i] > maxScore) { | |
| maxScore = scores[i]; | |
| } | |
| } | |
| for (int j = 0; j < scores.length; j++) { | |
| if (maxScore == scores[j]) { | |
| win.add(j); | |
| } | |
| } | |
| winners = new int[win.size()]; | |
| for (int i = 0; i < winners.length; i++) { | |
| winners[i] = win.get(i); | |
| } | |
| return winners; | |
| } | |
| } // end class ScoreCard |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment