Class: GamesAndRpgParadise::TicTacToe::Game

Inherits:
Hasu::Window
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/game.rb

Constant Summary collapse

Z_FRONT =
#

Z_FRONT

#
100
TITLE =
#

TITLE

#
'Tic Tac Toe'
FILE_CURSOR =
#

FILE_CURSOR

#
::GamesAndRpgParadise.project_base_directory?+'images/tic_tac_toe/cursor.png'
WIDTH =
#

WIDTH

#
620
HEIGHT =
#

HEIGHT

#
600

Instance Method Summary collapse

Constructor Details

#initializeGame

#

initialize

#


51
52
53
54
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/game.rb', line 51

def initialize
  super(WIDTH, HEIGHT)
  set_title(TITLE)
end

Instance Method Details

#button_down(id) ⇒ Object

#

button_down

#


84
85
86
87
88
89
90
91
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/game.rb', line 84

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

#check_win?Boolean

#

check_win?

#

Returns:

  • (Boolean)


123
124
125
126
127
128
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/game.rb', line 123

def check_win?
  if @board.check_win?
    @state = :winner
    @winner = @current_letter
  end
end

#drawObject

#

draw

#


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
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/game.rb', line 156

def draw
  Gosu.draw_rect(0, 0, width, height, Gosu::Color::WHITE)
  @board.draw
  draw_overlay unless @state == :playing

  @cursor.draw mouse_x, mouse_y, Z_FRONT, 0.5, 0.5

  case @state
  # ======================================================================= #
  # === :new_game
  # ======================================================================= #
  when :new_game
    font = Gosu::Font.new(25)
    font.draw_text_rel(
      "Click to start a new game", width/2, height/2, 0, 0.5, 0.5, 1, 1,  Gosu::Color::RED
    )
  # ======================================================================= #
  # === :winner
  # ======================================================================= #
  when :winner
    font = Gosu::Font.new(40)
    font.draw_text_rel(
      "#{@winner} is the WINNER!!!!!", width / 2, height / 2, 0, 0.5, 0.5, 1, 1, Gosu::Color::GREEN
    )
  # ======================================================================= #
  # === :cats_game
  # ======================================================================= #
  when :cats_game
    font = Gosu::Font.new(40)
    font.draw_text_rel(
      "CAT'S GAME :(", width / 2, height / 2, 0, 0.5, 0.5, 1, 1, Gosu::Color::BLUE
    )
  when :playing
  end
end

#draw_overlayObject

#

draw_overlay

#


195
196
197
198
199
200
201
202
203
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/game.rb', line 195

def draw_overlay
  Gosu.draw_rect(
    0,
    0,
    width,
    height,
    Gosu::Color.argb(0xdd_ffffff)
  )
end

#full_board?Boolean

#

full_board?

#

Returns:

  • (Boolean)


133
134
135
136
137
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/game.rb', line 133

def full_board?
  if @board.full?
    @state = :cats_game
  end
end

#process_clickObject

#

process_click

#


105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/game.rb', line 105

def process_click
  case @state
  when :new_game,
       :cats_game,
       :winner
    start_game
  # === :playing
  when :playing
    if @board.mark_letter @current_letter, mouse_x, mouse_y
      check_win? || full_board?
      swap_letter
    end
  end
end

#resetObject

#

reset

#


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/game.rb', line 59

def reset
  # ======================================================================= #
  # === @state
  # ======================================================================= #
  @state = :new_game
  # ======================================================================= #
  # === @board
  # ======================================================================= #
  @board = Board.new(width, height)
  @cursor = Gosu::Image.new(FILE_CURSOR, tileable: false)
  # ======================================================================= #
  # === @current_letter
  # ======================================================================= #
  @current_letter = 'X'
end

#start_gameObject

#

start_game

#


96
97
98
99
100
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/game.rb', line 96

def start_game
  @winner = nil
  @board.clear
  @state = :playing
end

#swap_letter(i = @current_letter) ⇒ Object

#

swap_letter

#


142
143
144
145
146
147
148
149
150
151
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/game.rb', line 142

def swap_letter(
    i = @current_letter
  )
  case i
  when 'X'
    @current_letter = 'O'
  when 'O'
    @current_letter = 'X'
  end
end

#updateObject

#

update

#


78
79
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/game.rb', line 78

def update
end