java新手抽牌_Java初学者有问题求教

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

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值