[Ruby] TicTacToe [Updated] 03-23-2015, 05:23 PM
#1
Hey guys. I got bored on a study trip and I made this.
Only thing is, it doesn't tell the players when one wins.
I'm planning to add that as well as an AI to play against, while fixing the bug where one move can go over another.
Anyway, enjoy!
Update 1.5:
-Added AI (sorry, @Eclipse, it's still about as smart as a Mac user)
-Fixed placement bug
Only thing is, it doesn't tell the players when one wins.
I'm planning to add that as well as an AI to play against, while fixing the bug where one move can go over another.
Anyway, enjoy!
Code:
#TicTacToe by RubyRose (Nightcored) ©2015
def pick
@move = "#{@nextmove}"
if @move == "x"
@nextmove = "o"
elsif @move == "o"
@nextmove = "x"
else
exit
end
@box = """
TicTacToe 1.3 by Nightcored
Move: #{@move}
1 2 3
1 #{@oneone}I #{@onetwo}I #{@onethree}
-- -- --
2 #{@twoone}I #{@twotwo}I #{@twothree}
-- -- --
3 #{@threeone}I #{@threetwo}I #{@threethree}
"""
puts "#{@box}"
puts 'enter box location'
puts 'x:'
@v = gets.chomp
system "cls"
puts "#{@box}"
puts 'enter box location'
puts "x: #{@v}"
puts "y:"
@y = gets.chomp
pos = "#{@v} #{@y}"
disp = "#{@move}"
if pos == "1 1"
if @oneone == " "
@oneone = "#{disp}"
else
system "cls"
puts "error"
sleep
end
elsif pos == "1 2"
if @onetwo == " "
@onetwo = "#{disp}"
else
system "cls"
puts "error"
sleep
end
elsif pos == "1 3"
if @onethree == " "
@onethree = "#{disp}"
else
system "cls"
puts "error"
sleep
end
elsif pos == "2 1"
if @twoone == " "
@twoone = "#{disp}"
else
system "cls"
puts "error"
sleep
end
elsif pos == "2 2"
if @twotwo == " "
@twotwo = "#{disp}"
else
system "cls"
puts "error"
sleep
end
elsif pos == "2 3"
if @twothree == " "
@twothree = "#{disp}"
else
system "cls"
puts "error"
sleep
end
elsif pos == "3 1"
if @threeone == " "
@threeone = "#{disp}"
else
system "cls"
puts "error"
sleep
end
elsif pos == "3 2"
if @threetwo == " "
@threetwo = "#{disp}"
else
system "cls"
puts "error"
sleep
end
elsif pos == "3 3"
if @threethree == " "
@threethree = "#{disp}"
else
system "cls"
puts "error"
sleep
end
else
exit
end
system "cls"
pick
end
@oneone = " "
@onetwo = " "
@onethree = " "
@twoone = " "
@twotwo = " "
@twothree = " "
@threeone = " "
@threetwo = " "
@threethree = " "
@nextmove = "x"
pickUpdate 1.5:
-Added AI (sorry, @Eclipse, it's still about as smart as a Mac user)
-Fixed placement bug
Code:
#TicTacToe 1.5 by RubyRose (Nightcored) ©2015
def pick
@move = "#{@nextmove}"
if @move == "x"
@nextmove = "o"
elsif @move == "o"
@nextmove = "x"
else
exit
end
@box = """
TicTacToe 1.5 by Nightcored
Move: #{@move}
1 2 3
1 #{@oneone}I #{@onetwo}I #{@onethree}
-- -- --
2 #{@twoone}I #{@twotwo}I #{@twothree}
-- -- --
3 #{@threeone}I #{@threetwo}I #{@threethree}
"""
if @move == "o" and @opp == "1"
aimx = @possiblesx.to_a.sample
aimy = @possiblesy.to_a.sample
pos = "#{aimx} #{aimy}"
else
puts "#{@box}"
puts 'enter box location'
puts 'x:'
@v = gets.chomp
system "cls"
puts "#{@box}"
puts 'enter box location'
puts "x: #{@v}"
puts "y:"
@y = gets.chomp
pos = "#{@v} #{@y}"
end
disp = "#{@move}"
if pos == "1 1"
if @oneone == " "
@oneone = "#{disp}"
else
@nextmove = "#{@move}"
end
elsif pos == "1 2"
if @onetwo == " "
@onetwo = "#{disp}"
else
@nextmove = "#{@move}"
end
elsif pos == "1 3"
if @onethree == " "
@onethree = "#{disp}"
else
@nextmove = "#{@move}"
end
elsif pos == "2 1"
if @twoone == " "
@twoone = "#{disp}"
else
@nextmove = "#{@move}"
end
elsif pos == "2 2"
if @twotwo == " "
@twotwo = "#{disp}"
else
@nextmove = "#{@move}"
end
elsif pos == "2 3"
if @twothree == " "
@twothree = "#{disp}"
else
@nextmove = "#{@move}"
end
elsif pos == "3 1"
if @threeone == " "
@threeone = "#{disp}"
else
@nextmove = "#{@move}"
end
elsif pos == "3 2"
if @threetwo == " "
@threetwo = "#{disp}"
else
@nextmove = "#{@move}"
end
elsif pos == "3 3"
if @threethree == " "
@threethree = "#{disp}"
else
@nextmove = "#{@move}"
end
else
exit
end
system "cls"
pick
end
@oneone = " "
@onetwo = " "
@onethree = " "
@twoone = " "
@twotwo = " "
@twothree = " "
@threeone = " "
@threetwo = " "
@threethree = " "
@nextmove = "x"
@possiblesx = (1..3)
@possiblesy = (1..3)
puts "vs. AI[1] or player[2]?"
@opp = gets.chomp
system "cls"
pickIt's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.
don't have much in the first place, who feel the new currents and ride them the farthest.

















![[+]](https://sinister.li/images/modern/collapse_collapsed.png)





