문제

I am writing a tic-tac-toe program. Right now when someone gets three in a row, the game just ends. I would like to let the user know whether X or O won the game before it ends. Any suggestions?

Here is my code:

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
                puts "  1   2   3"
                @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]="\n2  _"+@turn+"|" if y==1 && x==2
@board[5]="_"+@turn    if y==2 && x==2
@board[6]="|_"+@turn   if y==3 && x==2
@board[7]="\n3   "+@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
도움이 되었습니까?

해결책

I recommend that you create a Board class that knows how to draw itself based on the turns. This would remove a lot of the code from Game and make it easier to follow.

To answer your question, the code you have here:

                    if str == "xxx" or str == "ooo"
                            return true
                    end

doesn't distinguish between X winning or O winning. Here you can modify the code to check separately for X win and O win, and display the appropriate message.

다른 팁

There are a lot of things you could do. One way might be to change the check_win method to return a string. For example it could return "X", "O", or "none". Then use

while g.check_win.eql?("none")
 ...
end
puts g.check_win + " WINS!"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top