Cyzanfar
Cyzanfar

Reputation: 7136

Tic Tac Toe game logic not working

This is my second post about this "code challenge" I am working on.

Here is the code for my Tic Tac Toe game written in Ruby:

class Morpion

   def initialize
      create_grid
      get_player
      show_grid
   end
   def get_player
      puts "Let play some Tic Tac Toe"
      puts ""
      @player1 ='X'
      @player2='O'
          puts ""
          puts "Where would you like to move? (check out the grid below and type any number 1-9 to place your symbol): "
          puts " 1 | 2 | 3 "
          puts "---+---+---"
          puts " 4 | 5 | 6 "
          puts "---+---+---"
          puts " 7 | 8 | 9 "
   end

   def create_grid
        @grid = {
            '1' => ' ',
            '2' => ' ',
            '3' => ' ',
            '4' => ' ',
            '5' => ' ',
            '6' => ' ',
            '7' => ' ',
            '8' => ' ',
            '9' => ' '
        }
   end

    def show_grid
        puts ""
        puts "#{@grid['1']}|#{@grid['2']}|#{@grid['3']}"
        puts "-----"
        puts "#{@grid['4']}|#{@grid['5']}|#{@grid['6']}"
        puts "-----"
        puts "#{@grid['7']}|#{@grid['8']}|#{@grid['9']}"
        puts ""
    end

   def play
       number_turns=1
       while number_turns < 10
        number_turns.odd? ? player_turn(@player1) : player_turn(@player2)
        # game_checker
        if game_checker
          break
        end
        number_turns+=1
       end  
   end

   def player_turn(player)
        puts player == 'X' ? "It's X's turn!" : "It's O's turn!"
        puts ""
        cell = gets.chomp
        unless @grid.keys.include?(cell) #check if the user entered a number corresponding to the grid
            puts ""
            puts "it has to be a number from 1 to 9"
            player_turn(player)
        end
        if @grid[cell] == ' ' #check if cell in grid is empty for user input
            @grid[cell] = player
        else
            puts ""
            puts "That cell is occupied. Choose again!"
            player_turn(player)
        end
        show_grid
   end

   def game_checker
        end_game = false
      if @grid['1'] != ' ' && @grid['5'] != ' ' && @grid['9'] != ' '
        if (@grid['1'] == @grid['2'] && @grid['1'] == @grid['3'])
            end_game=true
            victory=@grid['1']
        elsif (@grid['4'] == @grid['5'] && @grid['4'] == @grid['6'])
            end_game=true
            victory=@grid['4']
        elsif (@grid['7'] == @grid['8'] && @grid['7'] == @grid['9'])
            end_game=true
            victory=@grid['7']
        elsif (@grid['1'] == @grid['4'] && @grid['1'] == @grid['7'])
            end_game=true
            victory=@grid['1']
        elsif (@grid['2'] == @grid['5'] && @grid['2'] == @grid['8'])
            end_game=true
            victory=@grid['2']
        elsif (@grid['3'] == @grid['6'] && @grid['3'] == @grid['9'])
            end_game=true
            victory=@grid['3']
        elsif (@grid['1'] == @grid['5'] && @grid['1'] == @grid['9'])
            end_game=true
            victory=@grid['1']
        elsif (@grid['7'] == @grid['5'] && @grid['7'] == @grid['3'])
            end_game=true
            victory=@grid['7']
        else
            end_game = false
        end
      end
        if end_game 
           puts "the victory of this game is #{victory}"
           return true
        end
   end


end


m=Morpion.new
m.play

So here in my game_checker method I am giving some logic to the program so it knows when the game is over and who is the winner: end_game = true. The problem is when I play the game only the diagonal cells @grid['1] == @grid['5] && @grid['1] == @grid['9'] return true and the game ends. For all the other possible winning combination, the game end_game = false return false and therefore the game doesn't stop when a player gets a winning combo.

SOLUTION

I found out what to do for my program to correctly check if there is a winner or not. Below is the corrected game_checker method:

 def game_checker # checking all possible winning combination
        end_game = false
        # condition is: whether cells are equal to each other and not empty!
        if    ((@grid['1'] == @grid['2'] && @grid['1'] == @grid['3']) && (@grid['1'] != ' ' && @grid['2'] != ' ' && @grid['3'] != ' ')) 
            end_game=true
            victory=@grid['1']
        elsif ((@grid['4'] == @grid['5'] && @grid['4'] == @grid['6']) && (@grid['4'] != ' ' && @grid['5'] != ' ' && @grid['6'] != ' '))
            end_game=true
            victory=@grid['4']
        elsif ((@grid['7'] == @grid['8'] && @grid['7'] == @grid['9']) && (@grid['7'] != ' ' && @grid['8'] != ' ' && @grid['9'] != ' '))
            end_game=true
            victory=@grid['7']
        elsif ((@grid['1'] == @grid['4'] && @grid['1'] == @grid['7']) && (@grid['1'] != ' ' && @grid['4'] != ' ' && @grid['7'] != ' '))
            end_game=true
            victory=@grid['1']
        elsif ((@grid['2'] == @grid['5'] && @grid['2'] == @grid['8']) && (@grid['2'] != ' ' && @grid['5'] != ' ' && @grid['8'] != ' '))
            end_game=true
            victory=@grid['2']
        elsif ((@grid['3'] == @grid['6'] && @grid['3'] == @grid['9']) && (@grid['3'] != ' ' && @grid['6'] != ' ' && @grid['9'] != ' '))
            end_game=true
            victory=@grid['3']
        elsif ((@grid['1'] == @grid['5'] && @grid['1'] == @grid['9']) && (@grid['1'] != ' ' && @grid['5'] != ' ' && @grid['9'] != ' '))
            end_game=true
            victory=@grid['1']
        elsif ((@grid['7'] == @grid['5'] && @grid['7'] == @grid['3']) && (@grid['7'] != ' ' && @grid['5'] != ' ' && @grid['3'] != ' '))
            end_game=true
            victory=@grid['7']
        else
            end_game=false
        end

Upvotes: 0

Views: 278

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 993901

The following seems out of place:

  if @grid['1'] != ' ' && @grid['5'] != ' ' && @grid['9'] != ' '

Here you're checking to see whether the whole top-left-bottom-right diagonal is filled in, but that if statement covers all your victory checking cases. None of them will be checked unless that diagonal is completely filled in.

You do want to check, of course, that each line of cells you're checking isn't all blank, so you've got a bit more work to do in the game_checker function.

Upvotes: 2

Related Questions