![]() |
|
[Ruby] TicTacToe [Updated] - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Coding (https://sinister.li/Forum-Coding--71) +--- Thread: [Ruby] TicTacToe [Updated] (/Thread-Ruby-TicTacToe-Updated) |
[Ruby] TicTacToe [Updated] - Inori - 03-23-2015 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! 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"
pickRE: [Ruby] TicTacToe - Eclipse - 03-23-2015 You should search deep learning and try to implement it. It's not easy, but it'll be an interesting challenge. RE: [Ruby] TicTacToe - Inori - 03-23-2015 (03-23-2015, 05:30 PM)Eclipse Wrote: You should search deep learning and try to implement it. It's not easy, but it'll be an interesting challenge. Dude, you're speaking Spanish to me. No idea what it is but I'll poke around for it. RE: [Ruby] TicTacToe - Eclipse - 03-24-2015 (03-23-2015, 05:23 PM)Nightc||ed Wrote: sorry, @Eclipse, it's still about as smart as a Mac user Kek'd. |