本文介绍了我怎么不让正方形被tic-tac-toe覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个tic-tac-toe程序。我编写了代码,以便用户无法覆盖一个正方形。它不会让o覆盖其中一个x方格,但由于某种原因x可以覆盖一个o方格。
这里是我写这段代码的if语句(如果你想要其他程序,请告诉我):
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
推荐答案
这篇关于我怎么不让正方形被tic-tac-toe覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!