【问题标题】:How to I add numbers to the top of my tic tac toe program?如何在井字游戏顶部添加数字?
【发布时间】:2013-11-17 23:44:01
【问题描述】:

我写了一个井字游戏程序。我做的板子是这样的:

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"

【问题讨论】:

  • 最好将存储数据的方式与呈现数据的方式分开。请注意,每次您要处理该数据时,都必须将其从字符串中提取出来,以及演示详细信息是如何进入每个方法的,因此您无法在不更改所有内容的情况下更改它们。

标签: ruby-on-rails ruby class tic-tac-toe


【解决方案1】:

在您的show_board 函数中,您可以在那里打印1 2 3

def show_board
    puts "   1 2 3"
    @board.each do |i|
        print i
    end
    puts ""

或者您可以将其作为initializemake_move 块的一部分作为

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

旁注:在show_board 函数内,您可以使用@board.each .. end 块而不是

@board.each {|i| print i}

【讨论】:

    【解决方案2】:

    既然我们正在提出建议,为什么不将 make move 方法也更改为类似的方法

     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
    

    然后把puts移到这里

    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
    

    【讨论】:

      猜你喜欢
      • 2019-05-14
      • 1970-01-01
      • 2015-06-12
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多