📄 a.c
字号:
/******************************************************scan bomb game bomb.c file******************************************************/#include <stdio.h>#include <time.h>#include <stdlib.h>#include "a.h"#ifdef ENABLE_CONTROL_RANGEint ROW = 0, LINE = 0;int a[MAX_ROW][MAX_LINE];int bomb[MAX_ROW][MAX_LINE]; int show[MAX_ROW][MAX_LINE];#elseint a[ROW][LINE];int bomb[ROW][LINE]; int show[ROW][LINE];#endifint undiplay_seat = 0;#ifdef DEBUG_ENABLEvoid printf_string(int type){int i=0, j=0;for( i=0; i<ROW; i++ ) { for( j=0; j<LINE; j++ ) { if( type == 0 ) printf("a[%d][%d]=%d ", i, j, a[i][j]); else if( type == 1) printf("%d ", a[i][j]); else if( type == 2) printf("%d ", bomb[i][j]); } printf("\n"); }}#elsevoid printf_string(int type){}//don't print message#endifvoid printf_sign(unsigned char model){ printf("%c ",model);}int count_the_whole_bomb_number( void ){int number = 0, i, j;for( i = 0; i < ROW; i++ ) for( j = 0; j < LINE; j++ ) number += bomb[i][j];return number;}int judgment_success( int bomb_number ){int i, j, count_detect_bomb_number = 0, flag = 0;for( i = 0; i < ROW; i++ ) for( j = 0; j < LINE; j++ ) if( ( show[i][j] == CONFORM_BOMB_SIGN ) && ( bomb[i][j] == BOMB ) ) count_detect_bomb_number += 1; if( bomb_number == ( count_detect_bomb_number + undiplay_seat ) ) { flag = 1; } undiplay_seat = 0;return flag;}#if 0void show_all_bomb_seat( void ){int i=0, j=0;printf("\n stupid guy you touched the bomb !!! \n game over !!! \n"); printf(" ");for(i=0; i<ROW; i++) printf(" %2d",i+1);printf("\n");printf("\n");for( i=0; i<ROW; i++ ) { printf("%2d ",i+1); for( j=0; j<LINE; j++ ) if( (show[i][j] == CONFORM_BOMB_SIGN) && ( bomb[i][j] == EMPTY ) ) printf_sign(ERROR_MARK); else if( bomb[i][j] == BOMB ) printf_sign(BOMB_SIGN); else if(show[i][j] == UNKNOW_SEAT_DEFAULT_VALUE) printf_sign(UNKNOW_STATE_SIGN); else if( show[i][j] == SPACE_EMPTY ) printf_sign(SPACE_EMPTY); else printf("%d ",show[i][j]); printf("\n"); }}#endifvoid error_state_show(int row, int line){if( (show[row][line] == CONFORM_BOMB_SIGN) && ( bomb[row][line] == EMPTY ) ) printf_sign(ERROR_MARK);else if( bomb[row][line] == BOMB ) printf_sign(BOMB_SIGN);else if(show[row][line] == UNKNOW_SEAT_DEFAULT_VALUE) printf_sign(UNKNOW_STATE_SIGN); else if( show[row][line] == SPACE_EMPTY ) printf_sign(SPACE_EMPTY);else printf("%d ",show[row][line]);}void display_show_table_state( state ){int i = 0, j = 0;undiplay_seat = 0;printf(" ");for(i = 0; i < LINE; i++) printf(" %2d",i+1);printf("\n");printf("\n");for( i=0; i<ROW; i++ ) { printf("%2d ",i+1); for( j=0; j<LINE; j++ ) switch( state ) { case FIRST_INIT_STATE: printf_sign(UNKNOW_STATE_SIGN); break; case LOOP_STATE: case SUCCESS_STATE: if(show[i][j] == UNKNOW_SEAT_DEFAULT_VALUE) { undiplay_seat += 1; if( state == SUCCESS_STATE ) printf_sign(CONFORM_BOMB_SIGN); else printf_sign(UNKNOW_STATE_SIGN); } else if( show[i][j] == CONFORM_BOMB_SIGN ) printf_sign(CONFORM_BOMB_SIGN); else if( show[i][j] == SPACE_EMPTY ) printf_sign(SPACE_EMPTY); else printf("%d ",show[i][j]); break; case ERROR_STATE: error_state_show(i,j); break; default: break; } printf("\n"); }}int count_number(int row, int line){int i = 0, j = 0, value = 0 , mid_row = 0, mid_line = 0; for( i = -1 ; i <= 1 ; i++ ) for(j = -1 ; j <= 1 ; j++ ) { mid_row = row + i; mid_line = line + j; if( ( mid_row >= 0 ) && ( mid_line >= 0 ) && ( mid_row < ROW ) && ( mid_line < LINE )) value += bomb[mid_row][mid_line] ; } return value;} void init_a_table( ){int i = 0, j = 0;printf("init a table:\n");for( i = 0; i < ROW; i++ ) for( j = 0; j < LINE; j++ ) { if( bomb[i][j] == BOMB ) a[i][j] = -1; else a[i][j] = count_number(i,j); }printf_string(STRING_A);printf_string(STRING_A_BOMB);}int useful_input_number_conform( int row , int line , int flag ){ if( ( row >= 0 ) && ( row < ROW ) && ( line >= 0 ) && ( line < LINE ) && ( ( flag <= 1) && (flag >= 0) ) ) return TRUE; else return FALSE;}int show_near_empty_seat_value(int row , int line){int i = 0, j = 0, mid_row = 0, mid_line = 0;for( i = -1 ; i <= 1 ; i++ ) for(j = -1 ; j <= 1 ; j++ ) { mid_row = row + i; mid_line = line + j; if( i == 0 && j == 0 ) continue; if( ( mid_row >= 0 ) && ( mid_line >= 0 ) && ( mid_row < ROW ) && ( mid_line < LINE )) { if( show[mid_row][mid_line] == UNKNOW_SEAT_DEFAULT_VALUE )//for died loop { if( a[mid_row][mid_line] == 0 ) show[mid_row][mid_line] = SPACE_EMPTY; else show[mid_row][mid_line] = a[mid_row][mid_line]; if( show[mid_row][mid_line] == SPACE_EMPTY ) show_near_empty_seat_value(mid_row,mid_line); } } }}int conform_choice_seat( int row , int line , int flag ){int judgment_flag=0; // define for return 0 died , 1 live if( flag == 0 ) if( bomb[row][line] == 0 ) { if( a[row][line] == 0 ) { show[row][line] = SPACE_EMPTY; show_near_empty_seat_value(row, line); } else show[row][line] = a[row][line]; judgment_flag = 1; } else { judgment_flag = 0; } else { judgment_flag = 1; show[row][line] = CONFORM_BOMB_SIGN; }return judgment_flag; }int random_bomb_table( int step ){int i,ii,j,jj,rand_number,check_repeat_number, check_repeat_buffer[ROW][LINE],flag = 0;time_t t;for( i=0; i<ROW; i++) for( j=0; j<LINE; j++) check_repeat_buffer[i][j] = -1;printf("create bomb table :\n");for( i=0; i<ROW; i++) for( j=0; j<LINE; j++) { srand( (unsigned)time(&t)); flag = 0; while( flag == 0 ) { rand_number = rand()%(ROW*LINE); flag = 1; for( ii=0; ii<ROW; ii++) for( jj=0; jj<LINE; jj++) { if( rand_number == check_repeat_buffer[ii][jj] ) { flag = 0; } } check_repeat_buffer[i][j] = rand_number; } check_repeat_number = rand_number; //printf("rand_number = %d\n",rand_number); if( rand_number < (step * ( (ROW*LINE) /10 ) ) ) bomb[i][j] = 1; else bomb[i][j] = 0; }printf("already create bomb table.\n");printf_string(STRING_BOMB);}void clear_all_string_table( void ){printf("clear all of the string table about the function :\n");int i,j;for( i=0; i<ROW; i++) for( j=0; j<LINE; j++) { show[i][j] = UNKNOW_SEAT_DEFAULT_VALUE; a[i][j] = -2; bomb[i][j] = 2; }}//void scan_bomb_game( void )main(){int row,line,flag,judement_flag, bomb_number, game_level;#ifdef ENABLE_CONTROL_RANGEint row_value, line_value;printf("please input row and line number to build the grid function :\n");scanf("%d %d",&row_value,&line_value);ROW = row_value;LINE = line_value;#endifclear_all_string_table();printf("please input game level you want 1-5,\n 5 is the hardest level,default value is 3 \\n if you input error system will use default level:\n");scanf("%d", &game_level);if(game_level > 5 || game_level < 0) { if( game_level == 100 ) ; else game_level = 3; }random_bomb_table(game_level);bomb_number = count_the_whole_bomb_number();init_a_table();display_show_table_state(FIRST_INIT_STATE);while(1) { //printf_string(a, STRING_A_BOMB); printf("please input 3 number for row(1-%d),line(1-%d) and flag(0 not bomb ,1 bomb) \n", ROW, LINE); scanf("%d %d %d", &row, &line, &flag); printf("row=%d line=%d flag=%d\n", row, line, flag ); if ( useful_input_number_conform( row-1, line-1, flag ) == FALSE ) continue; judement_flag = conform_choice_seat( row-1, line-1, flag ); if(judement_flag == 1) display_show_table_state(LOOP_STATE); else { //show_all_bomb_seat(); printf("\n stupid guy you touched the bomb !!! \n game over !!! \n"); display_show_table_state(ERROR_STATE); break; } if( judgment_success(bomb_number) == 1) { printf("Congratulation clever guys, you success ! \n"); display_show_table_state(SUCCESS_STATE); break; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -