Minor fixes
This commit is contained in:
@@ -1,19 +1,18 @@
|
|||||||
set pipe_path /tmp/infin
|
set pipe_path /tmp/infin
|
||||||
set oldsum (cat (find . -name '*.rb' | sort) | md5sum)
|
set oldsum (cat (find . \( -name '*.rb' -o -name '*.js' \) | sort) | md5sum)
|
||||||
if not test -p infinsweeper.db
|
if not test -e infinsweeper.db
|
||||||
sqlite3 infinsweeper.db < schema.sql
|
sqlite3 infinsweeper.db < schema.sql
|
||||||
end
|
end
|
||||||
if not test -p db.json
|
if not test -e db.json
|
||||||
echo "{\"account_num\":0,\"pass_num\":0}" > db.json
|
echo "{\"account_num\":0,\"pass_num\":0,\"signed_in_users\":{}}" > db.json
|
||||||
end
|
end
|
||||||
pkill ruby
|
pkill ruby
|
||||||
ruby main.rb -p8080 &
|
ruby main.rb -p8080 &
|
||||||
if not test -p $pipe_path
|
echo "" > $pipe_path
|
||||||
touch $pipe_path
|
|
||||||
end
|
|
||||||
while true
|
while true
|
||||||
sleep 1
|
sleep 1
|
||||||
set newsum (cat (find . -name '*.rb' | sort) | md5sum)
|
set newsum (cat (find . \( -name '*.rb' -o -name '*.js' \) | sort) | md5sum)
|
||||||
|
echo $newsum $oldsum
|
||||||
if test "$oldsum" != "$newsum"
|
if test "$oldsum" != "$newsum"
|
||||||
set oldsum $newsum
|
set oldsum $newsum
|
||||||
pkill ruby
|
pkill ruby
|
@@ -2,14 +2,15 @@ require "xxhash"
|
|||||||
|
|
||||||
# Game logic main class
|
# Game logic main class
|
||||||
class GameLogic
|
class GameLogic
|
||||||
def initialize(player)
|
attr_reader :pos
|
||||||
|
|
||||||
|
def initialize
|
||||||
@seed = rand(111_111..999_999)
|
@seed = rand(111_111..999_999)
|
||||||
@pos = {
|
@pos = {
|
||||||
board: {},
|
board: {},
|
||||||
lost: {},
|
lost: {},
|
||||||
cache: {},
|
cache: {},
|
||||||
}
|
}
|
||||||
@player = player
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a pseudorandom number between 0 and 100
|
# Returns a pseudorandom number between 0 and 100
|
||||||
@@ -25,7 +26,18 @@ class GameLogic
|
|||||||
return if clickable?(g_x, g_y)
|
return if clickable?(g_x, g_y)
|
||||||
case button
|
case button
|
||||||
when "left"
|
when "left"
|
||||||
|
if revealed?(g_x, g_y)
|
||||||
|
if count(:mines, g_x, g_y) == count(:flags, g_x, g_y)
|
||||||
|
(-1..1).each do |x|
|
||||||
|
(-1..1).each do |y|
|
||||||
|
next if x.zero? && y.zero?
|
||||||
|
reveal(g_x + x, g_y + y)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
reveal(g_x, g_y)
|
reveal(g_x, g_y)
|
||||||
|
end
|
||||||
when "right"
|
when "right"
|
||||||
flag(g_x, g_y)
|
flag(g_x, g_y)
|
||||||
end
|
end
|
||||||
@@ -35,9 +47,7 @@ class GameLogic
|
|||||||
s_x = (g_x / 9).floor
|
s_x = (g_x / 9).floor
|
||||||
s_y = (g_y / 9).floor
|
s_y = (g_y / 9).floor
|
||||||
if @pos.lost["#{s_x}:#{s_y}"] ||
|
if @pos.lost["#{s_x}:#{s_y}"] ||
|
||||||
@pos.board.key?("#{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
|
return false
|
||||||
end
|
end
|
||||||
(-1..1).each do |x|
|
(-1..1).each do |x|
|
||||||
@@ -58,6 +68,16 @@ class GameLogic
|
|||||||
@pos.board["#{s_x}:#{s_y}"][l_x][l_y][1]
|
@pos.board["#{s_x}:#{s_y}"][l_x][l_y][1]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def flagged?(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}")
|
||||||
|
return true if @pos.board["#{s_x}:#{s_y}"] == true && mine?(g_x, g_y)
|
||||||
|
@pos.board["#{s_x}:#{s_y}"][l_x][l_y][2]
|
||||||
|
end
|
||||||
|
|
||||||
def reveal(g_x, g_y)
|
def reveal(g_x, g_y)
|
||||||
l_x = ((g_x % 9) + 9) % 9
|
l_x = ((g_x % 9) + 9) % 9
|
||||||
l_y = ((g_y % 9) + 9) % 9
|
l_y = ((g_y % 9) + 9) % 9
|
||||||
@@ -89,7 +109,7 @@ class GameLogic
|
|||||||
l_y = ((g_y % 9) + 9) % 9
|
l_y = ((g_y % 9) + 9) % 9
|
||||||
s_x = (g_x / 9).floor
|
s_x = (g_x / 9).floor
|
||||||
s_y = (g_y / 9).floor
|
s_y = (g_y / 9).floor
|
||||||
return if @pos.board["#{s_x}:#{s_y}"] == true
|
return if @pos.board["#{s_x}:#{s_y}"] == true || revealed?(g_x, g_y)
|
||||||
build_sector(s_x, s_y) if @pos.board["#{s_x}:#{s_y}"].nil?
|
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
|
@pos.board["#{s_x}:#{s_y}"][l_x][l_y][2] ^= true
|
||||||
end
|
end
|
||||||
@@ -103,12 +123,16 @@ class GameLogic
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def count(g_x, g_y)
|
def count(type, g_x, g_y)
|
||||||
sum = 0
|
sum = 0
|
||||||
(-1..1).each do |x|
|
(-1..1).each do |x|
|
||||||
(-1..1).each do |y|
|
(-1..1).each do |y|
|
||||||
next if x.zero? && y.zero?
|
next if x.zero? && y.zero?
|
||||||
sum += 1 if mine?(g_x + x, g_y + y)
|
sum += 1 if case type
|
||||||
|
when :mines then mine?(g_x + x, g_y + y)
|
||||||
|
when :flags then flagged?(g_x + x, g_y + y)
|
||||||
|
else false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
sum
|
sum
|
||||||
@@ -122,7 +146,7 @@ class GameLogic
|
|||||||
g_x = l_x + s_x * 9
|
g_x = l_x + s_x * 9
|
||||||
g_y = l_y + s_y * 9
|
g_y = l_y + s_y * 9
|
||||||
@pos.board["#{s_x}:#{s_y}"][l_x][l_y] = [
|
@pos.board["#{s_x}:#{s_y}"][l_x][l_y] = [
|
||||||
mine?(g_x, g_y) ? -1 : count(g_x, g_y),
|
mine?(g_x, g_y) ? -1 : count(:mines, g_x, g_y),
|
||||||
false, false,
|
false, false,
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
@@ -139,7 +163,7 @@ class GameLogic
|
|||||||
g_x = l_x + s_x * 9
|
g_x = l_x + s_x * 9
|
||||||
g_y = l_y + s_y * 9
|
g_y = l_y + s_y * 9
|
||||||
@pos.cache["#{s_x}:#{s_y}"][l_x][l_y] =
|
@pos.cache["#{s_x}:#{s_y}"][l_x][l_y] =
|
||||||
mine?(g_x, g_y) ? [-1, false, true] : [count(g_x, g_y), true, false]
|
mine?(g_x, g_y) ? [-1, false, true] : [count(:mines, g_x, g_y), true, false]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user