Skip to content

Instantly share code, notes, and snippets.

@jitomesky
Created May 15, 2014 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jitomesky/e1632545b46ed2f8c01d to your computer and use it in GitHub Desktop.
Save jitomesky/e1632545b46ed2f8c01d to your computer and use it in GitHub Desktop.
高校生の時に作ったCUIマインスイーパープログラム
/* マインスイーパー もどき */
#include<stdio.h>
#include<stdlib.h>
void in_bom(int **, int , int, int); // ボムを入れる関数(配列データ、x軸、y軸、ボムの数)
void in_num(int **, int , int); // ボムの数を数え、その数を配列に入れる関数(配列データ、x軸、y軸)
main(int argc,char *argv)
{
int i, j, x, y, bom, masu_x, masu_y;
int **masu;
//
while(1){
printf("縦と横のマスの数を入力してください\n");
printf("Input x y --> ");
scanf("%d %d", &x, &y); // 縦と横のますの数を入力
if ( ( (0 < x) && (x <= 100) ) && ( (0 < y) && (y <= 100) ) )
break; //縦と横のマス目が0より上、100以下になるまで繰り返す
printf("縦と横のマスの数は1以上100以下です!\n");
}
// Allocate
x += 2;
y += 2;
masu = (int**)malloc( sizeof(int*) * x);
for(i=0; i < x ; i++){
masu[i] = (int*)malloc( sizeof(int) * y );
//テスト用配列初期化
for(j=0; j < y; j++)
masu[i][j] = 0;
}
x -= 2;
y -= 2;
//
while(1)
{
printf("地雷の数を入力してください\n");
printf("Input bom --> ");
scanf("%d", &bom); // 地雷の数を入力
if ( (0 < bom) && (bom < (x * y) ) )
break; // 地雷の数は1~(x*y - 1)
printf("地雷の数は1以上、マス目の総数より下までです!\n");
}
in_bom(masu,x,y,bom); // in_bom関数で配列masu[101][101]にボムを入れる
in_num(masu,x,y); // in_num関数で配列に周りにあるボムの数を入れる
for (masu_y = 1; masu_y <= y; masu_y++){
// テスト用
for (masu_x = 1; masu_x <= x; masu_x++){
if(masu[masu_x][masu_y] == 9){
putchar('*');
putchar(' ');
}
else
printf("%d ",masu[masu_x][masu_y]);
}
putchar('\n');
}
// Release
x += 2;
for(i=0; i < x; i++){
free(masu[i]);
}
free(masu);
return 0;
}
void in_bom(int **masu, int x,int y,int bom)
{
int i = 0, masu_x, masu_y;
while(i < bom){ //地雷の数だけ繰り返す
masu_x = 1 + ( rand() % (x - 1 + 1) ); // ランダムでx軸を決定
masu_y = 1 + ( rand() % (y - 1 + 1) ); // ランダムでy軸を決定
if( masu[masu_x][masu_y] != 9){ // すでに地雷が入っていないなら
masu[masu_x][masu_y] = 9; // 地雷の代わりに9を代入。後で*と置き換える。
i++;
}
}
}
void in_num(int **masu , int x , int y )
{
int i = 0 , masu_x , masu_y ;
for (masu_x = 1; masu_x <= x;masu_x++){
for (masu_y = 1; masu_y <= y; masu_y++){
if(masu[masu_x][masu_y] == 0){
// 周りのボムのカウント開始
if(masu[masu_x - 1][masu_y - 1] == 9)
i++;
if(masu[masu_x - 1][masu_y] == 9)
i++;
if(masu[masu_x - 1][masu_y + 1] == 9)
i++;
if(masu[masu_x][masu_y - 1] == 9)
i++;
if(masu[masu_x][masu_y + 1] == 9)
i++;
if(masu[masu_x + 1][masu_y - 1] == 9)
i++;
if(masu[masu_x + 1][masu_y] == 9)
i++;
if(masu[masu_x + 1][masu_y + 1] == 9)
i++;
// カウント終了
masu[masu_x][masu_y] = i; // 数を代入
i = 0; // iを初期化
}
}
}
}
/* マインスイーパー もどき */
#include<stdio.h>
#include<stdlib.h>
void in_bom(int **, int , int, int); // ボムを入れる関数(配列データ、x軸、y軸、ボムの数)
void in_num(int **, int , int); // ボムの数を数え、その数を配列に入れる関数(配列データ、x軸、y軸)
int play_mine(int **,int , int, int); // 座標を指定して遊べるようにする関数 (配列データ、x軸、y軸、ボムの数)
// ゲームクリアで1を、ゲーム失敗で0を返す。
main(int argc,char *argv)
{
int i, j, x, y, bom, masu_x, masu_y;
int **masu;
//
while(1){
printf("縦と横のマスの数を入力してください\n");
printf("Input x y --> ");
scanf("%d %d", &x, &y); // 縦と横のますの数を入力
if ( ( (0 < x) && (x <= 100) ) && ( (0 < y) && (y <= 100) ) )
break; //縦と横のマス目が0より上、100以下になるまで繰り返す
printf("縦と横のマスの数は1以上100以下です!\n");
}
// Allocate
x += 2;
y += 2;
masu = (int**)malloc( sizeof(int*) * x);
for(i=0; i < x ; i++){
masu[i] = (int*)malloc( sizeof(int) * y );
//テスト用配列初期化
for(j=0; j < y; j++)
masu[i][j] = 0;
}
x -= 2;
y -= 2;
//
while(1)
{
printf("地雷の数を入力してください\n");
printf("Input bom --> ");
scanf("%d", &bom); // 地雷の数を入力
if ( (0 < bom) && (bom < (x * y) ) )
break; // 地雷の数は1~(x*y - 1)
printf("地雷の数は1以上、マス目の総数より下までです!\n");
}
in_bom(masu,x,y,bom); // in_bom関数で配列masu[101][101]にボムを入れる
in_num(masu,x,y); // in_num関数で配列に周りにあるボムの数を入れる
if( play_mine(masu,x,y,bom) == 1)
printf("ゲームクリア!おめでとう!\n");
else{
for (masu_y = 1; masu_y <= y; masu_y++){
// テスト用
for (masu_x = 1; masu_x <= x; masu_x++){
if(masu[masu_x][masu_y] == 9){
putchar('*');
putchar(' ');
}
else
printf("%d ",masu[masu_x][masu_y]);
}
putchar('\n');
}
printf("ゲームオーバー!また遊んでね!\n");
}
// Release
x += 2;
for(i=0; i < x; i++){
free(masu[i]);
}
free(masu);
return 0;
}
void in_bom(int **masu, int x,int y,int bom)
{
int i = 0, masu_x, masu_y;
while(i < bom){ //地雷の数だけ繰り返す
masu_x = 1 + ( rand() % (x - 1 + 1) ); // ランダムでx軸を決定
masu_y = 1 + ( rand() % (y - 1 + 1) ); // ランダムでy軸を決定
if( masu[masu_x][masu_y] != 9){ // すでに地雷が入っていないなら
masu[masu_x][masu_y] = 9; // 地雷の代わりに9を代入。後で*と置き換える。
i++;
}
}
}
void in_num(int **masu , int x , int y )
{
int i = 0 , masu_x , masu_y ;
for (masu_x = 1; masu_x <= x;masu_x++){
for (masu_y = 1; masu_y <= y; masu_y++){
if(masu[masu_x][masu_y] == 0){
// 周りのボムのカウント開始
if(masu[masu_x - 1][masu_y - 1] == 9)
i++;
if(masu[masu_x - 1][masu_y] == 9)
i++;
if(masu[masu_x - 1][masu_y + 1] == 9)
i++;
if(masu[masu_x][masu_y - 1] == 9)
i++;
if(masu[masu_x][masu_y + 1] == 9)
i++;
if(masu[masu_x + 1][masu_y - 1] == 9)
i++;
if(masu[masu_x + 1][masu_y] == 9)
i++;
if(masu[masu_x + 1][masu_y + 1] == 9)
i++;
// カウント終了
masu[masu_x][masu_y] = i; // 数を代入
i = 0; // iを初期化
}
}
}
}
int play_mine(int **masu,int x,int y, int bom)
{
int i , i2 , k = 0, masu_x , masu_y,result;
int **masu_tmp;
//Allocate
x += 2;
y += 2;
masu_tmp = (int**)malloc( sizeof(int*) * x);
for(i=0; i < x ; i++){
masu_tmp[i] = (int*)malloc( sizeof(int) * y );
//配列初期化
for(i2=0; i2 < y; i2++)
masu_tmp[i][i2] = 10;
}
x -= 2;
y -= 2;
// ゲームの処理開始
while(1){
for ( i = 0; i <= x; i++)
printf("%d|",i);
putchar('\n');
for( i = 1; i <= y; i++){
for (i2 = 0; i2 <= x; i2++)
printf("-+");
putchar('\n');
printf("%d|",i);
for (i2 = 1;i2 <= x; i2++){
if(masu_tmp[i2][i] == 9) // bomのとき
printf("*|");
else if(masu_tmp[i2][i] == 10) // 開いていないとき
printf(" |");
else
printf("%d|",masu_tmp[i2][i]);
}
putchar('\n');
}
// あけるマスを指定
while(1){
printf("Input (x y) -->");
scanf("%d %d",&masu_x,&masu_y);
if ( ((1 <= masu_x) && (masu_x <= x)) && ((1 <= masu_y) && (masu_y <= y)) )
break;
}
if(masu[masu_x][masu_y] == 9){ // ボムだった場合
result = 0;
break;
}
/* else if(masu[masu_x][masu_y] == 0){ // 0だった場合、周りすべてをあける
masu_tmp[masu_x - 1][masu_y - 1] = masu[masu_x - 1][masu_y - 1];
masu_tmp[masu_x - 1][ masu_y ] = masu[masu_x - 1][ masu_y ];
masu_tmp[masu_x - 1][masu_y + 1] = masu[masu_x - 1][masu_y + 1];
masu_tmp[masu_x][masu_y - 1] = masu[masu_x][masu_y - 1];
masu_tmp[masu_x][masu_y + 1] = masu[masu_x][masu_y + 1];
masu_tmp[masu_x + 1][masu_y - 1] = masu[masu_x + 1][masu_y - 1];
masu_tmp[masu_x + 1][masu_y] = masu[masu_x + 1][masu_y];
masu_tmp[masu_x + 1][masu_y + 1] = masu[masu_x + 1][masu_y + 1];
masu_tmp[masu_x][masu_y] = masu[masu_x][masu_y];
// k += 9;
} */
else{
masu_tmp[masu_x][masu_y] = masu[masu_x][masu_y];
k++;
}
if (k == ( (x * y) - bom )){
result = 1;
break;
}
}
// Release
x += 2;
for(i=0; i < x; i++){
free(masu_tmp[i]);
}
free(masu_tmp);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment