Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local list=function(...)
- local tArgs={...}
- return coroutine.wrap(function()
- for i=1,#tArgs do
- coroutine.yield(tArgs[i])
- end
- end)
- end
- local strList=function(str)
- return coroutine.wrap(function()
- for i=1,string.len(str) do
- coroutine.yield(string.sub(str,i,i),i)
- end
- end)
- end
- local tNodes=setmetatable({
- {linked={[2]=true,[10]=true},pos={1,1}},{linked={[1]=true,[3]=true,[5]=true},pos={4,1}},{linked={[2]=true,[15]=true},pos={7,1}},
- {linked={[5]=true,[11]=true},pos={2,2}},{linked={[2]=true,[4]=true,[6]=true,[8]=true},pos={4,2}},{linked={[5]=true,[14]=true},pos={6,2}},
- {linked={[8]=true,[12]=true},pos={3,3}},{linked={[5]=true,[7]=true,[9]=true},pos={4,3}},{linked={[8]=true,[13]=true},pos={5,3}},
- {linked={[1]=true,[11]=true,[22]=true},pos={1,4}},{linked={[4]=true,[10]=true,[12]=true,[19]=true},pos={2,4}},{linked={[7]=true,[11]=true,[16]=true},pos={3,4}},{linked={[9]=true,[14]=true,[18]=true},pos={5,4}},{linked={[6]=true,[13]=true,[15]=true,[21]=true},pos={6,4}},{linked={[3]=true,[14]=true,[24]=true},pos={7,4}},
- {linked={[11]=true,[20]=true},pos={3,5}},{linked={[16]=true,[18]=true,[20]=true},pos={4,5}},{linked={[13]=true,[17]=true},pos={5,5}},
- {linked={[11]=true,[20]=true},pos={2,6}},{linked={[17]=true,[19]=true,[21]=true,[23]=true},pos={4,6}},{linked={[14]=true,[20]=true},pos={6,6}},
- {linked={[10]=true,[23]=true},pos={1,7}},{linked={[20]=true,[22]=true,[24]=true},pos={4,7}},{linked={[15]=true,[23]=true},pos={7,7}},
- },{__call=function(self,x,y)
- for node=1,#self do
- if self[node].pos[1]==x and self[node].pos[2]==y then
- return self[node],node
- end
- end
- end})
- for k,v in pairs(tNodes) do
- v.content=v.content or "#"
- end
- local tMap={
- "#-----#-----#",
- "| | |",
- "| #---#---# |",
- "| | | | |",
- "| | #-#-# | |",
- "| | | | | |",
- "#-#-# #-#-#",
- "| | | | | |",
- "| | #-#-# | |",
- "| | | | |",
- "| #---#---# |",
- "| | |",
- "#-----#-----#",
- }
- local tLines={{1,2,3},{3,15,24},{22,23,24},{1,10,22},{2,5,8},{13,14,15},{17,20,23},{10,11,12},{4,5,6},{6,14,21},{19,20,21},{4,11,19},{7,8,9},{9,13,18},{16,17,18},{7,12,16}}
- local tCols={["#"]=colors.lime,B=colors.lightBlue,R=colors.red}
- local teams={red={placed=0,pieces=0,sym="R"},blue={placed=0,pieces=0,sym="B"}}
- local turn="red"
- local selected
- local done=false
- local winner
- local function render()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(16,2)
- term.write(turn.."'s turn")
- term.setCursorPos(16,3)
- term.write(6-teams[turn].placed+teams[turn].pieces.." spare pieces remaining")
- if teams[turn].placed<9 then
- term.setCursorPos(16,4)
- term.write(teams[turn].placed.."/9 placed")
- end
- for line=1,#tLines do
- term.setTextColor(tLines[line].inUse and colors.white or tLines[line].R and (tLines[line].B and colors.gray or colors.red) or tLines[line].B and colors.lightBlue or colors.lime)
- for x=tNodes[tLines[line][1]].pos[1]*2-1,tNodes[tLines[line][3]].pos[1]*2-1 do
- for y=tNodes[tLines[line][1]].pos[2]*2-1,tNodes[tLines[line][3]].pos[2]*2-1 do
- term.setCursorPos(x+1,y+1)
- term.write(tMap[y]:sub(x,x))
- end
- end
- end
- for node=1,#tNodes do
- term.setCursorPos(tNodes[node].pos[1]*2,tNodes[node].pos[2]*2)
- term.setTextColor(tNodes[node]==selected and colors.black or tCols[tNodes[node].content] or colors.white)
- term.setBackgroundColor(tNodes[node]==selected and colors.white or colors.black)
- write(tNodes[node].content)
- end
- end
- local function check()
- for line=1,#tLines do
- if tNodes[tLines[line][1]].content~="#" and tNodes[tLines[line][1]].content==tNodes[tLines[line][2]].content and tNodes[tLines[line][2]].content==tNodes[tLines[line][3]].content and not tLines[line][tNodes[tLines[line][1]].content] then
- tLines[line].inUse=true
- tLines[line][tNodes[tLines[line][1]].content]=true
- while true do
- render()
- local evts={os.pullEvent()}
- if evts[1]=="mouse_click" then
- local node,num=tNodes(evts[3]/2,evts[4]/2)
- if node and node.content==(turn=="red" and "B" or "R") then
- node.content="#"
- teams[turn=="red" and "blue" or "red"].pieces=teams[turn=="red" and "blue" or "red"].pieces-1
- tLines[line].inUse=false
- break
- end
- end
- end
- end
- end
- if teams.red.pieces+9-teams.red.placed<3 then
- done=true
- winner="blue"
- elseif teams.blue.pieces+9-teams.blue.placed<3 then
- done=true
- winner="red"
- end
- end
- while not done do
- render()
- local evts={os.pullEvent()}
- if evts[1]=="mouse_click" then
- local node,num=tNodes(evts[3]/2,evts[4]/2)
- if teams[turn].placed<9 and node and node.content=="#" then
- node.content=teams[turn].sym
- teams[turn].placed=teams[turn].placed+1
- teams[turn].pieces=teams[turn].pieces+1
- check()
- turn=(turn=="red" and "blue" or "red")
- elseif selected and teams[turn].placed==9 and node and node.content=="#" and selected.linked[num] then
- node.content=selected.content
- selected.content="#"
- selected=nil
- check()
- turn=(turn=="red" and "blue" or "red")
- elseif not selected and teams[turn].placed==9 and node and node.content==teams[turn].sym then
- selected=node
- elseif node==selected then
- selected=nil
- end
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- print("the "..winner.." team wins")
Advertisement
Add Comment
Please, Sign In to add comment