i made game in bash

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
User avatar
stuff by max
Level 1
Level 1
Posts: 2
Joined: Mon Mar 04, 2024 2:09 pm
Location: united kingdom
Contact:

i made game in bash

Post by stuff by max »

hi few moths ago i did make a game in bash is called simple race
here is the code
feel free to edit this

Code: Select all

#!/bin/bash

echo "simple race"
echo "made by stuff by max"
echo "======================"

# Print a message
echo "Press enter to continue"

# Wait for the user to press enter
read

# Do something else
echo "Continuing the script"

# Define the track length
track_length=200

# Define the starting positions of the players
player1_pos=0
player2_pos=0
player3_pos=0

# Define the characters for the players
player1_char="C"
player2_char="Z"
player3_char="E"

# Define the dice roll function
function roll_dice {
  echo $((1 + RANDOM % 6))
}

# Define the main game loop
while true; do
  # Clear the screen
  clear

  # Print the track
  printf "%${track_length}s\n" "Track"
  printf "%${track_length}s\n" "|"

  # Print the players
  printf "%${player1_pos}s" "$player1_char"
  printf "%$(($track_length - $player1_pos - 1))s\n" "|"

  printf "%${player2_pos}s" "$player2_char"
  printf "%$(($track_length - $player2_pos - 1))s\n" "|"

  printf "%${player3_pos}s" "$player3_char"
  printf "%$(($track_length - $player3_pos - 1))s\n" "|"

  # Check if any player has won
  if [ $player1_pos -ge $(($track_length - 1)) ]; then
    echo "Player 1 wins!"
    break
  fi

  if [ $player2_pos -ge $(($track_length - 1)) ]; then
    echo "Player 2 wins!"
    break
  fi

  if [ $player3_pos -ge $(($track_length - 1)) ]; then
    echo "Player 3 wins!"
    break
  fi

  # Wait for input from each player
  read -n 1 -s -r -p "Player 1, press any key to roll the dice..."
  player1_roll=$(roll_dice)
  player1_pos=$(($player1_pos + $player1_roll))

  read -n 1 -s -r -p "Player 2, press any key to roll the dice..."
  player2_roll=$(roll_dice)
  player2_pos=$(($player2_pos + $player2_roll))

  read -n 1 -s -r -p "Player 3, press any key to roll the dice..."
  player3_roll=$(roll_dice)
  player3_pos=$(($player3_pos + $player3_roll))
done
Shiva
Level 3
Level 3
Posts: 141
Joined: Thu Jul 07, 2022 11:25 am

Re: i made game in bash

Post by Shiva »

stuff by max wrote: Mon Mar 04, 2024 2:36 pm hi few moths ago i did make a game in bash is called simple race
The game works but has a lot of redundent code imho. Perhaps you could :
- investigate the "array way" : in your game, what defines a player is a letter-name and a position. If you want to avoid a double data/dimension array, you may even consider a string named Players and use Bash expansion to get the corresponding letter. Some examples with fixed positions :

Code: Select all

Players="CZE"; Pos=(50 57 63); for (( i=0; i<=2; i++ )); do printf "%s\n" "${Players:$i:1} is at ${Pos[$i]}"; done
NB : the first element of an array or character of a string has an index of 0.

If you want to printf the letters at the place given by its Pos element :

Code: Select all

Players="CZE"; Pos=(50 57 63); for (( i=0; i<=2; i++ )); do printf "%${Pos[$i]}s\n" "${Players:$i:1}"; done
- try to avoid infinite loops when unnecessary. Here it's relatively easy by creating a Win flag :

Code: Select all

Win=0
While [ $Win -eq 0 ]
do
    <play>
    # Got a winner : Win Flag is set to 1 to stop the loop. BTW, it's also fairer : each player rolls the dice the same number of times
    if "player reaches track_length"; then Win=1; fi
done
Example :

Code: Select all

Win=0; i=0; while [ $Win -eq 0 ]; do echo $i; (( i++ )); if [ $i -gt 15 ]; then Win=1; fi; done
As long as Win equals 0, echo will output the value of i and i will be incremented. Win is set to 1 when i reaches 15 and the loop ends there.

- the roll_dice function has no real interest :

Code: Select all

Dice=$((1+RANDOM%6))
should be enough and you add $Dice to the player's Pos element.
Post Reply

Return to “Scripts & Bash”