Login Register






[Ruby] TicTacTWO! [updated] filter_list
Author
Message
[Ruby] TicTacTWO! [updated] #1
I couldn't resist making that pun.
Anyway, this is TicTacToe V2.0, or TicTacTWO!.

I've compacted the actual play sequence, made the AI waaay smarter and actually implemented a win method!

Pretty proud of this, to be honest.
(if you find a bug while playing, please tell me)

Update:
made AI better
Added tie check

Code:
#TicTacTWO! by Touka, ©2015 #====default variables============= $moves = ["x", "o"] $move = 1 m_index = 0 $spots = Array.new(9, " ") $possiblesx = [1, 2, 3] $possiblesy = [1, 2, 3] #================================== #=============board================ def board puts """ TicTacTWO! by Touka #{$spots[0]}|#{$spots[1]}|#{$spots[2]} ----- #{$spots[3]}|#{$spots[4]}|#{$spots[5]} ----- #{$spots[6]}|#{$spots[7]}|#{$spots[8]} move: #{$c_move} """ end #================================== #=======ai movement================ def ai_move if $spots[3] == "#{$moves[$nextmove]}" && $spots[6] == "#{$moves[$nextmove]}" && $spots[0] == " " $pos_in = "1 1" elsif $spots[4] == "#{$moves[$nextmove]}" && $spots[8] == "#{$moves[$nextmove]}" && $spots[0] == " " $pos_in = "1 1" elsif $spots[1] == "#{$moves[$nextmove]}" && $spots[2] == "#{$moves[$nextmove]}" && $spots[0] == " " $pos_in = "1 1" elsif $spots[0] == "#{$moves[$nextmove]}" && $spots[2] == "#{$moves[$nextmove]}" && $spots[1] == " " $pos_in = "1 2" elsif $spots[4] == "#{$moves[$nextmove]}" && $spots[7] == "#{$moves[$nextmove]}" && $spots[1] == " " $pos_in = "1 2" elsif $spots[0] == "#{$moves[$nextmove]}" && $spots[1] == "#{$moves[$nextmove]}" && $spots[2] == " " $pos_in = "1 3" elsif $spots[6] == "#{$moves[$nextmove]}" && $spots[4] == "#{$moves[$nextmove]}" && $spots[2] == " " $pos_in = "1 3" elsif $spots[5] == "#{$moves[$nextmove]}" && $spots[8] == "#{$moves[$nextmove]}" && $spots[2] == " " $pos_in = "1 3" elsif $spots[0] == "#{$moves[$nextmove]}" && $spots[6] == "#{$moves[$nextmove]}" && $spots[3] == " " $pos_in = "2 1" elsif $spots[4] == "#{$moves[$nextmove]}" && $spots[5] == "#{$moves[$nextmove]}" && $spots[3] == " " $pos_in = "2 1" elsif $spots[0] == "#{$moves[$nextmove]}" && $spots[8] == "#{$moves[$nextmove]}" && $spots[4] == " " $pos_in = "2 2" elsif $spots[1] == "#{$moves[$nextmove]}" && $spots[7] == "#{$moves[$nextmove]}" && $spots[4] == " " $pos_in = "2 2" elsif $spots[2] == "#{$moves[$nextmove]}" && $spots[6] == "#{$moves[$nextmove]}" && $spots[4] == " " $pos_in = "2 2" elsif $spots[5] == "#{$moves[$nextmove]}" && $spots[3] == "#{$moves[$nextmove]}" && $spots[4] == " " $pos_in = "2 2" elsif $spots[2] == "#{$moves[$nextmove]}" && $spots[8] == "#{$moves[$nextmove]}" && $spots[5] == " " $pos_in = "2 3" elsif $spots[3] == "#{$moves[$nextmove]}" && $spots[4] == "#{$moves[$nextmove]}" && $spots[5] == " " $pos_in = "2 3" elsif $spots[0] == "#{$moves[$nextmove]}" && $spots[3] == "#{$moves[$nextmove]}" && $spots[6] == " " $pos_in = "3 1" elsif $spots[2] == "#{$moves[$nextmove]}" && $spots[4] == "#{$moves[$nextmove]}" && $spots[6] == " " $pos_in = "3 1" elsif $spots[7] == "#{$moves[$nextmove]}" && $spots[8] == "#{$moves[$nextmove]}" && $spots[6] == " " $pos_in = "3 1" elsif $spots[1] == "#{$moves[$nextmove]}" && $spots[4] == "#{$moves[$nextmove]}" && $spots[7] == " " $pos_in = "3 2" elsif $spots[6] == "#{$moves[$nextmove]}" && $spots[8] == "#{$moves[$nextmove]}" && $spots[7] == " " $pos_in = "3 2" elsif $spots[6] == "#{$moves[$nextmove]}" && $spots[7] == "#{$moves[$nextmove]}" && $spots[8] == " " $pos_in = "3 3" elsif $spots[0] == "#{$moves[$nextmove]}" && $spots[4] == "#{$moves[$nextmove]}" && $spots[8] == " " $pos_in = "3 3" elsif $spots[2] == "#{$moves[$nextmove]}" && $spots[5] == "#{$moves[$nextmove]}" && $spots[8] == " " $pos_in = "3 3" else aimx = $possiblesx.sample aimy = $possiblesy.sample $pos_in = "#{aimx} #{aimy}" end end #================================== #===========checks================= def win system "cls" puts " _______________________________ | | | Player \"#{$c_move}\" has won TicTacTWO! | |_______________________________|" board sleep end def cats if $spots.include?(" ") game else system "cls" puts " _______________________________ | | | Cat's game | |_______________________________|" board sleep end end def has_won if $spots[0] == $c_move && $spots[1] == $c_move && $spots[2] == $c_move win elsif $spots[3] == $c_move && $spots[4] == $c_move && $spots[5] == $c_move win elsif $spots[6] == $c_move && $spots[7] == $c_move && $spots[8] == $c_move win elsif $spots[0] == $c_move && $spots[3] == $c_move && $spots[6] == $c_move win elsif $spots[1] == $c_move && $spots[4] == $c_move && $spots[7] == $c_move win elsif $spots[2] == $c_move && $spots[5] == $c_move && $spots[8] == $c_move win elsif $spots[0] == $c_move && $spots[4] == $c_move && $spots[8] == $c_move win elsif $spots[2] == $c_move && $spots[4] == $c_move && $spots[6] == $c_move win else cats end end #================================== #=========turn cycle=============== def game system "cls" if $move == 1 $move = 0 $nextmove = 1 else $move = 1 $nextmove = 0 end $c_move = $moves[$move] board if $opp == 2 && $c_move == "o" ai_move else puts "Enter move coordinates (ex \"1 1\"):" $pos_in = gets.chomp end case $pos_in when "1 1" m_index = 0 when "1 2" m_index = 1 when "1 3" m_index = 2 when "2 1" m_index = 3 when "2 2" m_index = 4 when "2 3" m_index = 5 when "3 1" m_index = 6 when "3 2" m_index = 7 when "3 3" m_index = 8 end if $spots[m_index] == " " $spots[m_index] = "#{$c_move}" else if $opp == 2 && $c_move == "o" $move = $c_move game end system "cls" puts " Error. Re-enter move." sleep(3) system "cls" $move = $c_move game end system "cls" has_won end #================================== puts "Play vs. [1]Friend [2]AI" $opp = gets.to_i game
It'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.

Reply