KaoSDlanor

9 men's morris

Feb 21st, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.30 KB | None | 0 0
  1. local list=function(...)
  2.     local tArgs={...}
  3.     return coroutine.wrap(function()
  4.         for i=1,#tArgs do
  5.             coroutine.yield(tArgs[i])
  6.         end
  7.     end)
  8. end
  9.  
  10. local strList=function(str)
  11.     return coroutine.wrap(function()
  12.         for i=1,string.len(str) do
  13.             coroutine.yield(string.sub(str,i,i),i)
  14.         end
  15.     end)
  16. end
  17.  
  18. local tNodes=setmetatable({
  19. {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}},
  20. {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}},
  21. {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}},
  22.  
  23. {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}},
  24.  
  25. {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}},
  26. {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}},
  27. {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}},
  28. },{__call=function(self,x,y)
  29.     for node=1,#self do
  30.         if self[node].pos[1]==x and self[node].pos[2]==y then
  31.             return self[node],node
  32.         end
  33.     end
  34. end})
  35.  
  36. for k,v in pairs(tNodes) do
  37.     v.content=v.content or "#"
  38. end
  39.  
  40. local tMap={
  41. "#-----#-----#",
  42. "|     |     |",
  43. "| #---#---# |",
  44. "| |   |   | |",
  45. "| | #-#-# | |",
  46. "| | |   | | |",
  47. "#-#-#   #-#-#",
  48. "| | |   | | |",
  49. "| | #-#-# | |",
  50. "| |   |   | |",
  51. "| #---#---# |",
  52. "|     |     |",
  53. "#-----#-----#",
  54. }
  55.  
  56. 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}}
  57.  
  58. local tCols={["#"]=colors.lime,B=colors.lightBlue,R=colors.red}
  59. local teams={red={placed=0,pieces=0,sym="R"},blue={placed=0,pieces=0,sym="B"}}
  60. local turn="red"
  61. local selected
  62. local done=false
  63. local winner
  64.  
  65. local function render()
  66.     term.setBackgroundColor(colors.black)
  67.     term.setTextColor(colors.white)
  68.     term.clear()
  69.     term.setCursorPos(16,2)
  70.     term.write(turn.."'s turn")
  71.     term.setCursorPos(16,3)
  72.     term.write(6-teams[turn].placed+teams[turn].pieces.." spare pieces remaining")
  73.     if teams[turn].placed<9 then
  74.         term.setCursorPos(16,4)
  75.         term.write(teams[turn].placed.."/9 placed")
  76.     end
  77.     for line=1,#tLines do
  78.         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)
  79.         for x=tNodes[tLines[line][1]].pos[1]*2-1,tNodes[tLines[line][3]].pos[1]*2-1 do
  80.             for y=tNodes[tLines[line][1]].pos[2]*2-1,tNodes[tLines[line][3]].pos[2]*2-1 do
  81.                 term.setCursorPos(x+1,y+1)
  82.                 term.write(tMap[y]:sub(x,x))
  83.             end
  84.         end
  85.     end
  86.     for node=1,#tNodes do
  87.         term.setCursorPos(tNodes[node].pos[1]*2,tNodes[node].pos[2]*2)
  88.         term.setTextColor(tNodes[node]==selected and colors.black or tCols[tNodes[node].content] or colors.white)
  89.         term.setBackgroundColor(tNodes[node]==selected and colors.white or colors.black)
  90.         write(tNodes[node].content)
  91.     end
  92. end
  93.  
  94. local function check()
  95.     for line=1,#tLines do
  96.         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
  97.             tLines[line].inUse=true
  98.             tLines[line][tNodes[tLines[line][1]].content]=true
  99.             while true do
  100.                 render()
  101.                 local evts={os.pullEvent()}
  102.                 if evts[1]=="mouse_click" then
  103.                     local node,num=tNodes(evts[3]/2,evts[4]/2)
  104.                     if node and node.content==(turn=="red" and "B" or "R") then
  105.                         node.content="#"
  106.                         teams[turn=="red" and "blue" or "red"].pieces=teams[turn=="red" and "blue" or "red"].pieces-1
  107.                         tLines[line].inUse=false
  108.                         break
  109.                     end
  110.                 end
  111.             end
  112.         end
  113.     end
  114.     if teams.red.pieces+9-teams.red.placed<3 then
  115.         done=true
  116.         winner="blue"
  117.     elseif teams.blue.pieces+9-teams.blue.placed<3 then
  118.         done=true
  119.         winner="red"
  120.     end
  121. end
  122.  
  123. while not done do
  124.     render()
  125.     local evts={os.pullEvent()}
  126.     if evts[1]=="mouse_click" then
  127.         local node,num=tNodes(evts[3]/2,evts[4]/2)
  128.         if teams[turn].placed<9 and node and node.content=="#" then
  129.             node.content=teams[turn].sym
  130.             teams[turn].placed=teams[turn].placed+1
  131.             teams[turn].pieces=teams[turn].pieces+1
  132.             check()
  133.             turn=(turn=="red" and "blue" or "red")
  134.         elseif selected and teams[turn].placed==9 and node and node.content=="#" and selected.linked[num] then
  135.             node.content=selected.content
  136.             selected.content="#"
  137.             selected=nil
  138.             check()
  139.             turn=(turn=="red" and "blue" or "red")
  140.         elseif not selected and teams[turn].placed==9 and node and node.content==teams[turn].sym then
  141.             selected=node
  142.         elseif node==selected then
  143.             selected=nil
  144.         end
  145.     end
  146. end
  147.  
  148. term.clear()
  149. term.setCursorPos(1,1)
  150. term.setTextColor(colors.white)
  151. term.setBackgroundColor(colors.black)
  152. print("the "..winner.." team wins")
Advertisement
Add Comment
Please, Sign In to add comment