I tried to create Bulls and Cows with a shell program

Introduction

I created it as a small story of Bash programming. This is the 6th bullet. This time it was quite tough.

What is Bulls and Cows?

For Bulls and Cows, see Wikipedia and here.

Originally a string guessing game, This time I will create it as a number guessing game.

Source

#!/bin/bash

function chkAns {
  bull=0
  cow=0
  num=$1
  dig=$2
  ans=$3

  for i in $(seq 1 $dig)
  do
    for j in $(seq 1 $dig)
    do
      if [[ ${num:i:1} == ${ans:j:1} && $i == $j ]]
      then
#       echo bull: $bull        #for debug
        (( bull = bull + 1 ))
      elif [[ ${num:i:1} == ${ans:j:1} && $i != $j ]]
      then
#       echo cow: $cow          #for debug
        (( cow = cow + 1 ))
      fi
    done
  done

  if (( bull == dig ))
  then
    echo -e "$dig bulls ! You did it !"
    exit
  else
    echo -e "$bull bull and $cow cow."
  fi
}

echo "Welcome 'Bulls and Cows' Game"
echo -e "Please input digits of numbers => \c"
read digits

tmp=$(shuf -i 0-9 -n $digits -z)
answer=${tmp:0:3}

#echo $answer  #for debug

for k in $(seq 1 10)
do
  echo -e "Input $digits numbers of your answer. (${k} time) => \c"
  read num

  chkAns $num $digits $answer
  if (( $k == 10 ))
  then
    echo "You failed 10 times..."
  fi
done

echo -e "The answer is '$answer'"

$ RANDOM was fine, but I didn't use it because I don't trust it. Instead, I used the shuf command.

Execution result (correct answer)

$ ./bulls_and_cows.sh
Welcome 'Bulls and Cows' Game
Please input digits of numbers => 3
./bulls_and_cows.sh:Line 39:warning: command substitution: ignored null byte in input
Input 3 numbers of your answer. (1 time) => 321
2 bull and 0 cow.
Input 3 numbers of your answer. (2 time) => 324
2 bull and 0 cow.
Input 3 numbers of your answer. (3 time) => 325
3 bulls ! You did it !

The "Warning: command substitution: ignored null byte in input" message seems to come out when it contains null bytes. However, I'm ignoring it because it was difficult to generate N digits without using Null. Please comment if there is a good solution.

Execution result (fails)

~~ I'm seriously trying to answer correctly. ~~

$ ./bulls_and_cows.sh
Welcome 'Bulls and Cows' Game
Please input digits of numbers => 3
./bulls_and_cows.sh:Line 39:warning: command substitution: ignored null byte in input
Input 3 numbers of your answer. (1 time) => 345
1 bull and 0 cow.
Input 3 numbers of your answer. (2 time) => 219
1 bull and 0 cow.
Input 3 numbers of your answer. (3 time) => 398
1 bull and 0 cow.
Input 3 numbers of your answer. (4 time) => 470
1 bull and 1 cow.
Input 3 numbers of your answer. (5 time) => 709
1 bull and 0 cow.
Input 3 numbers of your answer. (6 time) => 507
2 bull and 0 cow.
Input 3 numbers of your answer. (7 time) => 504
1 bull and 0 cow.
Input 3 numbers of your answer. (8 time) => 570
1 bull and 1 cow.
Input 3 numbers of your answer. (9 time) => 407
2 bull and 0 cow.
Input 3 numbers of your answer. (10 time) => 307
2 bull and 0 cow.
You failed 10 times...
The answer is '567'

in conclusion

By creating this program

--Variables --Variable manipulation --Conditional expression --Repeating syntax --Function --read command --shuf command

I understand.

Recommended Posts

I tried to create Bulls and Cows with a shell program
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I tried to create a table only with Django
I tried to automatically create a report with Markov chain
I tried to create a linebot (implementation)
I tried to create a linebot (preparation)
I tried to create a list of prime numbers with python
I tried to make a periodical process with Selenium and Python
I tried to create a plug-in with HULFT IoT Edge Streaming [Development] (2/3)
I tried to create a plug-in with HULFT IoT Edge Streaming [Execution] (3/3)
[Outlook] I tried to automatically create a daily report email with Python
I tried to create a plug-in with HULFT IoT Edge Streaming [Setup] (1/3)
I tried to create a sample to access Salesforce using Python and Bottle
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I tried to read and save automatically with VOICEROID2 2
I tried to implement and learn DCGAN with PyTorch
I tried to draw a route map with Python
When I tried to create a virtual environment with Python, it didn't work
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Zip-compress any file with the [shell] command to create a file and delete the original file.
I tried to automatically read and save with VOICEROID2
I tried to easily create a fully automatic attendance system with Selenium + Python
I tried to automatically generate a password with Python3
I want to manually create a legend with matplotlib
I tried to create a button for Slack with Raspberry Pi + Tact Switch
I tried to implement Grad-CAM with keras and tensorflow
I tried to make a simple image recognition API with Fast API and Tensorflow
I tried to create a model with the sample of Amazon SageMaker Autopilot
I made a program to convert images into ASCII art with Python and OpenCV
I tried to create a reinforcement learning environment for Othello with Open AI gym
[Python] I tried to automatically create a daily report of YWT with Outlook mail
I tried to create a class to search files with Python's Glob method in VBA
I tried scraping food recall information with Python to create a pandas data frame
I tried to predict and submit Titanic survivors with Kaggle
I wrote a program quickly to study DI with Python ①
I tried to get started with Hy ・ Define a class
I tried to sort a random FizzBuzz column with bubble sort.
I tried to create a bot for PES event notification
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried to divide with a deep learning language model
I tried to create an article in Wiki.js with SQLAlchemy
I tried to create Quip API
[Shell startup] I tried to display the shell on the TV with a cheap Linux board G-cluster
I also tried to imitate the function monad and State monad with a generator in Python
[Graph drawing] I tried to write a bar graph of multiple series with matplotlib and seaborn
[5th] I tried to make a certain authenticator-like tool with python
I tried to create a server environment that runs on Windows 10
I want to use a wildcard that I want to shell with Python remove
I tried to create a simple credit score by logistic regression.
[2nd] I tried to make a certain authenticator-like tool with python
I tried to visualize bookmarks flying to Slack with Doc2Vec and PCA
I wanted to create a smart presentation with Jupyter Notebook + nbpresent
[3rd] I tried to make a certain authenticator-like tool with python
[Python] A memo that I tried to get started with asyncio
I tried to make a 2channel post notification application with Python
I want to create a pipfile and reflect it in docker
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
I tried to easily detect facial landmarks with python and dlib
[1st] I tried to make a certain authenticator-like tool with python
I tried to make a strange quote for Jojo with LSTM