Class: Upwords::Game
- Inherits:
-
Object
- Object
- Upwords::Game
- Defined in:
- lib/upwords/game.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
Instance Method Summary collapse
- #add_player(name = nil, letter_capacity = 7, cpu = false) ⇒ Object
- #all_refill_racks ⇒ Object
- #cpu_move ⇒ Object
-
#current_player ⇒ Object
Player Methods =========================================.
- #exit_game ⇒ Object
-
#game_over? ⇒ Boolean
Game over methods =================.
- #get_top_score ⇒ Object
- #get_winners ⇒ Object
-
#initialize(max_players = 4) ⇒ Game
constructor
A new instance of Game.
- #max_players ⇒ Object
- #pending_position?(row, col) ⇒ Boolean
- #pending_result ⇒ Object
-
#play_letter(letter, y = @cursor.y, x = @cursor.x) ⇒ Object
Move Manager Methods =========================================.
- #player_count ⇒ Object
-
#running? ⇒ Boolean
Game Loops & Non-Input Procedures =========================================.
- #skip_turn ⇒ Object
-
#standard_message ⇒ Object
Output messages that rely on MoveManager =========================================.
-
#submit_moves ⇒ Object
Game Procedures Bound to some Key Input =========================================.
- #swap_letter(letter) ⇒ Object
- #undo_last ⇒ Object
Constructor Details
#initialize(max_players = 4) ⇒ Game
Returns a new instance of Game.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/upwords/game.rb', line 5 def initialize(max_players = 4) @max_players = max_players @board = Board.new(10, 5) @letter_bank = LetterBank.new(ALL_LETTERS.dup) @cursor = Cursor.new(@board.num_rows, @board.num_columns, *@board.middle_square[0]) @dict = Dictionary.import(OSPD_FILE) @moves = MoveManager.new(@board, @dict) @players = [] @running = true end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
3 4 5 |
# File 'lib/upwords/game.rb', line 3 def board @board end |
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
3 4 5 |
# File 'lib/upwords/game.rb', line 3 def cursor @cursor end |
#players ⇒ Object (readonly)
Returns the value of attribute players.
3 4 5 |
# File 'lib/upwords/game.rb', line 3 def players @players end |
Instance Method Details
#add_player(name = nil, letter_capacity = 7, cpu = false) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/upwords/game.rb', line 32 def add_player(name = nil, letter_capacity = 7, cpu = false) raise ArgumentError, "No more players can join" if player_count >= max_players # Automatically name player if no name is provided name = "Player #{player_count + 1}" if name.nil? || name.length == 0 @players << Player.new(name, letter_capacity, cpu) end |
#all_refill_racks ⇒ Object
41 42 43 |
# File 'lib/upwords/game.rb', line 41 def all_refill_racks @players.each {|p| p.refill_rack(@letter_bank) } end |
#cpu_move ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/upwords/game.rb', line 87 def cpu_move move = current_player.cpu_move(@board, @dict, batch_size=50, min_score=10) if !move.nil? move.each { |pos, letter| play_letter(letter, *pos) } submit_moves else skip_turn end end |
#current_player ⇒ Object
Player Methods
20 21 22 |
# File 'lib/upwords/game.rb', line 20 def current_player @players.first end |
#exit_game ⇒ Object
122 123 124 |
# File 'lib/upwords/game.rb', line 122 def exit_game @running = false end |
#game_over? ⇒ Boolean
Game over methods
130 131 132 |
# File 'lib/upwords/game.rb', line 130 def game_over? @players.all? {|p| p.last_turn == "skipped turn"} || (@letter_bank.empty? && current_player.rack_empty?) end |
#get_top_score ⇒ Object
134 135 136 |
# File 'lib/upwords/game.rb', line 134 def get_top_score @players.map {|p| p.score}.max end |
#get_winners ⇒ Object
138 139 140 |
# File 'lib/upwords/game.rb', line 138 def get_winners @players.select{|p| p.score == get_top_score}.map{|p| p.name} end |
#max_players ⇒ Object
24 25 26 |
# File 'lib/upwords/game.rb', line 24 def max_players @max_players end |
#pending_position?(row, col) ⇒ Boolean
57 58 59 |
# File 'lib/upwords/game.rb', line 57 def pending_position?(row, col) @moves.include?(row, col) end |
#pending_result ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/upwords/game.rb', line 69 def pending_result new_words = @moves.pending_words unless new_words.empty? new_words.map do |w| "#{w} (#{w.score})".upcase end.join(", ") + " (Total = #{@moves.pending_score(current_player)})" end end |
#play_letter(letter, y = @cursor.y, x = @cursor.x) ⇒ Object
Move Manager Methods
49 50 51 |
# File 'lib/upwords/game.rb', line 49 def play_letter(letter, y = @cursor.y, x = @cursor.x) @moves.add(current_player, modify_letter_input(letter), y, x) end |
#player_count ⇒ Object
28 29 30 |
# File 'lib/upwords/game.rb', line 28 def player_count player_count = @players.size end |
#running? ⇒ Boolean
Game Loops & Non-Input Procedures
83 84 85 |
# File 'lib/upwords/game.rb', line 83 def running? @running end |
#skip_turn ⇒ Object
116 117 118 119 120 |
# File 'lib/upwords/game.rb', line 116 def skip_turn @moves.undo_all(current_player) current_player.last_turn = "skipped turn" # TODO: remove magic string from last move message next_turn end |
#standard_message ⇒ Object
Output messages that rely on MoveManager
65 66 67 |
# File 'lib/upwords/game.rb', line 65 def "#{current_player.name}'s pending words: #{pending_result}" end |
#submit_moves ⇒ Object
Game Procedures Bound to some Key Input
101 102 103 104 105 106 |
# File 'lib/upwords/game.rb', line 101 def submit_moves @moves.submit(current_player) current_player.refill_rack(@letter_bank) current_player.last_turn = "played word" # TODO: remove magic string from last move message next_turn end |
#swap_letter(letter) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/upwords/game.rb', line 108 def swap_letter(letter) letter = modify_letter_input(letter) @moves.undo_all(current_player) current_player.swap_letter(letter, @letter_bank) current_player.last_turn = "swapped letter" # TODO: remove magic string from last move message next_turn end |
#undo_last ⇒ Object
53 54 55 |
# File 'lib/upwords/game.rb', line 53 def undo_last @moves.undo_last(current_player) end |