/*ミニゲーム集 数字並べ替えゲーム ソース*/

/*目次へ戻る*/ /*数字並べ替えゲーム*/ /*1〜8までの数字キーで空いてるマスに数字を移動させて*/ /*123*/ /*456*/ /*78 */ /*の並びにしてください*/ #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> int main(){ int key; int count; int count2; int rnd1,rnd2; int w = 0; int ary_x = 0; int ary_y = 0; int i,j; int index = 0; int success_flag = 0; int break_flag = 1; int number[9] = {1,2,3,4,5,6,7,8,9}; int numbers[5][5] = { {0,0,0,0,0}, {0,1,2,3,0}, {0,4,5,6,0}, {0,7,8,9,0}, {0,0,0,0,0} }; srand((unsigned)time(NULL)); for (count = 0; count < 10; count++){ rnd1 = rand() % 3 + 1; rnd2 = rand() % 3 + 1; w = numbers[1][1]; numbers[1][1] = numbers[rnd1][rnd2]; numbers[rnd1][rnd2] = w; } system("cls"); for(i=0;i<5;i++){ for(j=0;j<5;j++){ if(numbers[i][j] == 0)printf("■"); else if(numbers[i][j] == 1)printf("1"); else if(numbers[i][j] == 2)printf("2"); else if(numbers[i][j] == 3)printf("3"); else if(numbers[i][j] == 4)printf("4"); else if(numbers[i][j] == 5)printf("5"); else if(numbers[i][j] == 6)printf("6"); else if(numbers[i][j] == 7)printf("7"); else if(numbers[i][j] == 8)printf("8"); else printf(" "); } printf("\n"); } for(count2 = 0;count2 < 50;count2++){ while(break_flag){ printf("残り%d回 z:終了\n",50 - count2); printf("動かす数字を選んでください(1〜8) > "); key = _getch(); if(key == 0 || key == 224)key = _getch(); if((key >= 49 && key <= 56) || key == 122){ break_flag = 0; if(key == 122){ count2 = 50; } } else{ printf("無効なキーです!\n"); } } for(i=0;i<5;i++){ for(j=0;j<5;j++){ if(numbers[i][j] == key - 48){ ary_y = i; ary_x = j; } } } if(numbers[ary_y + 1][ary_x] == 9){ w = numbers[ary_y + 1][ary_x]; numbers[ary_y + 1][ary_x] = numbers[ary_y][ary_x]; numbers[ary_y][ary_x] = w; } else if(numbers[ary_y - 1][ary_x] == 9){ w = numbers[ary_y - 1][ary_x]; numbers[ary_y - 1][ary_x] = numbers[ary_y][ary_x]; numbers[ary_y][ary_x] = w; } else if(numbers[ary_y][ary_x + 1] == 9){ w = numbers[ary_y][ary_x + 1]; numbers[ary_y][ary_x + 1] = numbers[ary_y][ary_x]; numbers[ary_y][ary_x] = w; } else if(numbers[ary_y][ary_x - 1] == 9){ w = numbers[ary_y][ary_x - 1]; numbers[ary_y][ary_x - 1] = numbers[ary_y][ary_x]; numbers[ary_y][ary_x] = w; } else{ printf("無効な数字です!"); } system("cls"); for(i=0;i<5;i++){ for(j=0;j<5;j++){ if(numbers[i][j] == 0)printf("■"); else if(numbers[i][j] == 1)printf("1"); else if(numbers[i][j] == 2)printf("2"); else if(numbers[i][j] == 3)printf("3"); else if(numbers[i][j] == 4)printf("4"); else if(numbers[i][j] == 5)printf("5"); else if(numbers[i][j] == 6)printf("6"); else if(numbers[i][j] == 7)printf("7"); else if(numbers[i][j] == 8)printf("8"); else printf(" "); } printf("\n"); } for (i = 1; i <= 3; i++){ for (j = 1; j <= 3; j++){ if (numbers[i][j] == number[index]){ success_flag++; } index++; } } if(success_flag == 9){ count2 = 50; printf("ステージクリア!\n"); } else{ success_flag = 0; index = 0; } break_flag = 1; } if(success_flag != 9){ printf("ゲームオーバー\n"); } return 0; } /*ページの先頭へ*/ /*目次へ戻る*/ /*HOME*/