Created
August 15, 2018 03:15
-
-
Save nambrot/9b8d12b9ae6e8aea0c1d59ccd9af5597 to your computer and use it in GitHub Desktop.
Minesweeper
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
require 'json' | |
class Minesweeper | |
def initialize(n, m, number_of_mines) | |
@n = n | |
@m = m | |
@number_of_mines = number_of_mines | |
@user_board = [] | |
@visible = [] | |
@finished = false | |
@visible_cells = 0 | |
(0..n-1).each {|i| | |
@user_board.push((0..(m-1)).map{ 0 }) | |
@visible.push((0..(m-1)).map{ 0 }) | |
} | |
(0..(n*m-1)) | |
.to_a | |
.shuffle | |
.take(number_of_mines) | |
.each {|pos| @user_board[grow(pos)][gcol(pos)] = -1 } | |
each_row_col do |row, col| | |
next if @user_board[row][col] == -1 | |
@user_board[row][col] = neighbors(row, col) { |nrow, ncol| if unbounded(nrow, ncol, 0) == -1 then 1 else 0 end }.sum | |
end | |
printBoard | |
end | |
def pick(sel, row, col) | |
if (sel != "r" and sel != "m") | |
puts "invalid action" | |
return | |
end | |
if !valid?(row, col) | |
puts "invalid coordintes" | |
return | |
end | |
if sel == "r" | |
expand(row, col) | |
else | |
end | |
end | |
def printBoard | |
puts " " + (0..(@n-1)).to_a.join(" ") | |
@user_board.each_with_index do |rowV, row| | |
puts "#{row} " + rowV.each_with_index.map { |cell, col| | |
case | |
when invisible?(row, col) then "_" | |
when visible?(row, col) then cell | |
else "?" | |
end | |
}.join(" ") | |
end | |
end | |
def finished?() @finished end | |
private | |
def expand(row, col) | |
return if !valid?(row, col) | |
return if visible?(row, col) | |
@visible[row][col] = 1 | |
@visible_cells += 1 | |
return unless @user_board[row][col] == 0 | |
[ | |
[-1, 0], | |
[0, -1], | |
[1, 0], | |
[0, 1] | |
].each {|nr, nc| expand(row+nr, col+nc) } | |
end | |
def grow(pos) (pos / @n).to_i end | |
def gcol(pos) (pos % @n) end | |
def mine?(row, col) @user_board[row][col] == -1 end | |
def invisible?(row, col) @visible[row][col] == 0 end | |
def visible?(row, col) @visible[row][col] == 1 end | |
def marked?(row, col) @visible[row][col] == 2 end | |
def each_row_col | |
(0..(@n-1)).each { |row| (0..(@m-1)).each {|col| yield(row, col) }} | |
end | |
def valid?(row, col) | |
row >= 0 && row < @n && col >= 0 && col < @m | |
end | |
def unbounded(row, col, default) | |
if valid?(row, col) | |
@user_board[row][col] | |
else | |
default | |
end | |
end | |
def neighbors(row, col) | |
[ | |
[-1, -1], | |
[-1, 0], | |
[-1, 1], | |
[0, -1], | |
[0, 0], | |
[0, 1], | |
[1, -1], | |
[1, 0], | |
[1, 1] | |
].map {|r, c| yield(row+r, col+c) } | |
end | |
end | |
game = Minesweeper.new(3, 3, 2) | |
while (!game.finished?) do | |
sel, row, col = gets.split(",") | |
game.pick(sel, row.to_i, col.to_i) | |
game.printBoard | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment