Skip to content

Instantly share code, notes, and snippets.

@logankilpatrick
Created January 2, 2022 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save logankilpatrick/e9eda1865f3bca3417b55d2863fbc178 to your computer and use it in GitHub Desktop.
Save logankilpatrick/e9eda1865f3bca3417b55d2863fbc178 to your computer and use it in GitHub Desktop.
# Computer Number Guessing Game in Julia
# Source: https://github.com/logankilpatrick/10-Julia-Projects-for-Beginners
using Random
function play_number_guess_computer()
print("Please enter a number between 1 and 50 for the computer to try and guess: ")
# Take in the user input and convert it to a number
target_number = parse(Int64, readline())
# Create an array of 50 numbers
guess_order = collect(1:50)
# Define our random seed
rng = MersenneTwister(1234)
# Shuffle the array randomly given ur seed
shuffled_guess = shuffle(rng, guess_order)
# Loop through each guess and see if it right
for guess in shuffled_guess
if guess == target_number
print("\nThe computer cracked the code and guessed it right!")
break # Stop the for loop if we get it right
end
print("\nComputer guessed: $guess")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment