LoginSignup
0
0

More than 1 year has passed since last update.

Bashでビンゴゲーム

Last updated at Posted at 2023-01-26

はじめに

Bashで配列の扱い方とWhile Do ~ Doneの扱い方を学ぶのにちょうどいいものを探した結果、ビンゴゲームを作るという課題を思いついた。

注意

ビンゴカードを配って、ビンゴカードの数字と出たボールの数字をチェックし、BINGOとする処理は省いてます。

コード

BingoGame.sh
#!/bin/sh

# ビンゴゲームに入れるボールを宣言
bingogame=(0 1 2 3 4 5)
history=()

# 条件を満たすまで無限ループ
#while:
do

# ゲーム操作の入力
read -p "Are You Ready? ( START / END / HISTORY):" operation

# ゲーム処理
case $operation in
  "START")

  # ビンゴゲームの中のボールが空になったらゲーム終了
  if [ ${#bingogame[@]} -eq 0 ]; then
    echo "Game Over !!"
    exit 0
  fi

  # ビンゴゲームをぐるぐる回してボールを出した
  element = $((RANDOM % ${ #bingomane[*] } ))

  # ボールを読み上げる
  getball={$bingogame[$element]}
  echo $getball

  # ボールを再抽選させない箱に放り込む  
  history+=getball

  # 取り出したボールをビンゴゲームに戻さない
  unset bingogame[$element]
  # インデックスを完全に削除してその分を詰める
  bingogame=( ${bingogame[@]} )
  ;;

  # 任意のタイミングでビンゴゲームを終わらせる
  "END")
  echo "Game Over !!"
  exit 0
  ;;

  # 今まで排出されたボールを読み上げる
  "HISTORY")
  echo ${history[@]}
  ;;

  # デバッグ用。ビンゴゲームの中にある数字を読み上げる
  "NOKORI")
  echo ${bingogame[@]}
  ;;

esac

done

# お終い
exit 0

参考

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