Search code examples
rrubyterminaltic-tac-toe

Ruby - TicTacToe Finding a Winner


I know TicTacToe questions have been posted before but after looking at the other posts and a lot of online research I am still stumped. I Would really appreciate some help.

Overall I can't figure out why the appropriate winner is not showing up and sometimes nil seems to be given a value as equal to "X" or "O". I'm also having trouble with figuring out Cat's Game, but I think I'm getting close.

Here is all my code, please let me know if any additional info can help find a resolution:

class TicTacToe
def initialize
    row1 = [nil,nil,nil]
    row2 = [nil,nil,nil]
    row3 = [nil,nil,nil]
    @row1 = row1
    @row2 = row2
    @row3 = row3
end

def show_board
    print @row1
    puts
    print @row2
    puts
    print @row3
    puts
end

def prompt
    print " >>> "
end

def turn
    puts "Pick row #"
    print prompt
    row_choice = gets.chomp
    @row_choice = row_choice
    puts "Pick column #"
    print prompt
    column_choice = gets.chomp
    @column_choice = column_choice
end

def winner
    print "checking for winner..."
    if @row1[0] == @row1[1] && @row1[2]
        puts "We have a winner1!"
        replay()
    elsif @row2[0] == @row2[1] && @row2[2]
        puts "We have a winner2!"
        replay()
    elsif @row3[0] == @row3[1] && @row3[2]
        puts "We have a winner3!"
        replay()
    elsif @row1[0] == @row2[1] && @row3[2]
        puts "We have a winner4!"
        replay()
    elsif @row3[0] == @row2[1] && @row1[2]
        puts "We have a winner5!"
        replay()
    elsif @row1[0] == @row2[0] && @row3[0]
        puts "We have a winner6!"
        replay()
    elsif @row1[1] == @row2[1] && @row3[1]
        puts "We have a winner7!"
        replay()
    elsif @row1[2] == @row2[2] && @row3[2]
        puts "We have a winner8!"
        replay()
    #elsif @row1 && @row2 && @row3 != nil
    #   puts "Catsgame - no one wins!"
    #   replay()
    else
        puts "nope"
    end
end 

def replay
    puts "---------------------"
    puts "Would you like to play again? YES or NO"
    select = gets.chomp
    if select.downcase == "yes"
        @row1 = [nil,nil,nil]
        @row2 = [nil,nil,nil]
        @row3 = [nil,nil,nil]
        player1()
    else
        Process.exit(0)
    end
end

def player1
    puts "---------------------"
    puts "It is Player 1's turn"
    turn()
    if @row_choice == 1.to_s
        if @row1[@column_choice.to_i - 1] == "O"
            puts
            puts "Space is taken by Player 2"
            player1()
        elsif @row1[@column_choice.to_i - 1] == "X"
            puts
            puts "You are already in this space"
            player1()   
        else
            @row1[@column_choice.to_i - 1] = "X"
        end
    elsif @row_choice == 2.to_s
        if @row2[@column_choice.to_i - 1] == "O"
            puts
            puts "Space is taken by Player 2"
            player1()
        elsif @row2[@column_choice.to_i - 1] == "X"
            puts
            puts "You are already in this space"
            player1()   
        else
            @row2[@column_choice.to_i - 1] = "X"
        end
    elsif @row_choice == 3.to_s
        if @row3[@column_choice.to_i - 1] == "O"
            puts
            puts "Space is taken by Player 2"
            player1()
        elsif @row3[@column_choice.to_i - 1] == "X"
            puts
            puts "You are already in this space"
            player1()   
        else
            @row3[@column_choice.to_i - 1] = "X"
        end
    else
        puts
        puts "Pick 1, 2, or 3"
        player1()
    end
    show_board()
    winner()
    player2()
end

def player2
    puts "---------------------"
    puts "It is Player 2's turn"
    turn()
    if @row_choice == 1.to_s
        if @row1[@column_choice.to_i - 1] == "X"
            puts
            puts "Space is taken by Player 1"
            player2()
        elsif @row1[@column_choice.to_i - 1] == "O"
            puts
            puts "You are already in this space"
            player2()   
        else
            @row1[@column_choice.to_i - 1] = "O"
        end
    elsif @row_choice == 2.to_s
        if @row2[@column_choice.to_i - 1] == "X"
            puts
            puts "Space is taken by Player 1"
            player2()
        elsif @row2[@column_choice.to_i - 1] == "O"
            puts
            puts "You are already in this space"
            player2()   
        else
            @row2[@column_choice.to_i - 1] = "O"
        end
    elsif @row_choice == 3.to_s
        if @row3[@column_choice.to_i - 1] == "X"
            puts
            puts "Space is taken by Player 1"
            player2()
        elsif @row3[@column_choice.to_i - 1] == "O"
            puts
            puts "You are already in this space"
            player2()   
        else
            @row3[@column_choice.to_i - 1] = "O"
        end
    else
        puts
        puts "Pick 1, 2, or 3"
        player2()
    end
    show_board()
    winner()
    player1()
end
end

puts "The game begins!"
new_game = TicTacToe.new()
new_game.show_board()
new_game.player1()

Thank you!


Solution

  • Actually you'd ideally check for who the winner is -

     if @row1[0] == @row1[1] && @ro1[0] == @row1[2] && @row1[0] == "X"
         puts "Player 1 Wins!"
         replay()
     elsif @row1[0] == @row1[1] && @row1[0] == @row1[2] && @row1[0] == "O"
         puts "Player 2 Wins!"
         replay()
     .....
    

    You'll also want to put the replay function above the winner function.