Class: GamePlayscript

Inherits:
Object
  • Object
show all
Defined in:
lib/ttt/interfaces/limelight/playscripts/game_playscript.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ GamePlayscript

Returns a new instance of GamePlayscript.



3
4
5
6
7
8
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 3

def initialize(options)
  self.context    = options.fetch(:context)
  self.game_id    = options.fetch(:game_id)
  self.production = options.fetch(:production)
  self
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



2
3
4
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 2

def context
  @context
end

#game_idObject

Returns the value of attribute game_id.



2
3
4
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 2

def game_id
  @game_id
end

#productionObject

Returns the value of attribute production.



2
3
4
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 2

def production
  @production
end

Instance Method Details

#ai_moveObject



121
122
123
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 121

def ai_move
  production.context.ai_move(@game_id)
end

#boardObject



142
143
144
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 142

def board
  production.context.board(@game_id)
end

#cell_locationObject



82
83
84
85
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 82

def cell_location
  return @move[-2..-1] if @move[-2..-1] !~ (/[a-z]/i)
  @move[-1..-1]
end

#current_move_indexObject



36
37
38
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 36

def current_move_index
  production.move_index
end

#display_move_historyObject



108
109
110
111
112
113
114
115
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 108

def display_move_history
  history = production.context.get_history(@game_id)
  @context.find_by_id("move_history").text = "Move History"
  result_string = ''
  history.each do |value|
    @context.find_by_id("move_history").text += "\n#{value.side} #{value.move}"
  end
end

#display_resultsObject



26
27
28
29
30
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 26

def display_results
  update_context_board
  display_move_history
  game_over_prompt
end

#finished?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 150

def finished?
  production.context.finished?(@game_id)
end

#game_move_indexObject



32
33
34
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 32

def game_move_index
  context.get_history(game_id).length
end

#game_over_promptObject



87
88
89
90
91
92
93
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 87

def game_over_prompt
  if winner?
    @context.find_by_id("player_turn").text = "#{winner} is the winner!"
  else
    @context.find_by_id("player_turn").text = "It's a draw"
  end
end

#generate_history_boardObject



40
41
42
43
44
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 40

def generate_history_board
  game = context.get_game(game_id)
  game.history.move_traverser.adjust_move_index(production.move_index)
  game.history.move_traverser.history_board_builder(game.board, production.move_index)
end

#initialize_historyObject



10
11
12
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 10

def initialize_history
  production.context.initialize_history(game_id)
end

#mark_moveObject



129
130
131
132
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 129

def mark_move
  player = production.context.which_current_player?(@game_id)
  production.context.update_game(@game_id, move, player_side(player))
end

#moveObject



78
79
80
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 78

def move
  cell_location.to_i
end

#next_move_is_ai?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 125

def next_move_is_ai?
  production.context.ai_move?(@game_id)
end

#not_finished?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 154

def not_finished?
  !finished?
end

#player_move(cell) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 46

def player_move(cell)
  @move = cell.id
  return unless valid_move? && not_finished?
  mark_move
  update_context_board
  update_move_history
  prompt_next_player
  display_results if finished?
  process_ai_move unless finished?
end

#player_side(player_num) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 134

def player_side(player_num)
  if player_num == "Player 1"
    "x"
  else
    "o"
  end
end

#process_ai_moveObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 57

def process_ai_move
  return unless next_move_is_ai?
  ai_move
  update_context_board
  update_move_history
  prompt_next_player
  display_results if finished?
  if next_move_is_ai? && not_finished?
    sleep 1
    process_ai_move
  end
end

#process_game_historyObject



21
22
23
24
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 21

def process_game_history
  history_board = production.context.get_history_board(game_id)
  update_context_board(history_board)
end

#prompt_next_playerObject



117
118
119
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 117

def prompt_next_player
  @context.find_by_id("player_turn").text = "#{which_current_player?}'s turn"
end

#set_move(cell) ⇒ Object



74
75
76
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 74

def set_move(cell)
  @move = cell.id
end

#update_context_board(new_board = board) ⇒ Object



95
96
97
98
99
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 95

def update_context_board(new_board = board)
  new_board.each_with_index do |square, index|
    @context.find_by_id("square_text_#{index}").text = square
  end
end

#update_move_historyObject



101
102
103
104
105
106
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 101

def update_move_history
  history = production.context.get_history(@game_id)
  size    = history.size
  value   = history[size - 1]
  @context.find_by_id("move_history").text += "\n#{value.side} #{value.move}"
end

#valid_move?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 166

def valid_move?
  production.context.valid_move?(@game_id, move)
end

#view_history(index_diff) ⇒ Object



14
15
16
17
18
19
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 14

def view_history(index_diff)
  game = production.context.get_game(game_id)
  game.adjust_move_index(index_diff)
  production.context.save_game(game_id, game)
  process_game_history
end

#which_board?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 70

def which_board?
  production.context.which_board(@game_id)
end

#which_current_player?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 146

def which_current_player?
  production.context.which_current_player?(@game_id)
end

#winnerObject



162
163
164
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 162

def winner
  production.context.winner(@game_id)
end

#winner?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/ttt/interfaces/limelight/playscripts/game_playscript.rb', line 158

def winner?
  production.context.winner?(@game_id)
end