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.



21
22
23
24
25
26
27
28
29
30
# File 'lib/threesmodel.rb', line 21

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



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/threesmodel.rb', line 90

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



79
80
81
# File 'lib/threesmodel.rb', line 79

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

#fold_down(id) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/threesmodel.rb', line 59

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



49
50
51
52
53
54
55
56
57
# File 'lib/threesmodel.rb', line 49

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



39
40
41
42
43
44
45
46
47
# File 'lib/threesmodel.rb', line 39

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



69
70
71
72
73
74
75
76
77
# File 'lib/threesmodel.rb', line 69

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



103
104
105
# File 'lib/threesmodel.rb', line 103

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

#prepare_for_next_fold(candidates, game, id) ⇒ Object



83
84
85
86
87
88
# File 'lib/threesmodel.rb', line 83

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



107
108
109
# File 'lib/threesmodel.rb', line 107

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



32
33
34
35
36
37
# File 'lib/threesmodel.rb', line 32

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