Class: ConnectFour

Inherits:
Object
  • Object
show all
Includes:
IpConfig, MSG
Defined in:
lib/connect_four.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

Instance Method Summary collapse

Methods included from MSG

#INPUT_ERR_MSG, #LOADER_MSG, #TIMES_UP_MSG, #VICTORY_MSG, #WAITING_FOR_PLYR_MSG

Constructor Details

#initializeConnectFour

Returns a new instance of ConnectFour.



13
14
15
# File 'lib/connect_four.rb', line 13

def initialize
  @game_engine = nil
end

Instance Attribute Details

#game_engineObject (readonly)

Returns the value of attribute game_engine.



11
12
13
# File 'lib/connect_four.rb', line 11

def game_engine
  @game_engine
end

Instance Method Details

#loiterObject



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
# File 'lib/connect_four.rb', line 54

def loiter
  # make initial curl request
  username = ENV["USER"]
  foe = ""
  p1 = ""
  # send /init request
  response = `curl -s "#{P2P_IP}/init?player=#{username}"`.chomp # => '...patient'
  until response == "start"
    print WAITING_MSG
    # send /start request; if game ready to start, will return foe
    response = `curl -s "#{P2P_IP}/start?player=#{username}"`.chomp
    if response.include?("start")
      start_responses = response.split(" ")
      foe = start_responses[1]
      p1 = start_responses[2]  # p1 used to set @current_player
      response = "start"
      break
    end
    LOADER_MSG(7)  # wait 7 seconds
  end

  puts ""
  puts OPP_FOUND
  @game_engine.player1 = Player.new(username, "X")
  @game_engine.player2 = Player.new(foe, "O")
  @game_engine.set_current_player(p1)
  print "game ready to start"
  LOADER_MSG(3)  # wait 3 seconds
end


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/connect_four.rb', line 17

def main_menu
  puts WELCOME_MSG
  play_quit = CLI.get_input
  # play_quit = "P" # testing
  if play_quit == "Q"
    abort(BYE_MSG)
  elsif play_quit == "P"
    puts PLAY_MSG
    set_game_engine
  else
    puts P_OR_Q_ERR_MSG
    main_menu
  end
end

#set_game_engineObject

returns game engine MP or SP



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/connect_four.rb', line 33

def set_game_engine
  puts ONEPLAYER_TWOPLAYER
  game_mode = CLI.get_input
  # game_mode = "2P" # testing
  if game_mode == "1P"
    @game_engine = GameEngine.new
  elsif game_mode == "2P"
    @game_engine = MPGameEngine.new
  else
    puts INPUT_ERR_MSG(game_mode)
    set_game_engine
  end
  start
end

#startObject



48
49
50
51
52
# File 'lib/connect_four.rb', line 48

def start
  loiter if @game_engine.instance_of?(MPGameEngine)
  @game_engine.play_game
  main_menu
end