Created
January 2, 2022 19:37
-
-
Save logankilpatrick/e9eda1865f3bca3417b55d2863fbc178 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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