Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 1:f46ed1683d87, committed 2010-11-21
- 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ÌÖðgp·é½ßÌwb_[t@C */
-#define NPLAYERS 4 /* v[Ìl */
-
-#define NGAMES 20000 /* Q[ÌV~
[Vñ */
-
-/* gvÌíÞ: NuC_CAhCn[gCXy[h */
-#define CLUB 0
-#define DIAMOND 1
-#define HEART 2
-#define SPADE 3
-
-struct card { /* gv\¢ÌÌè` */
- int pips; /* F1©ç13ÜÅ */
- int suit; /* íÞFNuC_CACn[gCXy[h */
-};
-
-/* main()©çÄÔÖÌvg^CvÌé¾ */
-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]; /* 4lÌv[ðé¾ */
-
-int main(void) {
- int i, j; /* i=Q[ñCj=v[Ìl */
- int straights = 0, flushes = 0, fullhouses = 0; /* »ê¼ê̽èÌ */
-
- srand(1); /* Ìú» */
- init_card(deck); /* ÛèPFJ[hÌú» */
- //print_deck(deck);
- shuffle(deck);
- //print_deck(deck);
- for (i = 0; i < NGAMES; i++) { /* NGAMESñQ[ðsȤ */
- shuffle(deck); /* ÛèQFJ[hÌVbtƪz */
- deal(deck, hand); /* ÛèQFJ[hÌVbtƪz */
- if (i == 0) {
- //print_hand(hand[0]);
- }
- for (j = 0; j < NPLAYERS; j++) { /* ev[ÌèÌàð`FbN */
- if (is_straight(hand[j])) { /* ÛèRFXg[g©H */
- straights++;
- }
- if (is_flush(hand[j])) { /* ÛèRFtbV
©H */
- flushes++;
- }
- if (is_fullhouse(hand[j])) { /* ÛèRFtnEX©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ðvg·é */
- 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[hfbLðvg·é */
- 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[1lÔñÌJ[h5ðvg·é */
- 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++];
-}
-
-/* qg: ±ÌÖð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); /* 課é¡ï¼ï¼ã«ã¼ãã®åæå */
+ //print_deck(deck);
+ shuffle(deck);
+ //print_deck(deck);
+ lcd.cls();
+ lcd.locate(0, 0);
+ for (i = 0; i < NGAMES; i++) { /* NGAMESåã²ã¼ã ãè¡ãªã */
+ shuffle(deck); /* 課é¡ï¼ï¼ã«ã¼ãã®ã·ã£ããã«ã¨åé
*/
+ deal(deck, hand); /* 課é¡ï¼ï¼ã«ã¼ãã®ã·ã£ããã«ã¨åé
*/
+ if (i == 0) {
+ print_hand(hand[0]);
+ }
+ for (j = 0; j < NPLAYERS; j++) { /* åãã¬ã¼ã¤ã®æã®å
ããã§ã㯠*/
+ if (is_straight(hand[j])) { /* 課é¡ï¼ï¼ã¹ãã¬ã¼ããï¼ */
+ straights++;
+ }
+ if (is_flush(hand[j])) { /* 課é¡ï¼ï¼ãã©ãã·ã¥ãï¼ */
+ flushes++;
+ }
+ if (is_fullhouse(hand[j])) { /* 課é¡ï¼ï¼ãã«ãã¦ã¹ãï¼ */
+ 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;
+ }
+ }
+}