Class: GamesAndRpgParadise::TicTacToe
- Inherits:
-
Object
- Object
- GamesAndRpgParadise::TicTacToe
- Defined in:
- lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb
Constant Summary collapse
- WIN_COMBINATIONS =
#
WIN_COMBINATIONS
This constant includes all winning combinations that are possible.
#
[ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [6, 4, 2], [0, 4, 8] ]
Instance Attribute Summary collapse
-
#board ⇒ Object
Returns the value of attribute board.
Instance Method Summary collapse
-
#current_player ⇒ Object
# === current_player ========================================================================= #.
-
#display_board ⇒ Object
# === display_board ========================================================================= #.
-
#draw? ⇒ Boolean
# === draw? ========================================================================= #.
-
#full? ⇒ Boolean
# === full? ========================================================================= #.
-
#game_over? ⇒ Boolean
# === game_over? ========================================================================= #.
-
#initialize ⇒ TicTacToe
constructor
# === initialize ========================================================================= #.
-
#input_to_index(i) ⇒ Object
# === input_to_index ========================================================================= #.
-
#move(position, token = 'X') ⇒ Object
# === move ========================================================================= #.
-
#play ⇒ Object
# === play ========================================================================= #.
-
#position_taken?(i) ⇒ Boolean
# === position_taken? ========================================================================= #.
-
#take_a_turn ⇒ Object
# === take_a_turn ========================================================================= #.
-
#turn_count ⇒ Object
# === turn_count ========================================================================= #.
-
#valid_move?(i) ⇒ Boolean
# === valid_move? ========================================================================= #.
-
#winner ⇒ Object
# === winner ========================================================================= #.
-
#won? ⇒ Boolean
# === won? ========================================================================= #.
Constructor Details
#initialize ⇒ TicTacToe
#
initialize
#
43 44 45 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 43 def initialize @board = Array.new(9,' ') end |
Instance Attribute Details
#board ⇒ Object
Returns the value of attribute board.
38 39 40 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 38 def board @board end |
Instance Method Details
#current_player ⇒ Object
#
current_player
#
118 119 120 121 122 123 124 125 126 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 118 def current_player player = nil if turn_count % 2 == 0 player = 'X' else player = 'O' end return player end |
#display_board ⇒ Object
#
display_board
#
50 51 52 53 54 55 56 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 50 def display_board e " #{@board[0]} | #{@board[1]} | #{@board[2]} " e " ----------- " e " #{@board[3]} | #{@board[4]} | #{@board[5]} " e " ----------- " e " #{@board[6]} | #{@board[7]} | #{@board[8]} " end |
#draw? ⇒ Boolean
#
draw?
#
149 150 151 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 149 def draw? !won? && full? end |
#full? ⇒ Boolean
#
full?
#
142 143 144 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 142 def full? turn_count == 9 end |
#game_over? ⇒ Boolean
#
game_over?
#
156 157 158 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 156 def game_over? won? || full? || draw? end |
#input_to_index(i) ⇒ Object
#
input_to_index
#
61 62 63 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 61 def input_to_index(i) i.to_i - 1 end |
#move(position, token = 'X') ⇒ Object
#
move
#
68 69 70 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 68 def move(position, token = 'X') @board[position] = token end |
#play ⇒ Object
#
play
#
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 174 def play until game_over? take_a_turn end if won? this_player_has_won = winner e "Congratulation - #{this_player_has_won} has won the game." elsif draw? e "Cat's Game!" end end |
#position_taken?(i) ⇒ Boolean
#
position_taken?
#
75 76 77 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 75 def position_taken?(i) @board[i] == 'X' || @board[i] == 'O' end |
#take_a_turn ⇒ Object
#
take_a_turn
#
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 89 def take_a_turn e "Choose a spot between 1-9" spot = gets.strip spot = input_to_index(spot) if valid_move?(spot) move(spot, current_player) else take_a_turn end display_board end |
#turn_count ⇒ Object
#
turn_count
#
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 104 def turn_count taken = 0 @board.each { |i| case i when 'X','O' taken += 1 end } return taken end |
#valid_move?(i) ⇒ Boolean
#
valid_move?
#
82 83 84 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 82 def valid_move?(i) i.between?(0, 8) && !position_taken?(i) end |
#winner ⇒ Object
#
winner
#
163 164 165 166 167 168 169 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 163 def winner won = '' if winner = won? won = @board[winner.first] end return won end |
#won? ⇒ Boolean
#
won?
#
131 132 133 134 135 136 137 |
# File 'lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe.rb', line 131 def won? WIN_COMBINATIONS.detect { |combo| @board[combo[0]] == @board[combo[1]] && @board[combo[1]] == @board[combo[2]] && position_taken?(combo[0]) } end |