Class: ConnectN::Game

Inherits:
Object
  • Object
show all
Includes:
Winnable
Defined in:
lib/connect_n/game/game.rb

Constant Summary collapse

PERMITTED_CLASSES =
[Symbol, Game, Board, HumanPlayer, ComputerPlayer]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Winnable

#win?

Constructor Details

#initialize(board:, first_player:, second_player:, min_to_win: 4) ⇒ Game

Returns a new instance of Game.



19
20
21
22
23
24
25
26
27
28
# File 'lib/connect_n/game/game.rb', line 19

def initialize(
  board:,
  first_player:,
  second_player:,
  min_to_win: 4
)
  @board = board
  @players = [first_player, second_player]
  @min_to_win = min_to_win
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



15
16
17
# File 'lib/connect_n/game/game.rb', line 15

def board
  @board
end

#min_to_winObject (readonly)

Returns the value of attribute min_to_win.



15
16
17
# File 'lib/connect_n/game/game.rb', line 15

def min_to_win
  @min_to_win
end

#playersObject (readonly)

Returns the value of attribute players.



15
16
17
# File 'lib/connect_n/game/game.rb', line 15

def players
  @players
end

Class Method Details

.games(yaml_fn) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/connect_n/game/game.rb', line 76

def self.games(yaml_fn)
  YAML.safe_load_file(
    yaml_fn,
    permitted_classes: PERMITTED_CLASSES,
    aliases: true
  ) || {}
end

.load(name, yaml_fn) ⇒ Object



72
73
74
# File 'lib/connect_n/game/game.rb', line 72

def self.load(name, yaml_fn)
  games(yaml_fn)[name.to_sym]
end

.name_gameObject



91
92
93
# File 'lib/connect_n/game/game.rb', line 91

def self.name_game
  PROMPT.ask 'Name your game : '
end

.resume(game) ⇒ Object



68
69
70
# File 'lib/connect_n/game/game.rb', line 68

def self.resume(game)
  game.play
end

.resume?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/connect_n/game/game.rb', line 64

def self.resume?
  PROMPT.yes? 'Do you want to resume a game?'
end

.save(game, name, yaml_fn) ⇒ Object



84
85
86
87
88
89
# File 'lib/connect_n/game/game.rb', line 84

def self.save(game, name, yaml_fn)
  games = games(yaml_fn)
  games[name.to_sym] = game
  dumped_games = YAML.dump(games)
  File.write(yaml_fn, dumped_games)
end

.save?(input) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/connect_n/game/game.rb', line 60

def self.save?(input)
  input == ':w'
end

.select_game_name(yaml_fn) ⇒ Object



95
96
97
98
# File 'lib/connect_n/game/game.rb', line 95

def self.select_game_name(yaml_fn)
  games = games(yaml_fn).keys.map.with_index(1) { "#{_2} -> #{_1}" }
  PROMPT.select 'Choose a saved game : ', games, convert: :sym
end

Instance Method Details

#invalid_pickObject



115
116
117
# File 'lib/connect_n/game/game.rb', line 115

def invalid_pick
  PROMPT.error 'Invalid Column Number'
end

#over(winner) ⇒ Object



119
120
121
122
# File 'lib/connect_n/game/game.rb', line 119

def over(winner)
  phrase = board.filled? ? 'It is a tie!' : "#{winner.name} has won!"
  puts TTY::Box.sucess(phrase)
end

#over?(board, row, col, disc) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/connect_n/game/game.rb', line 52

def over?(board, row, col, disc)
  win?(board, row, col, disc) || board.filled?
end

#play(yaml_fn = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/connect_n/game/game.rb', line 30

def play(yaml_fn = nil)
  welcome
  loop do
    current_player = players.first

    pick = current_player.pick

    break self.class.save(self, self.class.name_game, yaml_fn) if self.class.save? pick

    next invalid_pick unless board.valid_pick? pick

    row_num, col_num, disc = board.drop_disc(current_player.disc, at_col: pick)

    clear_display
    board.draw

    break over(current_player) if over?(board, row_num, col_num, disc)

    players.rotate!
  end
end

#play_again?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/connect_n/game/game.rb', line 56

def play_again?
  PROMPT.yes? 'Would you like to play again?'
end

#welcomeObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/connect_n/game/game.rb', line 100

def welcome
  text = <<~TEXT
    Welcome to Connect Four
    #{'    '}
    To play, Enter a number from 1
    to #{board.cols_amount}
    The number corresponds to the
    column order starting from the
    left.
    Enter anything to proceed.
  TEXT
  puts TTY::Box.frame text, padding: 2, align: :center
  board.draw
end