ジャンケンの処理部分。
あいこでの繰り返し処理や手を出したときの判定はこれよりも上層で行うので
このモジュールでは単純な判定しか行わないのです。
main.c
/*--------------------------------------------------------------------------------- janken module test version 0.01 Jan 18, 2010 By. REGEKATSU ---------------------------------------------------------------------------------*/ #include <stdio.h> #include <nds/ndstypes.h> #include <nds/interrupts.h> #include <nds/arm9/input.h> #include <nds/arm9/console.h> #include "Janken.h" //表示するジャンケンの手を定義 char *janken_hand[] = { "GOO", "CHOKI", "PAR" }; //--------------------------------------------------------------------------------- int main(void){ //--------------------------------------------------------------------------------- JANKEN_HAND player, cpu; JANKEN_JUDGE judge; consoleDemoInit(); //コンピュータの手の初期化とゲームタイトル表示 Janken_InitRandHand(); printf("janken module test\n\nversion 0.01\nJan 18, 2010\n\nBy. REGEKATSU\n\n\n"); //入力要求の表示 printf("input player hand.\n" "%c:GOO %c:CHOKI %c:PAR\n\n", 0x1b, 0x18, 0x1a); //コンピュータの手、プレイヤーの手を取得後に勝敗の判断 while(1){ swiWaitForVBlank(); scanKeys(); cpu = Janken_GetRandHand(); if(keysDown() & KEY_LEFT){ player = JANKEN_GOO; break; }else if(keysDown() & KEY_UP){ player = JANKEN_CHOKI; break; }else if(keysDown() & KEY_RIGHT){ player = JANKEN_PAR; break; } } judge = Janken_JudgeHand(player, cpu); //プレイヤーの手、コンピュータの手を表示後に勝敗の表示 printf("YOU:"); printf("%s", janken_hand[ player ]); printf(", "); printf("CPU:"); printf("%s", janken_hand[ cpu ]); printf("\n"); switch(judge){ case JANKEN_AIKO: printf("DRAW GAME\n"); break; case JANKEN_KACHI: printf("YOU WIN\n"); break; case JANKEN_MAKE: printf("CPU WIN\n"); break; default: break; } return 0; } //---------------------------------------------------------------------------------
JankenTypes.h
/*--------------------------------------------------------------------------------- jankenTypes define header version 0.01 Jan 18, 2010 By. REGEKATSU ---------------------------------------------------------------------------------*/ #ifndef _JANKEN_TYPES_H_ #define _JANKEN_TYPES_H_ typedef enum{ JANKEN_MAKE = 0, JANKEN_AIKO = 1, JANKEN_KACHI = 2 }JANKEN_JUDGE; typedef enum{ JANKEN_GOO = 0, JANKEN_CHOKI = 1, JANKEN_PAR = 2 }JANKEN_HAND; #ifdef __cplusplus extern "C" { #endif #ifdef __cplusplus } #endif #endif //_JANKEN_TYPES_H_
Janken.h
/*--------------------------------------------------------------------------------- janken module header version 0.01 Jan 18, 2010 By. REGEKATSU ---------------------------------------------------------------------------------*/ #ifndef _JANKEN_H_ #define _JANKEN_H_ #include "JankenTypes.h" #ifdef __cplusplus extern "C" { #endif void Janken_InitRandHand(void); JANKEN_HAND Janken_GetRandHand(void); JANKEN_JUDGE Janken_JudgeHand(JANKEN_HAND hand1, JANKEN_HAND hand2); #ifdef __cplusplus } #endif #endif //_JANKEN_H_
Janken.c
/*--------------------------------------------------------------------------------- janken module routine version 0.01 Jan 18, 2010 By. REGEKATSU ---------------------------------------------------------------------------------*/ #include <stdlib.h> #include <time.h> #include "Janken.h" //--------------------------------------------------------------------------------- void Janken_InitRandHand(void){ //--------------------------------------------------------------------------------- srand(time(NULL)); } //--------------------------------------------------------------------------------- JANKEN_HAND Janken_GetRandHand(void){ //--------------------------------------------------------------------------------- return (JANKEN_HAND)(rand() % 2 + 1); } //--------------------------------------------------------------------------------- JANKEN_JUDGE Janken_JudgeHand(JANKEN_HAND hand1, JANKEN_HAND hand2){ //--------------------------------------------------------------------------------- JANKEN_JUDGE judge; if(hand1 == hand2){ judge = JANKEN_AIKO; }else if( (hand1 + 1) % 3 == hand2 ){ judge = JANKEN_KACHI; }else{ judge = JANKEN_MAKE; } return judge; } //---------------------------------------------------------------------------------