Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wrote a tic-tac-toe program in ruby. Everything is working except that the game can't end in a tie. Any suggestions?

Here is my code:

class Game

        def initialize
                @board=Array.new
                @board[1]="1  __|"
                @board[2]="__"
                @board[3]="|__"
                @board[4]="\n2  __|"
                @board[5]="__"
                @board[6]="|__"
                @board[7]="\n3    |"
                @board[8]="  "
                @board[9]="|  "
                @turn="o"
                @win_status = false
        end

        def turn
                @turn
        end

        def show_board
                puts "   1  2  3"
                @board.each do |i|
                        print i
                end
                puts ""
        end

        def set_turn #switches turns
        if @turn == "x"
                @turn = "o"
        else @turn == "o"
                @turn = "x"
        end
        end

        def make_move
                puts "Enter x coordinate"
                x=gets.to_i
                puts "Enter y coordinate"
                y=gets.to_i
if y==1 && x==1 && !@board[1].match(/[xy]/)
        @board[1]="1  _"+@turn+"|"
elsif y==2 && x==1 && !@board[2].match(/[xy]/)
        @board[2]="_"+@turn
elsif  y==3 && x==1 && !@board[3].match(/[xy]/)
        @board[3]="|_"+@turn
elsif y==1 && x==2 && !@board[4].match(/[xy]/)
        @board[4]="\n2  _"+@turn+"|"
elsif y==2 && x==2 && !@board[5].match(/[xy]/)
        @board[5]="_"+@turn
elsif y==3 && x==2 && !@board[6].match(/[xy]/)
        @board[6]="|_"+@turn
elsif y==1 && x==3 && !@board[7].match(/[xy]/)
        @board[7]="\n3   "+@turn+"|"
elsif y==2 && x==3 && !@board[8].match(/[xy]/)
        @board[8]=" "+@turn
elsif y==3 && x==3 && !@board[9].match(/[xy]/)
        @board[9]="| "+@turn+" \n"
else
        puts "You either entered an invalid coordinate or a a taken square. Please enter another coordinate"
        make_move
end

                        end

        def win_combo
                return [[@board[1][4] + @board[2][1] + @board[3][2]], [@board[4][5] + @board[5][1] + @board[6][2]], [@board[7][5] + @board[8][1] + @board[9][2]],[@board[1][4] + @board[4][5] + @board[7][5]], [@board[2][1] + @board[5][1] + @board[8][1]], [@board[3][2] + @board[6][2] + @board[9][2]], [@board[1][4] + @board[5][1] + @board[9][2]], [@board[3][2] + @board[5][1] + @board[7][5]]]
        end

        def check_win
                #if some row or column or diagonal is "xxx" or "ooo" then set @win_status = true
                self.win_combo.each do |arr|
                        str = arr.join
                        if str == "xxx"
                        puts "\nX Wins!"
                        return true
                        elsif str == "ooo"
                        puts "\nO Wins!"
                        return true
                        end
                end
                return false
        end
<pre lang="vb">g = Game.new
while g.check_win != true
        g.show_board
        g.set_turn
        g.make_move
end
end
Posted

1 solution

I think you need to add one more clause to check_win function after
Ruby
if str == "xxx"
    puts "\nX Wins!"
    return true
elsif str == "ooo"
    puts "\nO Wins!"
    return true
else
    #check for empty slots left
    #if empty slot count = 0 
    #    then game draw
    #    return true
    #end
end

My primary language is not ruby so can't write code for that :).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900