1
0
mirror of https://github.com/LDDestroier/CC/ synced 2024-12-11 18:50:29 +00:00

Update connect4.lua

This commit is contained in:
LDDestroier 2018-12-13 18:07:25 -05:00 committed by GitHub
parent 1abb9a8450
commit f87ee38392
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,9 @@ local scr_x, scr_y = term.getSize()
local midX, midY = .5 * scr_x, .5 * scr_y local midX, midY = .5 * scr_x, .5 * scr_y
local origTX, origBG = term.getTextColor(), term.getBackgroundColor() local origTX, origBG = term.getTextColor(), term.getBackgroundColor()
local winLength = 4
local sleepDelay = 0.05 local sleepDelay = 0.05
local moveCount = 0
local board = {} -- connect 4 board; formatted like board[y][x] local board = {} -- connect 4 board; formatted like board[y][x]
local block = {} -- bottom blockage; formatted like block[x] local block = {} -- bottom blockage; formatted like block[x]
@ -26,15 +28,17 @@ local tiles = {
local you = "P1" local you = "P1"
local nou = "P2" local nou = "P2"
local cwrite = function(text, y) local cwrite = function(text, y, doClear)
local cx, cy = term.getCursorPos() local cx, cy = term.getCursorPos()
term.setCursorPos(midX - math.floor(#text / 2), y or cy) term.setCursorPos(midX - math.floor(#text / 2), y or cy)
if doClear then term.clearLine() end
term.write(text) term.write(text)
end end
local cblit = function(char, text, back, y) local cblit = function(char, text, back, y, doClear)
local cx, cy = term.getCursorPos() local cx, cy = term.getCursorPos()
term.setCursorPos(midX - math.floor(#text / 2), y or cy) term.setCursorPos(midX - math.floor(#text / 2), y or cy)
if doClear then term.clearLine() end
term.blit(char, text, back) term.blit(char, text, back)
end end
@ -53,7 +57,7 @@ end
local addPiece = function(owner, x) local addPiece = function(owner, x)
if board[1][x][1] == "bl" then if board[1][x][1] == "bl" then
board[1][x] = {owner, 0, x, -1} board[1][x] = {owner, 0, x, 1}
return true return true
else else
return false return false
@ -135,47 +139,36 @@ term.clear()
local checkIfWinner = function() local checkIfWinner = function()
local conditions = {} local conditions = {}
local winLength = 4
-- check horizontal -- check horizontal
for y = 1, boardY do for y = 1, boardY do
for x = 1, boardX - winLength + 1 do for x = 1, boardX - winLength + 1 do
conditions[#conditions+1] = { conditions[#conditions+1] = {}
board[y][x+0], for w = 0, winLength - 1 do
board[y][x+1], conditions[#conditions][w+1] = board[y][x+w]
board[y][x+2], end
board[y][x+3]
}
end end
end end
-- check vertical -- check vertical
for y = 1, boardY - winLength + 1 do for y = boardY - winLength + 1, 1, -1 do
for x = 1, boardX do for x = 1, boardX do
conditions[#conditions+1] = { conditions[#conditions+1] = {}
board[y+0][x], for w = 0, winLength - 1 do
board[y+1][x], conditions[#conditions][w+1] = board[y+w][x]
board[y+2][x], end
board[y+3][x]
}
end end
end end
-- check diagonals -- check diagonals
for y = 1, boardY - winLength + 1 do for y = 1, boardY - winLength + 1 do
for x = 1, boardX - winLength + 1 do for x = 1, boardX - winLength + 1 do
conditions[#conditions+1] = { conditions[#conditions+1] = {}
board[y+3][x+0], conditions[#conditions+1] = {}
board[y+2][x+1], for w = 0, winLength - 1 do
board[y+1][x+2], conditions[#conditions-1][w+1] = board[y+(winLength-w-1)][x+w]
board[y+0][x+3] conditions[#conditions][w+1] = board[y+w][x+w]
} end
conditions[#conditions+1] = {
board[y+0][x+0],
board[y+1][x+1],
board[y+2][x+2],
board[y+3][x+3]
}
end end
end end
@ -208,7 +201,7 @@ local renderBoard = function()
term.setCursorPos(midX - (boardX) + (x - 1) * #tileChar[1][1], 4) term.setCursorPos(midX - (boardX) + (x - 1) * #tileChar[1][1], 4)
term.write(x) term.write(x)
end end
cwrite("SPACE to clear", scr_y) cwrite("SPACE to clear", scr_y, false)
else else
for ymod = 1, #tileChar[1] do for ymod = 1, #tileChar[1] do
for x = 0, boardX do for x = 0, boardX do
@ -246,9 +239,12 @@ local getInput = function()
if tonumber(evt[2]) then if tonumber(evt[2]) then
if tonumber(evt[2]) >= 1 and tonumber(evt[2]) <= boardX then if tonumber(evt[2]) >= 1 and tonumber(evt[2]) <= boardX then
if not waiting then if not waiting then
addPiece(you, tonumber(evt[2])) if board[1][tonumber(evt[2])][1] == "bl" then
you, nou = nou, you addPiece(you, tonumber(evt[2]))
waiting = true moveCount = moveCount + 1
you, nou = nou, you
waiting = true
end
end end
end end
end end
@ -263,6 +259,7 @@ local getInput = function()
block[x] = false block[x] = false
sleep(0.05) sleep(0.05)
end end
moveCount = 0
you, nou = "P1", "P2" you, nou = "P1", "P2"
sleep(1) sleep(1)
for x = 1, boardX do for x = 1, boardX do
@ -286,13 +283,12 @@ local main = function()
winner, winPieces = checkIfWinner() winner, winPieces = checkIfWinner()
term.setTextColor(palette.txt) term.setTextColor(palette.txt)
if winner then if winner then
term.setCursorPos(1,1)
term.clearLine()
cblit( cblit(
"Winner: " .. winner, "Winner: " .. winner,
to_blit[palette.txt]:rep(8) .. to_blit[tiles[winner]]:rep(#winner), to_blit[palette.txt]:rep(8) .. to_blit[tiles[winner]]:rep(#winner),
to_blit[palette.bg]:rep(8 + #winner), to_blit[palette.bg]:rep(8 + #winner),
1 1,
true
) )
parallel.waitForAny(function() parallel.waitForAny(function()
while true do while true do
@ -313,15 +309,17 @@ local main = function()
evt = {os.pullEvent()} evt = {os.pullEvent()}
until evt[1] == "clear_board" until evt[1] == "clear_board"
end) end)
elseif moveCount >= boardX * boardY then
cwrite("It's a tie.", 1, true)
waiting = true
else else
term.setCursorPos(1,1)
term.clearLine()
waiting = false waiting = false
cblit( cblit(
"It's " .. you .. "'s turn.", "It's " .. you .. "'s turn.",
to_blit[palette.txt]:rep(5) .. to_blit[tiles[you]]:rep(#you) .. to_blit[palette.txt]:rep(8), to_blit[palette.txt]:rep(5) .. to_blit[tiles[you]]:rep(#you) .. to_blit[palette.txt]:rep(8),
to_blit[palette.bg]:rep(13 + #you), to_blit[palette.bg]:rep(13 + #you),
1 1,
true
) )
end end
sleep(sleepDelay) sleep(sleepDelay)
@ -329,6 +327,6 @@ local main = function()
end end
parallel.waitForAny(main, getInput) parallel.waitForAny(main, getInput)
cwrite("Thanks for playing!", 1) cwrite("Thanks for playing!", 1, true)
term.setCursorPos(1, scr_y) term.setCursorPos(1, scr_y)
term.clearLine() term.clearLine()