0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ビンゴ

Last updated at Posted at 2018-11-26

C言語でビンゴの判定プログラムを書いたときのメモ

bingo.c
# include <stdio.h> // 標準入出力
# include <stdlib.h> // 乱数生成用
# include <time.h> // 現時刻取得用
# include <unistd.h> // sleep関数用

# define NUM 20
# define MAP_NUM 9
# define bingo_pattern 8

// プロトタイプ宣言
void shuffle(int ary[], int size);

int main(void) {
  // ビンゴになったときの添字、全8パターン
  int bingo[bingo_pattern][3] = {
    {0, 1, 2},
    {0, 3, 6},
    {0, 4, 8},
    {1, 4, 7},
    {2, 4, 6},
    {2, 5, 8},
    {3, 4, 5},
    {6, 7, 8}
  };

  int i, j, tmp; // カウンタ

  // 1から20までの値を入れた配列をつくる
  int numbers[NUM];
  for(i=0;i<NUM;i++)
    numbers[i] = i+1;

  int map[MAP_NUM]; // ビンゴマップをつくる
  shuffle(numbers, NUM); // シャッフルする

  // シャッフルしたものをmapに入れる
  for(i=0;i<MAP_NUM;i++)
      map[i] = numbers[i];

  // 表示->入力->判定を繰り返す
  while(1) {
    // 表示
    printf("==================\n");
    for(i=0;i<3;i++) {
      for(j=0;j<3;j++) {
        printf("%d ", map[3*i+j]);
      }
      printf("\n");
    }
    printf("==================\n");

    // 入力
    printf("数字を入力してください : ");
    scanf("%d", &tmp);
    // map内の数字を0にする
    for(i=0;i<MAP_NUM;i++) {
      if(map[i] == tmp) {
        map[i] = 0;
      }
    }

    // ビンゴの判定 ビンゴのパターンに一致している時
    for(i=0;i<bingo_pattern;i++) {
      int flag = 0; // フラグのリセット

      // 縦、横、斜めに揃ったときにflag=3を立てる
      for(j=0;j<3;j++) {
        if(map[bingo[i][j]] == 0) {
          flag++;
        }
      }

      // ビンゴの時
      if(flag == 3) {
        printf("ビンゴです。\n");
        return 0;
      }
    }
  }

    return 0;
}

// シャッフル用関数
void shuffle(int ary[],int size) {
  int i, j, tmp; // カウンタ
  srand(time(NULL)); // 乱数の初期化

  // 前からランダムな値と入れ換える
  for(i=0;i<size;i++) {
      j = rand()%size;
      tmp = ary[i];
      ary[i] = ary[j];
      ary[j] = tmp;
  }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?