📄 deal.java
字号:
package questions.c5;
// interface definition here
class Card {
String suit;
int value;
Card( int i ) {
if ( i < 13 ) suit = "Spades";
else if ( i < 26 ) suit = "Hearts";
else if ( i < 39 ) suit = "Diamonds";
else suit = "Clubs";
value = i % 13 + 1;
}
public String toString () {
return Integer.toString( value ) + " of " + suit;
}
}
// non-public Hand class to implement Stackable Here
// one constructor that takes one int argument for
// size of Hand
public class Deal {
private static int nplayers, sizehand;
static private Card[] deck = new Card[52];
public static void main( String[] args ) {
// create a hand for each player
Hand[] players = new Hand[nplayers];
//
// deal the cards: up to sizehand to a player
for (int i = 0; i < nplayers; i++) {
players[i] = new Hand( sizehand );
}
int count = 0, whichPlayer = 0;
for ( int i = 0; i < 52; i ++ ) {
// see which player gets the next card
whichPlayer = i % nplayers;
players[whichPlayer].push( deck_[i] );
// Stop as soon as one player has a full hand
count++;
if ( players[whichPlayer].isFull() ) break;
}
System.out.println( "Cards dealt: " + count );
// see which card was last dealt
System.out.println( "last card dealt: "
+ (Card) players[whichPlayer].peek() );
// now print the hands dealt:
for ( int i = 0; i < nplayers; i ++ ) {
System.out.println( " Player " + i + ":" );
while ( ! players[i].isEmpty() )
System.out.println( "\t"
+ (Card) players[i].pop() );
}
}
static {
nplayers = 5; sizehand = 5;
for (int i = 0; i < 52; i++) {
deck[i] = new Card( i );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -