#Glaucus package Players;
Glaucus
package Players;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import Mechanics.Card;
import Mechanics.Player;
public class Glaucus extends Player {
static final double LosePay = 0;
static final double WinPay = 10;
static final double DrawPay = 1;
static final int NUMBEROFSIMS = 100;
Random r;
public Glaucus()
{
this.name = "Glaucus";
r = new Random();
}
@Override
public void getResponse(int[] wins, boolean isPlayerOne,
Collection<Card> yourHand, Collection<Card> opponentHand,
Collection<Card> yourSideDeck, int opponentSideDeckCount,
Action opponentAction, boolean opponentDidPlay) {
//Make Sum of hands
int sumMyHand = 0;
int sumOpponentHand = 0;
//Make an array of the remaining cards
List<Integer> cards = new LinkedList<Integer>();
int[] cardsLeft = {4,4,4,4,4,4,4,4,4,4};
for (Card card : yourHand) {
cardsLeft[card.getValue()-1] -= 1;
sumMyHand+=card.getValue();
}
for (Card card : opponentHand) {
cardsLeft[card.getValue()-1] -= 1;
sumOpponentHand += card.getValue();
}
if(sumMyHand<=10)
{
this.action = Action.END;
}
else if (sumMyHand >= 20)
{
this.action = Action.STAND;
}
else if (sumOpponentHand > 20)
{
this.action = Action.STAND;
}
else
{
for (int i = 0; i < cardsLeft.length; i++) {
while(cardsLeft[i] > 0)
{
cards.add(i + 1);
cardsLeft[i] -= 1;
}
}
//System.out.println(Arrays.toString(cards));
double standPayoff = 0;
double endPayoff = 0;
double[] sideDeckPayoffs = new double[yourSideDeck.size()];
//Run some simulations
for(int sim = 0; sim<NUMBEROFSIMS; sim++)
{
Collections.shuffle(cards, r);
standPayoff += getPayoff(sumMyHand, sumOpponentHand, cards, Action.STAND, opponentAction, false, 0);
endPayoff += getPayoff(sumMyHand, sumOpponentHand, cards, Action.END, opponentAction, false, 0);
for(int i = 0; i<sideDeckPayoffs.length; i++)
{
sideDeckPayoffs[i] += getPayoff(sumMyHand+((Card)yourSideDeck.toArray()[i]).getValue(), sumOpponentHand, cards, Action.STAND, opponentAction, false, 0);
}
}
double maxSidePay = 0;
int sideDeckChoice = 0;
for (int i = 0; i < sideDeckPayoffs.length; i++) {
double d = sideDeckPayoffs[i];
if(d>maxSidePay)
{
maxSidePay = d;
sideDeckChoice = i;
}
}
/*System.out.println(standPayoff);
System.out.println(endPayoff);
System.out.println(maxSidePay);*/
if(maxSidePay>standPayoff && maxSidePay>endPayoff)
{
this.action = Action.PLAY;
this.cardToPlay = (Card)yourSideDeck.toArray()[sideDeckChoice];
}
else if(standPayoff > endPayoff)
{
this.action = Action.STAND;
}
else
{
this.action = Action.END;
}
}
}
private double getPayoff(int sumMyHand, int sumOpponentHand,
List<Integer> cards, Action myAction, Action opponentAction, boolean myTurn, int index) {
//SHort circuit some logic
if(sumMyHand>20 && sumOpponentHand>20)
{
return DrawPay;
}
else if(sumMyHand>20)
{
return LosePay;
}
else if(sumOpponentHand>20)
{
return WinPay;
}
else if(myAction == Action.STAND && opponentAction == Action.STAND)
{
if(sumMyHand>sumOpponentHand)
{
return WinPay;
}
else if(sumMyHand<sumOpponentHand)
{
return LosePay;
}
else
{
return DrawPay;
}
}
else
{
double standPayoff = 0;
double endPayoff = 0;
if(myTurn)
{
if(opponentAction == Action.END)
{
sumOpponentHand += cards.get(index);
index++;
}
if(myAction == Action.STAND)
{
return getPayoff(sumMyHand, sumOpponentHand, cards, myAction, opponentAction, false, index);
}
else
{
standPayoff = getPayoff(sumMyHand, sumOpponentHand, cards, Action.STAND, opponentAction, false, index);
endPayoff = getPayoff(sumMyHand, sumOpponentHand, cards, Action.END, opponentAction, false, index);
if(standPayoff>endPayoff)
{
return standPayoff;
}
else
{
return endPayoff;
}
}
}
else
{
if(myAction == Action.END)
{
sumMyHand += cards.get(index);
index++;
}
if(opponentAction == Action.STAND)
{
return getPayoff(sumMyHand, sumOpponentHand, cards, myAction, opponentAction, true, index);
}
else
{
standPayoff = getPayoff(sumMyHand, sumOpponentHand, cards, myAction, Action.STAND, true, index);
endPayoff = getPayoff(sumMyHand, sumOpponentHand, cards, myAction, Action.END, true, index);
if(standPayoff<endPayoff)
{
return standPayoff;
}
else
{
return endPayoff;
}
}
}
}
}
}