Class: GamesAndRpgParadise::TicTacToe

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeTicTacToe

#

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

#boardObject

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_playerObject

#

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_boardObject

#

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?

#

Returns:

  • (Boolean)


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?

#

Returns:

  • (Boolean)


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?

#

Returns:

  • (Boolean)


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

#playObject

#

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?

#

Returns:

  • (Boolean)


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_turnObject

#

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_countObject

#

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?

#

Returns:

  • (Boolean)


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

#winnerObject

#

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?

#

Returns:

  • (Boolean)


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