|
public class TestPoker1 { public static void main(String[] args) { DeckOfCards a; Card[] player1 = new Card[5]; Card[] player2 = new Card[5]; int i; a = new DeckOfCards(); System.out.println(a); System.out.println("Shuffle cards...."); a.shuffle(1000); System.out.println(a); /* ============================= Deal cards to 2 players ============================= */ for ( i = 0; i < 5; i++ ) { player1[i] = a.deal(); player2[i] = a.deal(); } System.out.print("player1's hand: "); for ( i = 0; i < 5; i++ ) System.out.print( player1[i] + " "); System.out.println(); System.out.print("player2's hand: "); for ( i = 0; i < 5; i++ ) System.out.print( player2[i] + " "); System.out.println(); } } |
Sample output:
Ad 2d 3d 4d 5d 6d 7d 8d 9d 10d Jd Qd Kd Ac 2c 3c 4c 5c 6c 7c 8c 9c 10c Jc Qc Kc Ah 2h 3h 4h 5h 6h 7h 8h 9h 10h Jh Qh Kh As 2s 3s 4s 5s 6s 7s 8s 9s 10s Js Qs Ks Shuffle cards.... 9s 8s 3c 10h Qh 4s 7h Jh 5c 2s As Kc Ac Qc 4h Ah Qs 4d 8d 8h 10c 6c 6s Jd 10s Qd Ks 5d 5h 8c 7s 9c 4c 3h 9d Kh 2h 5s 3s 9h 6d 2d Ad 7c 6h 10d Kd 2c Js 3d 7d Jc player1's hand: 9s 3c Qh 7h 5c player2's hand: 8s 10h 4s Jh 2s |
How to run the program:
|
public class PokerHand { private Card[] hand; public PokerHand() { hand = new Card[5]; } } |
Advantage:
|
Disadvantage:
|
That's why I use an array to represent a player's hand:
Card[] player1 = new Card[5]; Card[] player2 = new Card[5]; |
It is not very safe...
After dealing some cards:
/* ============================= Deal cards to 2 players ============================= */ for ( i = 0; i < 5; i++ ) { player1[i] = a.deal(); player2[i] = a.deal(); } |
You can modify some of the cards in the hand easily:
player1[0] = a.deal(); player2[2] = a.deal(); |
(I think simplicity should be the prime concern in an introductory course)