Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/game.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#player1 ⇒ Object
readonly
Returns the value of attribute player1.
-
#player2 ⇒ Object
readonly
Returns the value of attribute player2.
-
#turn ⇒ Object
readonly
Returns the value of attribute turn.
Instance Method Summary collapse
- #draw_game ⇒ Object
- #game_pc_take_turn ⇒ Object
- #game_user_take_turn ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #main_menu ⇒ Object
- #overall_win_game ⇒ Object
- #play_again ⇒ Object
- #player1_take_turn ⇒ Object
- #player2_take_turn ⇒ Object
- #player_win_game ⇒ Object
- #quit_game ⇒ Object
- #start ⇒ Object
- #test_start ⇒ Object
- #two_player_start ⇒ Object
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
8 9 10 11 12 13 |
# File 'lib/game.rb', line 8 def initialize @player1 = nil @player2 = nil @board = nil @turn = nil end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
7 8 9 |
# File 'lib/game.rb', line 7 def board @board end |
#player1 ⇒ Object (readonly)
Returns the value of attribute player1.
7 8 9 |
# File 'lib/game.rb', line 7 def player1 @player1 end |
#player2 ⇒ Object (readonly)
Returns the value of attribute player2.
7 8 9 |
# File 'lib/game.rb', line 7 def player2 @player2 end |
#turn ⇒ Object (readonly)
Returns the value of attribute turn.
7 8 9 |
# File 'lib/game.rb', line 7 def turn @turn end |
Instance Method Details
#draw_game ⇒ Object
113 114 115 116 |
# File 'lib/game.rb', line 113 def draw_game puts "Thank you for playing! This game is a draw." play_again end |
#game_pc_take_turn ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/game.rb', line 67 def game_pc_take_turn overall_win_game if board.win_game? != false draw_game if board.endgame? puts "--------------------------------" turn.computer_take_turn board.print_board game_user_take_turn end |
#game_user_take_turn ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/game.rb', line 56 def game_user_take_turn overall_win_game if board.win_game? != false draw_game if board.endgame? puts "--------------------------------" puts "Please enter a letter between A and G" puts "--------------------------------" turn.user_take_turn board.print_board game_pc_take_turn end |
#main_menu ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/game.rb', line 15 def puts "Welcome to Connect Four!" puts "To play against PC, press c. To play with a friend, press p. To quit, press q." want_to_play = gets.chomp.downcase if want_to_play == "c" start elsif want_to_play == "p" two_player_start elsif want_to_play == "q" quit_game else puts "Invalid input, please press p or q" end end |
#overall_win_game ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/game.rb', line 118 def overall_win_game puts "--------------------------------" if board.win_game? == "X" puts "You've won!" else puts "You've lost!" end play_again end |
#play_again ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/game.rb', line 98 def play_again puts "To play against PC, press c. To play with a friend, press p. To quit, press q." want_to_play = gets.chomp.downcase if want_to_play == "c" start elsif want_to_play == "p" two_player_start elsif want_to_play == "q" quit_game else puts "Invalid input, please press p or q" play_again end end |
#player1_take_turn ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/game.rb', line 76 def player1_take_turn player_win_game if board.win_game? != false draw_game if board.endgame? puts "--------------------------------" puts "#{player1.name}, please enter a letter between A and G" puts "--------------------------------" turn.user_take_turn board.print_board player2_take_turn end |
#player2_take_turn ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/game.rb', line 87 def player2_take_turn player_win_game if board.win_game? != false draw_game if board.endgame? puts "--------------------------------" puts "#{player2.name}, please enter a letter between A and G" puts "--------------------------------" turn.two_player_take_turn board.print_board player1_take_turn end |
#player_win_game ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/game.rb', line 128 def player_win_game winner = nil loser = nil if board.win_game? == "X" winner = player1 loser = player2 else winner = player2 loser = player1 end puts "--------------------------------" puts "Congratulations #{winner.name}, you've won! Better luck next time, #{loser.name}." play_again end |
#quit_game ⇒ Object
143 144 145 146 147 |
# File 'lib/game.rb', line 143 def quit_game puts "--------------------------------" puts "Goodbye!" abort end |
#start ⇒ Object
49 50 51 52 53 54 |
# File 'lib/game.rb', line 49 def start @board = Board.new @turn = Turn.new(@board) board.print_board game_user_take_turn end |
#test_start ⇒ Object
42 43 44 45 46 47 |
# File 'lib/game.rb', line 42 def test_start @board = Board.new @turn = Turn.new(board) @player1 = Player.new("Mike") @player2 = Player.new("Sam") end |
#two_player_start ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/game.rb', line 31 def two_player_start puts "Please enter player 1 name" @player1 = Player.new(gets.chomp) puts "Please enter player 2 name" @player2 = Player.new(gets.chomp) @board = Board.new @turn = Turn.new(board) board.print_board player1_take_turn end |