## Trending News

# A java program about pokergame

I've learned Java for 2 month.

Because I pre-choosed the corse, I meet a program assignment.

You are to write a program that simulates a poker game between three parties, a dealer and two other players A and B. In each round of the game, each player is dealt a hand of two cards. Number-cards (2, 3, 4,…, 10) count as their natural value; the jack, queen, and king count as 10; aces is valued as 1. Hands are valued according to the rightmost digit of the sum of their constituent cards: for example, a hand consisting of 2 and 3 is worth 5, but a hand consisting of 6 and 7 is worth 3 (the rightmost digit of the total, 13). The highest possible hand value is 9. We then compare the hands of A and B with the banker’s hand to determine the outcome: “banker win”, “player win”, or “tie.”

We assume that the deck of 52 cards is well shuffled, and thus the order of each card is random. Your program will prompt the user for either one game at a time or a sequence of games. In the first case, your program will show the hands of each game and the outcome for both players A and B. In the latter case, the user must enter the number of games to be played. Your program will then print the following outcomes: (1) numbers of wins and losses for both players A and B; (2) number of games in which both A and B win; and (3) number of games A wins given that B wins.

Note that you have to use the pseudo random generator to generate the random numbers needed for dealing the hands.

Eventhough I know I can use enum to perform this problem, I still fell confused about this program. Could somebody help me to figure out how to solve this question?

Thank you for answering 黑田兩光 :) . The meaning of (3) is "In the condition of B wins, what is the probability of A wins". I also feel confused about this question :( .

### 3 Answers

- 黑田憂Lv 59 years agoFavorite Answer
英文苦手...裡面是指兩個玩家玩21點嗎

2012-03-15 20:51:14 補充：

要輸出的部份都在showOutcomes這方法裡,除了number of games A wins given that B wins.這個我看不懂它的意思orz,所以沒寫.

基本上每回合開始秀出3個人的手牌和它的分數然後判定誰贏,當牌數少於6張時會重新產生新的牌並洗牌再繼續牌局.

三人都一樣才算平手,但若只有兩個人(莊家和一名玩家)則算莊家贏...

因字數的限制,以下是其中一部份

完整的在https://skydrive.live.com/?cid=706822152af153ef#ci...

public Poker(){

name=new String[]{"banker","playerA","playerB","tie"};

faces=new String[]{"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};

suits=new String[]{"Hearts","Diamonds","Clubs","Spades"};

player=new Card[3][];

for (int i=0;i < 3;i++){

player[i]=new Card[2];

}

produce();

shuffle();

game(12);//set to play how much game

}

public void game(int time){

games=0;

winTable=new int[time];

int[] who=new int[3];

while (time-->0){

System.out.println("GAME "+(games+1)+" :");

//produce & shuffle

if (deck.size()<=6){

produce();

shuffle();

}

//start

for (int i=0;i <3;i++){

player[i]=dealing();

who[i]=worth(player[i]);

System.out.println(name[i]+":");

show(player[i]);

System.out.println("\tWorth:"+who[i]);

}

//WINER IS

if (who[0]==who[1] && who[0]==who[2]){

winTable[games]=3;

}else if (Math.max(who[1],who[2])>who[0]){

if (who[1]>who[2]){

winTable[games]=1;

}else{

winTable[games]=2;

}

}else{

winTable[games]=0;

}

//show

System.out.println("WINER IS "+name[winTable[games]]);

games++;

}

showOutcomes();

}

//produce 52 card

private void produce(){

deck=new LinkedList();

for (int i=0;i < 52;i++){

deck.add(new Card(faces[i%13],suits[i/13]));

}

}

//shuffle

private void shuffle(){

for (int i=0;i < 52;i++){

int j = (int) (Math.random()*52);

Card temp = deck.get(i);

deck.set(i,deck.get(j));

deck.set(j,temp);

}

}

//dealing

private Card[] dealing(){

Card[] two = new Card[2];

for (int i=0;i < 2;i++){

two[i]=deck.remove((int)(Math.random()*deck.size()));

}

return two;

}

//worth

private int worth(Card[] c){

int num=c[0].getNum()+c[1].getNum();

while (num>=10){

num=(num/10)+(num%10);

}

return num;

}

2012-03-16 16:03:49 補充：

那是說輸嬴是和莊家比就對了嗎?那我改一下判斷輸嬴的程式碼就好了

2012-03-16 16:13:22 補充：

我重新上傳修改過的,再試試看

2012-03-16 16:35:21 補充：

我還是不懂第3題要的是什麼...

每局輸贏都記錄在winTable中,0:banker 1:playerA 2:playerB 3:A&B 4:tie

看要什麼結果可以自行再修改

Source(s): 自己 - Anonymous6 years ago
＊ ***** http://***** ＊

[電子遊戲]

拉霸、水果盤、7PK、5PK

[運彩遊戲]

棒球、籃球、足球、網球、冰球、各種體育經典賽事

[真人遊戲]

百家樂、21點、骰寶、輪盤、牌九、三公、輪盤、翻攤、牛牛、二八杠

[對戰遊戲]

台灣麻將、德州撲克、骰盅吹牛、四支刀、鬥地主、十三支、二八槓、暗棋、接龍

[彩球遊戲]

香港六合、台灣樂透、今彩539、樂合彩、大陸時時彩、基諾彩、北京賽車、賽狗、賽馬、指數

[優惠活動]

1. 新舊會員儲值就送500點

2. 真人百家樂彩金等你拿

***** http://*****

歡迎免費體驗試玩!!

線上影片直播、正妹圖、討論區免費註冊 ~

- ΨετμουνΤLv 79 years ago
For me, (2) and (3) are asking for the same thing.

(2) is asking for the number of games where A wins and B wins, which is quite obvious and straightforward; (3) is asking for the "number" of games where B wins, and A also wins. Doesn't that look just like (2)?