
layout=--全屏框架
{
  LinearLayout;--线性控件
  orientation='vertical';--布局方向
  layout_width='fill';--布局宽度
  layout_height='fill';--布局高度
  gravity='center';--控件内容的重力方向
  {
    GridView,
    numColumns=3,
    layout_height='100%w',
    layout_width='100%w',
    id="grid",
  },
}
activity.setContentView(loadlayout(layout))


import "android.graphics.Paint"
import "com.androlua.LuaDrawable"

w=activity.width

--第一版，待完善

item={
  TextView,
  layout_width=w/3,
  layout_height=w/3,
  TextSize="50",
  Gravity="center",
  id="tv",
}

data={}
adp=LuaAdapter(activity,data,item)
grid.setAdapter(adp)

for i=1,9 do
  table.insert(data,{tv=""})
end

--画个UI框
myLuaDrawable=LuaDrawable(function(mCanvas,mPaint,mDrawable)

  mPaint1 = Paint();
  mPaint1.setStrokeWidth(20)
  mPaint1.setAntiAlias(true)
  mPaint1.setStyle(Paint.Style.STROKE)
  mPaint1.setColor(0x88000000)

  mCanvas.drawLine(0,w/3,w,w/3,mPaint1)
  mCanvas.drawLine(0,w*2/3,w,w*2/3,mPaint1)
  mCanvas.drawLine(w/3,0,w/3,w,mPaint1)
  mCanvas.drawLine(w*2/3,0,w*2/3,w,mPaint1)

end)

grid.background=myLuaDrawable

a=true

grid.onItemClick=function(l,v,p,i)

  --轮流下棋的逻辑
  if v.Text=="" then

    if a==true then
      v.Text="✔"
      a=false
     else
      v.Text="✘"
      a=true
    end
   else
    print("这里下过了,不能再下了哦")
  end

  function 判断三棋连珠(x,y,z)

    if x==y and y==z and x==z then

      if x=="" and y=="" and z=="" then

       elseif x=="✔" and y=="✔" and z=="✔" then
        print("✔胜利,游戏结束")
       elseif x=="✘" and y=="✘" and z=="✘" then
        print("✘胜利,游戏结束")
      end
      --return true
    end
    --return false
  end

  --判断横竖是否相连
  for i=0,8 do

    if i%3 == 0 then
      x=grid.getChildAt(i)
      y=grid.getChildAt(i+1)
      z=grid.getChildAt(i+2)
      判断三棋连珠(x.Text,y.Text,z.Text)
    end

    if i<=2 then
      x=grid.getChildAt(i)
      y=grid.getChildAt(i+3)
      z=grid.getChildAt(i+6)
      判断三棋连珠(x.Text,y.Text,z.Text)
    end
  end

  --判断两条交叉斜线是否相连
  x=grid.getChildAt(0)
  y=grid.getChildAt(4)
  z=grid.getChildAt(8)
  判断三棋连珠(x.Text,y.Text,z.Text)

  x=grid.getChildAt(2)
  y=grid.getChildAt(4)
  z=grid.getChildAt(6)
  判断三棋连珠(x.Text,y.Text,z.Text)
end