In my game I've made a feature where you can sprint.
I've separated the animation part of the script from the main sprinting part.
I got the error message "Workspace.Vlo_tz.SprintingAnimation:19: Expected ')' (to close '(' at column 29), got ','".
This code is for the animation of the character and the sprinting bar
local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
local players = game:GetService("Players")
local stamina = players.LocalPlayer.PlayerGui.StaminaHealth.MainFrame.Stamina
UIS.InputBegan:connect(function(input)
local humanoid = Player.Character.Humanoid
if input.KeyCode == Enum.KeyCode.LeftShift and humanoid:GetState() == Enum.HumanoidStateType.RunningNoPhysics and humanoid.MoveDirection.Magnitude > 0 then
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://05168161960'
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
while true do
wait(0.05)
if stamina.Position <= (-0.343, 0, 0.274, 0) then
stamina.Position = UDim2.new(-0.343, 0, 0, 0)
else
stamina.Position = stamina.Position - UDim2.new(0.01, 0, 0, 0)
end
end
end
end)
UIS.InputEnded:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
PlayAnim:Stop()
while true do
wait(0.005)
if stamina.Position >= (0.487, 0, 0.274, 0) then
stamina.Position = UDim2.new(0.487, 0, 0, 0)
else
stamina.Position = stamina.Position + UDim2.new(0.01, 0, 0, 0)
end
end
end
end)
local humanoid = Player.Character.Humanoid
if humanoid.MoveDirection.Magnitude == 0 then
PlayAnim:Stop()
while true do
wait(0.005)
if stamina.Position >= (0.487, 0, 0.274, 0) then
stamina.Position = UDim2.new(0.487, 0, 0, 0)
else
stamina.Position = stamina.Position + UDim2.new(0.01, 0, 0, 0)
end
end
end
Line 19 is this : if stamina.Position <= (-0.343, 0, 0.274, 0) then
You have this check in a few different places, but this is not valid lua and you cannot compare UDim2 values like this. You'll have to inspect the values individually.
local isLessThanX = stamina.Position.X.Scale <= -0.343
local isLessThanY = stamina.Position.Y.Scale <= 0.274
if isLessThanX and isLessThanY then
Related
I don't know where to put the variables.
local frontflips, backflips = 0, 0
local upVector = cv.DesiredCF.UpVector
local lastDirection = "None"
-- local process = "None" -- FRontflip or backflip
local physics; physics = game:GetService("RunService").Heartbeat:Connect(function()
local lookVector = cart.PrimaryPart.CFrame.LookVector
local dotProduct = lookVector:Dot(upVector)
if ( math.abs(dotProduct) < .3) then
--// Facing downwards
if (lastDirection == "backflip") then
backflips = backflips + 1
frontflips = math.clamp(frontflips - 1, 0, 10)
elseif lastDirection == "frontflip" then
frontflips = frontflips + 1
backflips = math.clamp(backflips - 1, 0, 10)
end
lastDirection = "None"
elseif (dotProduct > .8) and lastDirection == "None" then
--// In the process of a back-flip
lastDirection = "backflip"
elseif dotProduct < -.8 and lastDirection == "None" then
lastDirection = "frontflip"
end
-- warn("Dot is ",dotProduct, " and ",lastDirection)
end)
When I do 1 frontflip it tells me
But now when I do 2 frontflips it tells me I only did 1.
If I remove the
frontflips = math.clamp(frontflips - 1, 0, 10)
and
backflips = math.clamp(backflips - 1, 0, 10)
then when I do 2 frontflips
the result tells me
I have performed 2 frontflips and 1 backflip.
i am making a health bar to my humanoid/enemy but it keeps saying attempt to index nil with health
here is the code:
local Humanoid = script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")
while true do
wait(0.1)
if Humanoid.Health <= 100 then
script.Parent.Healthbar.BackgroundColor3 = Color3.fromRGB(17, 255, 0)
end
if Humanoid.Health <= 50 then
script.Parent.Healthbar.BackgroundColor3 = Color3.fromRGB(255, 255, 0)
end
script.Parent.Healthbar.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
end
script.Parent.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth)
local pie = (Humanoid.Health / Humanoid.MaxHealth)
script.Parent.Healthbar.Size = UDim2.new(pie, 0, 1, 0)
end
So, I am trying to make a game that replicates pong, and I am currently stuck on one piece. I want it so that when the ball collides with either paddle, then it will change colour. Except it doesn't do as I say. It would be much appreciated if anyone can give a tip as to fixing this issue I am currently having, anyways, here is the code. I am only giving the main file and ball class, as those are the only relevant item I think you will need. This is written in Lua with the help of Love2D. This is an old version of Love2D, to be exact 0.10.2
main.lua:
push = require 'push'
Class = require 'class'
require 'Paddle'
require 'Ball'
windowWidth = 1280
windowHeight = 720
virtualWidth = 432
virtualHeight = 243
paddleSpeed = 200
-- Boolean values that can be changed if we want a paddle to become an AI
AIMode = false -- left paddle AI
AIMode_2 = false -- right paddle AI
AISpeed = 0
function love.load()
love.graphics.setDefaultFilter('nearest', 'nearest')
love.window.setTitle('Pong')
math.randomseed(os.time())
smallFont = love.graphics.newFont('font.ttf', 8)
largeFont = love.graphics.newFont('font.ttf', 16)
scoreFont = love.graphics.newFont('font.ttf', 32)
sounds = {
['paddle_hit'] = love.audio.newSource('sounds/paddle_hit.wav', 'static'),
['score'] = love.audio.newSource('sounds/score.wav', 'static'),
['wall_hit'] = love.audio.newSource('sounds/wall_hit.wav', 'static')
}
love.graphics.setFont(smallFont)
push:setupScreen(virtualWidth, virtualHeight, windowWidth, windowHeight, {
fullscreen = false,
resizable = false,
vsync = true
})
p1_score = 0
p2_score = 0
servingPlayer = 1
winningPlayer = 0
player_1 = Paddle(10, 30, 5, 20)
player_2 = Paddle(virtualWidth - 10, virtualHeight - 30, 5, 20)
ball = Ball(virtualWidth / 2 - 2, virtualHeight / 2 - 2, 4, 4)
gameState = 'start'
end
function love.update(dt)
if gameState == 'serve' then
ball.dy = math.random(-50, 50)
if servingPlayer == 1 then
ball.dx = math.random(140, 200)
else
ball.dx = -math.random(140, 200)
end
elseif gameState == 'play' then
if ball:collides(player_1) then
ball.dx = -ball.dx * 1.03
ball.x = player_1.x + 5
if ball.dy < 0 then
ball.dy = -math.random(10, 150)
else
ball.dy = math.random(10, 150)
end
sounds['paddle_hit']:play()
end
if ball:collides(player_2) then
ball.dx = -ball.dx * 1.03
ball.x = player_2.x - 4
if ball.dy < 0 then
ball.dy = -math.random(10, 150)
else
ball.dy = math.random(10, 150)
end
sounds['paddle_hit']:play()
end
if ball.y <= 0 then
ball.y = 0
ball.dy = -ball.dy
sounds['wall_hit']:play()
end
if ball.y >= virtualHeight - 4 then
ball.y = virtualHeight - 4
ball.dy = -ball.dy
sounds['wall_hit']:play()
end
end
if ball.x < 0 then
ball:reset()
gameState = 'serve'
servingPlayer = 1
p2_score = p2_score + 1
sounds['score']:play()
end
if p2_score == 10 then
winningPlayer = 2
gameState = 'done'
end
if ball.x > virtualWidth then
ball:reset()
gameState = 'serve'
servingPlayer = 2
p1_score = p1_score + 1
sounds['score']:play()
end
if p1_score == 10 then
winningPlayer = 1
gameState = 'done'
end
if AIMode == true and AIMode_2 == true then
if p1_score == 3 then
winningPlayer = 1
gameState = 'done'
elseif p2_score == 3 then
winningPlayer = 2
gameState = 'done'
end
end
-- For Single Player Mode
-- If only the left paddle is AI, then left paddle's Y position will be the same as the ball's Y position. Only the right will be controlled by the player by pressing keys 'up', and 'down'
if AIMode == true and AIMode_2 == false then
player_1.y = ball.y
if love.keyboard.isDown('up') then
player_2.dy = -paddleSpeed
elseif love.keyboard.isDown('down') then
player_2.dy = paddleSpeed
else
player_2.dy = 0
end
end
-- For Auto Mode
-- If both paddles are AI, then both paddle's y position will be the same as the ball's y postion
if AIMode == true and AIMode_2 == true then
player_1.y = ball.y
player_2.y = ball.y
end
-- For 2 Player Mode
-- If no paddles are AI, then left paddle will be controlled by pressing 'w' and 's', and the right paddle will be controlled by pressing 'up' and 'down'
if AIMode == false and AIMode_2 == false then
if love.keyboard.isDown('w') then
player_1.dy = -paddleSpeed
elseif love.keyboard.isDown('s')then
player_1.dy = paddleSpeed
else
player_1.dy = 0
end
if love.keyboard.isDown('up') then
player_2.dy = -paddleSpeed
elseif love.keyboard.isDown('down') then
player_2.dy = paddleSpeed
else
player_2.dy = 0
end
end
if gameState == 'play' then
ball:update(dt)
end
player_1:update(dt)
player_2:update(dt)
end
function love.keypressed(key)
-- if key '1' is pressed in start screen, then left paddle will become AI
-- Single Player Mode
if key == '1' and gameState == 'start' then
AIMode = true
AIMode_2 = false
-- if key '2' is pressed in start screen, then both paddles will become AI
-- Auto Mode
elseif key == '2' and gameState == 'start' then
AIMode_2 = true
AIMode = true
-- if key '3' is pressed, then no paddles will become AI
-- 2 Player Mode
elseif key == '3' and gameState == 'start' then
AIMode = false
AIMode_2 = false
end
if key == 'escape' then
love.event.quit()
elseif key == 'enter' or key == 'return' then
if gameState == 'start' then
gameState = 'serve'
elseif gameState == 'serve' then
gameState = 'play'
elseif gameState == 'done' then
ball:reset()
p1_score = 0
p2_score = 0
if winningPlayer == 1 then
servingPlayer = 2
else
servingPlayer = 1
end
if key == 'enter' or key == 'return' then
gameState = 'serve'
end
-- if playing and enter is pressed, then ball will reset, and player will be brought back to the start screen
-- soft reset
elseif gameState == 'play' then
gameState = 'start'
ball:reset()
end
end
-- if 'r' is pressed in any state, then all score will reset, as well as the entire game, and player will be brought back to the start screen
-- hard reset
if key == 'r' then
if gameState == 'start' or gameState == 'serve' or gameState == 'play' or gameState == 'done' then
gameState = 'start'
p1_score = 0
p2_score = 0
ball:reset()
end
end
end
function love.draw()
push:apply('start')
love.graphics.clear(40, 45, 52, 255)
love.graphics.setFont(smallFont)
displayScore()
if gameState == 'start' then
love.graphics.setFont(smallFont)
if AIMode == true and AIMode_2 == false then
love.graphics.printf('AI Mode ON! Computer vs. Player!', 0, 30, virtualWidth, 'center')
elseif AIMode_2 == true and AIMode == true then
love.graphics.printf('AI Mode ON! Computer vs. Computer!', 0, 30, virtualWidth, 'center')
elseif AIMode == false and AIMode_2 == false then
love.graphics.printf('AI Mode OFF! 2 Player Mode!', 0, 30, virtualWidth, 'center')
end
love.graphics.printf('Welcome to Pong', 0, 10, virtualWidth, 'center')
love.graphics.printf('Press Enter to Begin! Press 1 for Single Player Mode, 2 for Automatic Mode, 3 for 2 Player Mode!', 0, 20, virtualWidth, 'center')
elseif gameState == 'serve' then
if AIMode == false and AIMode_2 == false then
love.graphics.setFont(smallFont)
love.graphics.printf('Player ' .. tostring(servingPlayer) .. "'s serve!", 0, 10, virtualWidth, 'center')
love.graphics.printf('Press Enter to Serve!', 0, 20, virtualWidth, 'center')
elseif AIMode == true and AIMode_2 == false then
if servingPlayer == 1 then
love.graphics.setFont(smallFont)
love.graphics.printf('Computer Serve!', 0, 10, virtualWidth, 'center')
love.graphics.printf('Press Enter to Serve!', 0, 20, virtualWidth, 'center')
else
love.graphics.setFont(smallFont)
love.graphics.printf('Player Serve!', 0, 10, virtualWidth, 'center')
love.graphics.printf('Press Enter to Serve!', 0, 20, virtualWidth, 'center')
end
elseif AIMode == true and AIMode_2 == true then
love.graphics.setFont(smallFont)
love.graphics.printf('Computer ' .. tostring(servingPlayer) .. "'s serve!", 0, 10, virtualWidth, 'center')
love.graphics.printf('Press Enter to Serve!', 0, 20, virtualWidth, 'center')
end
elseif gameState == 'play' then
elseif gameState == 'done' then
if AIMode == false and AIMode_2 == false then
love.graphics.setFont(largeFont)
love.graphics.printf('Player ' .. tostring(winningPlayer) .. ' wins!', 0, 10, virtualWidth, 'center')
love.graphics.setFont(smallFont)
love.graphics.printf('Press Enter to Restart!', 0, 30, virtualWidth, 'center')
elseif AIMode == true and AIMode_2 == false then
if winningPlayer == 1 then
love.graphics.setFont(largeFont)
love.graphics.printf('Computer Wins!', 0, 10, virtualWidth, 'center')
love.graphics.setFont(smallFont)
love.graphics.printf('Press Enter to Restart!', 0, 30, virtualWidth, 'center')
else
love.graphics.setFont(largeFont)
love.graphics.printf('Player Wins!', 0, 10, virtualWidth, 'center')
love.graphics.setFont(smallFont)
love.graphics.printf('Press Enter to Restart!', 0, 30, virtualWidth, 'center')
end
elseif AIMode == true and AIMode_2 == true then
love.graphics.setFont(largeFont)
love.graphics.printf('Computer ' .. tostring(winningPlayer) .. ' wins!', 0, 10, virtualWidth, 'center')
love.graphics.setFont(smallFont)
love.graphics.printf('Press Enter to Restart!', 0, 30, virtualWidth, 'center')
end
end
love.graphics.setColor(255, 0, 0, 255)
player_1:render()
love.graphics.setColor(255, 255, 0, 255)
player_2:render()
love.graphics.setColor(255, 255, 255, 255)
-- this is where I am having the issue
if ball:collides(player_1) then
love.graphics.setColor(255, 0, 0, 255)
elseif ball:collides(player_2) then
love.graphics.setColor(255, 255, 0, 255)
end
ball:render()
displayFPS()
push:apply('end')
end
function displayFPS()
love.graphics.setFont(smallFont)
love.graphics.setColor(0, 255, 0, 255)
love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 10, 10)
end
function displayScore()
love.graphics.setFont(scoreFont)
love.graphics.setColor(255, 0, 0, 255)
love.graphics.print(tostring(p1_score), virtualWidth / 2 - 50,
virtualHeight / 3)
love.graphics.setColor(255, 255, 0, 255)
love.graphics.print(tostring(p2_score), virtualWidth / 2 + 30,
virtualHeight / 3)
love.graphics.setColor(255, 255, 255, 255)
end
Ball.lua:
Ball = Class{}
function Ball:init(x, y, width, height)
self.x = x
self.y = y
self.width = width
self.height = height
self.dy = math.random(2) == 1 and -100 or 100
self.dx = math.random(2) == 1 and math.random(-80, -100) or math.random(80, 100)
end
function Ball:collides(paddle)
if self.x > paddle.x + paddle.width or paddle.x > self.x + self.width then
return false
end
if self.y > paddle.y + paddle.height or paddle.y > self.y + self.height then
return false
end
love.graphics.setColor(math.random(0, 255), math.random(0, 255), math.random(0, 255), 255)
return true
end
function Ball:reset()
self.x = virtualWidth / 2 - 2
self.y = virtualHeight / 2 - 2
self.dy = math.random(2) == 1 and -100 or 100
self.dx = math.random(-50, 50)
end
function Ball:update(dt)
self.x = self.x + self.dx * dt
self.y = self.y + self.dy * dt
end
function Ball:render()
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
end
Paddle.lua
Paddle = Class{}
function Paddle:init(x, y, width, height)
self.x = x
self.y = y
self.width = width
self.height = height
self.dy = 0
end
function Paddle:update(dt)
if self.dy < 0 then
self.y = math.max(0, self.y + self.dy * dt)
else
self.y = math.min(virtualHeight - self.height, self.y + self.dy * dt)
end
end
function Paddle:render()
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
end
nevermind I did some stuff, and made a new variable called ballCollDetect, where the default value is 0. If it hits the left paddle, then it turns into 1, and if it hits right paddle, then it turns into 2. So, if it = 1, then the ball's colour will be red, and if it = 2, then the ball's colour will be yellow, and the default value will just make the ball's colour white. Pretty simple actually.
I'm making my script and the Script Analysis tool says
Error: (54,2) Expected , got 'end'
I thought maybe you guys might help.
Here is the code
-- to be placed in StarterPlayer > StarterPlayerScripts
local Players = game:GetService("Players")
-- wait for local player PlayerGui
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- create a ScreenGui
local screenGui = Instance.new("ScreenGui", playerGui)
-- create a holder for our bar
local frame = Instance.new("Frame", screenGui)
frame.AnchorPoint = Vector2.new(0.0, 0.0)
frame.Position = UDim2.new(0.0, 0, 0.0, 0)
frame.Size = UDim2.new(0.0, 0, 0.0, 0)
-- create a bar
local bar = Instance.new("Frame", frame)
bar.Position = UDim2.new(0, 0, 0, 0)
bar.Size = UDim2.new(1, 0, 1, 0)
bar.BackgroundColor3 = Color3.new(0, 1, 0)
-- create a sound
local sound = Instance.new("Sound", screenGui)
local lastLoudness = 0
sound.SoundId = "rbxassetid://697821987"
sound.Looped = true
sound:Play()
-- define a max loudness
local maxLoudness = 30
-- animate the amplitude bar
while true do
local amplitude = math.clamp(sound.PlaybackLoudness / maxLoudness, 0, 1)
print(80-(game.Workspace.CurrentCamera.FieldOfView))
bar.Size = UDim2.new(sound.PlaybackLoudness / maxLoudness, 0, 0, 0)
wait(0.00001)
local lastLoudness = 0
local PBS = ((sound.PlaybackLoudness/50)^(sound.PlaybackLoudness/50))
if lastLoudness~=0 then
local formula = math.abs((lastLoudness*15)) + ((lastLoudness+PBS)/1.9)
if (math.abs(PBS) > formula) == true then
game.Workspace.CurrentCamera.FieldOfView = (lastLoudness)
if game.Workspace.CurrentCamera.FieldOfView <= 10 then
print(formula, PBS)
else
game.Workspace.CurrentCamera.FieldOfView = 0
print(formula, PBS)
end
end
end
end
end
end
I genuinely Don't know how to fix the code, So...
Could you guys Help me?
I spent a long time (a couple of hours) on this and I'm legitimately stuck in the deepest pits of coding hell. And I want this fixed SO BADLY.
Also, Just To Clarify, The script is supposed to change FOV when sound.PlaybackLoudness ÷ 50 × sound.PlaybackLoudness ÷ 50 is over 0.
Egor Skriptunoff is right. Let me reformat your while-loop to show you why.
-- animate the amplitude bar
while true do
local amplitude = math.clamp(sound.PlaybackLoudness / maxLoudness, 0, 1)
print(80-(game.Workspace.CurrentCamera.FieldOfView))
bar.Size = UDim2.new(sound.PlaybackLoudness / maxLoudness, 0, 0, 0)
wait(0.00001)
local lastLoudness = 0
local PBS = ((sound.PlaybackLoudness/50)^(sound.PlaybackLoudness/50))
if lastLoudness~=0 then
local formula = math.abs((lastLoudness*15)) + ((lastLoudness+PBS)/1.9)
if (math.abs(PBS) > formula) == true then
game.Workspace.CurrentCamera.FieldOfView = (lastLoudness)
if game.Workspace.CurrentCamera.FieldOfView <= 10 then
print(formula, PBS)
else
game.Workspace.CurrentCamera.FieldOfView = 0
print(formula, PBS)
end
end
end
end
end -- <-- these bad boys don't go with anything
end -- <-- just remove them to get rid of the error
EDIT : Here's a more complete answer to help with your other script issues. You were resizing your animation stuff to 0 pixels tall, so you couldn't see anything.
-- to be placed in StarterPlayer > StarterPlayerScripts
local Players = game:GetService("Players")
-- wait for local player PlayerGui
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- create a ScreenGui
local screenGui = Instance.new("ScreenGui", playerGui)
-- create a holder for our bar
local frame = Instance.new("Frame", screenGui)
frame.AnchorPoint = Vector2.new(0.0, 0.0)
frame.Position = UDim2.new(0.0, 0, 0.0, 0)
frame.Size = UDim2.new(1.0, 0, 0.0, 50) -- <-- this is no longer invisible
-- create a bar
local bar = Instance.new("Frame", frame)
bar.Position = UDim2.new(0, 0, 0, 0)
bar.Size = UDim2.new(1, 0, 1, 0)
bar.BackgroundColor3 = Color3.new(0, 1, 0)
-- create a sound
local sound = Instance.new("Sound", screenGui)
local lastLoudness = 0
sound.SoundId = "rbxassetid://697821987"
sound.Looped = true
sound:Play()
-- define a max loudness
local maxLoudness = 30.0 -- <-- this needed to be a decimal value
local lastLoudness = 0
-- animate the amplitude bar
while true do
-- PlaybackLoudness values range from 0 to 500, so downscale it
local sampledLoundness = (sound.PlaybackLoudness / 50)
local amplitude = math.clamp(sampledLoundness / maxLoudness, 0, 1)
print("sound values : ", sound.PlaybackLoudness, maxLoudness, amplitude)
-- animate the bar
bar.Size = UDim2.new(amplitude, 0, 1, 0) -- <-- this was squashed to 0 pixels
wait(0.00001)
-- create a camera shake effect
-- NOTE - not sure what the expected behavior is here, it just zooms in
local PBS = sampledLoundness ^ sampledLoundness
if lastLoudness ~=0 then
local formula = math.abs(lastLoudness * 15) + ((lastLoudness + PBS) / 1.9)
if (math.abs(PBS) > formula) == true then
game.Workspace.CurrentCamera.FieldOfView = lastLoudness
if game.Workspace.CurrentCamera.FieldOfView <= 10 then
print("FOV is less than 10 : ", formula, PBS)
else
game.Workspace.CurrentCamera.FieldOfView = 0
print("FOV forced to 0 : ", formula, PBS)
end
end
end
-- update lastLoudness and loop
lastLoudness = sampledLoundness
end
function table_merge(t1, t2)
for _, v in ipairs(t2) do
table.insert(t1, v)
end
end
function getMaster(tbl, rules)
local result = false
for _, rule in ipairs(rules) do
for i, v in ipairs(tbl) do
result = v
if tostring(v) ~= tostring(rule) then
result = false
break
end
end
if result then break end
end
return result
end
function start(data, rules)
local master_key, master_val
local _temp, continue = {}, true
for i, tbl in ipairs(data) do
local master = getMaster(tbl, rules)
if master and master ~= master_val then
continue = true
end
if continue then
if master then
master_key = i
master_val = master
elseif tbl[#tbl] == master_val then
tbl[#tbl] = nil
table.insert(_temp[master_key], master_val)
elseif master_key then
continue = false
end
end
_temp[i] = tbl
end
local result = {}
for i, tbl in ipairs(_temp) do
table_merge(result, tbl)
end
return table.concat(result, "")
end
-- RULES
local rules = { 0, 1}
local data = {
{ 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 1, 1, 0 },
{ 0, 0, 0, 8, 1, 0 },
{ 1, 1, 1, 1, 8, 8 },
{ 0, 0, 0, 0, 0, 0 },
}
start(data, rules)
OUTPUT:
000000001111100081111188000000
The expected results should be this:
000000001111110008111188000000
How do I achieve the required results? the table can contain more than six elements but initially it is six. so when all the elements are 1, it will check the next immediate Table to see if the last element is also one; if true it removes it and adds to the six making seven, it will then check again for the next immediate table if the last digit is 1, if true it removes that also and adds to the seven making 8. It will check again and if false return to where it left. So here the table whose element were removed become five in number.
So I was hoping that they could satisfy the rule also if all the five elemnts are 1 or 0. But it seems only the six elements satisfy the rule...
I hope it's clear Thanks
function table_merge(t1, t2)
for _, v in ipairs(t2) do
table.insert(t1, v)
end
end
function getMaster(tbl, rules, w)
local result = false
for _, rule in ipairs(rules) do
for i = 1, w do
local v = tbl[i]
result = v
if tostring(v) ~= tostring(rule) then
result = false
break
end
end
if result then break end
end
return result
end
function start(data, rules)
local width = #data[1] -- assuming all data rows have the same width
local master_keys = {}
local master_values = {}
local continue_idx = width + 1
for i, tbl in ipairs(data) do
for w = width, 1, -1 do
if w >= continue_idx and tbl[w] == master_values[w] then
table.insert(data[master_keys[w]], master_values[w])
tbl[w] = nil
else
local master = getMaster(tbl, rules, w)
if master then
master_keys[w] = i
master_values[w] = master
continue_idx = w
else
continue_idx = w + 1
end
break
end
end
end
local result = {}
for i, tbl in ipairs(data) do
table_merge(result, tbl)
end
return table.concat(result, "")
end
-- RULES
local rules = { 0, 1 }
local data = {
{ 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 1, 1, 0 },
{ 0, 0, 0, 8, 1, 0 },
{ 1, 1, 1, 1, 8, 8 },
{ 0, 0, 0, 0, 0, 0 },
}
print(start(data, rules))