I am officially a Linux nerd now
Hello! I have not been active on here in a while, I've been dealing with finishing school and other such things but now I am a NEET for 2 months graduated, so I may starting posting here more :D hi.
Anyway, I've worked with bash stuff before but I haven't made my own scripts for anything, just used other peoples (maybe some minor modifications by me here and there, but usually not.) Then I watched a video by the underrated Linux youtuber Bread on Penguins about useful and obscure gnu coreutils which included the factor coreutil. Being a math nerd, I thought of making a factoring game, which would probably be simple enough for a first script.
In practice, I stole the check for primes from Baeldung, and probably used more if statements than I should have, but I'm very happy with the result
Here's the code!
#!/bin/bash
#Variables
declare -i x=$RANDOM
declare -i isfactorable=0
#make sure x is factorable
while [ $isfactorable == 0 ] ; do
x=$RANDOM
factor $x | grep -qE '^(.*): \1$' && isfactorable=0 || isfactorable=1
done
#first question
read -p " Your number is $x. Find a factor to divide it by! - " n
#game loop
while [ true ] ; do
#unique factor requirement
if (($n == $x)) || (($n == 1)) ; then
read -p " Too easy, no cheating! Your number is still $x. Find a factor to divide it by! - " n
#unique factor selected
elif (( $x%$n == 0 )) ; then
x=$x/$n
factor $x | grep -qE '^(.*): \1$' && isfactorable=0 || isfactorable=1
#nonprime result = continue game
if (( isfactorable == 1 )) ; then
read -p " Good job! Your number is now $x. Find a factor to divide it by! - " n
else
#prime result = win
echo "Congratulations! You have factored the number down to a prime, and have won the game!"
sleep 2
exit
fi
#l bozo rip lol
else
echo "Sorry, you have lost the game! $n was not a factor of $x."
sleep 2
exit
fi
done