문제

I wrote a tic-tac-toe program which is user vs user. There is s while loop in the code which makes it run over and over for turns. However, this loop doesn't break when someone wins. Any suggestions?

Here is My code:

   class Game

        def initialize
                @board=Array.new
                @board[1]="__|"
                @board[2]="__"
                @board[3]="|__"
                @board[4]="\n__|"
                @board[5]="__"
                @board[6]="|__"
                @board[7]="\n  |"
                @board[8]="  "
                @board[9]="|  "
                @turn="x"
                @win_status = false
        end


        def win_status
                return @win_status
        end

        def show_board
                @board.each do |i|
                        print i
                end
        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]="_"+@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
@win_combo = [*[@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
                @win_combo.each do |str|
                        if str == "xxx" or str == "ooo"
                                @win_status = true

                        end
                end
                puts @win_status
        end
end


g = Game.new
while g.win_status != true
g.set_turn
g.make_move
g.show_board
g.win_combo
g.check_win
end
도움이 되었습니까?

해결책

You don't check if you won (check_win) and you don't change @win_combo

while g.win_status != true
    #puts g.check_win
    g.set_turn
    g.make_move
    g.show_board
    g.win_combo #this line
    g.check_win #and this line
end

And in your @win_combo suppose to be array of strings not array of arrays of strings. For example, your old code:

[["ooo"], ["_xx"], ["   "], ["o_ "], ["ox "], ["ox "], ["ox "], ["ox "]]

new one:

["ooo", ["_x_"], ["   "], ["o_ "], ["ox "], ["o_ "], ["ox "], ["ox "]]

Just add * ad the beginning(as I did here) or just don't make arrays:

@win_combo = [*[@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]]]

Edit:

Ok, * applied only to the first array, my bad. This is a correct version. You can see where I've put () instead of []; By this I made array of strings.

Additionally you had you have checked wrong position on following combinations.

 @win_combo = [
                (@board[1][1] + @board[2][1] + @board[3][2]), # ok
                (@board[4][2] + @board[5][1] + @board[6][2]), # ok
                (@board[7][2] + @board[8][1] + @board[9][1]), # this # [7][2] not [7][1]
                (@board[1][1] + @board[4][2] + @board[7][2]), # this # [4][2] not [4][1]; [7][2]
                (@board[2][1] + @board[5][1] + @board[8][1]), # ok
                (@board[3][2] + @board[6][2] + @board[9][1]), # ok
                (@board[1][1] + @board[5][1] + @board[9][1]), # ok
                (@board[3][2] + @board[5][1] + @board[7][2])  # this [7][2] not [7][1]
               ]

       puts "\n#@win_combo\n"
       @win_combo
  end

ps. puts "\n#@win_combo\n" is for debuging your application. Delete it in release version.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top