Class: GameEngine
Constant Summary
Constants included from MSG
MSG::BYE_MSG, MSG::ONEPLAYER_TWOPLAYER, MSG::OPP_FOUND, MSG::PLAYER_TURN_MSG, MSG::PLAY_MSG, MSG::P_OR_Q_ERR_MSG, MSG::TIE_GAME_MSG, MSG::WAITING_MSG, MSG::WELCOME_MSG
Instance Attribute Summary collapse
-
#ai ⇒ Object
readonly
Returns the value of attribute ai.
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#current_player ⇒ Object
readonly
Returns the value of attribute current_player.
-
#piece_count ⇒ Object
readonly
Returns the value of attribute piece_count.
-
#player1 ⇒ Object
readonly
Returns the value of attribute player1.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
Instance Method Summary collapse
- #drop_token(column, token) ⇒ Object
-
#initialize ⇒ GameEngine
constructor
A new instance of GameEngine.
-
#play_game ⇒ Object
Player has hit āpā.
- #valid_input(column) ⇒ Object
-
#whose_turn ⇒ Object
returns player object and increments queue.
Methods included from MSG
#INPUT_ERR_MSG, #LOADER_MSG, #TIMES_UP_MSG, #VICTORY_MSG, #WAITING_FOR_PLYR_MSG
Constructor Details
Instance Attribute Details
#ai ⇒ Object (readonly)
Returns the value of attribute ai.
9 10 11 |
# File 'lib/game_engine.rb', line 9 def ai @ai end |
#board ⇒ Object (readonly)
Returns the value of attribute board.
9 10 11 |
# File 'lib/game_engine.rb', line 9 def board @board end |
#current_player ⇒ Object (readonly)
Returns the value of attribute current_player.
9 10 11 |
# File 'lib/game_engine.rb', line 9 def current_player @current_player end |
#piece_count ⇒ Object (readonly)
Returns the value of attribute piece_count.
9 10 11 |
# File 'lib/game_engine.rb', line 9 def piece_count @piece_count end |
#player1 ⇒ Object (readonly)
Returns the value of attribute player1.
9 10 11 |
# File 'lib/game_engine.rb', line 9 def player1 @player1 end |
#players ⇒ Object (readonly)
Returns the value of attribute players.
9 10 11 |
# File 'lib/game_engine.rb', line 9 def players @players end |
Instance Method Details
#drop_token(column, token) ⇒ Object
92 93 94 |
# File 'lib/game_engine.rb', line 92 def drop_token(column, token) @board.drop_token(column, token) end |
#play_game ⇒ Object
Player has hit āpā
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/game_engine.rb', line 24 def play_game # Player has hit 'p' game_over = false until game_over puts @board.display # keep false until valid input => in-range, un-filled column turn_over = false plyr = whose_turn token_x, token_y = nil, nil # returned from #drop_token and passed to #win_condition # if human player if plyr == @player1 # force user to enter valid input until turn_over # only runs once (right after opponent takes turn) puts PLAYER_TURN_MSG column = CLI.get_input if valid_input(column) token_x, token_y = drop_token(column, plyr.token) turn_over = true else # continue to run until HUMAN user enters valid input puts INPUT_ERR_MSG(column) end end else # computer's turn # TODO trash talk # keep false until computer picks unfilled column (idx always in-range) until turn_over column = Board::COLUMNS.sample if valid_input(column) token_x, token_y = drop_token(column, plyr.token) turn_over = true end end end if @board.check_win(token_x, token_y, plyr.token) puts @board.display puts VICTORY_MSG(plyr.name) game_over = true end if @board.board_full? puts @board.display puts TIE_GAME_MSG game_over = true end end end |
#valid_input(column) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/game_engine.rb', line 72 def valid_input(column) idx = @board.column_to_index(column) if Board::COLUMNS.include?(column) && @board.board[0][idx] == "." true else false end end |
#whose_turn ⇒ Object
returns player object and increments queue
82 83 84 85 86 87 88 89 90 |
# File 'lib/game_engine.rb', line 82 def whose_turn plyr = @current_player @current_player = if plyr == @players[0] @players[1] else @players[0] end plyr end |