Decks part2:
playersCards=new int[HAND_SIZE];
computersCards=new int[HAND_SIZE];
}
/** deal - (re-)start a game
@param none
@return none
deal two cards to the players hiding the computer's first card.
*/
public void deal()
{
trace("deal: begins");
//initialise hands to empty
for (int i=0; i>
{
playersCards=-1;
computersCards=-1;
}
currentCard=-1;
numPlayersCards=0;
totalPlayersCards=0;
numComputersCards=0;
totalComputersCards=0;
//deal first four cards
drawCard(true);
drawCard(false);
drawCard(true);
drawCard(false);
//reveal computer's second card
totalComputersCards=CARD_VALUES[computersCards[1]%cards.length%NUM_SUITS];
trace("draw: ends");
}
/** drawCard - draw the next card
@param booean -- player's turn?
@return none
draw a fresh card for the nominated player
*/
public void drawCard(boolean playersTurn)
{
int deck;
int card;
int result;
boolean duplicate;
trace("drawCard: begins");
do
{
//determine next card
deck=generator.nextInt(cards.length);
card=generator.nextInt(NUM_CARDS*NUM_SUITS);
result=deck*NUM_CARDS+card;
//check if already drawn
duplicate=false;
for (int i=0; i>
{
if ((playersCards==result) || (computersCards==result))
{
duplicate=true;
}
}
}
while (duplicate);
//update player/computer's hand
if (playersTurn)
{
playersCards[numPlayersCards]=result;
numPlayersCards++;
totalPlayersCards+=CARD_VALUES[card%NUM_CARDS];
}
else
{
computersCards[numComputersCards]=result;
numComputersCards++;
totalComputersCards+=CARD_VALUES[card%NUM_CARDS];
}
currentCard=result;
trace("drawCard: ends");
}
/** computersTurn - complete computer's turn
@param none
@return none
Determine whether computer should have a turn, and if so, draw the card
*/
public void computersTurn()
{
trace("computersTurn: begins");
if (!((numComputersCards == FIVE_AND_UNDER) || (totalComputersCards >= COMPUTER_SITS)))
{
drawCard(! PLAYERS_TURN);
}
trace("computersTurn: ends");
}
/** isBust - determine if nominated player is bust
@param boolean -- checking the player?
@return boolean -- whether the nominated player is bust
Determine whether the total of the nominated player exceeds 21
*/
public boolean isBust(boolean playersTurn)
{
trace("isBust: begins and ends");
if (playersTurn == PLAYERS_TURN)
{
return totalPlayersCards > 21;
}
else
{
return totalComputersCards > 21;
}
}
/** refreshComputersTotal - recalculate computer's total
@param none
@return none
Reveal all of computers cards, recalculating the total
*/
public void refreshComputersTotal()
{
trace("refreshComputersTotal: begins");
totalComputersCards=0;
for (int i=0; i
{
totalComputersCards+=CARD_VALUES[computersCards%(NUM_CARDS*NUM_SUITS)%NUM_CARDS];
}
trace("refreshComputersTotal: ends");
}
/** gameOver - determine whether turns have/should end
@param none
@return boolean -- whether or not the game is over
Determine whether the game is effectively concluded
*/
public boolean gameOver()
{
boolean ans;
trace("gameOver: begins");
ans=false;
// check is someone exceeds 21
if (isBust(PLAYERS_TURN) || isBust(!PLAYERS_TURN))
{
trace("gameOver: a player is bust");
ans=true;
}
// check if player has "5 and under"
if ((getNumberOfCards(PLAYERS_TURN) == FIVE_AND_UNDER) && (!isBust(PLAYERS_TURN)))
{
trace("gameOver: player has 5 and under");
ans=true;
}
// check if computer sits
if (getTotalOfCards(!PLAYERS_TURN) >= COMPUTER_SITS)
{
trace("gameOver: computer sits");
ans=true;
}
trace("gameOver: ends with return value of " + ans);
return ans;
}
/** whoWon - determine who (if anyone) has won
@param none
@return int -- a value indicating noone, player, or computer
Determine who (if anyone) has won
*/