import java.util.Random;
import javax.swing.JOptionPane;
public class A2
{
public static void main(String[]args)
{
Deck deck = new Deck();
Player human = new Player();
Player naive = new Player();
Player smart = new Player();
boolean clockWise = true;
int number;
Card lastCardPlayed;
String input;
int sum;
Pile pile = new Pile();
System.out.println("The Total Number of Cards in the Pile is:" + pile.getTotal() + "\n");
while(clockWise)
{
for(int i = 0; i < NUMBER_OF_CARDS_IN_HAND; i++)
{
int n = i+1;
System.out.println("The Human Card Number " + n + ": " + human.getCard(i));
}
input = JOptionPane.showInputDialog("What Card would you like to play? Please insert card number");
number = Integer.parseInt(input);
number--;
lastCardPlayed = human.play(number);
pile.setTotal(pile.add(lastCardPlayed.getValue()));
if(lastCardPlayed.getValue() == 99)
pile.setTotal(99);
sum = pile.getTotal();
System.out.println("The Last Card Played was:" + lastCardPlayed.getName() + "\n");
System.out.println("The Total Number of Cards in the pile is:" + sum + "\n");
if(sum > 99)
{
System.out.println("you lose....");
System.exit(0);
}
if(lastCardPlayed.getName().equals("Four"))
{
System.out.println("The direction has been reversed");
clockWise = !clockWise;
}
System.out.println("Naive player's turn\n");
lastCardPlayed = naive.naivePlay(pile.getTotal());
pile.setTotal(pile.add(lastCardPlayed.getValue()));
if(lastCardPlayed.getValue() == 99)
pile.setTotal(99);
sum = pile.getTotal();
System.out.println("The Last Card played was:" + lastCardPlayed.getName() + "\n");
System.out.println("The Total Number of Cards in the Pile is:" + sum + "\n");
}
}
public static final int NUMBER_OF_CARDS_IN_HAND = 5;
}
class Player extends Deck
{
private Card[] hand;
public Player()
{
hand = new Card[5];
for(int i = 0; i < 5; i++)
{
hand[i] = draw();
}
}
public String getCard(int i)
{
return hand[i].getName();
}
public Card play(int i)
{
Card lastCard = discard(i);
draw();
return lastCard;
}
public Card naivePlay(int i)
{
int cardNumber = 0;
int sum = i;
for(int j = 0; j < 5; j++)
{
if(sum < 99)
{
int temp = hand[j].getValue();
sum+=temp;
if(sum > 99)
sum-=temp;
else
{
cardNumber = j;
break;
}
}
if(sum == 99)
{
int temp = hand[j].getValue();
if(temp == 0)
cardNumber = j;
}
}
if(sum == 99 && hand[0].getValue()!= 0 || hand[0].getValue()!= -10 || hand[1].getValue()!= 0 || hand[1].getValue()!= -10 || hand[2].getValue()!= 0 || hand[2].getValue()!= -10 || hand[3].getValue()!= 0 || hand[3].getValue()!= -10 ||hand[4].getValue()!= 0 || hand[4].getValue()!= -10)
{
Random generator = new Random();
cardNumber = generator.nextInt(5);
return hand[cardNumber];
}
return hand[cardNumber];
}
public Card smartPlay(int i)
{
Card card = discard(i);
draw();
return card;
}
public Card draw()
{
Card card = super.draw();
return card;
}
public Card discard(int i)
{
Card card = hand[i];
System.arraycopy(hand, i + 1, hand, i, hand.length - i - 1);
return card;
}
}
class Pile extends Player
{
private int total;
public Pile()
{
total = 0;
}
public int add(int i)
{
int sum = getTotal();
int cardValue = i;
sum += cardValue;
return sum;
}
public void setTotal(int i)
{
total = i;
}
public int getTotal()
{
return total;
}
}
class Deck
{
private Card[] deck;
private int card;
public Deck()
{
deck = new Card[TOTAL_NUMBER_OF_CARDS];
deck[0] = new Card("Ace", 1);
deck[1] = new Card("Ace", 1);
deck[2] = new Card("Ace", 1);
deck[3] = new Card("Ace", 1);
deck[4] = new Card("Two", 2);
deck[5] = new Card("Two", 2);
deck[6] = new Card("Two", 2);
deck[7] = new Card("Two", 2);
deck[8] = new Card("Three", 3);
deck[9] = new Card("Three", 3);
deck[10] = new Card("Three", 3);
deck[11] = new Card("Three", 3);
deck[12] = new Card("Four", 0);
deck[13] = new Card("Four", 0);
deck[14] = new Card("Four", 0);
deck[15] = new Card("Four", 0);
deck[16] = new Card("Nine", 0);
deck[17] = new Card("Nine", 0);
deck[18] = new Card("Nine", 0);
deck[19] = new Card("Nine", 0);
deck[20] = new Card("Ten", 10);
deck[21] = new Card("Ten", 10);
deck[22] = new Card("Ten", 10);
deck[23] = new Card("Ten", 10);
deck[24] = new Card("Jack", 10);
deck[25] = new Card("Jack", 10);
deck[26] = new Card("Jack", 10);
deck[27] = new Card("Jack", 10);
deck[28] = new Card("Queen", -10);
deck[29] = new Card("Queen", -10);
deck[30] = new Card("Queen", -10);
deck[31] = new Card("Queen", -10);
deck[32] = new Card("King", 99);
deck[33] = new Card("King", 99);
deck[34] = new Card("King", 99);
deck[35] = new Card("King", 99);
}
public String getCard(int i)
{
return deck[i].getName();
}
public int shuffle()
{
Random generator = new Random();
card = generator.nextInt(deck.length);
return card;
}
public Card draw()
{
int cardSlot = shuffle();
Card card = deck[cardSlot];
System.arraycopy(deck , cardSlot + 1, deck, cardSlot, deck.length - cardSlot - 1);
return card;
}
public int getLength()
{
return deck.length;
}
public final static int NUMBER_OF_SUITS = 4;
public final static int NUMBER_OF_CARDS_IN_SUITE = 9;
public final static int TOTAL_NUMBER_OF_CARDS = 36;
}
class Card
{
private String name;
private int value;
public Card()
{
name = "";
value = 0;
}
public Card(String aName, int aValue)
{
name = aName;
value = aValue;
}
public int getValue()
{
return value;
}
public String getName()
{
return name;
}
}
|