Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a tic-tac-toe program. I wrote code so that the user can't overwrite a taken square. It does't let "o" overwrite one of the taken "x" square but for some reason "x" can overwrite a taken "o" square.

Here is the if-statement where I wrote this code (let me know if you want the rest of the program):

 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
Posted
Comments
ZurdoDev 20-Nov-13 10:15am    
Did you write the code or copy it? If you wrote it, do you understand it? I guess I'm confused as to why you can't add in additional logic or understand your own code.

Just debug it.

1 solution

The y in the character class of your regular expression literal looks suspicious.
I dare say it should be replaced with an o.

Regards,
—Manfred
 
Share this answer
 
v2

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