Class: MPGameEngine
- Inherits:
-
Object
- Object
- MPGameEngine
- Defined in:
- lib/multiplayer_game_engine.rb
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
Constants included from IpConfig
IpConfig::P2P_IP, IpConfig::TEST_IP
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#current_player ⇒ Object
Returns the value of attribute current_player.
-
#player1 ⇒ Object
Returns the value of attribute player1.
-
#player2 ⇒ Object
Returns the value of attribute player2.
Instance Method Summary collapse
- #drop_token(column, token) ⇒ Object
-
#initialize ⇒ MPGameEngine
constructor
A new instance of MPGameEngine.
-
#play_game ⇒ Object
Player has hit āpā.
- #players ⇒ Object
- #set_current_player(p1) ⇒ Object
- #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
#initialize ⇒ MPGameEngine
Returns a new instance of MPGameEngine.
16 17 18 19 20 21 |
# File 'lib/multiplayer_game_engine.rb', line 16 def initialize @player1 = nil @player2 = nil @current_player = nil @board = Board.new end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
11 12 13 |
# File 'lib/multiplayer_game_engine.rb', line 11 def board @board end |
#current_player ⇒ Object
Returns the value of attribute current_player.
12 13 14 |
# File 'lib/multiplayer_game_engine.rb', line 12 def current_player @current_player end |
#player1 ⇒ Object
Returns the value of attribute player1.
12 13 14 |
# File 'lib/multiplayer_game_engine.rb', line 12 def player1 @player1 end |
#player2 ⇒ Object
Returns the value of attribute player2.
12 13 14 |
# File 'lib/multiplayer_game_engine.rb', line 12 def player2 @player2 end |
Instance Method Details
#drop_token(column, token) ⇒ Object
139 140 141 |
# File 'lib/multiplayer_game_engine.rb', line 139 def drop_token(column, token) @board.drop_token(column, token) end |
#play_game ⇒ Object
Player has hit āpā
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/multiplayer_game_engine.rb', line 31 def play_game # Player has hit 'p' game_over = false until game_over puts @board.display turn_over = false plyr = whose_turn token_x, token_y = nil, nil # returned from #drop_token and passed to #win_condition # local client if plyr == @player1 input_received = false column = nil mutex = Mutex.new input_thread = Thread.new do puts PLAYER_TURN_MSG # keep false until valid input => in-range, un-filled column until turn_over ready = IO.select([$stdin], nil, nil, 1.6) # 4th arg is 'release after time' (s) if ready column = $stdin.gets.chomp.upcase if valid_input(column) mutex.synchronize do input_received = true turn_over = true end else puts INPUT_ERR_MSG(column) # continue to run until local client enters valid input # loops back to until turn_over end end end end timer_thread = Thread.new do 10.times do |i| print "#{10 - i}. . " sleep 1.6 break if mutex.synchronize { input_received } end # if the player doesn't select a column, random valid selection will occur until turn_over column = Board::COLUMNS.sample if valid_input(column) puts TIMES_UP_MSG(column) mutex.synchronize do input_received = true turn_over = true end end end end [input_thread, timer_thread].each(&:join) token_x, token_y = drop_token(column, plyr.token) # send valid column selection to server `curl -s "#{P2P_IP}/move?player=#{plyr.name}?column=#{column}"` else # remote client || @player2 until turn_over 11.times do puts WAITING_FOR_PLYR_MSG(plyr.name) response = `curl -s "#{P2P_IP}/status?player=#{@player1.name}"`.chomp # require 'pry'; binding.pry unless response.include?("patience") # unless this fails => response == <valid letter> token_x, token_y = drop_token(response, plyr.token) turn_over = true break end sleep 1.6 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 sleep 1.6 # send /reset command to server to clear out stored variables `curl -s "#{P2P_IP}/reset"` end |
#players ⇒ Object
23 24 25 |
# File 'lib/multiplayer_game_engine.rb', line 23 def players [@player1, @player2] end |
#set_current_player(p1) ⇒ Object
27 28 29 |
# File 'lib/multiplayer_game_engine.rb', line 27 def set_current_player(p1) @current_player = players.find { |player| player.name == p1 } end |
#valid_input(column) ⇒ Object
123 124 125 126 |
# File 'lib/multiplayer_game_engine.rb', line 123 def valid_input(column) idx = @board.column_to_index(column) Board::COLUMNS.include?(column) && @board.board[0][idx] == "." end |
#whose_turn ⇒ Object
returns player object and increments queue
129 130 131 132 133 134 135 136 137 |
# File 'lib/multiplayer_game_engine.rb', line 129 def whose_turn plyr = @current_player @current_player = if plyr == players[0] players[1] else players[0] end plyr end |