Created
June 11, 2015 09:54
-
-
Save HaruhiroTakahashi/861fb25bb7e2dbad63a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <conio.h> | |
#include <time.h> | |
int main(){ | |
int key, count, count2, rnd1, rnd2, i, j; | |
int w = 0; | |
int ary_x = 0; | |
int ary_y = 0; | |
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 } | |
}; | |
/* | |
【ランダム処理の原理】 | |
例えばrnd1 = 2, rnd2 = 2 つまりnumbers[2][2];=5の場合 | |
① w = numbers[1][1]; で最初に必ずwが1を受け取る | |
② numbers[1][1] = numbers[rnd1][rnd2]; で1があった場所に5が入る | |
③ numbers[rnd1][rnd2] = w; で5があった場所に1が入る | |
④ 再び①の処理が行われるが、次にwが受け取る数字は5になる | |
つまり10回 numbers[1][1] にある数字を入れ替えていく処理である | |
*/ | |
srand((unsigned)time(NULL)); | |
for (count = 0; count < 100; count++){//10回入れ替え | |
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", 80 - 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"); | |
// count2--;//裏ワザ | |
} | |
} | |
for (i = 0; i < 5; i++){ | |
for (j = 0; j < 5; j++){ | |
if (numbers[i][j] == key - 48){ | |
ary_y = i; | |
ary_x = j; | |
} | |
} | |
} | |
/* 【選んだ数字が空白(9)の場所と入れ替わる処理の原理】 | |
5×5マスの表を、y軸が下に+、x軸が右に+のグラフ([y][x])として見て考える | |
例えば[3][3]の位置にある数字を左の空白[3][2]に動かしたい場合 | |
左へ動かす = [x] - 1 の値の地点へ行くことになる | |
よって、ランダム処理の原理と同じ式で場所を入れ替えれば良い | |
*/ | |
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"); | |
} | |
/*【クリア条件】 | |
number[9]==number[index]にすること | |
*/ | |
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"); | |
if (count2 <= 50){ | |
printf("あなたの脳内年齢は20代くらいです!\n"); | |
printf("∑d(*゚∀゚*)チゴィネ!!\n"); | |
} | |
else if (count2 > 70 && 50 > count2){ | |
printf("あなたの脳内年齢は30代くらいです!\n"); | |
} | |
else if (count2 > 71){ | |
printf("あなたの脳内年齢は40代くらいです......\n"); | |
} | |
} | |
else{ | |
success_flag = 0; | |
index = 0; | |
} | |
break_flag = 1; | |
} | |
if (success_flag != 9){ | |
printf("ゲームオーバー\n"); | |
printf("(゚ε゚)頭固いですね\n"); | |
printf("再チャレンジは何かキー押してから Ctrl + F5\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment