Package games.war

Source Code of games.war.PlayerController

package games.war;

import game.Card;
import games.war.behaviors.CheatingBehavior;
import hand.WarHand;

import java.util.ArrayList;
import java.util.Vector;

/**
* PlayerController is the game engine. This class takes care of
* telling players to take turns and placing cards on the screen.
* @author Chris Hersh
*
*/
public class PlayerController extends TimeObserver
{
    protected Vector<WarPlayer> players;

    public static Card previousCard;

    protected Card player1Card;

    protected Card player2Card;

    protected Card[] warCards1;

    protected Card[] warCards2;

    protected ArrayList<Card> cardsOnScreen;

    protected WarTimer time;

    /**
     * @param player1 The CheatingBehavior for player 1
     * @param player2 The CheatingBehavior for player 2
     */
    public PlayerController(CheatingBehavior player1, CheatingBehavior player2)
    {
        players = new Vector<WarPlayer>();
        players.add(new WarPlayer(new WarHand(), player1));
        players.add(new WarPlayer(new WarHand(), player2));
        player1Card = null;
        player2Card = null;
        cardsOnScreen = new ArrayList<Card>();
        time = WarTimer.getInstance();
        //view.addCard(new Card(9, 'h'), 0, 0, false);
        //killCardsOnScreen();

    }

    @Override
    public void update()
    {
        killCardsOnScreen();
        if (players.get(0).getHandSize() == 0)
        {
            System.out.print("Player 2 won in ");
            time.setFlag(false);
        }
        else if (players.get(1).getHandSize() == 0)
        {
            System.out.print("Player 1 won in ");
            time.setFlag(false);
        }
        else
        {
            playerNormalDraw(0);
            playerNormalDraw(1);

            while (checkForWar(player1Card, player2Card))
            {
                warActions();
            }
            players.get(findWinner()).addCardsToHand(cardsOnScreen);
        }
    }

    /**
     * warActions makes sure each player plays 3 cards, if able, followed by another 1
     */
    protected void warActions()
    {
        warCards1 = goToWar(players.get(0));
        warCards2 = goToWar(players.get(1));
        for (int i = 0; i < 3; i++)
        {
            placeCard(0, warCards1[i]);
            placeCard(1, warCards2[i]);
        }
        playerNormalDraw(0);
        playerNormalDraw(1);
    }

    /**
     * playerNormalDraw tells the given player to draw their next card
     * @param playerNum The player whose turn it is, 0 for player 1; 1 for player 2
     */
    protected void playerNormalDraw(int playerNum)
    {
        Card currCard = players.get(playerNum).takeTurn();
        previousCard = currCard;
        placeCard(playerNum, currCard);

        if (playerNum == 0)
        {
            player1Card = currCard;
        }
        else
        {
            player2Card = currCard;
        }
    }

    /**
     * Checks the given cards to see if the players need to go to war or not
     * @param pl1 Player 1's last card
     * @param pl2 Player 2's last card
     * @return Whether or not the players will go to war
     */
    protected boolean checkForWar(Card pl1, Card pl2)
    {
        //System.out.println(players.get(0).getHand());
        //System.out.println(players.get(1).getHand());
        if (pl1 == null || pl2 == null)
            return false;
        return pl1.getValue() == pl2.getValue();
    }

    /**
     * Tells the given player to play upto 3 cards, for use during war.
     * @param player The player whose turn it is, 0 for player 1; 1 for player 2
     * @return A 3 element Card array
     *      If any card is null then the player didn't have enough cards
     */
    protected Card[] goToWar(WarPlayer player)
    {
        return player.war();
    }

    /**
     * Checks the last card from each player and see who has won
     *      Is called after war is settled.
     * @return 0 for player 1, 1 for player 2
     */
    protected int findWinner()
    {
        if (player1Card == null)
        {
            return 1;
        }
        if (player2Card == null)
        {
            return 0;
        }
        if (player1Card.getValue() > player2Card.getValue())
            return 0;
        return 1;
    }

    /**
     * Places the card onto the screen (console)
     * @param playerNum The player who played the card
     * @param card The card that was played
     */
    protected void placeCard(int playerNum, Card card)
    {
        String cardOutput = card + "";
        if (card == null)
        {
            cardOutput = "";
        }
        else
            cardsOnScreen.add(card);
        if (playerNum == 0)
        {
            System.out.print(cardOutput);
        }
        if (playerNum == 1)
        {
            System.out.println(" \t\t" + cardOutput);
        }
    }

    /**
     * Removes all cards from the screen and outputs a new updated header.
     */
    protected void killCardsOnScreen()
    {
        System.out.print("\n");
        for (int i = 0; i < 80; i++)
        {
            System.out.print("-");
        }
        System.out.println("\nPlayer One\tPlayer Two");
        System.out.print("Cards: " + players.get(0).getHandSize() + "\tCards: ");
        System.out.println(players.get(1).getHandSize());
        cardsOnScreen.clear();
    }
}
TOP

Related Classes of games.war.PlayerController

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.