Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------SETTINGS--------------
- backStab=true --Can standard chess backStab
- kingType=false --false - king beat on INF cell; true king beat on 1 cell
- -----------------------------------
- local gpu=require("component").gpu
- local event=require("event")
- --colB colF char
- local images={
- red={
- {{"alpha"},{nil,0xff0000,"▄"},{0xff0000,nil," "},{0xff0000,nil," "},{nil,0xff0000,"▄"},{"alpha"}},
- {{0xff0000,nil," "},{0xff0000,nil," "},{0xff0000,nil," "},{0xff0000,nil," "},{0xff0000,nil," "},{0xff0000,nil," "}},
- {{"alpha"},{nil,0xff0000,"▀"},{0xff0000,nil," "},{0xff0000,nil," "},{nil,0xff0000,"▀"},{"alpha"}},
- },
- reD={
- {{"alpha"},{nil,0xff0000,"▄"},{0xff0000,nil," "},{0xff0000,nil," "},{nil,0xff0000,"▄"},{"alpha"}},
- {{0xff0000,nil," "},{0xff0000,nil," "},{0xffff00,nil," "},{0xffff00,nil," "},{0xff0000,nil," "},{0xff0000,nil," "}},
- {{"alpha"},{nil,0xff0000,"▀"},{0xff0000,nil," "},{0xff0000,nil," "},{nil,0xff0000,"▀"},{"alpha"}},
- },
- whi={
- {{"alpha"},{nil,0xffffff,"▄"},{0xffffff,nil," "},{0xffffff,nil," "},{nil,0xffffff,"▄"},{"alpha"}},
- {{0xffffff,nil," "},{0xffffff,nil," "},{0xffffff,nil," "},{0xffffff,nil," "},{0xffffff,nil," "},{0xffffff,nil," "}},
- {{"alpha"},{nil,0xffffff,"▀"},{0xffffff,nil," "},{0xffffff,nil," "},{nil,0xffffff,"▀"},{"alpha"}},
- },
- whD={
- {{"alpha"},{nil,0xffffff,"▄"},{0xffffff,nil," "},{0xffffff,nil," "},{nil,0xffffff,"▄"},{"alpha"}},
- {{0xffffff,nil," "},{0xffffff,nil," "},{0xffff00,nil," "},{0xffff00,nil," "},{0xffffff,nil," "},{0xffffff,nil," "}},
- {{"alpha"},{nil,0xffffff,"▀"},{0xffffff,nil," "},{0xffffff,nil," "},{nil,0xffffff,"▀"},{"alpha"}},
- },
- }
- turn="white"
- --turn="red"
- turnsM={white="red",red="white"}
- isF=false
- mustBit=false
- cantUnselect=false
- player={}
- -----------------------------------------------------------
- function centerText(x,y,w,text)
- gpu.set(x+math.floor(w/2-string.len(text)/2),y,text)
- end
- function messageBox(title,text,color)
- local x,y=4*6-6,4*3-3
- local len1,len2=string.len(title),string.len(text)
- local len3=math.max(len1,len2)+2
- gpu.setBackground(0xffffff)
- gpu.fill(x,y,len3,2+3," ")
- gpu.setForeground(color or 0xFF0000)
- centerText(x,y+1,len3,title)
- gpu.setForeground(0x000000)
- centerText(x,y+3,len3,text)
- gpu.setBackground(color or 0xFF0000)
- gpu.setForeground(0xffffff)
- gpu.fill(x,y+5,len3,3," ")
- centerText(x,y+6,len3,"OK!")
- end
- ---------------authorization-------------------------------
- do
- local obj={}
- local function testClick(self,x,y)
- return (x>=self.x)and(y>=self.y)and(x<self.x+self.w)and(y<self.y+self.h)
- end
- local function offObj()
- return false
- end
- local function newButton(x,y,w,h,id,type)
- table.insert(obj,{x=x,y=y,w=w,h=h,isClicked=testClick,id=id,type=type})
- end
- gpu.setBackground(0x999999)
- gpu.fill(1,1,10,4," ")
- gpu.fill(15,1,10,4," ")
- newButton(1,1,10,4)
- newButton(15,1,10,4)
- isRunning=true
- tmp=2
- while isRunning do
- _,_,x,y,_,name=event.pull("touch")
- for i=1,#obj do
- if obj[i]:isClicked(x,y) then
- player[i]=name
- gpu.setBackground(0x009900)
- gpu.fill(obj[i].x,obj[i].y,obj[i].w,obj[i].h," ")
- gpu.set(obj[i].x,obj[i].y,player[i])
- obj[i].isClicked=offObj
- tmp=tmp-1
- end
- end
- if tmp==0 then
- isRunning=false
- end
- end
- end
- -----------------------------------------------------------
- --nin - noCell; non - empty; red - red; reD - redD; whi - white; whD - whiteD
- map={
- {"nin","red","nin","red","nin","red","nin","red"},
- {"red","nin","red","nin","red","nin","red","nin"},
- {"nin","red","nin","red","nin","red","nin","red"},
- {"non","nin","non","nin","non","nin","non","nin"},
- {"nin","non","nin","non","nin","non","nin","non"},
- {"whi","nin","whi","nin","whi","nin","whi","nin"},
- {"nin","whi","nin","whi","nin","whi","nin","whi"},
- {"whi","nin","whi","nin","whi","nin","whi","nin"}
- }
- ma22p={
- {"nin","non","nin","non","nin","non","nin","non"},
- {"non","nin","whD","nin","non","nin","whD","nin"},
- {"nin","non","nin","non","nin","non","nin","non"},
- {"non","nin","non","nin","non","nin","non","nin"},
- {"nin","non","nin","non","nin","non","nin","non"},
- {"non","nin","red","nin","non","nin","red","nin"},
- {"nin","non","nin","non","nin","non","nin","non"},
- {"non","nin","non","nin","non","nin","non","nin"}
- }
- function resetMap()
- map={
- {"nin","red","nin","red","nin","red","nin","red"},
- {"red","nin","red","nin","red","nin","red","nin"},
- {"nin","red","nin","red","nin","red","nin","red"},
- {"non","nin","non","nin","non","nin","non","nin"},
- {"nin","non","nin","non","nin","non","nin","non"},
- {"whi","nin","whi","nin","whi","nin","whi","nin"},
- {"nin","whi","nin","whi","nin","whi","nin","whi"},
- {"whi","nin","whi","nin","whi","nin","whi","nin"}
- }
- end
- function clearMap()
- map={
- {"nin","non","nin","non","nin","non","nin","non"},
- {"non","nin","non","nin","non","nin","non","nin"},
- {"nin","non","nin","non","nin","non","nin","non"},
- {"non","nin","non","nin","non","nin","non","nin"},
- {"nin","non","nin","non","nin","non","nin","non"},
- {"non","nin","non","nin","non","nin","non","nin"},
- {"nin","non","nin","non","nin","non","nin","non"},
- {"non","nin","non","nin","non","nin","non","nin"}
- }
- end
- function drawImage(name,x,y,selectColor)
- if name=="non" then
- gpu.setBackground(0x000000)
- gpu.fill(x*6-6+1,y*3-3+1,6,3," ")
- return
- end
- local colB=0x000000
- local colF=0xffffff
- for y1=1,3 do
- for x1=1,6 do
- if images[name][y1][x1][1]~="alpha" then
- if images[name][y1][x1][1]==nil then
- colB=(selectColor or 0x000000)
- else
- colB=images[name][y1][x1][1]
- end
- if images[name][y1][x1][2]==nil then
- colF=0xffffff
- else
- colF=images[name][y1][x1][2]
- end
- gpu.setBackground(colB)
- gpu.setForeground(colF)
- gpu.set(x+x1-1,y+y1-1,images[name][y1][x1][3])
- else
- gpu.setBackground(selectColor or 0x000000)
- gpu.set(x+x1-1,y+y1-1," ")
- end
- end
- end
- end
- --drawImage("redD",5,5,0x00ff00)
- function drawField()
- gpu.setBackground(0x000000)
- require("term").clear()
- for y=1,8 do
- for x=1,8 do
- if map[y][x]=="nin" then
- gpu.setBackground(0xa0a0a0)
- gpu.fill(x*6-6+1,y*3-3+1,6,3," ")
- elseif map[y][x]~="non" then
- drawImage(map[y][x],x*6-6+1,y*3-3+1)
- end
- end
- end
- end
- selected={}
- function selecting(x,y,chess,beatX,beatY)
- if chess=="non" then
- gpu.setBackground(0x00ff00)
- gpu.fill(x*6-6+1,y*3-3+1,6,3," ")
- else
- drawImage(chess,x*6-6+1,y*3-3+1,0x00ff00)
- end
- table.insert(selected,{x,y,chess,beatX,beatY})
- end
- function select(x,y,chess,nShow)
- if cantUnselect then
- return
- end
- if nShow==nil then
- nShow=false
- end
- local bool=testBit(x,y,true)
- if bool then
- nShow=false
- end
- if ((mustBit)and(bool))or(not mustBit) then
- -- selecting(x,y,chess)
- if not nShow then
- if not bool then
- if turn=="white" then
- dir=-1
- else
- dir=1
- end
- if (map[y][x]=="whD")or(map[y][x]=="reD") then
- tmp1,tmp2,tmp3,tmp4=true,true,true,true
- for i=1,((kingType and 1)or 8) do
- if (testPoint(x-i,y-i))and(tmp1) then
- if map[y-i][x-i]~="non" then
- tmp1=false
- else
- selecting(x-i,y-i,map[y-i][x-i])
- end
- end
- if (testPoint(x-i,y+i))and(tmp2) then
- if map[y+i][x-i]~="non" then
- tmp2=false
- else
- selecting(x-i,y+i,map[y+i][x-i])
- end
- end
- if (testPoint(x+i,y-i))and(tmp3) then
- if map[y-i][x+i]~="non" then
- tmp3=false
- else
- selecting(x+i,y-i,map[y-i][x+i])
- end
- end
- if (testPoint(x+i,y+i))and(tmp4) then
- if map[y+i][x+i]~="non" then
- tmp4=false
- else
- selecting(x+i,y+i,map[y+i][x+i])
- end
- end
- end
- else
- if x>1 then
- if map[y+dir][x-1]=="non" then
- selecting(x-1,y+dir,"non")
- end
- end
- if x<8 then
- if map[y+dir][x+1]=="non" then
- selecting(x+1,y+dir,"non")
- end
- end
- end
- isF=true
- end
- selecting(x,y,chess)
- end
- end
- return bool
- end
- function unselectAll()
- if cantUnselect then
- return
- end
- for i=1,#selected do
- -- selected[i][3]=map[selected[i][2]][selected[i][1]]
- if selected[i][3]=="non" then
- gpu.setBackground(0x000000)
- gpu.fill(selected[i][1]*6-6+1,selected[i][2]*3-3+1,6,3," ")
- else
- drawImage(selected[i][3],selected[i][1]*6-6+1,selected[i][2]*3-3+1)
- end
- -- map[selected[i][2]][selected[i][1]]=selected[i][3]
- end
- selected={}
- end
- function testPoint(x,y)
- return (x>0)and(x<9)and(y>0)and(y<9)
- end
- function testBit(x,y,bool)
- local tmp=false
- local op=""
- if map[y][x]=="whi" then
- op="red"
- op2="reD"
- elseif map[y][x]=="red" then
- op="whi"
- op2="whD"
- elseif map[y][x]=="non" then
- print("error!")
- else
- if map[y][x]=="whD" then
- op="red"
- op2="reD"
- yo1="whi"
- yo2="whD"
- else
- op="whi"
- op2="whD"
- yo1="red"
- yo2="reD"
- end
- tmp1,tmp2,tmp3,tmp4=false,false,false,false --true if beating chess was found
- num1,num2,num3,num4=0,0,0,0 --number special for finding beat chess
- tmP1,tmP2,tmP3,tmP4=true,true,true,true --false if after beating chess was found chess which close road or your chess
- for i=1,((kingType and 1)or 8) do
- iPl=i+1
- if (testPoint(x-iPl,y-iPl))and(tmP1) then
- if ((map[y-iPl][x-iPl]~="non")and(tmp1))or((map[y-i][x-i]==yo1)or(map[y-i][x-i]==yo2)) then
- tmP1=false
- end
- if ((map[y-i][x-i]==op)or(map[y-i][x-i]==op2))then--and(map[y-iPl][x-iPl]=="non") then
- if map[y-iPl][x-iPl]=="non" then
- tmp1=true
- num1=i
- else
- tmP1=false
- end
- end
- if (tmp1)and(tmP1)and(bool) then
- selecting(x-iPl,y-iPl,"non",x-num1,y-num1)
- end
- end
- if (testPoint(x-iPl,y+iPl))and(tmP2) then
- if ((map[y+iPl][x-iPl]~="non")and(tmp2))or((map[y+i][x-i]==yo1)or(map[y+i][x-i]==yo2)) then
- tmP2=false
- end
- if ((map[y+i][x-i]==op)or(map[y+i][x-i]==op2))then--and(map[y+iPl][x-iPl]=="non") then
- if map[y+iPl][x-iPl]=="non" then
- tmp2=true
- num2=i
- else
- tmP2=false
- end
- end
- if (tmp2)and(tmP2)and(bool) then
- selecting(x-iPl,y+iPl,"non",x-num2,y+num2)
- end
- end
- if (testPoint(x+iPl,y-iPl))and(tmP3) then
- if ((map[y-iPl][x+iPl]~="non")and(tmp3))or((map[y-i][x+i]==yo1)or(map[y-i][x+i]==yo2)) then
- tmP3=false
- end
- if ((map[y-i][x+i]==op)or(map[y-i][x+i]==op2))then--and(map[y-iPl][x+iPl]=="non") then
- if map[y-iPl][x+iPl]=="non" then
- tmp3=true
- num3=i
- else
- tmP3=false
- end
- end
- if (tmp3)and(tmP3)and(bool) then
- selecting(x+iPl,y-iPl,"non",x+num3,y-num3)
- end
- end
- if (testPoint(x+iPl,y+iPl))and(tmP4) then
- if ((map[y+iPl][x+iPl]~="non")and(tmp4))or((map[y+i][x+i]==yo1)or(map[y+i][x+i]==yo2)) then
- tmP4=false
- end
- if ((map[y+i][x+i]==op)or(map[y+i][x+i]==op2))then--and(map[y+iPl][x+iPl]=="non") then
- if map[y+iPl][x+iPl]=="non" then
- tmp4=true
- num4=i
- else
- tmP4=false
- end
- end
- if (tmp4)and(tmP4)and(bool) then
- selecting(x+iPl,y+iPl,"non",x+num4,y+num4)
- end
- end
- end
- -- print((tmp1)or(tmp2)or(tmp3)or(tmp4))
- return (tmp1)or(tmp2)or(tmp3)or(tmp4)
- end
- if (x>2)and(y>2) then
- if ((map[y-1][x-1]==op)or(map[y-1][x-1]==op2))and(map[y-2][x-2]=="non")and(((turn=="red")and(backStab))or(turn=="white")) then
- if bool then
- selecting(x-2,y-2,"non")
- end
- tmp=true
- end
- end
- if (x>2)and(y<7) then
- if ((map[y+1][x-1]==op)or(map[y+1][x-1]==op2))and(map[y+2][x-2]=="non")and(((turn=="white")and(backStab))or(turn=="red")) then
- if bool then
- selecting(x-2,y+2,"non")
- end
- tmp=true
- end
- end
- if (x<7)and(y>2) then
- if ((map[y-1][x+1]==op)or(map[y-1][x+1]==op2))and(map[y-2][x+2]=="non")and(((turn=="red")and(backStab))or(turn=="white")) then
- if bool then
- selecting(x+2,y-2,"non")
- end
- tmp=true
- end
- end
- if (x<7)and(y<7) then
- if ((map[y+1][x+1]==op)or(map[y+1][x+1]==op2))and(map[y+2][x+2]=="non")and(((turn=="white")and(backStab))or(turn=="red")) then
- if bool then
- selecting(x+2,y+2,"non")
- end
- tmp=true
- end
- end
- -- print(tmp)
- return tmp
- end
- function testMustBit(turn)
- haveChess=false
- local mas={}
- if turn=="white" then
- mas={"whi","whD"}
- else
- mas={"red","reD"}
- end
- for y=1,8 do
- for x=1,8 do
- if (map[y][x]==mas[1])or(map[y][x]==mas[2]) then
- haveChess=true
- if testBit(x,y,false) then
- return true
- end
- end
- end
- end
- if not haveChess then
- -- print("GAME OVER WINNER: "..turnsM[turn])
- messageBox("GAME OVER","Winner: "..turnsM[turn])
- end
- return false
- end
- function onClick(_,_,x,y,_,name)
- if (turn=="white")and(name~=player[1]) then
- return
- end
- if (turn=="red")and(name~=player[2]) then
- return
- end
- x,y=math.floor((x-1)/6)+1,math.floor((y-1)/3)+1
- if ((map[y][x]=="red")or(map[y][x]=="reD"))and(turn=="red") then
- unselectAll()
- select(x,y,map[y][x])
- return
- elseif ((map[y][x]=="whi")or(map[y][x]=="whD"))and(turn=="white") then
- unselectAll()
- select(x,y,map[y][x])
- return
- else
- for i=1,#selected-1 do
- if (selected[i][1]==x)and(selected[i][2]==y) then
- if selected[i][3]~="non" then
- -- print("1")
- unselectAll()
- else
- map[y][x]=selected[#selected][3]
- map[selected[#selected][2]][selected[#selected][1]]="non"
- -- print(require("serialization").unserialize(selected[#selected]))
- selected[#selected][3]="non"
- selected[i][3]=map[y][x]
- if math.abs(x-selected[#selected][1])>1 then
- if (selected[i][4]==nil)and(mustBit) then
- y2=(y+selected[#selected][2])/2
- x2=(x+selected[#selected][1])/2
- -- print(x2,y2)
- map[y2][x2]="non"
- drawImage("non",x2,y2)
- isF=false
- elseif (mustBit) then
- map[selected[i][5]][selected[i][4]]="non"
- drawImage("non",selected[i][4],selected[i][5])
- -- print(selected[i][4],selected[i][5])
- isF=false
- else
- isF=true
- end
- else
- isF=true
- end
- if (turn=="white")and(y==1)and(map[y][x]~="whD") then
- map[y][x]="whD"
- selected[i][3]="whD"
- end
- if (turn=="red")and(y==8)and(map[y][x]~="reD") then
- map[y][x]="reD"
- selected[i][3]="reD"
- end
- cantUnselect=false
- unselectAll()
- -- print(isF)
- if not isF then
- isF=not select(x,y,map[y][x],true)
- -- print(isF)
- if not isF then
- cantUnselect=true
- end
- end
- if isF then
- isF=false
- turn=turnsM[turn]
- mustBit=testMustBit(turn)
- -- unselectAll()
- end
- end
- end
- end
- end
- end
- ------------------------------------------------------------------------------
- drawField()
- event.ignore("touch",onClick)
- event.listen("touch",onClick)
- mustBit=testMustBit(turn)
- event.pull("key_down")
- event.ignore("touch",onClick)
- --os.sleep(5)
- cantUnselect=false
- unselectAll()
- gpu.setBackground(0x000000)
- gpu.setForeground(0xffffff)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement