#!/bin/bash ###################################### # source-encoding >> UTF-8 # # ブラックジャック # author : 河合 佑太  ####################################### flag=0 #リダイレクトとコンソール出力を同時に行う。 #引数で渡された文字列が対象となる function outputFunc() { echo $1 if test $flag -eq 1; then echo $1 >> quiz3_output.txt else echo $1 > quiz3_output.txt flag=1 fi } #コンソール画面で入力したものがリダイレクトされる。 #loopFlagStr(仕様上グロバール変数)を参照すれば関数外部でもreadしたものを知ることができる。 function readFunc() { read tpNum if test $flag -eq 1; then echo "$tpNum" >> quiz3_output.txt else echo "$tpNum" > quiz3_output.txt fi loopFlagStr=$tpNum return } #スコアカウンタのための広域変数 score=0 #次の関数が呼ばれゲームループがスタートされる function startGameLoop() { #カードを引くかを尋ね、入力値がY以外ならば終了する outputFunc 'Do you take a card?[Y/n]' readFunc readStr=$loopFlagStr if [ $readStr != 'Y' ]; then return; fi #無限ループ while :; do #乱数を使用して1〜10までの数をcardNumに格納する cardNum=`expr $RANDOM % 10` #カードの数が0となる場合1〜10の数が出るまで再度引く while [ $cardNum = 0 ] do cardNum=`expr $RANDOM % 10` done #引いたカードの数と現時点でのトータルスコアを出力する outputFunc '---------------------------' outputFunc "Your card is $cardNum." score=`expr $score + $cardNum` outputFunc "Now, your score is $score." outputFunc " " #トータルスコアと21の大小関係を判別 if test $score -eq 21; then #ぴたっり21なので勝ち。ゲームループを抜ける outputFunc "Congratulation!!" return elif test $score -gt 21; then #21を超えたのでゲームオーバー。ゲームループを抜ける outputFunc "So, your score is over 21." outputFunc "Game over!!" return fi #カードを引くかを尋ね、入力値がY以外ならば終了する outputFunc 'Do you take a card?[Y/n]' readFunc readStr=$loopFlagStr if [ $readStr != 'Y' ]; then #途中終了。現在のスコアを表示してゲームループを抜ける outputFunc "Now, your score is $score and finish!" return; fi done } #ゲーム開始前にゲーム情報を出力する outputFunc " " outputFunc "+*****************************+" outputFunc "+ Black Jack ver 1.0" outputFunc "+ author : Yuta Kawai" outputFunc "+*****************************+" #startGameLoop関数を呼び出す startGameLoop #ゲーム終了前にtotalスコアを出力する outputFunc " " outputFunc "+*****************************+" outputFunc "+ total : $score" outputFunc "+ exit Black Jack Game!!" outputFunc "+*****************************+"