Last active
December 13, 2019 16:09
-
-
Save abhishalya/a70e0cabe88ef14ca161f09ea2d8d1ac to your computer and use it in GitHub Desktop.
This file contains 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
######################################### | |
# Minesweeper Game in Julia # | |
# This was quite demotivating :( # | |
######################################### | |
# Function to generate random mine positions | |
function get_mines(dimension) | |
mines = [] | |
mx = (dimension == 8) ? 9 : 20 | |
while length(mines) < mx | |
pair = rand(1 : dimension, 2) | |
if !(pair in mines) | |
push!(mines, pair) | |
end | |
end | |
return mines, mx | |
end | |
# Function to calculate all the numbers | |
function pre_calculate(mines, dimension) | |
uncovered_board = [] | |
for row in 1 : dimension | |
uncovered_row = "" | |
for col in 1 : dimension | |
r1 = max(1, row - 1) | |
r2 = min(row + 1, dimension) | |
c1 = max(1, col - 1) | |
c2 = min(col + 1, dimension) | |
cnt = 0 | |
for x in r1 : r2, y in c1 : c2 | |
if [x, y] in mines | |
cnt += 1 | |
end | |
end | |
uncovered_row *= string(cnt) | |
end | |
push!(uncovered_board, uncovered_row) | |
end | |
return uncovered_board | |
end | |
# Function to uncover current input | |
function uncover(uncovered_board, dimension, user_uncovered_board) | |
display_board = [] | |
for row in 1 : dimension | |
display_row = "" | |
for col in 1 : dimension | |
if [row, col] in user_uncovered_board | |
if uncovered_board[row][col] == '0' | |
display_row *= " " | |
else | |
display_row *= " " | |
display_row *= uncovered_board[row][col] | |
display_row *= " " | |
end | |
continue | |
end | |
display_row *= "|_|" | |
end | |
push!(display_board, display_row) | |
end | |
display(display_board, dimension) | |
end | |
# Display at the end of game | |
function display_end(user_uncovered_board, dimension, uncovered_board, mines, flag) | |
display_board = [] | |
for row in 1 : dimension | |
display_row = "" | |
for col in 1 : dimension | |
if [row, col] in mines | |
display_row *= " * " | |
continue | |
end | |
if [row, col] in user_uncovered_board | |
if uncovered_board[row][col] == '0' | |
display_row *= " " | |
else | |
display_row *= " " | |
display_row *= uncovered_board[row][col] | |
display_row *= " " | |
end | |
else | |
display_row *= "|_|" | |
end | |
end | |
push!(display_board, display_row) | |
end | |
display(display_board, dimension) | |
end | |
# Just to display the list of strings | |
function display(board, dimension) | |
println('_' ^ (dimension * 3)) | |
for i in 1 : length(board) | |
println(board[i]) | |
end | |
end | |
# The main function | |
function play() | |
# Initialize 'uncovered' mines as empty list | |
# uncover = a number or mine | |
user_uncovered_board = [] | |
# Ask for dimensions | |
println("Select dimensions of the board:\n1. 8 x 8\n2. 16 x 16") | |
input = parse(Int64, readline(stdin)) | |
if input != 1 && input != 2 | |
println("Invalid input. Please select either 1 or 2.") | |
return | |
end | |
dimension = (input == 1) ? 8 : 16 | |
# Generate the mines as per the dimensions | |
mines, mx = get_mines(dimension) | |
# Pre-calculate all numbers | |
uncovered_board = pre_calculate(mines, dimension) | |
# Now loop through the user inputs which he wants to uncover | |
while length(user_uncovered_board) < (dimension * dimension) - mx | |
println("Enter the row and column number separated by single space") | |
user_input = split(readline(stdin), ' ') | |
# Check if there are two args provided | |
if length(user_input) != 2 | |
println("Invalid input -- Two args expected") | |
continue | |
end | |
# Check if the inputs are integers | |
user_row, user_col = 0, 0 | |
try | |
user_row = parse(Int64, user_input[1]) | |
user_col = parse(Int64, user_input[2]) | |
catch | |
println("Invalid inputs -- Not an integer") | |
continue | |
end | |
# Check if the inputs are in the range | |
if !(user_row in 1 : dimension) || !(user_col in 1 : dimension) | |
println("Invalid input -- Not in range") | |
continue | |
end | |
# Check if the input is a mine, and end the game if true | |
if [user_row, user_col] in mines | |
println("Mine uncovered. Game over.") | |
display_end(user_uncovered_board, dimension, uncovered_board, mines, 0) | |
return | |
end | |
# If not mine, check if it has been already uncovered | |
# If uncovered, do nothing else add it to uncovered list | |
if !([user_row, user_col] in user_uncovered_board) | |
push!(user_uncovered_board, [user_row, user_col]) | |
uncover(uncovered_board, dimension, user_uncovered_board) | |
else | |
uncover(uncovered_board, dimension, user_uncovered_board) | |
end | |
end | |
# In case he uncovers all nom-mine cells | |
println("You won.") | |
display_end(user_uncovered_board, dimension, uncovered_board, mines, 1) | |
end | |
play() |
I do like the use of rand
to generate a different board each time. That makes it more playable!
Maybe you could add a few more prints though to let the user know things like why the game ended and what the significance of a blank space vs a number 1 would be.
That would make it a bit clattered if done using prints. Maybe I can add few comments. Besides, I would probably just suggest the user to go learn minesweeper first :P
I just ran the code and this iteration seems like a big improvement on the last. Nice work!
Yeah, there was a misunderstanding before. I think it should be fine now that it is cleared.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This looks great! Maybe you could add a few more prints though to let the user know things like why the game ended and what the significance of a blank space vs a number 1 would be.