Luiginico

CCraft Connect4

Mar 19th, 2013
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.23 KB | None | 0 0
  1. --Ccraft Connect4 by Luiginico
  2.  
  3. --Starting
  4.     function start()
  5.         if peripheral.getType("top") == nil or peripheral.getType("top") ~= "modem" then
  6.             print("Modem needs to be on the top (or you can edit the code at --Starting)")
  7.             return false
  8.         end
  9.         return true
  10.     end
  11.    
  12.     modem = peripheral.wrap("top")
  13.    
  14.     modem.open(os.getComputerID())
  15.    
  16. --Variables
  17.     opponent = 0
  18.  
  19.     board = {
  20.         {0,0,0,0,0,0,0}, --1
  21.         {0,0,0,0,0,0,0}, --2
  22.         {0,0,0,0,0,0,0}, --3
  23.         {0,0,0,0,0,0,0}, --4
  24.         {0,0,0,0,0,0,0}, --5
  25.         {0,0,0,0,0,0,0}, --6
  26.         {0,0,0,0,0,0,0}  --7
  27.     }
  28.  
  29.     air = 0
  30.     oPeg = 1
  31.     xPeg = 2
  32.    
  33.     last = 7
  34.    
  35.     x, y = term.getSize()
  36.    
  37.     win = true
  38.    
  39.     first = true
  40.    
  41.     messages = { }
  42.     blank = " "
  43.     for i=2,x-2 do
  44.         blank = blank.. " "
  45.     end
  46.     for i=13,y-2 do
  47.         messages[i-12] = blank
  48.     end
  49.    
  50. --Functions
  51.     function userlog(message)
  52.         for count=1,#messages-1 do
  53.             messages[#messages-count+1] = messages[#messages-count]
  54.         end
  55.         messages[1] = message
  56.         for count=1,#messages do
  57.             term.setCursorPos(2, 12+count)
  58.             write(blank)
  59.             term.setCursorPos(2, 12+count)
  60.             write(messages[count])
  61.         end
  62.         sleep(0.5)
  63.     end
  64.  
  65.     function clear()
  66.         term.clear()
  67.         term.setCursorPos(1,1)
  68.     end
  69.    
  70.     function screen()
  71.         clear()
  72.         write("+")
  73.         for i=1,x-2 do
  74.             write("-")
  75.         end
  76.         write("+")
  77.         for j=2,y-1 do
  78.             write("|")
  79.             for i=1,x-2 do
  80.                 write(" ")
  81.             end
  82.             write("|")
  83.         end
  84.         write("+")
  85.         for i=1,x-2 do
  86.             write("-")
  87.         end
  88.         write("+")
  89.         term.setCursorPos(x/2-15, y-1)
  90.         write("CCraft Connect4, By Luiginico")
  91.     end
  92.    
  93.     function loginScreen()
  94.         screen()
  95.         term.setCursorPos(x/2-2, 3)
  96.         write("Host")
  97.         term.setCursorPos(x/2-2, 5)
  98.         write("Login")
  99.         term.setCursorPos(x/2-2, 7)
  100.         write("Exit")
  101.         --term.setCursorPos(x/2-5, 9)
  102.         --write("How to play")
  103.         term.setCursorPos(2, y-3)
  104.         write("Click or press the first letter to choose")
  105.     end
  106.    
  107.     function gameScreen()
  108.         screen()
  109.         term.setCursorPos(3,3)
  110.         write("O_______O")
  111.         for iScreen=1,6 do
  112.             term.setCursorPos(3,3+iScreen)
  113.             write("|       |")
  114.         end
  115.         term.setCursorPos(3,10)
  116.         write("O=======O")
  117.         for iS = 1,7 do
  118.             for jS = 1,6 do
  119.                 if board[iS][jS] == air then
  120.                     term.setCursorPos(iS+3, 10-jS)
  121.                     write(".")
  122.                 elseif board[iS][jS] == oPeg then
  123.                     term.setCursorPos(iS+3, 10-jS)
  124.                     write("o")
  125.                 elseif board[iS][jS] == xPeg then
  126.                     term.setCursorPos(iS+3, 10-jS)
  127.                     write("x")
  128.                 end
  129.             end
  130.         end
  131.         term.setCursorPos(4,11)
  132.         write("1234567")
  133.     end
  134.    
  135.     function host()
  136.         screen()
  137.         term.setCursorPos(x/2-11, 3)
  138.         write("Waiting for an opponent")
  139.         term.setCursorPos(x/2-6, 5)
  140.         write("Game ID = " ..os.getComputerID())
  141.         local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  142.         opponent = replyChannel
  143.         first = true
  144.     end
  145.    
  146.     function loginGame()
  147.         screen()
  148.         term.setCursorPos(x/2-5, 3)
  149.         write("Connecting")
  150.         term.setCursorPos(x/2-6, 5)
  151.         write("Game ID = ")
  152.         opponent = tonumber(io.read())
  153.         modem.transmit(opponent, os.getComputerID(), " ")
  154.         first = false
  155.     end
  156.    
  157.     function loginGUI()
  158.         while true do
  159.             local event, p1, p2, p3 = os.pullEventRaw()
  160.             if event == "char" then
  161.                 if p1 == "h" then
  162.                     host()
  163.                     return false
  164.                 elseif p1 == "l" then
  165.                     loginGame()
  166.                     return false
  167.                 elseif p1 == "e" then
  168.                     clear()
  169.                     modem.closeAll()
  170.                     error()
  171.                 --elseif p1 == "h" then
  172.                     --return tutorialScreen()
  173.                 end
  174.             elseif event == "mouse_click" then
  175.                 if p3 == 3 then
  176.                     host()
  177.                     return false
  178.                 elseif p3 == 5 then
  179.                     loginGame()
  180.                     return false
  181.                 elseif p3 == 7 then
  182.                     clear()
  183.                     modem.closeAll()
  184.                     error()
  185.                 --elseif p3 == 9 then
  186.                     --return tutorialScreen()
  187.                 end
  188.             end
  189.         end
  190.     end
  191.    
  192.     function login()
  193.         loginScreen()
  194.         loginGUI()
  195.     end
  196.    
  197.     function refreshRow(row)
  198.         for i = 1,6 do
  199.             if board[row][i] == air then
  200.                 term.setCursorPos(row+3, 10-i)
  201.                 write(".")
  202.             elseif board[row][i] == oPeg then
  203.                 term.setCursorPos(row+3, 10-i)
  204.                 write("o")
  205.             elseif board[row][i] == xPeg then
  206.                 term.setCursorPos(row+3, 10-i)
  207.                 write("x")
  208.             end
  209.         end
  210.     end
  211.    
  212.     function winO(xP, yP)
  213.         if ((board[xP] and board[xP][yP] and board[xP-1] and board[xP-1][yP] and board[xP][yP] == board[xP-1][yP]) and (board[xP] and board[xP][yP] and board[xP-2] and board[xP-2][yP] and board[xP][yP] == board[xP-2][yP]) and (board[xP] and board[xP][yP] and board[xP-3] and board[xP-3][yP] and board[xP][yP] == board[xP-3][yP])) or ((board[xP] and board[xP][yP] and board[xP-1] and board[xP-1][yP] and board[xP][yP] == board[xP-1][yP]) and (board[xP] and board[xP][yP] and board[xP-2] and board[xP-2][yP] and board[xP][yP] == board[xP-2][yP]) and (board[xP] and board[xP][yP] and board[xP+1] and board[xP+1][yP] and board[xP][yP] == board[xP+1][yP])) or ((board[xP] and board[xP][yP] and board[xP-1] and board[xP-1][yP] and board[xP][yP] == board[xP-1][yP]) and (board[xP] and board[xP][yP] and board[xP+1] and board[xP+1][yP] and board[xP][yP] == board[xP+1][yP]) and (board[xP] and board[xP][yP] and board[xP+2] and board[xP+2][yP] and board[xP][yP] == board[xP+2][yP])) or ((board[xP] and board[xP][yP] and board[xP+1] and board[xP+1][yP] and board[xP][yP] == board[xP+1][yP]) and (board[xP] and board[xP][yP] and board[xP+2] and board[xP+2][yP] and board[xP][yP] == board[xP+2][yP]) and (board[xP] and board[xP][yP] and board[xP+3] and board[xP+3][yP] and board[xP][yP] == board[xP+3][yP])) then
  214.             return true
  215.         end
  216.         return false
  217.     end
  218.    
  219.     function winV(xP, yP)
  220.         if ((board[xP] and board[xP][yP] and board[xP][yP-1] and board[xP][yP] == board[xP][yP-1]) and (board[xP] and board[xP][yP] and board[xP][yP-2] and board[xP][yP] == board[xP][yP-2]) and (board[xP] and board[xP][yP] and board[xP][yP-3] and board[xP][yP] == board[xP][yP-3])) or ((board[xP] and board[xP][yP] and board[xP][yP-1] and board[xP][yP] == board[xP][yP-1]) and (board[xP] and board[xP][yP] and board[xP][yP-2] and board[xP][yP] == board[xP][yP-2]) and (board[xP] and board[xP][yP] and board[xP][yP+1] and board[xP][yP] == board[xP][yP+1])) or ((board[xP] and board[xP][yP] and board[xP][yP-1] and board[xP][yP] == board[xP][yP-1]) and (board[xP] and board[xP][yP] and board[xP][yP+1] and board[xP][yP] == board[xP][yP+1]) and (board[xP] and board[xP][yP] and board[xP][yP+2] and board[xP][yP] == board[xP][yP+2])) or ((board[xP] and board[xP][yP] and board[xP][yP+1] and board[xP][yP] == board[xP][yP+1]) and (board[xP] and board[xP][yP] and board[xP][yP+2] and board[xP][yP] == board[xP][yP+2]) and (board[xP] and board[xP][yP] and board[xP][yP+3] and board[xP][yP] == board[xP][yP+3])) then
  221.             return true
  222.         end
  223.         return false
  224.     end
  225.    
  226.     function winD(xP, yP)
  227.         if ((board[xP] and board[xP][yP] and board[xP-1] and board[xP-1][yP-1] and board[xP][yP] == board[xP-1][yP-1]) and (board[xP] and board[xP][yP] and board[xP-2] and board[xP-2][yP-2] and board[xP][yP] == board[xP-2][yP-2]) and (board[xP] and board[xP][yP] and board[xP-3] and board[xP-3][yP-3] and board[xP][yP] == board[xP-3][yP-3])) or ((board[xP] and board[xP][yP] and board[xP-1] and board[xP-1][yP-1] and board[xP][yP] == board[xP-1][yP-1]) and (board[xP] and board[xP][yP] and board[xP-2] and board[xP-2][yP-2] and board[xP][yP] == board[xP-2][yP-2]) and (board[xP] and board[xP][yP] and board[xP+1] and board[xP+1][yP+1] and board[xP][yP] == board[xP+1][yP+1])) or ((board[xP] and board[xP][yP] and board[xP-1] and board[xP-1][yP-1] and board[xP][yP] == board[xP-1][yP-1]) and (board[xP] and board[xP][yP] and board[xP+1] and board[xP+1][yP+1] and board[xP][yP] == board[xP+1][yP+1]) and (board[xP] and board[xP][yP] and board[xP+2] and board[xP+2][yP+2] and board[xP][yP] == board[xP+2][yP+2])) or ((board[xP] and board[xP][yP] and board[xP+1] and board[xP+1][yP+1] and board[xP][yP] == board[xP+1][yP+1]) and (board[xP] and board[xP][yP] and board[xP+2] and board[xP+2][yP+2] and board[xP][yP] == board[xP+2][yP+2]) and (board[xP] and board[xP][yP] and board[xP+3] and board[xP+3][yP+3] and board[xP][yP] == board[xP+3][yP+3])) then
  228.             return true
  229.         end
  230.         return false
  231.     end
  232.    
  233.     function winOD(xP, yP)
  234.         if ((board[xP] and board[xP][yP] and board[xP-1] and board[xP-1][yP+1] and board[xP][yP] == board[xP-1][yP+1]) and (board[xP] and board[xP][yP] and board[xP-2] and board[xP-2][yP+2] and board[xP][yP] == board[xP-2][yP+2]) and (board[xP] and board[xP][yP] and board[xP-3] and board[xP-3][yP+3] and board[xP][yP] == board[xP-3][yP+3])) or ((board[xP] and board[xP][yP] and board[xP-1] and board[xP-1][yP+1] and board[xP][yP] == board[xP-1][yP+1]) and (board[xP] and board[xP][yP] and board[xP-2] and board[xP-2][yP+2] and board[xP][yP] == board[xP-2][yP+2]) and (board[xP] and board[xP][yP] and board[xP+1] and board[xP+1][yP-1] and board[xP][yP] == board[xP+1][yP-1])) or ((board[xP] and board[xP][yP] and board[xP-1] and board[xP-1][yP+1] and board[xP][yP] == board[xP-1][yP+1]) and (board[xP] and board[xP][yP] and board[xP+1] and board[xP+1][yP-1] and board[xP][yP] == board[xP+1][yP-1]) and (board[xP] and board[xP][yP] and board[xP+2] and board[xP+2][yP-2] and board[xP][yP] == board[xP+2][yP-2])) or ((board[xP] and board[xP][yP] and board[xP+1] and board[xP+1][yP-1] and board[xP][yP] == board[xP+1][yP-1]) and (board[xP] and board[xP][yP] and board[xP+2] and board[xP+2][yP-2] and board[xP][yP] == board[xP+2][yP-2]) and (board[xP] and board[xP][yP] and board[xP+3] and board[xP+3][yP-3] and board[xP][yP] == board[xP+3][yP-3])) then
  235.             return true
  236.         end
  237.         return false
  238.     end
  239.    
  240.     function win(xP, yP)
  241.         if winO(xP, yP) or winV(xP, yP) or winD(xP, yP) or winOD(xP, yP) then
  242.             return true
  243.         end
  244.         return false
  245.     end
  246.    
  247.     function getCol()
  248.         while true do
  249.             local event, p1, p2, p3 = os.pullEventRaw()
  250.             if event == "char" then
  251.                 return tonumber(p1)
  252.             elseif event == "mouse_click" then
  253.                 return p2-3
  254.             end
  255.         end
  256.     end
  257.    
  258.     function yourTurn()
  259.         userlog("It's your turn")
  260.         while true do
  261.             userlog("Where do you want to insert the peg?")
  262.             userlog(">")
  263.             term.setCursorPos(3, 13)
  264.             local col = getCol()
  265.             if col >= 1 and col <= 7 then
  266.                 if board[col][last] < 6 then
  267.                     board[col][board[col][last]+1] = 1
  268.                     board[col][last] = board[col][last]+1
  269.                     modem.transmit(opponent, os.getComputerID(), col)
  270.                     refreshRow(col)
  271.                     return win(col, board[col][last])
  272.                 else
  273.                     userlog("This coloumn is full, please, choose another one")
  274.                 end
  275.             else
  276.                 userlog("Coloumn #"..col.." does not exist")
  277.             end
  278.         end
  279.     end
  280.    
  281.     function opponentTurn()
  282.         userlog("It's your opponent turn")
  283.         local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  284.         local col = tonumber(message)
  285.         userlog("The opponent inserted a peg in col. #"..col)
  286.         board[col][board[col][last]+1] = 2
  287.         board[col][last] = board[col][last]+1
  288.         refreshRow(col)
  289.         return win(col, board[col][last])
  290.     end
  291.    
  292.     function winners()
  293.         if win then
  294.             userlog("You win")
  295.         else
  296.             userlog("You lose")
  297.         end
  298.     end
  299.    
  300.     function game()
  301.         screen()
  302.         gameScreen()
  303.         while true do
  304.             if first then
  305.                 yourTurn()
  306.                 first = false
  307.             end
  308.             if opponentTurn() then
  309.                 win = false
  310.                 break
  311.             end
  312.             if yourTurn() then
  313.                 win = true
  314.                 break
  315.             end
  316.         end
  317.         winners()
  318.     end
  319.    
  320. --Main
  321.     if start() then
  322.         login()
  323.         game()
  324.         userlog("Press a key to continue")
  325.         os.pullEventRaw()
  326.     end
  327.     modem.closeAll()
  328.     clear()
Advertisement
Add Comment
Please, Sign In to add comment