
const minInt as integer = &H80000000
const maxInt as integer = &H7FFFFFFF

function Minimum(a() as integer) as integer
  dim result as integer = maxInt
  for x as integer = lbound(a) to ubound(a)
    if a(x) < result then result = a(x)
  next x
  return result
end function

function Maximum(a() as integer) as integer
  dim result as integer = minInt
  for x as integer = lbound(a) to ubound(a)
    if a(x) > result then result = a(x)
  next x
  return result
end function

function NumberOf(a() as integer, n as integer) as integer
  dim result as integer = 0
  for x as integer = lbound(a) to ubound(a)
    if a(x) = n then result += 1
  next x
  return result
end function

function IndexOf(a() as integer, n as integer) as integer
  dim result as integer = 0
  dim x as integer = lbound(a)
  do while (x <= ubound(a)) and (result = 0)
    if a(x) = n then result = x else x += 1
  loop
  return result
end function

type tLine
  as integer x1
  as integer y1
  as integer x2
  as integer y2
  as integer dx
  as integer dy
end type

dim shared lines(24) as tLine

for x as integer = 0 to 24
  read lines(x).x1
  read lines(x).y1
  read lines(x).x2
  read lines(x).y2
  read lines(x).dx
  read lines(x).dy
next x

data 1, 1, 7, 1, 1, 0
data 1, 2, 7, 2, 1, 0
data 1, 3, 7, 3, 1, 0
data 1, 4, 7, 4, 1, 0
data 1, 5, 7, 5, 1, 0
data 1, 6, 7, 6, 1, 0
data 1, 1, 1, 6, 0, 1
data 2, 1, 2, 6, 0, 1
data 3, 1, 3, 6, 0, 1
data 4, 1, 4, 6, 0, 1
data 5, 1, 5, 6, 0, 1
data 6, 1, 6, 6, 0, 1
data 7, 1, 7, 6, 0, 1
data 1, 1, 6, 6, 1, 1
data 1, 2, 5, 6, 1, 1
data 1, 3, 4, 6, 1, 1
data 1, 4, 4, 1, 1,-1
data 1, 5, 5, 1, 1,-1
data 1, 6, 6, 1, 1,-1
data 7, 1, 2, 6,-1, 1
data 7, 2, 3, 6,-1, 1
data 7, 3, 4, 6,-1, 1
data 7, 4, 4, 1,-1,-1
data 7, 5, 3, 1,-1,-1
data 7, 6, 2, 1,-1,-1

const one     = 1
const four    = 4
const six     = 6
const seven   = 7
const nothing = 0
const piece   = 1
const border  = 2
const cBlack  =-1
const cWhite  = 1

enum boolean
  true = -1
  false = not true
end enum

type tGrid
  as integer array(one - 1 to seven + 1, one - 1 to six + 1)
  declare sub Empty
  declare sub Copy(g() as integer)
  declare function IsFull as boolean
  declare function Insert(p as integer, x as integer) as boolean
  declare function LinePoints(l as tLine, p as integer) as integer
  declare function GridPoints(p as integer) as integer
  declare function AsString as string
end type

sub tGrid.Empty
  dim as integer x, y
  for x = one - 1 to seven + 1
    for y = one - 1 to six + 1
      array(x, y) = border
    next y
  next x
  for x = one to seven
    for y = one to six
      array(x, y) = nothing
    next y
  next x
end sub

sub tGrid.Copy(g() as integer)
  for x as integer = one to seven
    for y as integer = one to six
      array(x, y) = g(x, y)
    next y
  next x
end sub

function tGrid.IsFull as boolean
  dim result as boolean = true
  dim x as integer = one
  do while (x <= seven) and result
    if array(x, six) = nothing then result = false
    x += 1
  loop
  return result
end function

function tGrid.Insert(p as integer, x as integer) as boolean
  dim y as integer = six
  dim result as boolean = (array(x, y) = nothing)
  if result then
    do while (array(x, y - 1) = nothing)
      y -= 1
    loop
    array(x, y) = p
  end if
  return result
end function

function tGrid.LinePoints(l as tLine, p as integer) as integer
  dim as integer x, y, n=0, o=0, nmx=0, omx=0, result
  x = l.x1
  y = l.y1
  do while array(x, y) <> border
    if array(x, y) = p then n += 1 else n = 0
    if array(x, y) <> cBlack * p then o += 1 else o = 0
    if n > nmx then nmx = n
    if o > omx then omx = o
    x += l.dx
    y += l.dy
  loop
  if omx < 4 then result = 0 else result = nmx
  return result
end function

function tGrid.GridPoints(p as integer) as integer
  dim aux(24) as integer
  for i as integer = 0 to 24
    aux(i) = LinePoints(lines(i), p)
  next i
  return Maximum(aux())
end function

function tGrid.AsString as string
  dim result as string = ""
  for y as integer = six + 1 to one - 1 step -1
    for x as integer = one - 1 to seven + 1
      select case array(x, y)
        case nothing
          result = result & " "
        case piece * cWhite
          result = result & "W"
        case piece * cBlack
          result = result & "B"
        case border
          result = result & "#"
      end select
    next x
    result = result & chr(13,10)
  next y
  return result
end function

type tEngine
  as integer flag
  as tGrid g1, g2, g3
  as integer a(one to seven)
  as integer b(one to seven, one to seven)
  as integer c(one to seven, one to seven, one to seven)
  declare function BestMove(g as tGrid, p as integer) as integer
end type

function tEngine.BestMove(g as tGrid, p as integer) as integer
  const impossible = -100
  dim as integer i, j, k, result
  dim aux(one to seven) as integer
  g1.Empty
  g2.Empty
  g3.Empty

  for i = one to seven
    g1.Copy(g.array())
    if g1.Insert(p, i) then
      for j = one to seven
        g2.Copy(g1.array())
        if g2.Insert(cBlack * p, j) then
          for k = one to seven
            g3.Copy(g2.array())
            if g3.Insert(p, k) then
              c(i, j, k) = g3.GridPoints(p)
            else
              c(i, j, k) = impossible
            end if
          next k
          b(i, j) = g2.GridPoints(cBlack * p)
        else
          b(i, j) = impossible
        end if
      next j
      a(i) = g1.GridPoints(p)
    else
      a(i) = impossible
    end if
  next i

  i = Maximum(a())
  if i >= four then
    result = IndexOf(a(), i)
    flag = 1
  else
    for i = one to seven
      for j = one to seven
        aux(j) = b(i, j)
      next j
      a(i) -= Maximum(aux())
    next i
    i = Maximum(a())
    if NumberOf(a(), i) <= 3 then
      result = IndexOf(a(), i)
      flag = 2
    else
      for i = one to seven
        for j = one to seven
          for k = one to seven
            aux(k) = c(i, j, k)
          next k
          b(i, j) = Maximum(aux())
        next j
        for j = one to seven
          aux(j) = b(i, j)
        next j
        a(i) += Minimum(aux())
      next i
      i = Maximum(a())
      if NumberOf(a(), i) <= 3 then
        result = IndexOf(a(), i)
        flag = 3
      else
        for i = one to seven
          a(i) -= Abs(i - 4)
        next i
        i = Maximum(a())
        result = IndexOf(a(), i)
        flag = 4
      end if
    end if
  end if

  return result
end function
