Epic - Tic Tac Toe

N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If 3 consecutive samecolor found, that color will get 1 point. So if 4 red are vertically then pointis 2. Find the winner.

 

def tic_tac_toe(board,n)

  red, black =0, 0

  n.times do |i|

    n.times do |j|

      if board[i][j]

        red += 1 if i<m-2 and board[i+1][j] and board[i+2][j]

        red += 1 if j<n-2 and board[i][j+1] and board[i][j+2]

        red += 1 if i<m-2 and j<n-2 and board[i+1][j+1] and board[i+2][j+2]

        red += 1 if i>=2 and j>=2 and board[i-1][j-1] and board[i-2][j-2]

      else

        black += 1 if i<m-2 and !board[i+1][j] and !board[i+2][j]

        black += 1 if j<n-2 and !board[i][j+1] and !board[i][j+2]

        black += 1 if i<m-2 and j<n-2 and !board[i+1][j+1] and !board[i+2][j+2]

        black += 1 if i>=2 and j>=2 and !board[i-1][j-1] and !board[i-2][j-2]

      end

    end

  end

  red > black

end

 

你可能感兴趣的:(c)