Yoshihiko Nagoya / Mbed 2 deprecated q9_poker

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Ayokura
Date:
Sun Nov 21 15:13:22 2010 +0000
Parent:
0:234f286e40c7
Commit message:
v2

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Nov 21 15:06:11 2010 +0000
+++ b/main.cpp	Sun Nov 21 15:13:22 2010 +0000
@@ -1,192 +1,191 @@
-#include "mbed.h"
-#include "TextLCD.h"
-
-TextLCD lcd(p24, p26, p27, p28, p29, p30);
-
-//#include <stdio.h>
-//#include <stdlib.h>  /* —””­¶—p‚̊֐”‚ðŽg—p‚·‚邽‚߂̃wƒbƒ_[ƒtƒ@ƒCƒ‹ */
-#define NPLAYERS 4   /* ƒvƒŒ[ƒ„‚̐l” */
-
-#define NGAMES 20000 /* ƒQ[ƒ€‚̃Vƒ~ƒ…ƒŒ[ƒVƒ‡ƒ“‰ñ” */
-
-/* ƒgƒ‰ƒ“ƒv‚ÌŽí—Þ: ƒNƒ‰ƒuCƒ_ƒCƒAƒ‚ƒ“ƒhCƒn[ƒgCƒXƒy[ƒh */
-#define CLUB 0
-#define DIAMOND 1
-#define HEART 2
-#define SPADE 3
-
-struct card { /* ƒgƒ‰ƒ“ƒv\‘¢‘̂̒è‹`                   */
-  int pips;   /* ”ŽšF1‚©‚ç13‚܂Š                     */
-  int suit;   /* Ží—ށFƒNƒ‰ƒuCƒ_ƒCƒACƒn[ƒgCƒXƒy[ƒh */
-};
-
-/* main()‚©‚çŒÄ‚Ԋ֐”‚̃vƒƒgƒ^ƒCƒv‚̐錾 */
-void init_card(struct card deck[]);
-void shuffle(struct card deck[]);
-void deal(struct card deck[], struct card hand[][5]);
-int is_straight(struct card h[]);
-int is_flush(struct card h[]);
-int is_fullhouse(struct card h[]);
-
-void print_deck(struct card deck[]);
-void print_hand(struct card deck[]);
-
-struct card deck[52];                 /* ƒJ[ƒh52–‡•ª‚ðéŒ¾ */
-struct card hand[NPLAYERS][5];        /* 4l‚̃vƒŒ[ƒ„‚ðéŒ¾ */
-
-int main(void) {
-  int i, j;                             /* i=ƒQ[ƒ€‰ñ”Cj=ƒvƒŒ[ƒ„‚̐l” */
-  int straights = 0, flushes = 0, fullhouses = 0; /* ‚»‚ꂼ‚ê‚Ì“–‚½‚è‚̐” */
-
-  srand(1);                             /* —”‚̏‰Šú‰» */
-  init_card(deck);                      /* ‰Û‘è‚PFƒJ[ƒh‚̏‰Šú‰» */
-  //print_deck(deck);
-  shuffle(deck);
-  //print_deck(deck);
-  for (i = 0; i < NGAMES; i++) {       /* NGAMES‰ñƒQ[ƒ€‚ðs‚È‚¤ */
-    shuffle(deck);          /* ‰Û‘è‚QFƒJ[ƒh‚̃Vƒƒƒbƒtƒ‹‚Æ•ª”z */
-    deal(deck, hand);       /* ‰Û‘è‚QFƒJ[ƒh‚̃Vƒƒƒbƒtƒ‹‚Æ•ª”z */
-    if (i == 0) {
-      //print_hand(hand[0]);
-    }
-    for (j = 0; j < NPLAYERS; j++) {    /* ŠeƒvƒŒ[ƒ„‚ÌŽè‚Ì“à‚ðƒ`ƒFƒbƒN */
-      if (is_straight(hand[j])) {       /* ‰Û‘è‚RFƒXƒgƒŒ[ƒg‚©H */
-        straights++;
-      }
-      if (is_flush(hand[j])) {          /* ‰Û‘è‚RFƒtƒ‰ƒbƒVƒ…‚©H */
-        flushes++;
-      }
-      if (is_fullhouse(hand[j])) {      /* ‰Û‘è‚RFƒtƒ‹ƒnƒEƒX‚©H */
-        fullhouses++;
-      }
-    }
-  }
-  lcd.cls();
-  lcd.locate(0, 0);
-  lcd.printf("%d, %d", straights, flushes);
-  lcd.locate(0, 1);
-  lcd.printf("%d",fullhouses);
-  return 0;
-}
-
-/* ‚±‚±‚©‚炪ŠÖ”‚Ì’è‹` */
-
-void print_card(struct card *c) {
-  /* 1–‡‚̃J[ƒh‚ðƒvƒŠƒ“ƒg‚·‚é */
-  char suit_name[4][10] = {"CLUB", "DIAMOND", "HEART", "SPADE" };
-  //printf("%s-%d", suit_name[c->suit], c->pips);
-}
-
-void print_deck(struct card deck[]) {
-  /* 52–‡‚̃J[ƒhƒfƒbƒL‚ðƒvƒŠƒ“ƒg‚·‚é */
-  int i;
-  //printf("--BEGIN--");
-  for (i = 0; i < 52; i++) {
-    if (i % 4 == 0) {
-      //printf("\n");
-    }
-    print_card(&deck[i]);
-    //printf(" ");
-  }
-  //printf("\n--END--\n");
-}
-
-void print_hand(struct card h[]) {
-  /* ƒvƒŒ[ƒ„1l‚Ô‚ñ‚̃J[ƒh5–‡‚ðƒvƒŠƒ“ƒg‚·‚é */
-  int i;
-  for (i = 0; i < 5; i++) {
-    //print_card(&h[i]); printf(" ");
-  }
-  //printf("\n");
-}
-
-void init_card(struct card deck[]) {
-  /* ‰Û‘è1 */
-  int i;
-  for(i=0; i<52; i++)
-  {
-    deck[i].suit=i/13;
-    deck[i].pips=i%13+1;
-  }
-}
-
-void shuffle(struct card deck[]) {
-  /* ‰Û‘è2 */
-  int i,j;
-  struct card tmp;
-  for(i=0; i<52; i++) {
-    j=rand()%52;
-    tmp=deck[i];
-    deck[i]=deck[j];
-    deck[j]=tmp;
-  }
-}
-
-void deal(struct card deck[], struct card hand[][5]) {
-  /* ‰Û‘è2 */
-  int i,j,idx;
-  idx=0;
-  for(i=0; i<5; i++)
-    for(j=0; j<NPLAYERS; j++)
-      hand[j][i]=deck[idx++];
-}
-
-/* ƒqƒ“ƒg: ‚±‚̊֐”‚ðŽg‚¤‚ƁC”»’肪ŠÈ’P‚©‚à */
-void distrib(struct card h[], int dist[]) {
-  int i;
-  for (i = 0; i < 14; i++) {
-    dist[i] = 0;
-  }
-  for (i = 0; i < 5; i++) {
-    dist[h[i].pips]++;
-  }
-}
-
-int is_straight(struct card h[]) {
-  /* ‰Û‘è3 */
-  int dist[14],i=1;
-  distrib(h,dist);
-  while(i<10) {
-    if(dist[i]) {
-      if(dist[i+1] && dist[i+2] && dist[i+3] && dist[i+4]){
-        return 1;
-      }else{
-        return 0;
-      }
-    }
-    i++;
-  }
-  return 0;
-}
-
-int is_flush(struct card h[]) {
-  /* ‰Û‘è3 */
-  int i;
-  for(i=4; i; i--) {
-    if(h[0].suit != h[i].suit) return 0;
-  }
-  return 1;
-}
-
-int is_fullhouse(struct card h[]) {
-  /* ‰Û‘è3 */
-  int dist[14],i,flag=0;
-  distrib(h,dist);
-  for(i=1; i<14; i++) {
-    switch(dist[i]) {
-      case 0:
-        break;
-      case 2:
-        if(flag&1) return 0;
-        if(flag&2) return 1;
-        flag |= 1;
-        break;
-      case 3:
-        if(flag&1) return 1;
-        flag |= 2;
-        break;
-      default:
-        return 0;
-    }
-  }
-}
+#include "mbed.h"
+#include "TextLCD.h"
+
+TextLCD lcd(p24, p26, p27, p28, p29, p30);
+
+//#include <stdio.h>
+//#include <stdlib.h>  /* 乱数発生用の関数を使用するためのヘッダーファイル */
+#define NPLAYERS 4   /* プレーヤの人数 */
+
+#define NGAMES 20000 /* ゲームのシミュレーション回数 */
+
+/* トランプの種類: クラブ,ダイアモンド,ハート,スペード */
+#define CLUB 0
+#define DIAMOND 1
+#define HEART 2
+#define SPADE 3
+
+struct card { /* トランプ構造体の定義                   */
+  int pips;   /* 数字:1から13まで                      */
+  int suit;   /* 種類:クラブ,ダイア,ハート,スペード */
+};
+
+/* main()から呼ぶ関数のプロトタイプの宣言 */
+void init_card(struct card deck[]);
+void shuffle(struct card deck[]);
+void deal(struct card deck[], struct card hand[][5]);
+int is_straight(struct card h[]);
+int is_flush(struct card h[]);
+int is_fullhouse(struct card h[]);
+
+void print_deck(struct card deck[]);
+void print_hand(struct card deck[]);
+
+struct card deck[52];                 /* カード52枚分を宣言 */
+struct card hand[NPLAYERS][5];        /* 4人のプレーヤを宣言 */
+
+int main(void) {
+  int i, j;                             /* i=ゲーム回数,j=プレーヤの人数 */
+  int straights = 0, flushes = 0, fullhouses = 0; /* それぞれの当たりの数 */
+
+  srand(1);                             /* 乱数の初期化 */
+  init_card(deck);                      /* 課題1:カードの初期化 */
+  //print_deck(deck);
+  shuffle(deck);
+  //print_deck(deck);
+  lcd.cls();
+  lcd.locate(0, 0);
+  for (i = 0; i < NGAMES; i++) {       /* NGAMES回ゲームを行なう */
+    shuffle(deck);          /* 課題2:カードのシャッフルと分配 */
+    deal(deck, hand);       /* 課題2:カードのシャッフルと分配 */
+    if (i == 0) {
+      print_hand(hand[0]);
+    }
+    for (j = 0; j < NPLAYERS; j++) {    /* 各プレーヤの手の内をチェック */
+      if (is_straight(hand[j])) {       /* 課題3:ストレートか? */
+        straights++;
+      }
+      if (is_flush(hand[j])) {          /* 課題3:フラッシュか? */
+        flushes++;
+      }
+      if (is_fullhouse(hand[j])) {      /* 課題3:フルハウスか? */
+        fullhouses++;
+      }
+    }
+  }
+  lcd.locate(0, 1);
+  lcd.printf("%d, %d, %d", straights, flushes, fullhouses);
+  return 0;
+}
+
+/* ここからが関数の定義 */
+
+void print_card(struct card *c) {
+  /* 1枚のカードをプリントする */
+  char suit_name[4][10] = {"C", "D", "H", "S" };
+  lcd.printf("%s%d", suit_name[c->suit], c->pips);
+}
+
+void print_deck(struct card deck[]) {
+  /* 52枚のカードデッキをプリントする */
+  int i;
+  //printf("--BEGIN--");
+  for (i = 0; i < 52; i++) {
+    if (i % 4 == 0) {
+      //printf("\n");
+    }
+    print_card(&deck[i]);
+    //printf(" ");
+  }
+  //printf("\n--END--\n");
+}
+
+void print_hand(struct card h[]) {
+  /* プレーヤ1人ぶんのカード5枚をプリントする */
+  int i;
+  for (i = 0; i < 5; i++) {
+    print_card(&h[i]);// lcd.printf(" ");
+  }
+  //printf("\n");
+}
+
+void init_card(struct card deck[]) {
+  /* 課題1 */
+  int i;
+  for(i=0; i<52; i++)
+  {
+    deck[i].suit=i/13;
+    deck[i].pips=i%13+1;
+  }
+}
+
+void shuffle(struct card deck[]) {
+  /* 課題2 */
+  int i,j;
+  struct card tmp;
+  for(i=0; i<52; i++) {
+    j=rand()%52;
+    tmp=deck[i];
+    deck[i]=deck[j];
+    deck[j]=tmp;
+  }
+}
+
+void deal(struct card deck[], struct card hand[][5]) {
+  /* 課題2 */
+  int i,j,idx;
+  idx=0;
+  for(i=0; i<5; i++)
+    for(j=0; j<NPLAYERS; j++)
+      hand[j][i]=deck[idx++];
+}
+
+/* ヒント: この関数を使うと,判定が簡単かも */
+void distrib(struct card h[], int dist[]) {
+  int i;
+  for (i = 0; i < 14; i++) {
+    dist[i] = 0;
+  }
+  for (i = 0; i < 5; i++) {
+    dist[h[i].pips]++;
+  }
+}
+
+int is_straight(struct card h[]) {
+  /* 課題3 */
+  int dist[14],i=1;
+  distrib(h,dist);
+  while(i<10) {
+    if(dist[i]) {
+      if(dist[i+1] && dist[i+2] && dist[i+3] && dist[i+4]){
+        return 1;
+      }else{
+        return 0;
+      }
+    }
+    i++;
+  }
+  return 0;
+}
+
+int is_flush(struct card h[]) {
+  /* 課題3 */
+  int i;
+  for(i=4; i; i--) {
+    if(h[0].suit != h[i].suit) return 0;
+  }
+  return 1;
+}
+
+int is_fullhouse(struct card h[]) {
+  /* 課題3 */
+  int dist[14],i,flag=0;
+  distrib(h,dist);
+  for(i=1; i<14; i++) {
+    switch(dist[i]) {
+      case 0:
+        break;
+      case 2:
+        if(flag&1) return 0;
+        if(flag&2) return 1;
+        flag |= 1;
+        break;
+      case 3:
+        if(flag&1) return 1;
+        flag |= 2;
+        break;
+      default:
+        return 0;
+    }
+  }
+}