samples/05_mouse/01_mouse_click/app/main.rb
def won? piece
    # performs action on all space combinations
    won = [[-1, 0, 1]].product([-1, 0, 1]).map do |xs, y|

      # Checks if the 3 grid spaces with the same y value (or same row) and
      # x values that are next to each other have pieces that belong to the same player.
      # Remember, the value of piece is equal to the current turn (which is the player).
      horizontal_match = state.spaces[xs[0]][y].piece == piece &&
                         state.spaces[xs[1]][y].piece == piece &&
                         state.spaces[xs[2]][y].piece == piece

      # Checks if the 3 grid spaces with the same x value (or same column) and
      # y values that are next to each other have pieces that belong to the same player.
      # The && represents an "and" statement: if even one part of the statement is false,
      # the entire statement evaluates to false.
      vertical_match = state.spaces[y][xs[0]].piece == piece &&
                       state.spaces[y][xs[1]].piece == piece &&
                       state.spaces[y][xs[2]].piece == piece

      horizontal_match || vertical_match # if either is true, true is returned
    end

    # Sees if there is a diagonal match, starting from the bottom left and ending at the top right.
    # Is added to won regardless of whether the statement is true or false.
    won << (state.spaces[-1][-1].piece == piece && # bottom left
            state.spaces[ 0][ 0].piece == piece && # center
            state.spaces[ 1][ 1].piece == piece)   # top right

    # Sees if there is a diagonal match, starting at the bottom right and ending at the top left
    # and is added to won.
    won << (state.spaces[ 1][-1].piece == piece && # bottom right
            state.spaces[ 0][ 0].piece == piece && # center
            state.spaces[-1][ 1].piece == piece)   # top left

    # Any false statements (meaning false diagonal matches) are rejected from won
    won.reject_false.any?
  end
# Determines if a player won by checking if there is a horizontal match or vertical match.
# Horizontal_match and vertical_match have boolean values. If either is true, the game has been won.
Determines if a player won by checking if there is a horizontal match or vertical match.
Determines if a player won by checking if there is a horizontal match or vertical match .
def won? piece
    won = [[-1, 0, 1]].product([-1, 0, 1]).map do |xs, y|
      horizontal_match = state.spaces[xs[0]][y].piece == piece &&
                         state.spaces[xs[1]][y].piece == piece &&
                         state.spaces[xs[2]][y].piece == piece
      vertical_match = state.spaces[y][xs[0]].piece == piece &&
                       state.spaces[y][xs[1]].piece == piece &&
                       state.spaces[y][xs[2]].piece == piece
      horizontal_match || vertical_match 
    end
    won << (state.spaces[-1][-1].piece == piece && 
            state.spaces[ 0][ 0].piece == piece && 
            state.spaces[ 1][ 1].piece == piece)   
    won << (state.spaces[ 1][-1].piece == piece && 
            state.spaces[ 0][ 0].piece == piece && 
            state.spaces[-1][ 1].piece == piece)   
    won.reject_false.any?
  end
def won? piece won = [ [ - 1 , 0 , 1 ] ] . product ( [ - 1 , 0 , 1 ] ) . map do | xs , y | horizontal_match = state . spaces [ xs [ 0 ] ] [ y ] . piece == piece && state . spaces [ xs [ 1 ] ] [ y ] . piece == piece && state . spaces [ xs [ 2 ] ] [ y ] . piece == piece vertical_match = state . spaces [ y ] [ xs [ 0 ] ] . piece == piece && state . spaces [ y ] [ xs [ 1 ] ] . piece == piece && state . spaces [ y ] [ xs [ 2 ] ] . piece == piece horizontal_match || vertical_match end won << ( state . spaces [ - 1 ] [ - 1 ] . piece == piece && state . spaces [ 0 ] [ 0 ] . piece == piece && state . spaces [ 1 ] [ 1 ] . piece == piece ) won << ( state . spaces [ 1 ] [ - 1 ] . piece == piece && state . spaces [ 0 ] [ 0 ] . piece == piece && state . spaces [ - 1 ] [ 1 ] . piece == piece ) won . reject_false . any? end
Determines if a player won by checking if there is a horizontal match or vertical match.
Determines if a player won by checking if there is a horizontal match or vertical match .
# performs action on all space combinations # Checks if the 3 grid spaces with the same y value (or same row) and # x values that are next to each other have pieces that belong to the same player. # Remember, the value of piece is equal to the current turn (which is the player). # Checks if the 3 grid spaces with the same x value (or same column) and # y values that are next to each other have pieces that belong to the same player. # The && represents an "and" statement: if even one part of the statement is false, # the entire statement evaluates to false. # if either is true, true is returned # Sees if there is a diagonal match, starting from the bottom left and ending at the top right. # Is added to won regardless of whether the statement is true or false. # bottom left # center # top right # Sees if there is a diagonal match, starting at the bottom right and ending at the top left # and is added to won. # bottom right # center # top left # Any false statements (meaning false diagonal matches) are rejected from won
piece