Skip to content

Instantly share code, notes, and snippets.

@Ninjex
Created October 26, 2014 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ninjex/b5e790c4403feb3b1881 to your computer and use it in GitHub Desktop.
Save Ninjex/b5e790c4403feb3b1881 to your computer and use it in GitHub Desktop.
Tic Tac Toe Ruby
ran_num = rand(0..1)
if ran_num == 0
PLAYER = "X"
COMPUTER = "O"
@turn = :player
else
PLAYER = "O"
COMPUTER = "X"
@turn = :computer
end
puts "Tic Tac Toe, 3 in a Row!"
AVAILABLE_SQUARES =* (1..9)
ROW_A = { 1 => nil, 2 => nil, 3 => nil }
ROW_B = { 1 => nil, 2 => nil, 3 => nil }
ROW_C = { 1 => nil, 2 => nil, 3 => nil }
ROWS = [ROW_A, ROW_B, ROW_C]
def print_board
puts "Board Available"
puts " _ _ _ _ _ _"
count = 0
ROWS.each do |row|
count += 1
row.each do |column, value|
if value == nil
print "|_"
elsif value == "O"
print "|O"
else
print "|X"
end
end
print "| "
(1..3).each do |i|
if count == 1
if ROW_A[i] == nil
print "|#{i+6}"
else
print "|_"
end
elsif count == 2
if ROW_B[i] == nil
print "|#{i+3}"
else
print "|_"
end
elsif count == 3
if ROW_C[i] == nil
print "|#{i}"
else
print "|_"
end
end
end
puts "|\r\n"
end
end
def spot_valid?(location)
if location == nil
true
else
false
end
end
def spot_taken?(spot)
if AVAILABLE_SQUARES.include?(spot) == false
true
else
AVAILABLE_SQUARES.delete(spot)
false
end
end
def place_choice(spot)
if spot_taken?(spot) == true
puts "That square can not be used."
elsif spot.between?(1,9)
puts "#{@turn.capitalize} chose square: #{spot}"
if spot > 3 && spot < 7
loc = ROW_B
spot -= 3
elsif spot >= 7
loc = ROW_A
spot -= 6
else
loc = ROW_C
end
if @turn == :player
loc[spot] = PLAYER
@turn = :computer
else
loc[spot] = COMPUTER
@turn = :player
end
end
end
def win_horizontal?
cur_row = 0
ROWS.compact.each do |row|
cur_row += 1
if row.values.compact.length == 3 && row.values.uniq.length == 1
if PLAYER == row.values[0]
puts "Player playing as #{row.values[0]}'s won horizontally on row: #{cur_row}"
else
puts "Computer playing as #{row.values[0]}'s won horizontally on row: #{cur_row}"
end
return true
end
end
end
def win_vertical?
(0..2).each do |i|
if ROW_A.values[i] != nil && ROW_A.values[i] == ROW_B.values[i]
if ROW_B.values[i] == ROW_C.values[i]
if PLAYER == ROW_C.values[i]
puts "Player playing as #{ROW_A.values[i]}'s won vertically on column #{i+1}"
else
puts "Computer playing as #{ROW_A.values[i]}'s won vertically on column #{i+1}"
end
return true
end
end
end
end
def win_diagonal?
if ROW_A.values[0] != nil
if ROW_A.values[0] == ROW_B.values[1] && ROW_B.values[1] == ROW_C.values[2]
if PLAYER == ROW_A.values[0]
puts "Player playing as #{ROW_A.values[0]}'s won diagonally with squares 1, 5, and 9"
else
puts "Computer playing as #{ROW_A.values[0]}'s won diagonally with squares 1, 5, and 9"
end
return true
end
end
if ROW_C.values[0] != nil
if ROW_C.values[0] == ROW_B.values[1] && ROW_B.values[1] == ROW_A.values[2]
if PLAYER == ROW_C.values[0]
puts "Player playing as #{ROW_C.values[0]}'s won diagonally with squares 3, 5, and 7"
else
puts "Computer playing as #{ROW_A.values[0]}'s won diagonally with squares 1, 5, and 9"
end
return true
end
end
end
def game_status
wins = [win_horizontal?, win_diagonal?, win_vertical?]
if wins.include?(true) || AVAILABLE_SQUARES.length == 0
return :over
end
puts "Available Squares: #{AVAILABLE_SQUARES}"
end
puts "You are #{PLAYER}'s"
print_board
until game_status == :over
if @turn == :player
print "Square Choice: "
spot = gets.chomp
else
spot = AVAILABLE_SQUARES[rand(0..AVAILABLE_SQUARES.size-1)]
end
place_choice(spot.to_i)
print_board
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment