Class: Gomoku::Window

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/gomoku/window.rb

Overview

Main Gomoku window and game loop

Instance Method Summary collapse

Constructor Details

#initializeWindow

Returns a new instance of Window.



4
5
6
7
8
9
10
11
12
13
# File 'lib/gomoku/window.rb', line 4

def initialize
  super 800, 800, false
  self.caption = 'Gomoku'
  @board = Board.new(self)
  @black_player = Human.new(self, @board, :black)
  @white_player = Human.new(self, @board, :white)
  # @white_player = Computer.new(self, @board, :white)
  # Start a new game
  new_game
end

Instance Method Details

#button_down(id) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/gomoku/window.rb', line 29

def button_down(id)
  case id
  when Gosu::KbEscape
    close
  when Gosu::MsLeft
    process_click
  end
end

#button_up(_id) ⇒ Object



55
56
# File 'lib/gomoku/window.rb', line 55

def button_up(_id)
end

#do_move(move) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gomoku/window.rb', line 73

def do_move(move)
  r = move[0]
  c = move[1]
  # Return if not blank
  return unless @board.state[[r, c]] == :empty
  # Perform move
  @board.state[[r, c]] = @turn
  # Update turn
  @turn = Utility.toggle_color(@turn)
  # Update flag
  @done_turn = true
end

#drawObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gomoku/window.rb', line 102

def draw
  @board.draw
  if @winner
    # Mark the winning sequence
    draw_winner
  else
    # No winner, draw the current player
    case @turn
    when :black
      @black_player.draw
    when :white
      @white_player.draw
    end
  end
end

#draw_winnerObject

Draw three adjacent 1px lines to make a single 3px line. Worst code I ever wrote.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/gomoku/window.rb', line 120

def draw_winner
  case @winner_direction
  when :horizontal
    line_x1_1 = Utility.c_to_x(@winner_c)
    line_y1_1 = Utility.r_to_y(@winner_r) + 20
    line_x1_2 = Utility.c_to_x(@winner_c + 5)
    line_y1_2 = Utility.r_to_y(@winner_r) + 20

    line_x2_1 = line_x1_1
    line_y2_1 = line_y1_1 + 1
    line_x2_2 = line_x1_2
    line_y2_2 = line_y1_2 + 1

    line_x3_1 = line_x1_1
    line_y3_1 = line_y1_1 + 2
    line_x3_2 = line_x1_2
    line_y3_2 = line_y1_2 + 2
  when :vertical
    line_x1_1 = Utility.c_to_x(@winner_c) + 20
    line_y1_1 = Utility.r_to_y(@winner_r)
    line_x1_2 = Utility.c_to_x(@winner_c) + 20
    line_y1_2 = Utility.r_to_y(@winner_r + 5)

    line_x2_1 = line_x1_1 + 1
    line_y2_1 = line_y1_1
    line_x2_2 = line_x1_2 + 1
    line_y2_2 = line_y1_2

    line_x3_1 = line_x1_1 + 2
    line_y3_1 = line_y1_1
    line_x3_2 = line_x1_2 + 2
    line_y3_2 = line_y1_2
  when :diagonal_up
    line_x1_1 = Utility.c_to_x(@winner_c)
    line_y1_1 = Utility.r_to_y(@winner_r + 1)
    line_x1_2 = Utility.c_to_x(@winner_c + 5)
    line_y1_2 = Utility.r_to_y(@winner_r - 4)

    line_x2_1 = line_x1_1
    line_y2_1 = line_y1_1 + 1
    line_x2_2 = line_x1_2 + 1
    line_y2_2 = line_y1_2

    line_x3_1 = line_x1_1 + 1
    line_y3_1 = line_y1_1 + 1
    line_x3_2 = line_x1_2 + 1
    line_y3_2 = line_y1_2 + 1
  when :diagonal_down
    line_x1_1 = Utility.c_to_x(@winner_c)
    line_y1_1 = Utility.r_to_y(@winner_r)
    line_x1_2 = Utility.c_to_x(@winner_c + 5)
    line_y1_2 = Utility.r_to_y(@winner_r + 5)

    line_x2_1 = line_x1_1 + 1
    line_y2_1 = line_y1_1
    line_x2_2 = line_x1_2
    line_y2_2 = line_y1_2 - 1

    line_x3_1 = line_x1_1
    line_y3_1 = line_y1_1 + 1
    line_x3_2 = line_x1_2 - 1
    line_y3_2 = line_y1_2
  end

  # Render the three lines
  draw_line(line_x1_1, line_y1_1, Gosu::Color.argb(0xffff0000),
            line_x1_2, line_y1_2, Gosu::Color.argb(0xffff0000), 2, :default)
  draw_line(line_x2_1, line_y2_1, Gosu::Color.argb(0xffff0000),
            line_x2_2, line_y2_2, Gosu::Color.argb(0xffff0000), 2, :default)
  draw_line(line_x3_1, line_y3_1, Gosu::Color.argb(0xffff0000),
            line_x3_2, line_y3_2, Gosu::Color.argb(0xffff0000), 2, :default)
end

#needs_cursor?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/gomoku/window.rb', line 25

def needs_cursor?
  true
end

#new_gameObject



15
16
17
18
19
20
21
22
23
# File 'lib/gomoku/window.rb', line 15

def new_game
  # Reset the board
  @board.reset
  # Black goes first
  @turn = :black
  # Setup flag to indicate a turn needs to be processed
  @done_turn = false
  @winner = false
end

#process_clickObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gomoku/window.rb', line 38

def process_click
  if @winner
    # We already have a winner, so start new game
    new_game
  else
    # Get the location of the click
    click_r = Utility.y_to_r(mouse_y)
    click_c = Utility.x_to_c(mouse_x)
    case @turn
    when :black
      @black_player.click(click_r, click_c)
    when :white
      @white_player.click(click_r, click_c)
    end
  end
end

#process_turnObject

Loop through board cells and check winner, break when found



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gomoku/window.rb', line 87

def process_turn
  Board.each_r_c do |r, c|
    win = @board.check_win(r, c)
    unless win == :none
      @winner = true
      @winner_direction = win
      @winner_r = r
      @winner_c = c
      break
    end
  end
  # Done processing, reset flag
  @done_turn = false
end

#updateObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gomoku/window.rb', line 58

def update
  case @turn
  when :black
    @black_player.update
    move = @black_player.pick_move
  when :white
    @white_player.update
    move = @white_player.pick_move
  end
  # Perform the move
  do_move(move) if move
  # Process the turn if done
  process_turn if @done_turn
end