Fix and update GameLogic class
This commit is contained in:
@@ -2,23 +2,63 @@ require "xxhash"
|
||||
|
||||
# Game logic main class
|
||||
class GameLogic
|
||||
def initialize(player)
|
||||
@seed = rand(111_111..999_999)
|
||||
@pos = {
|
||||
board: {},
|
||||
lost: {},
|
||||
cache: {},
|
||||
}
|
||||
@player = player
|
||||
end
|
||||
|
||||
# Returns a pseudorandom number between 0 and 100
|
||||
def self.hash(data)
|
||||
def hash(data)
|
||||
XXhash.xxh32(data.to_s, @seed) % 100
|
||||
end
|
||||
|
||||
def self.reset_pos
|
||||
def reset_pos
|
||||
@pos.each_value(&:clear)
|
||||
end
|
||||
|
||||
def self.reveal(g_x, g_y)
|
||||
def click(button, g_x, g_y)
|
||||
return if clickable?(g_x, g_y)
|
||||
case button
|
||||
when "left"
|
||||
reveal(g_x, g_y)
|
||||
when "right"
|
||||
flag(g_x, g_y)
|
||||
end
|
||||
end
|
||||
|
||||
def clickable?(g_x, g_y)
|
||||
s_x = (g_x / 9).floor
|
||||
s_y = (g_y / 9).floor
|
||||
if @pos.lost["#{s_x}:#{s_y}"] ||
|
||||
@pos.board.key?("#{s_x}:#{s_y}") ||
|
||||
@pos.board["#{s_x}:#{s_y}"] == true ||
|
||||
revealed?(g_x, g_y)
|
||||
return false
|
||||
end
|
||||
(-1..1).each do |x|
|
||||
(-1..1).each do |y|
|
||||
next if x.zero? && y.zero?
|
||||
return true if revealed?(g_x + x, g_y + y) || flagged?(g_x + x, g_y + y)
|
||||
end
|
||||
end
|
||||
false
|
||||
end
|
||||
|
||||
def revealed?(g_x, g_y)
|
||||
l_x = ((g_x % 9) + 9) % 9
|
||||
l_y = ((g_y % 9) + 9) % 9
|
||||
s_x = (g_x / 9).floor
|
||||
s_y = (g_y / 9).floor
|
||||
return false if @pos.board.key?("#{s_x}:#{s_y}") || @pos.board["#{s_x}:#{s_y}"] == true
|
||||
@pos.board["#{s_x}:#{s_y}"][l_x][l_y][1]
|
||||
end
|
||||
|
||||
def reveal(g_x, g_y)
|
||||
l_x = ((g_x % 9) + 9) % 9
|
||||
l_y = ((g_y % 9) + 9) % 9
|
||||
s_x = (g_x / 9).floor
|
||||
@@ -44,7 +84,17 @@ class GameLogic
|
||||
@pos.board["#{s_x}:#{s_y}"] = true if solved?(s_x, s_y)
|
||||
end
|
||||
|
||||
def self.solved?(s_x, s_y)
|
||||
def flag(g_x, g_y)
|
||||
l_x = ((g_x % 9) + 9) % 9
|
||||
l_y = ((g_y % 9) + 9) % 9
|
||||
s_x = (g_x / 9).floor
|
||||
s_y = (g_y / 9).floor
|
||||
return if @pos.board["#{s_x}:#{s_y}"] == true
|
||||
build_sector(s_x, s_y) if @pos.board["#{s_x}:#{s_y}"].nil?
|
||||
@pos.board["#{s_x}:#{s_y}"][l_x][l_y][2] ^= true
|
||||
end
|
||||
|
||||
def solved?(s_x, s_y)
|
||||
(0..8).each do |l_x|
|
||||
(0..8).each do |l_y|
|
||||
return false if !@pos.board["#{s_x}:#{s_y}"][l_x][l_y][1] && @pos.board["#{s_x}:#{s_y}"][l_x][l_y][0] != -1
|
||||
@@ -53,7 +103,7 @@ class GameLogic
|
||||
true
|
||||
end
|
||||
|
||||
def self.count(g_x, g_y)
|
||||
def count(g_x, g_y)
|
||||
sum = 0
|
||||
(-1..1).each do |x|
|
||||
(-1..1).each do |y|
|
||||
@@ -64,7 +114,7 @@ class GameLogic
|
||||
sum
|
||||
end
|
||||
|
||||
def self.build_sector(s_x, s_y)
|
||||
def build_sector(s_x, s_y)
|
||||
@pos.board["#{s_x}:#{s_y}"] = []
|
||||
(0..8).each do |l_x|
|
||||
@pos.board["#{s_x}:#{s_y}"][l_x] = []
|
||||
@@ -79,7 +129,7 @@ class GameLogic
|
||||
end
|
||||
end
|
||||
|
||||
def self.build_cache(s_x, s_y)
|
||||
def build_cache(s_x, s_y)
|
||||
return if @pos.cache["#{s_x}:#{s_y}"]
|
||||
return unless @pos.board["#{s_x}:#{s_y}"] == 1
|
||||
@pos.cache["#{s_x}:#{s_y}"] = []
|
||||
@@ -94,7 +144,7 @@ class GameLogic
|
||||
end
|
||||
end
|
||||
|
||||
def self.mine?(g_x, g_y)
|
||||
def mine?(g_x, g_y)
|
||||
g_x == 4 || g_y == 4 ? false : hash("#{g_x}:#{g_y}") < 17
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user