Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Ccraft Connect4 by Luiginico
- --Starting
- function start()
- if peripheral.getType("top") == nil or peripheral.getType("top") ~= "modem" then
- print("Modem needs to be on the top (or you can edit the code at --Starting)")
- return false
- end
- return true
- end
- modem = peripheral.wrap("top")
- modem.open(os.getComputerID())
- --Variables
- opponent = 0
- board = {
- {0,0,0,0,0,0,0}, --1
- {0,0,0,0,0,0,0}, --2
- {0,0,0,0,0,0,0}, --3
- {0,0,0,0,0,0,0}, --4
- {0,0,0,0,0,0,0}, --5
- {0,0,0,0,0,0,0}, --6
- {0,0,0,0,0,0,0} --7
- }
- air = 0
- oPeg = 1
- xPeg = 2
- last = 7
- x, y = term.getSize()
- win = true
- first = true
- messages = { }
- blank = " "
- for i=2,x-2 do
- blank = blank.. " "
- end
- for i=13,y-2 do
- messages[i-12] = blank
- end
- --Functions
- function userlog(message)
- for count=1,#messages-1 do
- messages[#messages-count+1] = messages[#messages-count]
- end
- messages[1] = message
- for count=1,#messages do
- term.setCursorPos(2, 12+count)
- write(blank)
- term.setCursorPos(2, 12+count)
- write(messages[count])
- end
- sleep(0.5)
- end
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function screen()
- clear()
- write("+")
- for i=1,x-2 do
- write("-")
- end
- write("+")
- for j=2,y-1 do
- write("|")
- for i=1,x-2 do
- write(" ")
- end
- write("|")
- end
- write("+")
- for i=1,x-2 do
- write("-")
- end
- write("+")
- term.setCursorPos(x/2-15, y-1)
- write("CCraft Connect4, By Luiginico")
- end
- function loginScreen()
- screen()
- term.setCursorPos(x/2-2, 3)
- write("Host")
- term.setCursorPos(x/2-2, 5)
- write("Login")
- term.setCursorPos(x/2-2, 7)
- write("Exit")
- --term.setCursorPos(x/2-5, 9)
- --write("How to play")
- term.setCursorPos(2, y-3)
- write("Click or press the first letter to choose")
- end
- function gameScreen()
- screen()
- term.setCursorPos(3,3)
- write("O_______O")
- for iScreen=1,6 do
- term.setCursorPos(3,3+iScreen)
- write("| |")
- end
- term.setCursorPos(3,10)
- write("O=======O")
- for iS = 1,7 do
- for jS = 1,6 do
- if board[iS][jS] == air then
- term.setCursorPos(iS+3, 10-jS)
- write(".")
- elseif board[iS][jS] == oPeg then
- term.setCursorPos(iS+3, 10-jS)
- write("o")
- elseif board[iS][jS] == xPeg then
- term.setCursorPos(iS+3, 10-jS)
- write("x")
- end
- end
- end
- term.setCursorPos(4,11)
- write("1234567")
- end
- function host()
- screen()
- term.setCursorPos(x/2-11, 3)
- write("Waiting for an opponent")
- term.setCursorPos(x/2-6, 5)
- write("Game ID = " ..os.getComputerID())
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- opponent = replyChannel
- first = true
- end
- function loginGame()
- screen()
- term.setCursorPos(x/2-5, 3)
- write("Connecting")
- term.setCursorPos(x/2-6, 5)
- write("Game ID = ")
- opponent = tonumber(io.read())
- modem.transmit(opponent, os.getComputerID(), " ")
- first = false
- end
- function loginGUI()
- while true do
- local event, p1, p2, p3 = os.pullEventRaw()
- if event == "char" then
- if p1 == "h" then
- host()
- return false
- elseif p1 == "l" then
- loginGame()
- return false
- elseif p1 == "e" then
- clear()
- modem.closeAll()
- error()
- --elseif p1 == "h" then
- --return tutorialScreen()
- end
- elseif event == "mouse_click" then
- if p3 == 3 then
- host()
- return false
- elseif p3 == 5 then
- loginGame()
- return false
- elseif p3 == 7 then
- clear()
- modem.closeAll()
- error()
- --elseif p3 == 9 then
- --return tutorialScreen()
- end
- end
- end
- end
- function login()
- loginScreen()
- loginGUI()
- end
- function refreshRow(row)
- for i = 1,6 do
- if board[row][i] == air then
- term.setCursorPos(row+3, 10-i)
- write(".")
- elseif board[row][i] == oPeg then
- term.setCursorPos(row+3, 10-i)
- write("o")
- elseif board[row][i] == xPeg then
- term.setCursorPos(row+3, 10-i)
- write("x")
- end
- end
- end
- function winO(xP, yP)
- 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
- return true
- end
- return false
- end
- function winV(xP, yP)
- 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
- return true
- end
- return false
- end
- function winD(xP, yP)
- 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
- return true
- end
- return false
- end
- function winOD(xP, yP)
- 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
- return true
- end
- return false
- end
- function win(xP, yP)
- if winO(xP, yP) or winV(xP, yP) or winD(xP, yP) or winOD(xP, yP) then
- return true
- end
- return false
- end
- function getCol()
- while true do
- local event, p1, p2, p3 = os.pullEventRaw()
- if event == "char" then
- return tonumber(p1)
- elseif event == "mouse_click" then
- return p2-3
- end
- end
- end
- function yourTurn()
- userlog("It's your turn")
- while true do
- userlog("Where do you want to insert the peg?")
- userlog(">")
- term.setCursorPos(3, 13)
- local col = getCol()
- if col >= 1 and col <= 7 then
- if board[col][last] < 6 then
- board[col][board[col][last]+1] = 1
- board[col][last] = board[col][last]+1
- modem.transmit(opponent, os.getComputerID(), col)
- refreshRow(col)
- return win(col, board[col][last])
- else
- userlog("This coloumn is full, please, choose another one")
- end
- else
- userlog("Coloumn #"..col.." does not exist")
- end
- end
- end
- function opponentTurn()
- userlog("It's your opponent turn")
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- local col = tonumber(message)
- userlog("The opponent inserted a peg in col. #"..col)
- board[col][board[col][last]+1] = 2
- board[col][last] = board[col][last]+1
- refreshRow(col)
- return win(col, board[col][last])
- end
- function winners()
- if win then
- userlog("You win")
- else
- userlog("You lose")
- end
- end
- function game()
- screen()
- gameScreen()
- while true do
- if first then
- yourTurn()
- first = false
- end
- if opponentTurn() then
- win = false
- break
- end
- if yourTurn() then
- win = true
- break
- end
- end
- winners()
- end
- --Main
- if start() then
- login()
- game()
- userlog("Press a key to continue")
- os.pullEventRaw()
- end
- modem.closeAll()
- clear()
Advertisement
Add Comment
Please, Sign In to add comment