How to resolve the algorithm Rock-paper-scissors step by step in the Bash programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Rock-paper-scissors step by step in the Bash programming language

Table of Contents

Problem Statement

Implement the classic children's game Rock-paper-scissors, as well as a simple predictive   AI   (artificial intelligence)   player. Rock Paper Scissors is a two player game. Each player chooses one of rock, paper or scissors, without knowing the other player's choice. The winner is decided by a set of rules:

If both players choose the same thing, there is no winner for that round. For this task, the computer will be one of the players. The operator will select Rock, Paper or Scissors and the computer will keep a record of the choice frequency, and use that information to make a weighted random choice in an attempt to defeat its opponent.

Support additional choices   additional weapons.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Rock-paper-scissors step by step in the Bash programming language

Source code in the bash programming language

#!/bin/bash
echo "What will you choose? [rock/paper/scissors]"
read response
aiThought=$(echo $[ 1 + $[ RANDOM % 3  ]])
case $aiThought in
	1) 	 aiResponse="rock"   ;;
	2) 	 aiResponse="paper" ;;
	3)  	 aiResponse="scissors"  ;;
esac
echo "AI - $aiResponse"
responses="$response$aiResponse"
case $responses in
	rockrock)  isTie=1  ;;
	rockpaper)  playerWon=0  ;;
	rockscissors)  playerWon=1  ;;
	paperrock)  playerWon=1  ;;
	paperpaper)  isTie=1  ;;
	paperscissors)  playerWon=0  ;;
	scissorsrock)  playerWon=0  ;;
	scissorspaper)  playerWon=1  ;;
	scissorsscissors)  isTie=1  ;;
esac
if [[ $isTie == 1 ]] ; then echo "It's a tie!" && exit 1 ; fi
if [[ $playerWon == 0 ]] ; then echo "Sorry, $aiResponse beats $response , try again.." && exit 1 ; fi
if [[ $playerWon == 1 ]] ; then echo "Good job, $response beats $aiResponse!" && exit 1 ; fi


  

You may also check:How to resolve the algorithm Keyboard input/Flush the keyboard buffer step by step in the Bash programming language
You may also check:How to resolve the algorithm Morse code step by step in the bash programming language
You may also check:How to resolve the algorithm Holidays related to Easter step by step in the BASH programming language
You may also check:How to resolve the algorithm Leap year step by step in the Bash programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the Bash programming language