ここではJavaを使ってポーカーのプログラムを作ってみようと思います。
コマンドプロンプト上で動くものにしようと思います。
ゲームの流れ
まず、ゲームの流れを整理します。
1.手札を5枚引く
2.手札の確認
3.交換するカードの選択
4.カード交換
5.手札の公開
6.結果の発表
ポーカーのソースコード
import java.util.*;
import java.io.*;
public class poker {
public static void main(String[] args) throws IOException{
String[][] card_mark={
{" ------- ",
"|A | ",
"| | ",
"| * | ",
"| | ",
"| | ",
" ------- "},
{" ------- ",
"|2 | ",
"| * | ",
"| | ",
"| * | ",
"| | ",
" ------- "},
{" ------- ",
"|3 | ",
"| * | ",
"| * | ",
"| * | ",
"| | ",
" ------- "},
{" ------- ",
"|4 | ",
"| * * | ",
"| | ",
"| * * | ",
"| | ",
" ------- "},
{" ------- ",
"|5 | ",
"| * * | ",
"| * | ",
"| * * | ",
"| | ",
" ------- "},
{" ------- ",
"|6 | ",
"| * * | ",
"| * * | ",
"| * * | ",
"| | ",
" ------- "},
{" ------- ",
"|7 | ",
"| * * | ",
"| * * * | ",
"| * * | ",
"| | ",
" ------- "},
{" ------- ",
"|8 | ",
"| * * * | ",
"| * * | ",
"| * | ",
"| * * | ",
" ------- "},
{" ------- ",
"|9 | ",
"| * * | ",
"| * * * | ",
"| * * | ",
"| * * | ",
" ------- "},
{" ------- ",
"|10 | ",
"| * * | ",
"| * * * | ",
"| * * * | ",
"| * * | ",
" ------- "},
{" ------- ",
"|J | ",
"| ( '-')| ",
"| /==$=|| ",
"||=$==/ | ",
"|(,-,) | ",
" ------- "},
{" ------- ",
"|Q | ",
"| ( '-')| ",
"| /==$=|| ",
"||=$==/ | ",
"|(,-,) | ",
" ------- "},
{" ------- ",
"|K | ",
"| ( '-')| ",
"| /==$=|| ",
"||=$==/ | ",
"|(,-,) | ",
" ------- "}};
int[] hand = new int[5];
int[] hand_number = new int[5];
String[] hand_suit = new String[5];
Card Card = new Card();
card_check card_check = new card_check();
Card.add_card();
int[] com_hand=new int[5];
int[] com_hand_number=new int[5];
String[] com_hand_suit=new String[5];
//コンピューター用
for (int i=0;i<5;i++) {
com_hand[i]=Card.take_one_pages();
com_hand_number[i]=card_check.get_card_number(com_hand[i]);
com_hand_suit[i]=card_check.get_suit(com_hand[i]);
}
for (int i=0;i<5;i++)
hand[i] = Card.take_one_pages();
//手札の数字、スートを取得
for (int i = 0;i<5;i++) {
hand_number[i]=card_check.get_card_number(hand[i]);
hand_suit[i]=card_check.get_suit(hand[i]);
}
//画面表示
System.out.println();
System.out.println(" 1 2 3 4 5 ");
for (int i=0;i<5;i++)
System.out.print(hand_suit[i]);
System.out.println();
int j=0;
for (int i=0;i<7;i++)
System.out.println(card_mark[hand_number[j]-1][i]+card_mark[hand_number[j+1]-1][i]+card_mark[hand_number[j+2]-1][i]+card_mark[hand_number[j+3]-1][i]+card_mark[hand_number[j+4]-1][i]);
System.out.println("交換するカードの番号を入力してください");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int i=0;i<5;i++) {
String str=br.readLine();
if (str.equals("end")) break;
int num = Integer.parseInt(str);
hand[num-1]=Card.take_one_pages();
}
for (int i = 0;i<5;i++) {
hand_number[i]=card_check.get_card_number(hand[i]);
hand_suit[i]=card_check.get_suit(hand[i]);
}
//コンピューター手札表示
for (int i=0;i<20;i++)
System.out.println();
System.out.println("コンピューター");
for (int i=0;i<5;i++)
System.out.print(com_hand_suit[i]);
System.out.println();
j=0;
for (int i=0;i<7;i++)
System.out.println(card_mark[com_hand_number[j]-1][i]+card_mark[com_hand_number[j+1]-1][i]+card_mark[com_hand_number[j+2]-1][i]+card_mark[com_hand_number[j+3]-1][i]+card_mark[com_hand_number[j+4]-1][i]);
role_judgment role_judgment = new role_judgment();
String str = role_judgment.judgment(com_hand_number,com_hand_suit);
System.out.println();
System.out.println(str);
int score_com=score(str);
for (int i=0;i<3;i++)
System.out.println();
System.out.println("プレイヤー");
for (int i=0;i<5;i++)
System.out.print(hand_suit[i]);
System.out.println();
j=0;
for (int i=0;i<7;i++)
System.out.println(card_mark[hand_number[j]-1][i]+card_mark[hand_number[j+1]-1][i]+card_mark[hand_number[j+2]-1][i]+card_mark[hand_number[j+3]-1][i]+card_mark[hand_number[j+4]-1][i]);
//判定
str = role_judgment.judgment(hand_number,hand_suit); System.out.println();
System.out.println(str);
int score=score(str);
for (int i=0;i<3;i++)
System.out.println();
if (score>score_com) System.out.println("あなたの勝ちです");
if (score<score_com) System.out.println("あなたの負けです");
else if (score==score_com) System.out.println("引き分けです");
}
static int score(String str) {
if (str.equals("ロイヤルストレートフラッシュ")) return 9;
else if (str.equals("ストレートフラッシュ")) return 8;
else if (str.equals("フォーカード")) return 7;
else if (str.equals("フルハウス")) return 6;
else if (str.equals("フラッシュ")) return 5;
else if (str.equals("ストレート")) return 4;
else if (str.equals("スリーカード")) return 3;
else if (str.equals("ツーペア")) return 2;
else if (str.equals("ワンペア")) return 1;
else return 0;
}
}
class role_judgment{
String judgment(int[] hand_number,String[] hand_suit) {
int[] X = aggregate(hand_number);
if (royal_straight_flush(hand_number,hand_suit)) return "ロイヤルストレートフラッシュ";
if (straight(hand_number)) return flush(hand_suit)? "ストレートフラッシュ":"ストレート";
if (X == 1) return "フォーカード";
if (X == 1) return X==1 ? "フルハウス":"スリーカード";
if (flush(hand_suit)) return "フラッシュ";
if (X == 2) return "ツーペア";
if (X == 1) return "ワンペア";
return "ノーペア";
}
private int[] aggregate(int hand_number[]) {
int[] count = new int[14];
int[] aggregate = new int[5];
for (int i=0;i<5;i++) count[hand_number[i]] += 1;
for (int m:count) ++aggregate[m];
return aggregate;
}
private int[] sort(int hand_number[]){
for (int i=0;i<4;i++)
for (int j=0;j<4-i;j++)
if (hand_number[j] > hand_number[j+1]) {
int temp = hand_number[j];
hand_number[j] = hand_number[j+1];
hand_number[j+1] = temp;
}
return hand_number;
}
private boolean straight(int hand_number[]) {
hand_number = sort(hand_number);
return (hand_number[0]==1 &&
hand_number==10 &&
hand_number==11 &&
hand_number==12 &&
hand_number==13) ||
(hand_number == hand_number[0]+1) &&
(hand_number == hand_number[0]+2) &&
(hand_number == hand_number[0]+3) &&
(hand_number == hand_number[0]+4);
}
private boolean flush(String hand_suit[]) {
return (hand_suit[0]==hand_suit) &&
(hand_suit[0]==hand_suit) &&
(hand_suit[0]==hand_suit) &&
(hand_suit[0]==hand_suit);
}
private boolean royal_straight_flush(int hand_number[],String hand_suit[]) {
hand_number = sort(hand_number);
return (flush(hand_suit)==true) &&
(straight(hand_number)==true) &&
(hand_number[0]==1) &&
(hand_number==10) &&
(hand_number==11) &&
(hand_number==12) &&
(hand_number==13);
}
}
class Card {
ArrayList<Integer> all_card = new ArrayList<Integer>();
void add_card() {
for (int i = 0;i<52;++i) all_card.add(i);
Collections.shuffle(all_card);
}
public void shuffle() {
Collections.shuffle(all_card);
}
public int take_one_pages() {
return all_card.remove(0);
}
}
class card_check {
private int ID;
private final String suit[] = {" ハート ","スペード "," ダイヤ ","クローバー "};
private final String card_number[] = {"0","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
public int get_card_number(int ID) {return ID%13 + 1;}
public String get_suit(int ID) {return suit[ID/13];}
}
実行結果
感想
コマンドプロンプトにカードを表示させるようにしてみましたが、
調整がめんどくさかった。
今回は片手間にパパッと作ったので、ソースに汚いところが多々あるが見逃して欲しい。
細かい説明はまた今度やろうと思います。
最後にアンケートにご協力ください。今後のサイト運営の参考にさせていただきます。