package newpackage;
public class Belote {
static Player[] players;
static Card[] cards;
public Belote() {
// create four Player objects
String[] playerNames = { "Adam", "Bob", "Charlie", "Daniel" };
players = new Player[4];
for(int i =0; i<players.length;i++){
players[i]= new Player(playerNames[i]);
}
// create 52 Card objects
String[] cardName = { "A", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King" };
//char[] suitName = { '\u2665', '\u2666', '\u2663', '\u2660' };
char[] suitName = { '\u0003', '\u0004', '\u0005', '\u0006' };
cards = new Card[52];
for(int i = 0; i<suitName.length;i++)
for(int j = 0; j< cardName.length;j++){
cards[i*13+j] = new Card(cardName[j],j+1,suitName[i]);
}
// Shuffling
Card temp;
for (int i = 0; i < cards.length; i++) {
//pick a random index j between 0 and card.length
java.util.Random random = new java.util.Random();
int j = random.nextInt(52);
//swap the cards at index i and j
temp = cards[i];
cards[i] = cards[j];
cards[j] = temp;
}
// distribution
for (int i = 0; i < cards.length; i++) {
players[i%4].addCard(cards[i]);
}
}
public static void main(String[] args) {
Belote cardGame = new Belote();
cardGame.playGame();
System.out.println("---------game over--------");
for (int i = 0; i < 4; i++) {
System.out.println(players[i].getName() + "'s score is "
+ players[i].getScore());
}
}
void playGame() {
int firstPlayer = 0;
int turn = 1;
Card fourCards[] = new Card[4];
char curSuit;
while ((fourCards[0] = players[firstPlayer].playFirst())!=null) {
System.out.println("----------turn " + turn + "-----------");
// get the cards of the current turn
curSuit = fourCards[0].getSuit();
fourCards[1] = players[(firstPlayer+1)%4].play(curSuit);
fourCards[2] = players[(firstPlayer+2)%4].play(curSuit);
fourCards[3] = players[(firstPlayer+3)%4].play(curSuit);
// print the four cards of the current turn
for (int i = 0; i < 4; i++) {
waitAMoment(1);
System.out.print(players[(firstPlayer + i) % 4].getName()
+ "\t");
fourCards[i].printOut();
}
// find the winner of the current turn
firstPlayer = (firstPlayer+findTurnWinner(fourCards))%4;
players[firstPlayer].winTurn(fourCards);
// print winner message of the current turn
waitAMoment(1);
System.out.println("The turn Winner is "
+ players[firstPlayer].getName() + "\n with current score "
+ players[firstPlayer].getScore());
turn++;
}
}
int findTurnWinner(Card[] fourCards) {
char curSuit = fourCards[0].getSuit();
int value = fourCards[0].getValue();
int indexOfTurnWinner = 0;
for (int i = 1; i < 4; i++) {
if (fourCards[i].getSuit() != curSuit)
continue;
if (fourCards[i].getValue() > value) {
indexOfTurnWinner = i;
value = fourCards[i].getValue();
}
}
return indexOfTurnWinner;
}
void waitAMoment(int n) {
try {
Thread.sleep(n * 1000);
} catch (InterruptedException e) {
}
}
}