Package games.war.behaviors

Source Code of games.war.behaviors.Cheating

package games.war.behaviors;

import game.Card;
import games.war.PlayerController;
import hand.Hand;

/**
* Allows a player to cheat by looking for the lowest winning card in their hand
* @author Chris Hersh
*
*/
public class Cheating implements CheatingBehavior
{

    @Override
    public Card getNextCard(Hand currHand)
    {
        Card toBeat = PlayerController.previousCard;
        Card bestCard = null;
        int bestPos = 0;
        if (toBeat == null)
        {
            bestCard = currHand.getCard(0);
            bestPos = 0;
        }
        else
        {
            for (int i = 1; i < currHand.getSize(); i++)
            {
                //if(currHand.getCard(i).getValue() > bestCard.getValue() && currHand.getCard(i).getValue() > toBeat.getValue())
                if (checkCard(currHand.getCard(i), toBeat, bestCard))
                {
                    bestCard = currHand.getCard(i);
                    bestPos = i;
                }
            }
        }
        if (bestCard == null)
        {
            bestCard = currHand.getCard(0);
            bestPos = 0;
        }
        currHand.removeCard(bestPos);
        //System.out.print("Best" +bestCard);
        return bestCard;
    }

    /**
     * Checks the current card to see if it is better than the current best card
     * @param currCard Card being looked at
     * @param theirCard The last placed card
     * @param bestCard The current best card to play
     * @return True if the current card is better than the best, false otherwise
     */
    public boolean checkCard(Card currCard, Card theirCard, Card bestCard)
    {
        if (bestCard == null)
        {
            if (currCard.getValue() >= theirCard.getValue())
            {
                return true;
            }
            return false;
        }

        int currCardInt = currCard.getValue();
        int theirCardInt = theirCard.getValue();
        int bestCardInt = bestCard.getValue();
        if (bestCardInt > theirCardInt)
        {
            if (currCardInt < bestCardInt && currCardInt > theirCardInt)
            {
                return true;
            }
            if (currCardInt > bestCardInt || currCardInt < theirCardInt)
            {
                return false;
            }

        }
        return false;
    }
}
TOP

Related Classes of games.war.behaviors.Cheating

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.