Class: Threesmodel::GameController

Inherits:
Object
  • Object
show all
Defined in:
lib/threesmodel.rb

Instance Method Summary collapse

Constructor Details

#initializeGameController

Returns a new instance of GameController.



78
79
80
81
82
83
84
85
86
87
# File 'lib/threesmodel.rb', line 78

def initialize
  @games = {}
  @next_number = {}
  @candidate_extractor = CandidateExtractor.new
  @candidate_translator = CandidateTranslator.new
  @game_over_checker = GameOverChecker.new
  @next_number_determinator = NextNumberDeterminator.new
  @number_positioner = NextNumberPositioner.new
  @game_board_folder = GameBoardFolder.new
end

Instance Method Details

#add_next_number(id, candidates, game) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/threesmodel.rb', line 147

def add_next_number(id, candidates, game)
  coordinates = @number_positioner.select_position(candidates)
  new_state = []
  game.row_vectors.each_index {|i|
    values = game.row_vectors[i].to_a
    if (coordinates[0] == i)
      values[coordinates[1]] = @next_number[id]
    end
    new_state << values
  }
  Matrix.rows(new_state)
end

#did_not_fold(game, id) ⇒ Object



136
137
138
# File 'lib/threesmodel.rb', line 136

def did_not_fold(game, id)
  respond_to_player(id, @next_number[id])
end

#fold_down(id) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/threesmodel.rb', line 116

def fold_down(id)
  game = @games[id]
  unless (@game_board_folder.can_fold_down?(game))
    return did_not_fold(game, id)
  end
  candidates = @candidate_translator.translate_down_fold(@candidate_extractor.fold_down_candidates(game))
  game = @game_board_folder.fold_down(game)
  prepare_for_next_fold(candidates, game, id)
end

#fold_left(id) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/threesmodel.rb', line 106

def fold_left(id)
  game = @games[id]
  unless (@game_board_folder.can_fold_left?(game))
    return did_not_fold(game, id)
  end
  candidates = @candidate_translator.translate_left_fold(@candidate_extractor.fold_left_candidates(game))
  game = @game_board_folder.fold_left(game)
  prepare_for_next_fold(candidates, game, id)
end

#fold_right(id) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/threesmodel.rb', line 96

def fold_right(id)
  game = @games[id]
  unless (@game_board_folder.can_fold_right?(game))
    return did_not_fold(game, id)
  end
  candidates = @candidate_translator.translate_right_fold(@candidate_extractor.fold_right_candidates(game))
  game = @game_board_folder.fold_right(game)
  prepare_for_next_fold(candidates, game, id)
end

#fold_up(id) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/threesmodel.rb', line 126

def fold_up(id)
  game = @games[id]
  unless (@game_board_folder.can_fold_up?(game))
    return did_not_fold(game, id)
  end
  candidates = @candidate_translator.translate_up_fold(@candidate_extractor.fold_up_candidates(game))
  game = @game_board_folder.fold_up(game)
  prepare_for_next_fold(candidates, game, id)
end

#getNextNumber(game) ⇒ Object



160
161
162
# File 'lib/threesmodel.rb', line 160

def getNextNumber(game)
  @next_number_determinator.select_number(game)
end

#prepare_for_next_fold(candidates, game, id) ⇒ Object



140
141
142
143
144
145
# File 'lib/threesmodel.rb', line 140

def prepare_for_next_fold(candidates, game, id)
  game = add_next_number(id, candidates, game)
  @next_number[id] = getNextNumber(game)
  @games[id] = game
  respond_to_player(id, @next_number[id])
end

#respond_to_player(id, next_number) ⇒ Object



164
165
166
# File 'lib/threesmodel.rb', line 164

def respond_to_player(id, next_number)
  {:id => id, :game => @games[id], :next_number => next_number, :game_over => @game_over_checker.game_over?(@games[id]), :score => ScoreCalculator.score_for(@games[id]) }
end

#start_new_gameObject



89
90
91
92
93
94
# File 'lib/threesmodel.rb', line 89

def start_new_game
  id = SecureRandom.uuid
  @games[id] = GameCreator.create_new_game
  @next_number[id] = getNextNumber(@games[id])
  respond_to_player(id, @next_number[id])
end