Class: JbcdenTtt::TicTacToe
- Inherits:
-
Object
- Object
- JbcdenTtt::TicTacToe
- Defined in:
- lib/jbcden_ttt.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#computer ⇒ Object
readonly
Returns the value of attribute computer.
-
#game ⇒ Object
readonly
Returns the value of attribute game.
-
#player ⇒ Object
readonly
Returns the value of attribute player.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
-
#turn_num ⇒ Object
readonly
Returns the value of attribute turn_num.
Class Method Summary collapse
Instance Method Summary collapse
- #choose_human ⇒ Object
- #game_loop ⇒ Object
-
#initialize ⇒ TicTacToe
constructor
A new instance of TicTacToe.
- #initialize_game_state ⇒ Object
- #play ⇒ Object
- #print_intro ⇒ Object
- #set_players_order ⇒ Object
Constructor Details
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
12 13 14 |
# File 'lib/jbcden_ttt.rb', line 12 def board @board end |
#computer ⇒ Object (readonly)
Returns the value of attribute computer.
12 13 14 |
# File 'lib/jbcden_ttt.rb', line 12 def computer @computer end |
#game ⇒ Object (readonly)
Returns the value of attribute game.
12 13 14 |
# File 'lib/jbcden_ttt.rb', line 12 def game @game end |
#player ⇒ Object (readonly)
Returns the value of attribute player.
12 13 14 |
# File 'lib/jbcden_ttt.rb', line 12 def player @player end |
#players ⇒ Object (readonly)
Returns the value of attribute players.
12 13 14 |
# File 'lib/jbcden_ttt.rb', line 12 def players @players end |
#turn_num ⇒ Object (readonly)
Returns the value of attribute turn_num.
12 13 14 |
# File 'lib/jbcden_ttt.rb', line 12 def turn_num @turn_num end |
Class Method Details
.start ⇒ Object
14 15 16 |
# File 'lib/jbcden_ttt.rb', line 14 def self.start new.play end |
Instance Method Details
#choose_human ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jbcden_ttt.rb', line 43 def choose_human error = "" while 1 begin clear_screen puts "Which player would you like to be? (\"x\" or \"o\"): " puts error unless error.empty? human_symbol = gets.chomp @player = Player.new(human_symbol) break rescue Player::InvalidCharacterError => e error = e. retry end end @computer = Computer.new(choose_computer(human_symbol)) end |
#game_loop ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/jbcden_ttt.rb', line 75 def game_loop while !@game.end_state? clear_board current_player = @players[@turn_num % 2] take_turn(current_player) @turn_num += 1 @game = GameState.new(@board, @turn_num, @players[@turn_num % 2]) end clear_board puts "The winner is: #{game.end_state?}" end |
#initialize_game_state ⇒ Object
71 72 73 |
# File 'lib/jbcden_ttt.rb', line 71 def initialize_game_state @game = GameState.new(@board, @turn_num, @players.first) end |
#play ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/jbcden_ttt.rb', line 24 def play trap('INT') do puts 'exiting!' exit! end print_intro choose_human set_players_order initialize_game_state game_loop end |
#print_intro ⇒ Object
37 38 39 40 41 |
# File 'lib/jbcden_ttt.rb', line 37 def print_intro clear_screen print "Welcome to my Tic Tac Toe game!" end |
#set_players_order ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/jbcden_ttt.rb', line 61 def set_players_order if player.symbol == "x" @players << @player @players << @computer else @players << @computer @players << @player end end |