Search code examples
ruby-on-railsrubyruby-on-rails-3classtic-tac-toe

How can I make a turn stay in my tic-tac-toe game?


I am writing a tic-tac-toe program. This program is hot seat type game (user v user). I wrote the part of the program which prints an x anywhere on the board, but i need help on making the move stay. Right now it puts an x where you want it on the board, but it doesn't save for the next move.

Here is the code (I'm not done with the rest of the program):

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"
  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
    if y==1

      if y==1
        if x==1
          @board[1]="_x|"
        end
      end

      if y==2
        if x==1
          @board[2]="_x"
        end
      end

      if y==3
        if x==1
          @board[3]="|x_"
        end
      end

      if y==1
        if x==2
          @board[4]="\n_x|"
        end
      end

      if y==2
        if x==2
          @board[5]="_x"
        end
      end

      if y==3
        if x==2
          @board[6]="|x_"
        end
      end

      if y==1
        if x==3
          @board[7]="\nx |"
        end
      end



      if y==2
        if x==3
          @board[8]="x "
        end
      end
    end

    if y==3
      if x==3
        @board[9]="|x"
      end
    end


  end

  def check_win

  end
end


class Square
  def set_value

  end

  def value

  end

  def initialize
    @value
  end
end

g=Game.new
g.show_board
g.make_move
g.show_board

Solution

  • An aside: So this would probably be a good exercise to start yourself tdd-ing your work. Write the test for the behavior you want to see, watch it fail then make it pass. That will help you drive out a solution.

    Anyway, you have mismatched if/end opening and closing in there right now. That is probably causing you the problems. In make move you have if y==1 twice.

    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"
      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
    
        # if y==1
        if y==1
          if x==1
            @board[1]="_x|"
          end
        end
    
        if y==2
          if x==1
            @board[2]="_x"
          end
        end
    
        if y==3
          if x==1
            @board[3]="|x_"
          end
        end
    
        if y==1
          if x==2
            @board[4]="\n_x|"
          end
        end
    
        if y==2
          if x==2
            @board[5]="_x"
          end
        end
    
        if y==3
          if x==2
            @board[6]="|x_"
          end
        end
    
        if y==1
          if x==3
            @board[7]="\nx |"
          end
        end
    
        if y==2
          if x==3
            @board[8]="x "
          end
        end
    
        if y==3
          if x==3
            @board[9]="|x"
          end
        end
      end
    
      def check_win
      end
    end
    
    class Square
      def set_value
      end
    
      def value
      end
    
      def initialize
        @value
      end
    end
    

    Fixed and working :-)

    Also - this construction would make it easier to see those if/end mismatches:

    def make_move
      puts "Enter x coordinate"
      x=gets.to_i
    
      puts "Enter y coordinate"
      y=gets.to_i
    
      @board[1]="_x|"   if y==1 && x==1
      @board[2]="_x"    if y==2 && x==1
      @board[3]="|x_"   if y==3 && x==1
      @board[4]="\n_x|" if y==1 && x==2
      @board[5]="_x"    if y==2 && x==2
      @board[6]="|x_"   if y==3 && x==2
      @board[7]="\nx |" if y==1 && x==3
      @board[8]="x "    if y==2 && x==3
      @board[9]="|x"    if y==3 && x==3
    end