You are just returning the string: "You entered an invalid coordinate".
I suspect that you want to display it using:
puts "You entered an invalid coordinate"
Otherwise it is passed as the result of g.make_move
and then ignored.
문제
I wrote a tic-tac-toe program. The problem I am experiencing is that in my if
statement, which allows the user enter his/her desired coordinate, my else
condition is not working. The else
condition is in place in case the user enters a coordinate not on the board.
This 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]="1 _"+@turn+"|"
elsif y==2 && x==1
@board[2]="_"+@turn
elsif y==3 && x==1
@board[3]="|_"+@turn
elsif y==1 && x==2
@board[4]="\n2 _"+@turn+"|"
elsif y==2 && x==2
@board[5]="_"+@turn
elsif y==3 && x==2
@board[6]="|_"+@turn
elsif y==1 && x==3
@board[7]="\n3 "+@turn+"|"
elsif y==2 && x==3
@board[8]=" "+@turn
elsif y==3 && x==3
@board[9]="| "+@turn+" \n"
else
"You entered an invalid coordinate"
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 "X Wins!"
return true
elsif str == "ooo"
puts "O Wins!"
return true
end
end
return false
end
g = Game.new
while g.check_win != true
g.show_board
g.set_turn
g.make_move
end
end
해결책
You are just returning the string: "You entered an invalid coordinate".
I suspect that you want to display it using:
puts "You entered an invalid coordinate"
Otherwise it is passed as the result of g.make_move
and then ignored.
다른 팁
I'm assuming you would like to print: "You entered an invalid coordinate" to the console in the event of an invalid x, y coordinate. You need to add a method to that statement like:
else
puts "You entered an invalid coordinate"
end
Or:
else
abort "You entered an invalid coordinate"
end
It looks you you forgot to use puts
or print
in front of your "You entered an invalid coordinate"
string. As it is currently written it is returned from the method.
In Ruby, the return value of a method is the value returned by the last statement evaluated. For example, both of these methods will return the same value if x=3
:
def square_example(x)
if x ==3
x_squared = 9
end
end
def square_example2(x)
if x == 3
x_squared = 9
end
return x_squared
end
For simplicity of testing you might try using explicit returns so that you can easily tell what it is you are returning from the method. Or (as a beginner with Ruby myself) you could add in a puts
statement with each if/else
result so that you can easily monitor the results of each move and then remove those puts
lines when you know everything is working properly.
Looks like this is a misinterpretation of the site below, but if you're interested in the difference between 'and' and '&&' you should check out the comments below. From: http://www.tutorialspoint.com/ruby/ruby_operators.htm
You will want to use "and" instead of "&&", an example:
if y==1 and x==1
# do move
elsif y==2 and x==1
# do move
.
.
.
else
"invalid coordinate"
end
The "&&" operator will check that the values on either side of it are nonzero. If they are both nonzero then it will return true. In your case it is doing the operation
false && false
Where false != 0, so it returns true.
Here is another discussion of this: http://archive.railsforum.com/viewtopic.php?id=27353