数当てクイズ。


シーケンス遷移で組んでます。
たったこれだけのプログラムなら順次実行な記述でも不便なく動作します。
for とか while とか continue とかガンガン使っても大丈夫です。


でもまあ折角ゲーム機で組むのだから
それならシーケンス遷移で組みましょうってことで。


MSX2J風フォントを使い ANK コードで日本語表示を実現してます。
L/R/スタート/セレクト 何れかのボタンを押しながらプログラム起動することで
英語表示になるという無駄な機能付き(苦笑)。ぶっちゃけ要らない機能ですね。


数当てクイズ


main.c

/*---------------------------------------------------------------------------------
	
	KAZUATE Quiz
	
	version 0.01
	Dec 28, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <nds/ndstypes.h>
#include <nds/interrupts.h>
#include <nds/arm9/input.h>

#include "Bg.h"
#include "Kazuate.h"


void main_Init(void);


//---------------------------------------------------------------------------------
void main_Init(void){
//---------------------------------------------------------------------------------
	
	Bg_Init();
	
}



//---------------------------------------------------------------------------------
int main(void){
//---------------------------------------------------------------------------------
	
	main_Init();
	
	while(1) {
		
		swiWaitForVBlank();
		scanKeys();
		
		Kazuate_Update();
		
		if(Kazuate_IsExit()){
			return 0;
		}
		
	}
	
}

//---------------------------------------------------------------------------------


Bg.h

/*---------------------------------------------------------------------------------
	
	bg module header
	
	version 0.01
	Dec 28, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _BG_H_
#define _BG_H_


#ifdef __cplusplus
extern "C" {
#endif


void Bg_Init(void);


#ifdef __cplusplus
}
#endif

#endif	// _BG_H_


Bg.c

/*---------------------------------------------------------------------------------
	
	bg module routine
	
	version 0.01
	Dec 28, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>

#include <nds/system.h>
#include <nds/arm9/console.h>

#include "Bg.h"
#include "font.h"


typedef struct{
	PrintConsole bottom_cons;
	ConsoleFont font;
}BgStatus;


static BgStatus st_bg;


//---------------------------------------------------------------------------------
void Bg_Init(void){
//---------------------------------------------------------------------------------
	
	//initStatus
	memset(&st_bg, 0, sizeof(st_bg));
	
	//initConsole
	st_bg.bottom_cons = *consoleDemoInit();
	
	//initConsoleFont
	st_bg.font.gfx = (u16*)fontTiles;
	st_bg.font.bpp = 4;
	st_bg.font.asciiOffset = 0;
	st_bg.font.numChars = 256;
	st_bg.font.convertSingleColor = true;
	
	consoleSetFont(&st_bg.bottom_cons, &st_bg.font);
	
	//selectConsole
	consoleSelect(&st_bg.bottom_cons);
	
	//swapLCD
	lcdSwap();
	
}

//---------------------------------------------------------------------------------


Kazuate.h

/*---------------------------------------------------------------------------------
	
	kazuate module header
	
	version 0.01
	Dec 28, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _KAZUATE_H_
#define _KAZUATE_H_


#include <nds/ndstypes.h>


#ifdef __cplusplus
extern "C" {
#endif


void Kazuate_Update(void);
bool Kazuate_IsExit(void);


#ifdef __cplusplus
}
#endif

#endif	// _KAZUATE_H_


Kazuate.c

/*---------------------------------------------------------------------------------
	
	kazuate module routine
	
	version 0.01
	Dec 28, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "Kazuate.h"

#include <nds/arm9/input.h>


#define NUM_MIN   1
#define NUM_MAX 100


typedef enum{
	MSG_ENG,
	MSG_JPN
}MSG_LNG;

typedef enum{
	MSG_TITLE, 
	MSG_BEGINE, 
	MSG_NEAR, 
	MSG_SMALLER, 
	MSG_LARGER, 
	MSG_CORRECT, 
	MSG_CONTINUE
}MSG_CONTENT;


const static char *msg[2][7] = {
	
	{
		"KAZUATE Quiz!\n", 
		"Please input the number \nfrom", 
		"It's near!\n", 
		"It's smaller\n", 
		"It's larger\n", 
		"It's correct!\n", 
		"Do you continue?\nA:Yes, B:No\n"
	}, 
	{
		"カズアテクイズ!\n", 
		"ノスウジヲニュウリョクシテネ\n", 
		"オシイ!\n", 
		"モットチイサイカズデス\n", 
		"モットオオキイカズデス\n", 
		"セイカイデス!\n", 
		"ツヅケマスカ?\nA:ハイ, B:イイエ\n"
	}
	
};


typedef enum{
	KAZUATE_INIT, 
	KAZUATE_BEGINE, 
	KAZUATE_EXEC, 
	KAZUATE_STOP, 
	KAZUATE_END, 
	KAZUATE_EXIT
}KAZUATE_ACT;


typedef struct{
	
	u16 act;
	
	u32 kdr;
	
	int lng;
	int num;
	int ans;
	
}KazuateStatus;


static KazuateStatus st_kazuate;


void kazuate_Init(void);
void kazuate_Begine(void);
void kazuate_Exec(void);
void kazuate_Stop(void);
void kazuate_End(void);
void kazuate_Exit(void);


//---------------------------------------------------------------------------------
void kazuate_Init(void){
//---------------------------------------------------------------------------------
	
	keysSetRepeat(12, 6);
	srand(time(NULL));
	
	
	st_kazuate.lng = MSG_JPN;
	
	if(keysCurrent() & (KEY_SELECT | KEY_START | KEY_L | KEY_R)){
		st_kazuate.lng = MSG_ENG;
	}
	
	
	st_kazuate.act = KAZUATE_BEGINE;
	
}

//---------------------------------------------------------------------------------
void kazuate_Begine(void){
//---------------------------------------------------------------------------------
	
	st_kazuate.num = 0;
	st_kazuate.ans = rand() % NUM_MAX + 1;
	
	
	printf("%s", msg[st_kazuate.lng][MSG_TITLE]);
	
	if(st_kazuate.lng){
		printf("%d~%d %s", NUM_MIN, NUM_MAX, msg[st_kazuate.lng][MSG_BEGINE]);
	}else{
		printf("%s %d to %d\n", msg[st_kazuate.lng][MSG_BEGINE], NUM_MIN, NUM_MAX);
	}
	
	printf("%d", st_kazuate.num);
	
	
	st_kazuate.act = KAZUATE_EXEC;
	
}

//---------------------------------------------------------------------------------
void kazuate_Exec(void){
//---------------------------------------------------------------------------------
	
	st_kazuate.kdr = keysDownRepeat();
	
	if(st_kazuate.kdr & KEY_UP){
		
		if(st_kazuate.num < NUM_MAX){
			st_kazuate.num++;
			printf("\x01b[2K\r%d", st_kazuate.num);
		}
		
	}else if(st_kazuate.kdr & KEY_DOWN){
		
		if(st_kazuate.num > NUM_MIN){
			st_kazuate.num--;
			printf("\x01b[2K\r%d", st_kazuate.num);
		}
		
	}else if(keysDown() & KEY_A){
		
		printf("\n");
		
		if( (st_kazuate.num == st_kazuate.ans - 1) || (st_kazuate.num == st_kazuate.ans + 1) ){
			printf("%s", msg[st_kazuate.lng][MSG_NEAR]);
			
		}else if( st_kazuate.num > st_kazuate.ans + 1 ){
			printf("%s", msg[st_kazuate.lng][MSG_SMALLER]);
			
		}else if( st_kazuate.num < st_kazuate.ans - 1 ){
			printf("%s", msg[st_kazuate.lng][MSG_LARGER]);
			
		}else{
			printf("%s", msg[st_kazuate.lng][MSG_CORRECT]);
			st_kazuate.act = KAZUATE_STOP;
			
		}
		
		if(st_kazuate.act != KAZUATE_STOP){
			printf("\x01b[2K\r%d", st_kazuate.num);
		}
		
	}
	
}

//---------------------------------------------------------------------------------
void kazuate_Stop(void){
//---------------------------------------------------------------------------------
	
	printf("%s", msg[st_kazuate.lng][MSG_CONTINUE]);
	st_kazuate.act = KAZUATE_END;
	
}

//---------------------------------------------------------------------------------
void kazuate_End(void){
//---------------------------------------------------------------------------------
	
	if(keysDown() & KEY_A){
		st_kazuate.act = KAZUATE_BEGINE;
		
	}else if(keysDown() & KEY_B){
		st_kazuate.act = KAZUATE_EXIT;
		
	}
	
}

//---------------------------------------------------------------------------------
void kazuate_Exit(void){
//---------------------------------------------------------------------------------
	
	st_kazuate.act = KAZUATE_INIT;
	
}

//---------------------------------------------------------------------------------
void Kazuate_Update(void){
//---------------------------------------------------------------------------------
	
	switch(st_kazuate.act){
		
	case KAZUATE_INIT:
		kazuate_Init();
		break;
		
	case KAZUATE_BEGINE:
		kazuate_Begine();
		break;
		
	case KAZUATE_EXEC:
		kazuate_Exec();
		break;
		
	case KAZUATE_STOP:
		kazuate_Stop();
		break;
		
	case KAZUATE_END:
		kazuate_End();
		break;
		
	case KAZUATE_EXIT:
		kazuate_Exit();
		break;
		
	default:
		break;
		
	}
	
}

//---------------------------------------------------------------------------------
bool Kazuate_IsExit(void){
//---------------------------------------------------------------------------------
	
	return (st_kazuate.act == KAZUATE_INIT) ? true:false;
	
}

//---------------------------------------------------------------------------------


MSX2J 風のフォントリソースは「CAVE だもんね。(15)」エントリのものを含んでます。


CAVE だもんね。(15)
http://d.hatena.ne.jp/dumbo001/20090322/1237762244


毎度の如くプロジェクト一式は以下の URL からダウンロードできます。
ttp://page.freett.com/ntr/algorithm/kazuate.zip