Card Shark

This game is based loosely on the Money round of a TV Game show. A card is turned over, and you gamble whether the next card is higher or lower (a tie loses).

  1. Make a project with the Card, Deck, Player, and SinglePlayer classes.
  2. Make a subclass of Player and overload the guess and the makeBet methods to improve the default strategy
  3. Run the Muliplayer with your new subclass to try it out agains the default Player class
  4. When you are ready, compete against your group members' Player in Multiplayer or Tourney.
  5. Combine forces and see if you can make a subclass of Player called GroupX (with X as your group's name) to run a Tourney against the other groups

When you think you have a good subclass of Player, it can be used with Multiplayer and Tourney to compete with other students' subclasses.

Player.java

public class Player implements Comparable<Player>
{
	private String name;
	private int money;
	private int points;

        public Player()
        {
              super("player");
         }


         public Player(String name)
	{
		this.setName(name);
		setMoney(100);

	}

       // Make an algorithm of when to predict higher or lower - 
	public String guess(Card showing){
		if (Math.random() > .5)
			return "higher";
		return "lower";
	}
       //  Make an algorithm for how much to bet.... 
	public int makeBet(Card card)
	{
		if (getMoney()<50)
			return money;
		return 50;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}

	public int randInt(int a, int b)
	{
		int min=Math.min(a, b);
		int max=Math.max(a, b);
		return min+(int)((max-min+1)*Math.random());
	}

	public int compareTo(Player other)
	{
		return this.getMoney()-((Player)(other)).getMoney();
	}
	public void addPoints(int i) {
		points+=i;
	}
	public int getPoints()
	{
		return points;

	}
}

Card.java

public class Card 
{
	private int value; //1 to 13
	private String suit; // S,H,D, or C
	public Card(int value, String suit){
		this.value=value;
		this.suit=suit.toUpperCase();
	}
	public String toString(){
		if (value==1)
			return "A"+suit;
		if (value>10)
			return "JQK".substring(value-11, value-10)+suit;
		return value+suit;
	}
	public int getValue()
	{
		return value;
	}
	public String getSuit()
	{
		return suit;
	}
}

Deck.java

public class Deck 
{
	private Card[] deck;
	public Deck()
	{
		deck = new Card[52];
		int i=0;
		for (int s=0; s<4; s++)
			for (int v=1; v<=13; v++)
			{
				deck[i]=new Card(v,"SHDC".substring(s, s+1));
				i++;
			}
		for (int j=0; j<900; j++)
		{
			int a=rand();
			int b=rand();
			Card temp=deck[a];
			deck[a]=deck[b];
			deck[b]=temp;
		}
	}
	public int rand()
	{
		return (int)(52*Math.random());
	}
	public Card getCard(int i)
	{
		return deck[i];
	}
}

SinglePlayer.java

public class SinglePlayer {

	public static void main(String[] args) {

		Player p=new Player("Player 1");
		Deck d = new Deck();
		int turn=1;
		while(turn <= 10 && p.getMoney()>0)
		{
			Card card=d.getCard(turn-1);
			Card next=d.getCard(turn);
			int diff = next.getValue()-card.getValue();
			int bet=p.makeBet(card);
			String guess = p.guess(card);

			System.out.print("Turn "+turn+": "+card+" - ");
			System.out.print(p.getName()+" has "+p.getMoney()+" bets "+bet+" guesses "+guess);
			System.out.print("- next card is "+next+" ");

			if (guess.equals("higher") && diff>0 ||
					guess.equals("lower") && diff<0 )
			{
				System.out.println(" WIN!");
				p.setMoney(p.getMoney()+bet);
			}else {
				System.out.println(" LOSE!");
				p.setMoney(p.getMoney()-bet);
			}
			turn++;
		}
		System.out.println(p.getName()+" ends with "+p.getMoney());
	}

}

Multiplayer.java

import java.util.ArrayList;
import java.util.Collections;

public class Multiplayer 
{

	public static boolean notAllBroke(ArrayList<Player> p)
	{
		for (int i=0; i< p.size(); i++)
			if (p.get(i).getMoney()>0)
				return true;
		return false;
	}

	public static void main(String[] args) {
		ArrayList<Player> players = new ArrayList<Player>();

		players.add(new Player("Player 1"));
		players.add(new Player("Player 2"));
		players.add(new Player("Player 3"));
		//players.add(new RiskyPlayer("Risky"));
		Deck d = new Deck();
		int turn=1;
		while(turn <= 10 && notAllBroke(players))
		{
			Card card=d.getCard(turn-1);
			Card next=d.getCard(turn);
			int diff = next.getValue()-card.getValue();
			System.out.print("Turn "+turn+": ");
			for (Player p:players)
			{
				if (p.getMoney()>0)
				{
					int bet=p.makeBet(card);
					String guess = p.guess(card);
					if (guess.equals("higher") && diff>0 ||
							guess.equals("lower") && diff<0 )
					{
						System.out.print(p.getName()+" wins "+bet+" ");
						p.setMoney(p.getMoney()+bet);
					}else {
						System.out.print(p.getName()+" loses "+bet+" ");
						p.setMoney(p.getMoney()-bet);
					}
				}
			}
			System.out.println();

			turn++;
		}
		Collections.sort(players);
		for (Player p:players)
			System.out.println(p.getName()+" ends with $"+p.getMoney());
	}
}

Tourney.java

import java.util.ArrayList;
import java.util.Collections;

public class Tourney 
{

	public static boolean notAllBroke(ArrayList<Player> p)
	{
		for (int i=0; i< p.size(); i++)
			if (p.get(i).getMoney()>0)
				return true;
		return false;
	}

	public static void main(String[] args) {
		ArrayList<Player> players = new ArrayList<Player>();

		players.add(new Player("Player 1"));
		//players.add(new RiskyPlayer("RiskyPlayer"));
		players.add(new Player("Player 3"));

		for(int games=0; games<10000; games++)
		{
			Deck d = new Deck();
			int turn=1;
			while(turn <= 10 && notAllBroke(players))
			{
				Card card=d.getCard(turn-1);
				Card next=d.getCard(turn);
				int diff = next.getValue()-card.getValue();

				for (Player p:players)
				{
					if(p.getMoney()>0)
					{
						int bet=p.makeBet(card);
						String guess = p.guess(card);
						if (guess.equals("higher") && diff>0 ||
								guess.equals("lower") && diff<0 )
						{
							p.setMoney(p.getMoney()+bet);
						}else {
							p.setMoney(p.getMoney()-bet);
						}
					}
				}
				turn++;
			}
			// win is 2 points, tie is 1 point
			Collections.sort(players);
			int max=players.get(0).getMoney();
			if (players.get(1).getMoney()<max)
				players.get(0).addPoints(2);
			else 
			{	
				int i=0;
				while (i<players.size() && players.get(i).getMoney()==max)
				{
					players.get(i).addPoints(1);
					i++;
				}
			}
			for (Player p:players)
			{
				System.out.print(p.getName()+" - "+p.getPoints()+" ");
				p.setMoney(100);
			}
			System.out.println();
		}
	}
}