play=nil g=nil cx=1 cy=1 ai={[-1]=false,false} n={[false]='Humain','Ordi (facile)','Ordi (difficile)'} nn={[false]=2,false,1} np={[false]=1,2,false} pn={[-1]='noir','blanc'} b1={-158,-158,-158,-158,-104,-12,80} b2={106,132,161,195,211,211,211} b3={0,32,66,101,140,158,158} b4={54,64,75,87,100,126,167} u=true function c(gc,i) gc:setColorRGB(i,i,i) end function count(t) local c=0 for i=1,8 do for j=1,8 do c=c+t[j][i] end end return c end function offer(t2,x,y,xi,yi) local c=0 while true do x=x+xi y=y+yi if x*y==0 or x==9 or y==9 or t2[y][x]==0 then return false elseif t2[y][x]==-play then c=c+1 else if c>0 then for i=1,c do t2[y-yi*i][x-xi*i]=play end return true else return false end end end end function on.arrowDown() if u==0 then ai[1]=nn[ai[1]] elseif cy<8 then cy=cy+1 end r() end function on.arrowLeft() if u==0 then ai[-1]=nn[ai[-1]] elseif cx>1 then cx=cx-1 end r() end function on.arrowRight() if u==0 then ai[-1]=np[ai[-1]] elseif cx<8 then cx=cx+1 end r() end function on.arrowUp() if u==0 then ai[1]=np[ai[1]] elseif cy>1 then cy=cy-1 end r() end function on.escapeKey() if not u then u=0 else u=true end r() end function on.enterKey() if u==false then local t=test(g,cx,cy) if t then g=t play=-play end elseif u==0 then u=false else u=0 g={{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,1,-1,0,0,0},{0,0,0,-1,1,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}} play=1 end r() end function on.mouseDown(x,y) if u==0 and y<140 then x=x70 then if x then on.arrowDown() else on.arrowUp() end elseif x then on.arrowLeft() else on.arrowRight() end r() else on.enterKey() end end function on.mouseMove(x,y) local wp=platform.window:width()/2 local wl=platform.window:height()/10 if not u then if xwp+wl*4 or ywl*9 then on.tabKey() else cx=math.floor((x-wp)/wl)+5 cy=math.floor(y/wl) r() end end end function on.paint(gc) local ww=platform.window:width() local wh=platform.window:height() local wp=ww/2 local wl=wh/10 if u then c(gc,128) gc:fillRect(0,0,ww,wh) gc:setColorRGB(102,255,102) gc:fillPolygon({wp,54,wp+158,106,wp+158,211,wp-158,211,wp-158,106,wp,54}) gc:setColorRGB(0,51,0) gc:setPen('medium') for i=1,7 do gc:drawLine(wp+b1[i],b2[i],wp+b3[i],b4[i]) gc:drawLine(wp-b1[i],b2[i],wp-b3[i],b4[i]) end gc:setPen('thick') c(gc,0) gc:drawLine(wp-11,140,wp+12,151) gc:drawLine(wp+11,140,wp-12,151) gc:drawLine(wp-13,176,wp+14,189) gc:drawLine(wp+13,176,wp-14,189) c(gc,255) gc:drawLine(wp-53,158,wp-29,169) gc:drawLine(wp+28,158,wp+55,169) gc:drawLine(wp+53,158,wp+29,169) gc:drawLine(wp-28,158,wp-55,169) c(gc,0) gc:setPen('thin') gc:setFont('sansserif','b',6) local i="DEEP THOUGHT" gc:drawString(i,wp-gc:getStringWidth(i)/2,0,'top') gc:setFont('sansserif','b',24) i='Nspire Reversi' gc:drawString(i,wp-gc:getStringWidth(i)/2,10,'top') gc:setFont('sansserif','b',12) if u==0 then gc:setAlpha(170) gc:fillRect(0,0,ww,wh) gc:setAlpha(255) gc:fillArc(wp-wl-40,60-wl,wl,wl,0,360) gc:setAlpha(170) c(gc,255) gc:fillArc(wp-wl*0.9-40,60-wl*0.9,wl/2,wl/2,60,150) gc:setAlpha(255) c(gc,204) gc:fillArc(wp-wl-40,80,wl,wl,0,360) gc:setAlpha(170) c(gc,255) gc:fillArc(wp-wl*0.9-40,wl/10+80,wl/2,wl/2,60,150) gc:setAlpha(255) gc:drawString('< '..n[ai[-1]]..' >',wp-30,60,'bottom') gc:drawString('v '..n[ai[1]]..' ^',wp-30,80,'top') c(gc,0) end else local i=0 if play==1 then i=255 end c(gc,i) gc:fillRect(0,0,ww,wh) c(gc,255-i) if u==nil then i=count(g) if i>0 then i='Les blancs ont gagn\195\169!' elseif i<0 then i='Les noirs ont gagn\195\169!' else i='Égalit\195\169.' end else i="Tour des "..pn[play].."s" end gc:setFont('sansserif','b',12) gc:drawString(i,wp-gc:getStringWidth(i)/2,0,'top') gc:setColorRGB(102,255,102) gc:fillRect(wp-wh*0.4,wl,wh*0.8+1,wh*0.8+1) gc:setColorRGB(0,51,0) gc:setPen('medium') gc:drawRect(wp+((cx-5)*wl)+1,cy*wl+1,wl-2,wl-2) gc:setPen('thin') for i=1,9 do gc:drawLine(wp+(i-5)*wl,wl,wp+(i-5)*wl,wl*9) gc:drawLine(wp-wl*4,i*wl,wp+wl*4,i*wl) end for i=1,8 do for j=1,8 do if g[j][i] ~=0 then if g[j][i]==1 then c(gc,204) else c(gc,0) end gc:fillArc(wp+(i-5)*wl,j*wl,wl,wl,0,360) gc:setAlpha(170) c(gc,255) gc:fillArc(wp+(i-4.9)*wl,(j+0.1)*wl,wl/2,wl/2,60,150) gc:setAlpha(255) end end end if plot()[1]==0 then play=-play if plot()[1]==0 then u=nil end elseif ai[play] then local m=plot() if m[1] ~=0 then cx=m[1] cy=m[2] g=test(g,cx,cy) end play=-play end end end function on.tabKey() local m=plot() cx=m[1] cy=m[2] r() end function plot() local m={0,0,-128} for i=1,8 do for j=1,8 do local t=test(g,i,j) if t then local c=count(t)*play if (i==1 or i==8) and (j==1 or j==8) then c=c+64 end if ai[play]==2 then play=-play if test(t,1,1) or test(t,8,1) or test(t,1,8) or test(t,8,8) then c=c-64 end play=-play end if c>m[3] then m={i,j,c} end end end end return m end function r() platform.window:invalidate() end function test(t1,x,y) if t1[y][x]==0 then local t2={{},{},{},{},{},{},{},{}} for i=1,8 do for j=1,8 do t2[j][i]=t1[j][i] end end t2[y][x]=play local did=false did=offer(t2,x,y,-1,-1) or did did=offer(t2,x,y,0,-1) or did did=offer(t2,x,y,1,-1) or did did=offer(t2,x,y,-1,0) or did did=offer(t2,x,y,0,0) or did did=offer(t2,x,y,1,0) or did did=offer(t2,x,y,-1,1) or did did=offer(t2,x,y,0,1) or did did=offer(t2,x,y,1,1) or did if did then return t2 else return false end end end