1
#include <stdio.h>
const int NUM = 10;
const int CHANCE_MAX = 20;
int TREASURE_X[ 10 ] = { 5,3,0,5,1,2,4,0,3,2 };
int TREASURE_Y[ 10 ] = { 8,9,5,9,7,8,6,1,7,5 };
bool InputCheck( int input_x, int input_y );
void MapFeedback( int input_x, int input_y );
bool Success( int input_x, int input_y );
void SuccessMsg( bool get_treasure, int score );
bool GameOver( int chance );
void GameOverMsg( bool get_treasure );
int main( ) {
int chance = CHANCE_MAX;
int score = 0;
bool get_treasure = false;
//メインループ
while ( !GameOver( chance ) ) {
//入力
int input_x;
int input_y;
printf( "座標(biāo)入力してください。\n" );
scanf_s( "%d %d", &input_x, &input_y );
if ( !InputCheck( input_x, input_y ) ) {
printf( "入力エラーです。\n" );
continue;
}
//判定
chance--;
get_treasure = Success( input_x, input_y );
score += get_treasure;
MapFeedback( input_x, input_y );
SuccessMsg( get_treasure, score );
}
//ゲームオーバー
GameOverMsg( get_treasure );
return 0;
}
//入力座標(biāo)妥當(dāng)性を判定
bool InputCheck( int input_x, int input_y ) {
return
input_x >= 0 && input_x < NUM &&
input_y >= 0 && input_y < NUM;
}
//基盤目表示
void MapFeedback( int input_x, int input_y ) {
printf( "? " );
for ( int i = 0; i < NUM; i++ ) {
printf( "%2d", i );
}
printf( "\n" );
for ( int i = 0; i < NUM; i++ ) {
printf( "%2d", i );
for ( int j = 0; j < NUM; j++ ) {
if ( j == input_x && i == input_y ) {
printf( "■" );
} else {
printf( "□" );
}
}
printf( "\n" );
}
}
//寶物と入力座標(biāo)の一致判定
bool Success( int input_x, int input_y ) {
int i;
for ( i = 0; i < NUM; i++ ) {
if ( input_x == TREASURE_X[ i ] &&
input_y == TREASURE_Y[ i ] ) {
TREASURE_X[ i ] = -1;
TREASURE_Y[ i ] = -1;
return true;
}
}
if ( i >= 9 ) {
return false;
}
}
//"SUCCESS"/"FAILURE"表示
void SuccessMsg( bool get_treasure, int score ) {
if ( get_treasure ) {
printf( "あたり。\n" );
} else {
printf( "はずれ。\n" );
}
printf( "スコア:%d。\n", score );
}
//ゲーム終了判定
bool GameOver( int chance ) {
return chance == 0;
}
//ループ文脫出後の表示
void GameOverMsg( bool get_treasure ) {
if ( get_treasure ) {
printf( "COMPLETE\n" );
} else {
printf( "GAMEOVER\n" );
}
}