Ruby:当设置为整数时,变量充当nul

时间:2015-05-05 15:59:16

标签: ruby variables

在尝试提供更具信息性的标题10分钟之后,我的想法用完了。

我有2个问题。首先,每当我尝试输入pos_in的位置时,它会在nul语句中运行时注册为case,该语句确定该转弯的m_index。 第二,每当AI转弯时,总是选择1 1

我的问题是:有什么方法可以解决这个问题吗?我试过切换输入类型无济于事,我完全不知道该做什么。

我的代码:

$moves = ["x", "o"]
$move = 1
m_index = 0
$spots = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
possiblesx = (1..3)
possiblesy = (1..3)
def board
    puts """
TicTacToe 1.5 by Touka

        #{$spots[0]}|#{$spots[1]}|#{$spots[2]}
        -----
        #{$spots[3]}|#{$spots[4]}|#{$spots[5]}
        -----
        #{$spots[6]}|#{$spots[7]}|#{$spots[8]}
move: #{$c_move}
"""
end
def ai_move
    if $spots[3, 6] == "x" || $spots[4, 8] == "x" || $spots[1, 2] == "x"
        pos_in = "1 1"
    elsif $spots[1, 3] == "x" || $spots[4, 7] == "x"
        pos_in = "1 2"
    elsif $spots[0, 1] == "x" || $spots[6, 4] == "x" || $spots[5, 8] == "x"
        pos_in = "1 3"
    elsif $spots[0, 6] == "x" || $spots[4, 5] == "x"
        pos_in = "2 1"
    elsif $spots[0, 8] == "x" || $spots[1, 7] == "x" || $spots[2, 6] == "x" || $spots[5, 3] == "x"
        pos_in = "2 2"
    elsif $spots[2, 8] == "x" || $spots[3, 4] == "x"
        pos_in = "2 3"
    elsif $spots[0, 3] == "x" || $spots[2, 4] == "x" || $spots[7, 8] == "x"
        pos_in = "3 1"
    elsif $spots[1, 4] == "x" || $spots[6, 8] == "x"
        pos_in = "3 2"
    elsif $spots[6, 7] == "x" || $spots[0, 4] == "x" || $spots[2, 5] == "x"
        pos_in = "3 3"
    else
        aimx = possiblesx.to_a.sample
        aimy = possiblesy.to_a.sample
        pos_in = "#{aimx} #{aimy}"
    end 
end
def game
    system "cls"
    if $move == 1
        $move = 0
    else
        $move = 1
    end
    $c_move = $moves[$move]
    board
    if $opp == 2 && $c_move == "o"
        ai_move
    else
        puts "Enter move coordinates (ex \"1 1\"):"
        pos_in = gets.chomp
    end
    case pos_in
    when ["1 1"]
        m_index = 0
    when ["1 2"]
        m_index = 1
    when ["1 3"]
        m_index = 2
    when ["2 1"]
        m_index = 3
    when ["2 2"]
        m_index = 4
    when ["2 3"]
        m_index = 5
    when ["3 1"]
        m_index = 6
    when ["3 2"]
        m_index = 7
    when ["3 3"]
        m_index = 8
    end
    if $spots[m_index] == " "
        $spots[m_index] = "#{$c_move}"
    else
        if $opp == 2 && $c_move == "o"
            $move = $c_move
            game
        end
        system "cls"
        puts "

        Error.
    Re-enter $move."
        sleep(3)
        system "cls"
        $move = c_$move
        game
    end
    system "cls"
    game
end
puts "Play vs. [1]Friend or [2]AI"
$opp = gets.to_i
game

3 个答案:

答案 0 :(得分:1)

您指定

pos_in = "1 1"

作为String,在你的case语句中检查数组

case pos_in
when ["1 1"]

答案 1 :(得分:1)

你遇到的问题是你的范围,以及你试图将字符串与数组匹配的事实,正如Piotr Kruczek指出的那样。

pos_in变量设为全局变量,并将数组输入中的when语句更改为字符串。

以下是您的代码的工作版本:

$moves = ["x", "o"]
$move = 1
m_index = 0
$spots = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
$pos_in = nil

def board
    puts """
TicTacToe 1.5 by Touka

        #{$spots[0]}|#{$spots[1]}|#{$spots[2]}
        -----
        #{$spots[3]}|#{$spots[4]}|#{$spots[5]}
        -----
        #{$spots[6]}|#{$spots[7]}|#{$spots[8]}
move: #{$c_move}
"""
end
def ai_move
    if $spots[3, 6] == "x" || $spots[4, 8] == "x" || $spots[1, 2] == "x"
        $pos_in = "1 1"
    elsif $spots[1, 3] == "x" || $spots[4, 7] == "x"
        $pos_in = "1 2"
    elsif $spots[0, 1] == "x" || $spots[6, 4] == "x" || $spots[5, 8] == "x"
        $pos_in = "1 3"
    elsif $spots[0, 6] == "x" || $spots[4, 5] == "x"
        $pos_in = "2 1"
    elsif $spots[0, 8] == "x" || $spots[1, 7] == "x" || $spots[2, 6] == "x" || $spots[5, 3] == "x"
        $pos_in = "2 2"
    elsif $spots[2, 8] == "x" || $spots[3, 4] == "x"
        $pos_in = "2 3"
    elsif $spots[0, 3] == "x" || $spots[2, 4] == "x" || $spots[7, 8] == "x"
        $pos_in = "3 1"
    elsif $spots[1, 4] == "x" || $spots[6, 8] == "x"
        $pos_in = "3 2"
    elsif $spots[6, 7] == "x" || $spots[0, 4] == "x" || $spots[2, 5] == "x"
        $pos_in = "3 3"
    else
        aimx = (1..3).to_a.sample
        aimy = (1..3).to_a.sample
        $pos_in = "#{aimx} #{aimy}"
    end 
end
def game
  m_index = nil
    system "cls"
    if $move == 1
        $move = 0
    else
        $move = 1
    end
    $c_move = $moves[$move]
    board
    if $opp == 2 && $c_move == "o"
        ai_move
    else
        puts "Enter move coordinates (ex \"1 1\"):"
        $pos_in = gets.chomp
    end
    case $pos_in
    when "1 1"
        p m_index = 0
    when "1 2"
        p m_index = 1
    when "1 3"
        p m_index = 2
    when "2 1"
        p m_index = 3
    when "2 2"
        p m_index = 4
    when "2 3"
        p m_index = 5
    when "3 1"
        p m_index = 6
    when "3 2"
        p m_index = 7
    when "3 3"
        p m_index = 8
    end
    if $spots[m_index] == " "
        $spots[m_index] = "#{$c_move}"
    else
        if $opp == 2 && $c_move == "o"
            $move = $c_move
            game
        end
        system "cls"
        puts "

        Error.
    Re-enter $move."
        sleep(3)
        system "cls"
        $move = c_$move
        game
    end
    system "cls"
    game
end
puts "Play vs. [1]Friend or [2]AI"
$opp = gets.to_i
game

答案 2 :(得分:0)

正如Piotr和Menelik所说,这里有一些明显的错误。奇怪的是,ai_move的结构就是其中之一。工作代码如下。这不是最干净的东西,并且有一些错误需要解决,但它确实有效。

$moves = ["x", "o"]
$move = 1
m_index = 0
$spots = Array.new(9, " ")
$possiblesx = [1, 2, 3]
$possiblesy = [1, 2, 3]
def board
    puts """
TicTacToe 1.5 by Touka

        #{$spots[0]}|#{$spots[1]}|#{$spots[2]}
        -----
        #{$spots[3]}|#{$spots[4]}|#{$spots[5]}
        -----
        #{$spots[6]}|#{$spots[7]}|#{$spots[8]}
move: #{$c_move}
"""
end
def ai_move
    if $spots[3] == "x" && $spots[6] == "x"
        $pos_in = "1 1"
    elsif $spots[4] == "x" && $spots[8] == "x"
        $pos_in = "1 1"
    elsif $spots[1] == "x" && $spots[2] == "x"
        $pos_in = "1 1"
    elsif $spots[1] == "x" && $spots[3] == "x"
        $pos_in = "1 2"
    elsif $spots[4] == "x" && $spots[7] == "x"
        $pos_in = "1 2"
    elsif $spots[0] == "x" && $spots[1] == "x"
        $pos_in = "1 3"
    elsif $spots[6] == "x" && $spots[4] == "x"
        $pos_in = "1 3"
    elsif $spots[5] == "x" && $spots[8] == "x"
        $pos_in = "1 3"
    elsif $spots[0] == "x" && $spots[6] == "x"
        $pos_in = "2 1"
    elsif $spots[4] == "x" && $spots[5] == "x"
        $pos_in = "2 1"
    elsif $spots[0] == "x" && $spots[8] == "x"
        $pos_in = "2 2"
    elsif $spots[1] == "x" && $spots[7] == "x"
        $pos_in = "2 2"
    elsif $spots[2] == "x" && $spots[6] == "x"
        $pos_in = "2 2"
    elsif $spots[5] == "x" && $spots[3] == "x"
        $pos_in = "2 2"
    elsif $spots[2] == "x" && $spots[8] == "x"
        $pos_in = "2 3"
    elsif $spots[3] == "x" && $spots[4] == "x"
        $pos_in = "2 3"
    elsif $spots[0] == "x" && $spots[3] == "x"
        $pos_in = "3 1"
    elsif $spots[2] == "x" && $spots[4] == "x"
        $pos_in = "3 1"
    elsif $spots[7] == "x" && $spots[8] == "x"
        $pos_in = "3 1"
    elsif $spots[1] == "x" && $spots[4] == "x"
        $pos_in = "3 2"
    elsif $spots[6] == "x" && $spots[8] == "x"
        $pos_in = "3 2"
    elsif $spots[6] == "x" && $spots[7] == "x"
        $pos_in = "3 3"
    elsif $spots[0] == "x" && $spots[4] == "x"
        $pos_in = "3 3"
    elsif $spots[2] == "x" && $spots[5] == "x"
        $pos_in = "3 3"
    else
        aimx = $possiblesx.sample
        aimy = $possiblesy.sample
        $pos_in = "#{aimx} #{aimy}"
    end 
end
def game
    system "cls"
    if $move == 1
        $move = 0
    else
        $move = 1
    end
    $c_move = $moves[$move]
    board
    if $opp == 2 && $c_move == "o"
        ai_move
    else
        puts "Enter move coordinates (ex \"1 1\"):"
        $pos_in = gets.chomp
    end
    case $pos_in
    when "1 1"
        m_index = 0
    when "1 2"
        m_index = 1
    when "1 3"
        m_index = 2
    when "2 1"
        m_index = 3
    when "2 2"
        m_index = 4
    when "2 3"
        m_index = 5
    when "3 1"
        m_index = 6
    when "3 2"
        m_index = 7
    when "3 3"
        m_index = 8
    end
    if $spots[m_index] == " "
        $spots[m_index] = "#{$c_move}"
    else
        if $opp == 2 && $c_move == "o"
            $move = $c_move
            game
        end
        system "cls"
        puts "

        Error.
    Re-enter move."
        sleep(3)
        system "cls"
        $move = $c_move
        game
    end
    system "cls"
    game
end
puts "Play vs. [1]Friend [2]AI"
$opp = gets.to_i
game