class Upwords::Game

Attributes

board[R]
cursor[R]
players[R]

Public Class Methods

new(max_players = 4) click to toggle source
# 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

Public Instance Methods

add_player(name = nil, letter_capacity = 7, cpu = false) click to toggle source
# 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() click to toggle source
# File lib/upwords/game.rb, line 41
def all_refill_racks
  @players.each {|p| p.refill_rack(@letter_bank) }
end
cpu_move() click to toggle source
# 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() click to toggle source

Player Methods

# File lib/upwords/game.rb, line 20
def current_player
  @players.first
end
exit_game() click to toggle source
# File lib/upwords/game.rb, line 122
def exit_game
  @running = false
end
game_over?() click to toggle source

Game over methods

# 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() click to toggle source
# File lib/upwords/game.rb, line 134
def get_top_score
  @players.map {|p| p.score}.max
end
get_winners() click to toggle source
# 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() click to toggle source
# File lib/upwords/game.rb, line 24
def max_players
  @max_players
end
pending_position?(row, col) click to toggle source
# File lib/upwords/game.rb, line 57
def pending_position?(row, col)
  @moves.include?(row, col)
end
pending_result() click to toggle source
# 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) click to toggle source

Move Manager Methods

# 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() click to toggle source
# File lib/upwords/game.rb, line 28
def player_count
  player_count = @players.size
end
running?() click to toggle source

Game Loops & Non-Input Procedures

# File lib/upwords/game.rb, line 83
def running?
  @running
end
skip_turn() click to toggle source
# 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() click to toggle source

Output messages that rely on MoveManager

# File lib/upwords/game.rb, line 65
def standard_message
  "#{current_player.name}'s pending words: #{pending_result}"
end
submit_moves() click to toggle source

Game Procedures Bound to some Key Input

# 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) click to toggle source
# 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() click to toggle source
# File lib/upwords/game.rb, line 53
def undo_last
  @moves.undo_last(current_player)
end

Private Instance Methods

modify_letter_input(letter) click to toggle source

Capitalize letters, and convert 'Q' and 'q' to 'Qu'

# File lib/upwords/game.rb, line 154
def modify_letter_input(letter)
  if letter =~ /[Qq]/
    'Qu'
  else
    letter.capitalize
  end
end
next_turn() click to toggle source
# File lib/upwords/game.rb, line 144
def next_turn
  if game_over?
    # Subtract 5 points for each tile remaining
    @players.each { |p| p.score -= p.letters.size * 5 }
  else    
    @players.rotate!
  end
end