package com.exercise;
//Create a Card class with two attribute, suit and point
public class Card implements Comparable<Card>{
private String suit;
private String point;
final String[] cardSuit = {"diamond","club","spade","heart" };
final String[] cardPoint = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
/**
* return the position of the given string in the string array of cardSuit and cardPoint
* so that we can compare a card by suit and point
* @param str
* @param s
*
*/
public int getIndex(String[] str,String s){
int i=0;
while (!str[i].equals(s)){
i++;
}
return i;
}
public Card(String suit,String point){
this.suit = suit;
this.point = point;
}
public String getSuit(){
return suit;
}
public String getPoint(){
return point;
}
@Override
/**
* override the compareTo method to implement the idea as following:
* given two cards, firstly we compare both points, if the points are same
* we compare the suits according with the ascending order as the array of cardSuit
*/
public int compareTo(Card o) {
// TODO Auto-generated method stub
Integer index1 = getIndex(cardPoint,this.point);
Integer index2 = getIndex(cardPoint,o.point);
int result = index1.compareTo(index2);
if (result!=0)
return result;
else{
index1 = getIndex(cardSuit,this.suit);
index2 = getIndex(cardSuit,o.suit);
return index1.compareTo(index2);
}
}
}
package com.exercise;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* create player class with 3 attributes:name, id and a list of cards
* and one original method of taking cards, and one override method
* of compareTo
*/
public class Player implements Comparable<Player>{
private String name;
private String ID;
public List<Card> cards;
public Player(String name, String ID){
this.name = name;
this.ID = ID;
this.cards = new ArrayList<Card>();
}
public String getName(){
return name;
}
public String getID(){
return ID;
}
public void setName(String name){
this.name = name;
}
public void setID(String ID){
this.ID = ID;
}
//Take a card from the cardSet after shuffle
//parameter index indicates the order number of shuffle card
public void takeCard(List<Card> shuffleCard, int index){
cards.add(shuffleCard.get(index));
}
@Override
/**
* the method compare all the cards of two players
* firstly we sort all the card of one player
* then we compare the last card of the two players
*/
public int compareTo(Player o) {
Collections.sort(this.cards);
Collections.sort(o.cards);
return this.cards.get(1).compareTo(o.cards.get(1));
}
}
package com.exercise;
//create an exception in case the customer enter an username starting with an non-alphabet character
public class PlayerNameException extends Exception {
public PlayerNameException(){
}
public String toString(){
return "Game player name must start with an alphabet character, please try again: ";
}
}
package com.exercise;
public class PlayerNumberException extends Exception {
public PlayerNumberException(){
}
public String toString(){
return "The number should be an integer greater than 2, please try again: ";
}
}
package com.exercise;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/**
* Create the whole set of cards
* Create n game players, player name must start with an alphabet character, otherwise go exception handling
* Auto-generate game player ID with 3 digits, but the first digit can't be 0 *
*
*/
public class Game {
final String[] cardSuit = {"diamond","club","spade","heart" };
final String[] cardPoint = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
final int idNum = 3;
List<Player> playerList;
public Game(){
playerList = new ArrayList<Player>();
}
//the CardCase generation is a procedure of combination of suit and Point
public void genCardCase(List<Card> cardCase){
Card cd;
for (int i=0;i<cardSuit.length;i++){
for (int j=0;j<cardPoint.length;j++){
cd = new Card(cardSuit[i],cardPoint[j]);
cardCase.add(cd);
}
}
}
/**
* shuffle is a procedure as following
* generate a unique random index for each element, and then take the corresponding card from the sorted cardlist
* @param cardCaseShuffle stores the cards after shuffle
* @param cardCase stores the previous sorted cards
*/
public void shuffle(List<Card> cardCaseShuffle, List<Card> cardCase){
Random ran = new Random();
int index;
int size = cardCase.size();
List <Integer> idList = new ArrayList<Integer>();
for (int i=0;i<size;i++){
index = ran.nextInt(size);
//guarantee the generating number is unique, if yes take the corresponding card from
//sorted list to the shuffle list
while(idList.contains(index)){
index = ran.nextInt(size);
}
idList.add(index);
cardCaseShuffle.add(cardCase.get(index));
}
}