一个扑克牌程序

public class DeckOfCards extends Applet{
private Card deck[];
private int currentCard;

private Button dealButton, shuffleButton;
private TextField displayCard;

public void init(){
String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King" };
String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };

deck = new Card[ 52 ];
currentCard = -1;

for( int i = 0; i < deck.length; i++ )
deck[ i ] = new Card( faces[ i % 13 ], suits[ i / 13 ] );

dealButton = new Button( "Deal card" );
shuffleButton = new Button( "shuffle cards" );
displayCard = new TextField( 20 );
displayCard.setEditable( false );
add( dealButton );
add( shuffleButton );
add( displayCard );
}

public boolean action( Event event, Object object ){
if( event.target == dealButton ){
Card dealt = dealCard();
if( dealt != null ){
displayCard.setText( dealt.toString() );
showStatus( "Card #: " + currentCard );
}
else {
displayCard.setText( "NO MORE CARDS TO DEAL!" );
showStatus( "Shuffle cards to continue!" );
}
}
else if( event.target == shuffleButton ){
displayCard.setText( "SHUFFLE CARDS...." );
showStatus( "" );
shuffle();
displayCard.setText( "DECK IS SHUFFLED!" );
}

return true;
}

public void shuffle(){
currentCard = -1;
for( int i = 0; i < deck.length; i++ ){
int j = ( int )( Math.random() * 52 );
Card temp = deck[ i ];
deck[ i ] = deck[ j ];
deck[ j ] = temp;
}

dealButton.enable();
}

public Card dealCard(){
if( ++currentCard < deck.length )
return deck[ currentCard ];
else {
dealButton.disable();
return null;
}
}
}

class Card{
private String face;
private String suit;

public Card( String f, String s ){
face = f;
suit = s;
}

public String toString(){
return face + " of " + suit ;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值