Class: JbcdenTtt::TicTacToe

Inherits:
Object
  • Object
show all
Defined in:
lib/jbcden_ttt.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTicTacToe

Returns a new instance of TicTacToe.



18
19
20
21
22
# File 'lib/jbcden_ttt.rb', line 18

def initialize
  @board = Board.new(3,3)
  @turn_num = 0
  @players = []
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



12
13
14
# File 'lib/jbcden_ttt.rb', line 12

def board
  @board
end

#computerObject (readonly)

Returns the value of attribute computer.



12
13
14
# File 'lib/jbcden_ttt.rb', line 12

def computer
  @computer
end

#gameObject (readonly)

Returns the value of attribute game.



12
13
14
# File 'lib/jbcden_ttt.rb', line 12

def game
  @game
end

#playerObject (readonly)

Returns the value of attribute player.



12
13
14
# File 'lib/jbcden_ttt.rb', line 12

def player
  @player
end

#playersObject (readonly)

Returns the value of attribute players.



12
13
14
# File 'lib/jbcden_ttt.rb', line 12

def players
  @players
end

#turn_numObject (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

.startObject



14
15
16
# File 'lib/jbcden_ttt.rb', line 14

def self.start
  new.play
end

Instance Method Details

#choose_humanObject



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.message
      retry
    end
  end
  @computer = Computer.new(choose_computer(human_symbol))
end

#game_loopObject



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_stateObject



71
72
73
# File 'lib/jbcden_ttt.rb', line 71

def initialize_game_state
  @game = GameState.new(@board, @turn_num, @players.first)
end

#playObject



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


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_orderObject



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