Skip to content

Commit

Permalink
Add resignation
Browse files Browse the repository at this point in the history
  • Loading branch information
timbot1789 committed Jun 20, 2024
1 parent 282dd7c commit bfa673d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
11 changes: 9 additions & 2 deletions app/rubygo/model/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class Rubygo
module Model
class Game
attr_accessor :height, :width, :tokens, :cur_player, :game_over, :komi, :white_score, :black_score, :handicap
attr_accessor :height, :width, :tokens, :cur_player, :game_over, :komi, :white_score, :black_score, :handicap, :resigned

def initialize(height = 19, width = 19, handicap = 0, komi = 0.5)
@height = height
Expand Down Expand Up @@ -52,6 +52,7 @@ def reset(new_height = 0, new_width = 0)

def resume
self.game_over = false
self.resigned = nil
@tokens.each do |col|
col.each do |cell|
if cell.dead
Expand All @@ -63,6 +64,11 @@ def resume
end
end

def resign
self.resigned = self.cur_player
self.game_over = true
end

def find_score_group(group)
cell = group.last
player = cell.player
Expand Down Expand Up @@ -210,12 +216,13 @@ def revert_history(turns = 1)
def play(row, column)
@has_passed = false
token = tokens[row][column]
return if resigned
if game_over && token.player != 0
self.white_score -= 1 if token.player == 1
self.black_score -= 1 if token.player == -1
return token.dead = !token.dead
end
return unless token.player.zero?
return unless token.player.zero? && !game_over

token.player = cur_player
captured = capture(token)
Expand Down
44 changes: 43 additions & 1 deletion app/rubygo/view/rubygo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,39 @@ class GameOverWindow
end
end

class Rubygo
module View
class ResignWindow
include Glimmer::LibUI::CustomWindow
option :resignation
option :resume, default: lambda {}
option :restart, default: lambda {}

body {
window { |game_over_window|
title "Game Over"
margined true
vertical_box {
label("Game Over. #{resignation == 1 ? 'White' : 'Black'} resigned")
button("New Game") {
on_clicked do
restart.call
game_over_window.destroy
end
}
button("Resume Game") {
on_clicked do
resume.call
game_over_window.destroy
end
}
}
}
}
end
end
end

class Rubygo
module View
class Rubygo
Expand Down Expand Up @@ -250,7 +283,13 @@ class Rubygo
restart = lambda {
self.game = Model::Game.new(game.height, game.width, game.handicap, game.komi)
}
game_over_window(get_score: score, resume: resume, restart: restart).show if game_over
if game_over
if game.resigned
resign_window(resignation: game.resigned, restart: restart, resume: resume).show
else
game_over_window(get_score: score, resume: resume, restart: restart).show
end
end
end
horizontal_box {
stretchy false
Expand Down Expand Up @@ -284,6 +323,9 @@ class Rubygo
end
}
button('Resign') {
on_clicked do
game.resign
end
}
}
}
Expand Down

0 comments on commit bfa673d

Please sign in to comment.