Class: CLI::CLIGame

Inherits:
Object
  • Object
show all
Defined in:
lib/ttt/interfaces/cli/lib/cli/cli_game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, input, output) ⇒ CLIGame

Returns a new instance of CLIGame.



11
12
13
14
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 11

def initialize(context, input, output)
  self.context = context
  self.presenter = CLIPresenter.new(input, output)
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



10
11
12
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 10

def context
  @context
end

#gamesObject

Returns the value of attribute games.



10
11
12
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 10

def games
  @games
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 10

def id
  @id
end

#presenterObject

Returns the value of attribute presenter.



10
11
12
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 10

def presenter
  @presenter
end

Instance Method Details

#build_game(pselect, bselect) ⇒ Object



48
49
50
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 48

def build_game(pselect, bselect)
  @context.setup.new_game(player1: pselect.player1, player2: pselect.player2, board: bselect.board)
end

#eval_board_stateObject



108
109
110
111
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 108

def eval_board_state
  @presenter.process_winner(game.board_arr, game.show_history, game.last_player) if game.winner?
  @presenter.process_draw(game.board_arr, game.show_history)                     if game.finished? && !game.winner?
end

#gameObject



64
65
66
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 64

def game
  @context.get_game(@id)
end

#game_listObject



113
114
115
116
117
118
119
120
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 113

def game_list
  get_games
  if @games
    load_game
  else
    no_games
  end
end

#game_overObject



59
60
61
62
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 59

def game_over
  walk_history
  play_again
end

#get_board_selectionObject



44
45
46
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 44

def get_board_selection
  BoardSelection.new(@context.boards, @presenter).process
end

#get_gamesObject



122
123
124
125
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 122

def get_games
  @games = @context.game_list
  @games = nil if @games.length == 0
end

#get_next_moveObject



92
93
94
95
96
97
98
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 92

def get_next_move
  if game.ai_move?
    @context.ai_move(@id)
  else
    @presenter.input
  end
end

#get_player_selectionObject



40
41
42
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 40

def get_player_selection
  PlayerSelection.new(@context.players, @presenter).process
end

#init_gameObject



31
32
33
34
35
36
37
38
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 31

def init_game
  @presenter.welcome_prompt
  player_select   = get_player_selection
  board_select    = get_board_selection
  new_game        = build_game(player_select, board_select)
  @id             = @context.add_game(new_game)
  play_game
end

#load_gameObject



133
134
135
136
137
138
139
140
141
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 133

def load_game
  selection = @presenter.process_game_list(@games)
  if @context.get_game(selection.chomp)
    @id = selection.chomp
    resume_game
  else
    game_list
  end
end

#move_cycleObject



68
69
70
71
72
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 68

def move_cycle
  cur_game = game
  player_prompt(cur_game)
  process_next_move
end

#no_gamesObject



127
128
129
130
131
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 127

def no_games
  @presenter.no_games
  sleep 1
  play
end

#playObject



16
17
18
19
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 16

def play
  user_selection = @presenter.menu
  process_command(user_selection)
end

#play_againObject



175
176
177
178
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 175

def play_again
  exit unless PlayAgain.new(@presenter).play_again?
  play
end

#play_gameObject



52
53
54
55
56
57
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 52

def play_game
  while game.not_finished?
   move_cycle
  end
  game_over
end

#player_prompt(cur_game) ⇒ Object



74
75
76
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 74

def player_prompt(cur_game)
  @presenter.player_prompt(cur_game.board.board, cur_game.show_history, cur_game.current_player)
end

#process_command(selection) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 21

def process_command(selection)
  case selection.chomp
  when "1" then init_game
  when "2" then game_list
  when "3" then exit
  else
    play
  end
end

#process_next_moveObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 78

def process_next_move
  if @context.ai_move?(@id)
    @context.ai_move(@id)
  else
    move = get_next_move
    if validate_move(move)
      update_move(move)
    else
      move_cycle
    end
  end
  eval_board_state
end

#resume_gameObject



149
150
151
152
153
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 149

def resume_game
  @presenter.player_prompt(game.board[], game.show_history, game.current_player)
  eval_board_state
  play_game
end

#update_move(move) ⇒ Object



104
105
106
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 104

def update_move(move)
  @context.update_game(@id, move.to_i, game.current_player.side)
end

#validate_move(move) ⇒ Object



100
101
102
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 100

def validate_move(move)
  @context.valid_move?(@id, move.to_i)
end

#walk_historyObject



180
181
182
183
184
# File 'lib/ttt/interfaces/cli/lib/cli/cli_game.rb', line 180

def walk_history
  @walk_history = WalkHistory.new(@presenter, game)
  @walk_history.post_game_msg
  play_again
end