Ruby on rails 如何将数字添加到tic tac toe程序的顶部?

Ruby on rails 如何将数字添加到tic tac toe程序的顶部?,ruby-on-rails,ruby,class,tic-tac-toe,Ruby On Rails,Ruby,Class,Tic Tac Toe,我写了一个tic-tac-toe程序。我制作的董事会如下所示: 1 __|__|__ 2 __|__|__ 3 | | 我设置坐标的方法是,第一个框是(1,1),第二个框是(1,2),第三个框是(1,3),第四个框是(2,1),等等。我在侧面写了1,2和3,以向用户说明这一点。我还想将1、2、3添加到板的顶部。我需要一些关于如何做这件事的帮助 这是我的密码: class Game def initialize @board=Ar

我写了一个tic-tac-toe程序。我制作的董事会如下所示:

1  __|__|__
2  __|__|__
3    |  |  
我设置坐标的方法是,第一个框是(1,1),第二个框是(1,2),第三个框是(1,3),第四个框是(2,1),等等。我在侧面写了1,2和3,以向用户说明这一点。我还想将1、2、3添加到板的顶部。我需要一些关于如何做这件事的帮助

这是我的密码:

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="x"
                @win_status = false
        end

        def turn
                @turn
        end

        def show_board
                @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
@board[1]="1  _"+@turn+"|"   if y==1 && x==1
@board[2]="_"+@turn    if y==2 && x==1
@board[3]="|_"+@turn   if y==3 && x==1
@board[4]="\n_"+@turn+"|" if y==1 && x==2
@board[5]="_"+@turn    if y==2 && x==2
@board[6]="|_"+@turn   if y==3 && x==2
@board[7]="\n "+@turn+"|" if y==1 && x==3
@board[8]=" "+@turn    if y==2 && x==3
@board[9]="|"+@turn+" \n"    if y==3 && x==3
        end
def win_combo
                return [[@board[1][1] + @board[2][1] + @board[3][2]], [@board[4][2] + @board[5][1] + @board[6][2]], [@board[7][1] + @board[8][1] + @board[9][1]],[@board[1][1] + @board[4][2] + @board[7][1]], [@board[2][1] + @board[5][1] + @board[8][1]], [@board[3][2] + @board[6][2] + @board[9][1]], [@board[1][1] + @board[5][1] + @board[9][1]], [@board[3][2] + @board[5][1] + @board[7][1]]]
        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" or str == "ooo"
                                return true
                        end
                end
                return false
        end
end


g = Game.new
while g.check_win != true
        g.show_board
        g.set_turn
        g.make_move
end
puts "won"

show_board
功能中,您可以在那里打印
1 2 3

def show_board
    puts "   1 2 3"
    @board.each do |i|
        print i
    end
    puts ""
或者您可以将其作为
初始化
移动
块的一部分

@board[1]="   1 2 3\n" + "1  __|"

旁注:在
show_board
函数中,而不是
@board.each。。结束
块,您可以使用

@board.each {|i| print i}

既然我们正在提出建议,为什么不把makemove方法也改成这样呢

 def make_move(*coords)
       case coords
         when [1,1]    
           @board[1]="1  _"+@turn+"|"
         when [2,1]
           @board[2]="_"+@turn
         when [3,1]
           @board[3]="|_"+@turn
         #...... you get the point 
end
然后把球移到这里

g = Game.new
while g.check_win != true
    g.show_board
    g.set_turn
    putc "Enter x coordinate: "
    x=gets.to_i
    putc "Enter y coordinate: "
    y=gets.to_i
    g.make_move(y,x)
end

最好将存储数据的方式与显示数据的方式分开。请注意,每次您想要处理这些数据时,都必须将其从字符串中提取出来,以及演示细节是如何进入每个方法的,因此您不必更改所有内容就无法更改它们。