Ruby Code Sample
A Tic Tac Toe Game
row = Array.new
row[0] = " " # Top Left Space
row[1] = " " # Top Center
row[2] = " " # Top Right
row[3] = " " # Center Left
row[4] = " " # Center Center
row[5] = " " # Center Right
row[6] = " " # Bottom Left
row[7] = " " # Bottom Center
row[8] = " " # Bottom Right
pturn = "1" # Player's Turn, 1 or 2
numspac = [0, 1, 2, 3, 4, 5, 6, 7, 8]
def disp_board(row)
puts " #{row[0]} | #{row[1]} | #{row[2]} "
puts "---|---|---"
puts " #{row[3]} | #{row[4]} | #{row[5]} "
puts "---|---|---"
puts " #{row[6]} | #{row[7]} | #{row[8]} "
end
def check3row(row)
if row[0] == 'X' and row[1] == 'X' and row[2] == 'X'
puts 'Player 1 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[0] == 'X' and row[3] == 'X' and row[6] == 'X'
puts 'Player 1 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[0] == 'X' and row[4] == 'X' and row[8] == 'X'
puts 'Player 1 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[2] == 'X' and row[5] == 'X' and row[8] == 'X'
puts 'Player 1 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[2] == 'X' and row[4] == 'X' and row[6] == 'X'
puts 'Player 1 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[1] == 'X' and row[4] == 'X' and row[7] == 'X'
puts 'Player 1 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[3] == 'X' and row[4] == 'X' and row[5] == 'X'
puts 'Player 1 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[6] == 'X' and row[7] == 'X' and row[8] == 'X'
puts 'Player 1 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[0] == 'O' and row[1] == 'O' and row[2] == 'O'
puts 'Player 2 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[0] == 'O' and row[3] == 'O' and row[6] == 'O'
puts 'Player 2 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[0] == 'O' and row[4] == 'O' and row[8] == 'O'
puts 'Player 2 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[2] == 'O' and row[5] == 'O' and row[8] == 'O'
puts 'Player 2 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[2] == 'O' and row[4] == 'O' and row[6] == 'O'
puts 'Player 2 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[1] == 'O' and row[4] == 'O' and row[7] == 'O'
puts 'Player 2 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[3] == 'O' and row[4] == 'O' and row[5] == 'O'
puts 'Player 2 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
elsif row[6] == 'O' and row[7] == 'O' and row[8] == 'O'
puts 'Player 2 wins!!'
puts 'Press any key to exit...'
ex = gets
exit
end
end
disp_board(row)
# P1 Turn
puts "Player 1's turn"
puts "Select your space, enter the number that is in the space you want"
disp_board(numspac)
space = gets.to_i # Get the space they want!
while row[space] != " "
puts "Try again that one is taken!!!"
disp_board(numspac)
space = gets.to_i
end
row[space] = "X"
# End of Turn
disp_board(row)
check3row(row)
# P2 Turn
puts "Player 2's turn"
puts "Select your space, enter the number that is in the space you want"
disp_board(numspac)
space = gets.to_i # Get the space they want!
while row[space] != " "
puts "Try again that one is taken!!!"
disp_board(numspac)
space = gets.to_i
end
row[space] = "O"
# End Turn
disp_board(row)
check3row(row)
# P1 Turn
puts "Player 1's turn"
puts "Select your space, enter the number that is in the space you want"
disp_board(numspac)
space = gets.to_i # Get the space they want!
while row[space] != " "
puts "Try again that one is taken!!!"
disp_board(numspac)
space = gets.to_i
end
row[space] = "X"
# End of Turn
disp_board(row)
check3row(row)
# P2 Turn
puts "Player 2's turn"
puts "Select your space, enter the number that is in the space you want"
disp_board(numspac)
space = gets.to_i # Get the space they want!
while row[space] != " "
puts "Try again that one is taken!!!"
disp_board(numspac)
space = gets.to_i
end
row[space] = "O"
# End Turn
disp_board(row)
check3row(row)
# P1 Turn
puts "Player 1's turn"
puts "Select your space, enter the number that is in the space you want"
disp_board(numspac)
space = gets.to_i # Get the space they want!
while row[space] != " "
puts "Try again that one is taken!!!"
disp_board(numspac)
space = gets.to_i
end
row[space] = "X"
# End of Turn
disp_board(row)
check3row(row)
# P2 Turn
puts "Player 2's turn"
puts "Select your space, enter the number that is in the space you want"
disp_board(numspac)
space = gets.to_i # Get the space they want!
while row[space] != " "
puts "Try again that one is taken!!!"
disp_board(numspac)
space = gets.to_i
end
row[space] = "O"
# End Turn
disp_board(row)
check3row(row)
# P1 Turn
puts "Player 1's turn"
puts "Select your space, enter the number that is in the space you want"
disp_board(numspac)
space = gets.to_i # Get the space they want!
while row[space] != " "
puts "Try again that one is taken!!!"
disp_board(numspac)
space = gets.to_i
end
row[space] = "X"
# End of Turn
disp_board(row)
check3row(row)
# P2 Turn
puts "Player 2's turn"
puts "Select your space, enter the number that is in the space you want"
disp_board(numspac)
space = gets.to_i # Get the space they want!
while row[space] != " "
puts "Try again that one is taken!!!"
disp_board(numspac)
space = gets.to_i
end
row[space] = "O"
# End Turn
disp_board(row)
check3row(row)
# P1 Turn
puts "Player 1's turn"
puts "Select your space, enter the number that is in the space you want"
disp_board(numspac)
space = gets.to_i # Get the space they want!
while row[space] != " "
puts "Try again that one is taken!!!"
disp_board(numspac)
space = gets.to_i
end
row[space] = "X"
# End of Turn
disp_board(row)
check3row(row)
puts 'Cat game!'
puts 'Press any key to exit...'
ex = gets
exit