条件运算符未给出正确的结果

时间:2018-11-08 21:30:18

标签: ruby

我正在尝试在控制台中开发战舰游戏。我需要输入猜猜游戏在哪里的坐标。我做到了:

#array1 and array2 are not obvious boards of player1 and player2 respectly 
array1 = [ ['*', 'A', 'B', 'C', 'D', 'E'],['1',0, 1, 1, 1, 0], ['2',1, 0, 0, 0, 0], ['3',1, 0, 1, 0, 0], ['4',0, 0, 1, 0, 1], ['5',0, 0, 0, 0, 0] ]
array2 = [ ['*', 'A', 'B', 'C', 'D', 'E'],['1',1, 0, 1, 1, 0], ['2',0, 0, 0, 0, 1], ['3',0, 1, 0, 0, 1], ['4',0, 1, 0, 0, 1], ['5',0, 0, 0, 0, 0] ]

#arr1 and arr2 are obvious boards of player1 and player2 respectly
arr1 = [['*', 'A', 'B', 'C', 'D', 'E'],['1','.','.','.','.','.'],['2','.','.','.','.','.'],['3','.','.','.','.','.'],['4','.','.','.','.','.'],['5','.','.','.','.','.']]
arr2 = [['*', 'A', 'B', 'C', 'D', 'E'],['1', '.','.','.','.','.'],['2', '.','.','.','.','.'],['3', '.','.','.','.','.'],['4', '.','.','.','.','.'],['5', '.','.','.','.','.']]
coor = ['A', 'B', 'C', 'D', 'E']

#board1 and board2 are the final boards of player1 and player2
#this arrays will be seen during game 
#every changes will be seen on these boards
board1 = arr1.map { |x| x.join(' ') }
board2 = arr2.map { |x| x.join(' ') }

#num1 and num2 are the numbers of the parts of ships respectively
num1 = 8
num2 = 8

#count1 and count2 are the numbers of poped ship parts respectively
count1 = 0
count2 = 0

#Starting of the game and the printing the board 
#If we type "start" game will be started
#"Reset" game will be ended
#If we type words except "start" or "reset" program will ask "Do you want to start? (start/reset)" again
while true do
    puts "Welcome to the game!!!"
    puts "Do you want to start? (start/reset):"
    respond = gets.chomp
    if respond == 'start'
        puts "\n"
        puts "Player ONE"
        puts board1
        puts "\n"
        puts "Player TWO"
        puts board2

        while true do
            #Burada while ile player1 in shertleri olacaq 
            while true do 
                puts "\n"
                puts "Turn - Player ONE"
                puts "Enter coordinate: " 
                a = gets.chomp
                a1 = a.split('')
                b = coor.index(a1[0]) +1
                col1 = b
                row1= a1[1].to_i
                if a == '""'
                    puts "You have to enter any coordinate"
                    break
                elsif array1[row1][col1] == 'X' or array1[row1][col1] == '0'
                    puts "You have already bumped this coordinate!"
                elsif col1<1 or col1>5 or row1>5 or row1<1
                    puts "This coordinate is out of board"
                else
                    if array1[row1][col1] == 1
                        count1 = count1 + 1
                        arr1[row1][col1] = "X"
                    elsif array1[row1][col1] == 0
                        arr1[row1][col1] = "0"
                    end
                    board1 = arr1.map { |x| x.join(' ') }        
                    puts "\n"
                    puts "Player ONE"
                    puts board1
                    puts "\n"
                    puts "Player TWO"
                    puts board2

                    if count1 == num1
                        puts "Player ONE won the game!"
                        break
                    end
                end
                break
            end


            while true do 
                #Burada while ile player2 in shertleri olacaq 
                puts "\n"
                puts "Turn - Player TWO"
                puts "Enter coordinate: " 
                c = gets.chomp
                c1 = c.split('')
                d = coor.index(c1[0]) + 1
                col2 = d
                row2= c1[1].to_i
                if c == '""'
                    puts "You have to enter any coordinate"
                    break
                elsif array2[row2][col2] == 'X' or array2[row2][col2] == '0'
                    puts "You have already bumped this coordinate!"
                elsif col2<1 or col2>5 or row2>5 or row2<1
                    puts "This coordinate is out of board"
                else
                    if array2[row2][col2] == 1
                        count2 = count2 + 1
                        arr2[row2][col2] = "X"
                    elsif array2[row2][col2] == 0
                        arr2[row2][col2] = "0"
                    end
                    board2 = arr2.map { |x| x.join(' ') } 
                    puts "Player ONE"
                    puts board1
                    puts "\n"
                    puts "Player TWO"
                    puts board2

                    if count2 == num2
                        puts "Player TWO won the game!"
                        break
                    end
                end
                break
            end
        end

    elsif respond == 'reset'
        puts "You are off the game"
        break
    else
        puts "\n"
        puts "Answer can be only {start} or {reset}"
    end
end

有一些问题,我在代码中添加了if elsif else条件来解决它们。其中一个用于在板外输入坐标,第二个用于不输入任何坐标,最后一个用于凹凸坐标。如果绕过这三个条件,则玩家可以输入任何坐标。

但是这些代码不起作用。当我检查这三个条件时,结果是一个错误。谁能告诉我这些情况是什么问题?他们为什么不给出合适的结果?

1 个答案:

答案 0 :(得分:0)

输入A8作为值时,您得到的错误是:

battleship.rb:54:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)

这是第54行:

elsif array1[row1][col1] == 'X' or array1[row1][col1] == '0'

在这种情况下,错误是array1 [“ 8”]为nil,而nil [“ A”]未定义。一种简单的解决方法是将第56行移到该行上方(该行用于检查输入是否确实在电路板范围内)。

elsif col1<1 or col1>5 or row1>5 or row1<1

如果这样做,则由于8大于5,您将获得“ out of board”消息,而不是错误。


您提到的另一个错误是当您输入空字符串时。在这种情况下,错误是:

battleship.rb:48:in `<main>': undefined method `+' for nil:NilClass (NoMethodError)

第48行说:

b = coor.index(a1[0]) +1

在这种情况下,索引返回nil,并且您不能将1加到nil。

解决此问题的一种方法是将该部分向下移动几行,以检查a是否为空白到第48行。