Advertisement
ecco7777

CC 2D Chess

Nov 23rd, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.19 KB | None | 0 0
  1. --Chess
  2.  
  3. function initField()
  4. field={}
  5.     for x=1,8 do
  6.     field[x]={}
  7.         for y=1,8 do
  8.             field[x][y]={}
  9.             field[x][y].type="free"
  10.         end
  11.     end
  12. end
  13.  
  14. function cultivateField()
  15.     for i=1,8 do
  16.         addPawn(i,2,"black")
  17.         addPawn(i,7,"white")
  18.     end
  19.     addTower(1,1,"black")
  20.     addKnight(1,2,"black")
  21.     addBishop(1,3,"back")
  22.     addQueen(1,4,"black")
  23.     addKing(1,5,"black")
  24.     addBishop(1,6,"black")
  25.     addKnight(1,7,"back")
  26.     addTower(1,8,"black")
  27.    
  28.     addTower(1,1,"white")
  29.     addKnight(1,2,"white")
  30.     addBishop(1,3,"back")
  31.     addQueen(1,4,"white")
  32.     addKing(1,5,"white")
  33.     addBishop(1,6,"white")
  34.     addKnight(1,7,"back")
  35.     addTower(1,8,"white")
  36. end
  37.  
  38. function fieldValid(x,y)
  39.     if x>0 and y>0 and x<=8 and y<=8 then
  40.         return true
  41.     else
  42.         return false
  43.     end
  44. end
  45.  
  46. function playerOnField(x,y)
  47.         if field[x][y].type~="free" then
  48.             return field[x][y].color
  49.         else
  50.             return nil
  51.         end
  52. end
  53.  
  54. function addPawn(x,y,color)
  55.     pawn={}
  56.     pawn.x=x
  57.     pawn.y=y
  58.     pawn.color=color
  59.     movesTaken=0
  60.     function pawn.getLegalMoves()
  61.     possibleMoves={}
  62.         if color=="white" then
  63.             --check for killoptions
  64.             if fieldValid(x-1,y-1) then
  65.                 if playerOnField(x-1,y-1) then
  66.                     possibleMoves[#possibleMoves+1]={x=x-1,y=y-1}
  67.                 end
  68.             end
  69.             if fieldValid(x+1,y-1) then
  70.                 if playerOnField(x+1,y-1) then
  71.                     possibleMoves[#possibleMoves+1]={x=x+1,y=y-1}
  72.                 end
  73.             end
  74.             --check if first move
  75.             if movesTaken==0 then
  76.                 if fieldValid(x,y-2) then
  77.                     if playerOnField(x,y-2)==nil then
  78.                         possibleMoves[#possibleMoves+1]={x=x,y=y-2}
  79.                     end
  80.                 end
  81.             end
  82.             --check front
  83.             if fieldValid(x,y-1) then
  84.                 if playerOnField(x,y-1)==nil then
  85.                     possibleMoves[#possibleMoves+1]={x=x,y=y-1}
  86.                 end
  87.             end
  88.  
  89.         else if color=="black" then
  90.             --check for killoptions
  91.             if fieldValid(x-1,y+1) then
  92.                 if playerOnField(x-1,y+1) then
  93.                     possibleMoves[#possibleMoves+1]={x=x-1,y=y+1}
  94.                 end
  95.             end
  96.             if fieldValid(x+1,y+1) then
  97.                 if playerOnField(x+1,y+1) then
  98.                     possibleMoves[#possibleMoves+1]={x=x+1,y=y-1}
  99.                 end
  100.             end
  101.             --check if first move
  102.             if movesTaken==0 then
  103.                 if fieldValid(x,y+2) then
  104.                     if playerOnField(x,y+2)==nil then
  105.                         possibleMoves[#possibleMoves+1]={x=x,y=y+2}
  106.                     end
  107.                 end
  108.             end
  109.             --check front
  110.             if fieldValid(x,y+1) then
  111.                 if playerOnField(x,y+1)==nil then
  112.                     possibleMoves[#possibleMoves+1]={x=x,y=y+1}
  113.                 end
  114.             end
  115.         end
  116.     end
  117.     pawn.possibleMoves=possibleMoves
  118.     field[x][y]=pawn
  119.     return pawn
  120. end
  121.  
  122. function addTower(x,y,color)
  123.     tower={}
  124.     tower.x=x
  125.     tower.y=y
  126.     tower.color=color
  127.     movesTaken=0
  128.     function tower.getLegalMoves()
  129.         possibleMoves={}
  130.         i=1
  131.         noObstacle=true
  132.         while noObstacle do
  133.             if fieldValid(x+i,y) then
  134.                 if playerOnField(x+i,y)==nil then
  135.                     possibleMoves[#possibleMoves+1]={x=x+i,y=y}
  136.                 else
  137.                     if playerOnField(x+i,y)==color then
  138.                             noObstacle=false
  139.                     else
  140.                         possibleMoves[#possibleMoves+1]={x=x+i,y=y}
  141.                         noObstacle=false
  142.                     end
  143.                 end
  144.             end
  145.         end
  146.         i=1
  147.         noObstacle=true
  148.         while noObstacle do
  149.             if fieldValid(x-i,y) then
  150.                 if playerOnField(x-i,y)==nil then
  151.                     possibleMoves[#possibleMoves+1]={x=x-i,y=y}
  152.                 else
  153.                     if playerOnField(x-i,y)==color then
  154.                         noObstacle=false
  155.                     else
  156.                         possibleMoves[#possibleMoves+1]={x=x-i,y=y}
  157.                         noObstacle=false
  158.                     end
  159.                 end
  160.             end
  161.         end
  162.         i=1
  163.         noObstacle=true
  164.         while noObstacle do
  165.             if fieldValid(x,y-i) then
  166.                 if playerOnField(x,y-i)==nil then
  167.                     possibleMoves[#possibleMoves+1]={x=x,y=y-i}
  168.                 else
  169.                     if playerOnField(x,y-i)==color then
  170.                         noObstacle=false  
  171.                     else
  172.                         possibleMoves[#possibleMoves+1]={x=x,y=y-i}
  173.                         noObstacle=false
  174.                     end
  175.                 end
  176.             end
  177.         end
  178.         i=1
  179.         noObstacle=true
  180.         while noObstacle do
  181.             if fieldValid(x,y+i) then
  182.                 if playerOnField(x,y+i)==nil then
  183.                     possibleMoves[#possibleMoves+1]={x=x,y=y+i}
  184.                 else
  185.                     if playerOnField(x,y+i)==color then
  186.                         noObstacle=false
  187.                     else
  188.                         possibleMoves[#possibleMoves+1]={x=x,y=y+i}
  189.                         noObstacle=false
  190.                     end
  191.                 end
  192.             end
  193.         end
  194.     end
  195.     tower.possibleMoves=possibleMoves
  196.     field[x][y]=tower
  197.     return tower
  198. end
  199.  
  200. function addBishop(x,y,color)
  201.     bishop={}
  202.     bishop.x=x
  203.     bishop.y=y
  204.     bishop.color=color
  205.     movesTaken=0
  206.     function bishop.getLegalMoves()
  207.         possibleMoves={}
  208.         i=1
  209.         noObstacle=true
  210.         while noObstacle do
  211.             if fieldValid(x+i,y+i) then
  212.                 if playerOnField(x+i,y+i)==nil then
  213.                     possibleMoves[#possibleMoves+1]={x=x+i,y=y+i}
  214.                 else
  215.                     if playerOnField(x+i,y+i)==color then
  216.                         noObstacle=false
  217.                     else
  218.                         possibleMoves[#possibleMoves+1]={x=x+i,y=y+i}
  219.                         noObstacle=false
  220.                     end
  221.                 end
  222.             end
  223.         end
  224.         i=1
  225.         noObstacle=true
  226.         while noObstacle do
  227.             if fieldValid(x-i,y+i) then
  228.                 if playerOnField(x-i,y+i)==nil then
  229.                     possibleMoves[#possibleMoves+1]={x=x-i,y=y+i}
  230.                 else
  231.                     if playerOnField(x-i,y+i)==color then
  232.                         noObstacle=false
  233.                     else
  234.                         possibleMoves[#possibleMoves+1]={x=x-i,y=y+i}
  235.                         noObstacle=false
  236.                     end
  237.                 end
  238.             end
  239.         end
  240.         i=1
  241.         noObstacle=true
  242.         while noObstacle do
  243.             if fieldValid(x+i,y-i) then
  244.                 if playerOnField(x+i,y-i)==nil then
  245.                     possibleMoves[#possibleMoves+1]={x=x+i,y=y-i}
  246.                 else
  247.                     if playerOnField(x+i,y-i)==color then
  248.                         noObstacle=false
  249.                     else
  250.                         possibleMoves[#possibleMoves+1]={x=x+i,y=y-i}
  251.                         noObstacle=false
  252.                     end
  253.                 end
  254.             end
  255.         end
  256.         i=1
  257.         noObstacle=true
  258.         while noObstacle do
  259.             if fieldValid(x-i,y-i) then
  260.                 if playerOnField(x-i,y-i)==nil then
  261.                     possibleMoves[#possibleMoves+1]={x=x-i,y=y-i}
  262.                 else
  263.                     if playerOnField(x-i,y-i)==color then
  264.                         noObstacle=false  
  265.                     else
  266.                         possibleMoves[#possibleMoves+1]={x=x-i,y=y-i}
  267.                         noObstacle=false
  268.                     end
  269.                 end
  270.             end
  271.         end
  272.     end
  273.     bishop.possibleMoves=possibleMoves
  274.     field[x][y]=bishop
  275.     return bishop
  276. end
  277.  
  278. function addQueen(x,y,color)
  279.     queen={}
  280.     queen.x=x
  281.     queen.y=y
  282.     queen.color=color
  283.     movesTaken=0
  284.     function queen.getLegalMoves()
  285.         possibleMoves={}
  286.         noObstacle=true
  287.         while noObstacle do
  288.             if fieldValid(x+i,y) then
  289.                 if playerOnField(x+i,y)==nil then
  290.                     possibleMoves[#possibleMoves+1]={x=x+i,y=y}
  291.                 else
  292.                     if playerOnField(x+i,y)==color then
  293.                         noObstacle=false
  294.                     else
  295.                         possibleMoves[#possibleMoves+1]={x=x+i,y=y}
  296.                         noObstacle=false
  297.                     end
  298.                 end
  299.             end
  300.         end
  301.         i=1
  302.         noObstacle=true
  303.         while noObstacle do
  304.             if fieldValid(x-i,y) then
  305.                 if playerOnField(x-i,y)==nil then
  306.                     possibleMoves[#possibleMoves+1]={x=x-i,y=y}
  307.                 else
  308.                     if playerOnField(x-i,y)==color then
  309.                         noObstacle=false      
  310.                     else
  311.                         possibleMoves[#possibleMoves+1]={x=x-i,y=y}
  312.                         noObstacle=false
  313.                     end
  314.                 end
  315.             end
  316.         end
  317.         i=1
  318.         noObstacle=true
  319.         while noObstacle do
  320.             if fieldValid(x,y-i) then
  321.                 if playerOnField(x,y-i)==nil then
  322.                     possibleMoves[#possibleMoves+1]={x=x,y=y-i}
  323.                 else
  324.                     if playerOnField(x,y-i)==color then
  325.                         noObstacle=false
  326.                    
  327.                     else
  328.                         possibleMoves[#possibleMoves+1]={x=x,y=y-i}
  329.                         noObstacle=false
  330.                     end
  331.                 end
  332.             end
  333.         end
  334.         i=1
  335.         noObstacle=true
  336.         while noObstacle do
  337.             if fieldValid(x,y+i) then
  338.                 if playerOnField(x,y+i)==nil then
  339.                     possibleMoves[#possibleMoves+1]={x=x,y=y+i}
  340.                 else
  341.                     if playerOnField(x,y+i)==color then
  342.                         noObstacle=false
  343.                    
  344.                     else
  345.                         possibleMoves[#possibleMoves+1]={x=x,y=y+i}
  346.                         noObstacle=false
  347.                     end
  348.                 end
  349.             end
  350.         end
  351.         i=1
  352.         noObstacle=true
  353.         while noObstacle do
  354.             if fieldValid(x+i,y+i) then
  355.                 if playerOnField(x+i,y+i)==nil then
  356.                     possibleMoves[#possibleMoves+1]={x=x+i,y=y+i}
  357.                 else
  358.                     if playerOnField(x+i,y+i)==color then
  359.                         noObstacle=false
  360.                    
  361.                     else
  362.                         possibleMoves[#possibleMoves+1]={x=x+i,y=y+i}
  363.                         noObstacle=false
  364.                     end
  365.                 end
  366.             end
  367.         end
  368.         i=1
  369.         noObstacle=true
  370.         while noObstacle do
  371.             if fieldValid(x-i,y+i) then
  372.                 if playerOnField(x-i,y+i)==nil then
  373.                     possibleMoves[#possibleMoves+1]={x=x-i,y=y+i}
  374.                 else
  375.                     if playerOnField(x-i,y+i)==color then
  376.                         noObstacle=false
  377.                        
  378.                     else
  379.                         possibleMoves[#possibleMoves+1]={x=x-i,y=y+i}
  380.                         noObstacle=false
  381.                     end
  382.                 end
  383.             end
  384.         end
  385.         i=1
  386.         noObstacle=true
  387.         while noObstacle do
  388.             if fieldValid(x+i,y-i) then
  389.                 if playerOnField(x+i,y-i)==nil then
  390.                     possibleMoves[#possibleMoves+1]={x=x+i,y=y-i}
  391.                 else
  392.                     if playerOnField(x+i,y-i)==color then
  393.                         noObstacle=false
  394.                    
  395.                     else
  396.                         possibleMoves[#possibleMoves+1]={x=x+i,y=y-i}
  397.                         noObstacle=false
  398.                     end
  399.                 end
  400.             end
  401.         end
  402.         i=1
  403.         noObstacle=true
  404.         while noObstacle do
  405.             if fieldValid(x-i,y-i) then
  406.                 if playerOnField(x-i,y-i)==nil then
  407.                     possibleMoves[#possibleMoves+1]={x=x-i,y=y-i}
  408.                 else
  409.                     if playerOnField(x-i,y-i)==color then
  410.                         noObstacle=false
  411.                        
  412.                     else
  413.                         possibleMoves[#possibleMoves+1]={x=x-i,y=y-i}
  414.                         noObstacle=false
  415.                     end
  416.                 end
  417.             end
  418.         end
  419.     end
  420.     queen.possibleMoves=possibleMoves
  421.     field[x][y]=queen
  422.     return queen
  423. end
  424.  
  425. function addKing(x,y,color)
  426.     king={}
  427.     king.x=x
  428.     king.y=y
  429.     king.color=color
  430.     movesTaken=0
  431.     function king.getLegalMoves()
  432.     possibleMoves={}
  433.         for x1=-1,1 do
  434.             for y1=-1,1 do
  435.                 if x1~=0 and y1~=0 then
  436.                     if fieldValid(x+x1,y+y1) then
  437.                         if playerOnField(x+x1,y+y1)~=color then
  438.                             possibleMoves[#possibleMoves+1]={x=x+x1,y=y+y1}
  439.                         end
  440.                     end
  441.                 end
  442.             end
  443.         end
  444.     end
  445.     king.possibleMoves=possibleMoves
  446.     field[x][y]=king
  447.     return king
  448. end
  449.  
  450. function addKnight(x,y,color)
  451.     knight={}
  452.     knight.x=x
  453.     knight.y=y
  454.     knight.color=color
  455.     movesTaken=0
  456.     function knight.getLegalMoves()
  457.     possibleMoves={}
  458.         relativCoords={{x=2,y=1},{x=2,y=-1},{x=1,y=2},{x=1,y=-2},{x=-1,y=2},{x=-1,y=2},{x=-2,y=1},{x=-2,y=-1}}
  459.         for i=1,#relativCoords do
  460.             if fieldValid(x+x1,y+y1) then
  461.                 if playerOnField(x+x1,y+y1)~=color then
  462.                     possibleMoves[#possibleMoves+1]={x=x+x1,y=y+y1}
  463.                 end
  464.             end
  465.         end
  466.     end
  467.     knight.possibleMoves=possibleMoves
  468.     field[x][y]=knight
  469.     return knight
  470. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement