WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   lua language (https://www.wowinterface.com/forums/showthread.php?t=50627)

analytical 11-30-14 05:57 PM

lua language
 
1 Attachment(s)
Hi!

I am trying to create a function with a single argument. Then this function has to pick random square from the grid to start building the ship. There is no error, I don't know what's wrong. function addship can't build any ship...

gridWidth = 9
gridHeight = 14

grid = {}

for i=1, gridWidth, 1 do
grid[i] = {}
end


function tappedRect(event)
if event.target.ship then
event.target.ship = true
event.target:setFillColor(255, 0, 0)

else
event.target.ship = false
event.target:setFillColor(0,255, 0)

end
end






squareWidth = 25
squareHeight = 25
gap = 5


for i =1, gridWidth, 1 do
for j =1, gridHeight, 1 do

grid[i][j] = display.newRect((gap* i) + (i*squareWidth) , (gap* j) + (j* squareHeight), squareWidth, squareHeight)

end
end





for i =1, gridWidth, 1 do
for j =1, gridHeight, 1 do
grid[i][j].ship = false

grid[i][j]:addEventListener("tap", tappedRect)


end
end



function addShip(shipSize)
x = math.random(1, gridWidth)
y = math.random(1, gridHeight)

for i = 1, shipSize-1, 1 do
if grid[x-1][y].ship then
grid[x-i][y]:setFillColor(255,0,0)


end
end
end



addShip(2)





Please Help!

Torhal 11-30-14 10:41 PM

This is a forum for World of Warcraft AddOns. I don't even know what this is that you just posted, but it isn't that.

Duugu 12-02-14 10:35 AM

As you initialize your grid with
Lua Code:
  1. grid[i][j].ship = false
I would guess that
Lua Code:
  1. if grid[x-1][y].ship then
is true for all grid elements.

Try
Lua Code:
  1. if grid[x-1][y].ship == false then

AlleyKat 12-02-14 10:55 AM

First, to build a ship, you need some timber

sirann 12-02-14 11:13 AM

First I had to clean up your code lol:
Lua Code:
  1. gridWidth = 9
  2. gridHeight = 14
  3. grid = {}
  4.  
  5. for i=1, gridWidth, 1 do   
  6.     grid[i] = {}
  7. end
  8.  
  9. function tappedRect(event)
  10.     if event.target.ship then  
  11.         event.target.ship = true   
  12.         event.target:setFillColor(255, 0, 0)   
  13.     else
  14.         event.target.ship = false
  15.         event.target:setFillColor(0,255, 0)
  16.     end
  17. end
  18.  
  19. squareWidth = 25   
  20. squareHeight = 25  
  21. gap = 5
  22.  
  23. for i =1, gridWidth, 1 do  
  24.     for j =1, gridHeight, 1 do
  25.         grid[i][j] = display.newRect((gap* i) + (i*squareWidth) , (gap* j) + (j* squareHeight), squareWidth, squareHeight)
  26.     end
  27. end
  28.  
  29. for i =1, gridWidth, 1 do  
  30.     for j =1, gridHeight, 1 do
  31.         grid[i][j].ship = false
  32.         grid[i][j]:addEventListener("tap", tappedRect) 
  33.     end
  34. end
  35.  
  36. function addShip(shipSize)
  37.     x = math.random(1, gridWidth)
  38.     y = math.random(1, gridHeight)
  39.     for i = 1, shipSize-1, 1 do
  40.         if grid[x-1][y].ship then
  41.             grid[x-i][y]:setFillColor(255,0,0)
  42.         end
  43.     end
  44. end
  45.  
  46. addShip(2)

Then I saw
Lua Code:
  1. for i=1, gridWidth, 1 do    
  2.     grid[i] = {}
  3. end
which I'm pretty sure creates nested tables at keys of 1-9-1, which is a bit redundant. Should just be
Lua Code:
  1. for i = 1, gridWidth do

Then you try to perform an if check that will always be false as you never declared the event table:
Lua Code:
  1. if event.target.ship then  
  2.         event.target.ship = true    
  3.         event.target:setFillColor(255, 0, 0)    
  4.     else
  5.         event.target.ship = false
  6.         event.target:setFillColor(0,255, 0)
  7.     end

More redundancy of for loops over writing table values
Lua Code:
  1. for i =1, gridWidth, 1 do  
  2.     for j =1, gridHeight, 1 do

So, quite a bit of work (more than I posted) needs to be done to make this functional!

SDPhantom 12-02-14 02:24 PM

Quote:

Originally Posted by sirann (Post 301591)
Then you try to perform an if check that will always be false as you never declared the event table

Trying to index a table that isn't declared (value is nil) should throw an error regardless of it being in an if...then statement.

Without any clue as to the API being used, there could be many more errors than we can see because none of it has anything to do with the WoW API.

Phanx 12-02-14 10:38 PM

Quote:

Originally Posted by sirann (Post 301591)
Then I saw
Lua Code:
  1. for i=1, gridWidth, 1 do    
  2.     grid[i] = {}
  3. end
which I'm pretty sure creates nested tables at keys of 1-9-1, which is a bit redundant. Should just be
Lua Code:
  1. for i = 1, gridWidth do

While it is redundant, it's not for the reason you think -- the third parameter in a for loop just specifies how much to increment i on each loop. 1 is the default value, so you do not need to explicitly specify 1. However, if you wanted to iterate through all even numbers from 4 to 9000, for example, you could do this:
Code:

for i = 4, 9000, 2 do ... end

analytical 12-04-14 11:25 AM

lua language
 
Thank you all guys...

This function (addShip) need to decide randomly whether it will be horizontal or vertical rectangles. I am trying to change some rectangles from (ship) attributes to true in a line in order to add a ship. For example, if I try to call a ship of size 2 it will be 2 rectangles in a row or column with a ship attribute value of true.
This is where my last function is not doing anything...



local gridWidth = 9
local gridHeight = 14
local grid = {}


for i = 1, gridWidth do
grid[i] = {}
end

Touch function
local function tappedRect(event)
if event.target.ship then
event.target.ship = true
event.target:setFillColor(255,0,0)
else
event.target.ship = false
event.target:setFillColor(0,255,0)
end
end



local squareWidth = 25
local squareHeight = 25
local gap = 5


for i = 1, gridWidth do
for j = 1, gridHeight do


local xPos = (gap*i) + (i*squareWidth)
local yPos = (gap*j) + (j*squareHeight)
grid[i][j] = display.newRect(xPos, yPos, squareWidth, squareHeight)
grid[i][j]:addEventListener("tap", tappedRect)

end
end

--Add a ship
local function addShip( shipSize )

-- Set a random square location
local xPos = math.random(1, gridWidth)
local yPos = math.random(1, gridHeight)

for i = 1, shipSize-1, 1 do
if grid[x-i][y].ship then
grid[x-i][y]:setFillColor(255,0,0)


end
end
end



addShip(2)

If anyone has any idea please let me know

Thank you all...

Phanx 12-04-14 08:18 PM

1. Please use [code] tags around your code. Otherwise it becomes an unformatted mess and is difficult to read.

2. This site is for World of Warcraft UI addons. There is no "addShip" function or "display.newRect" function in the WoW API, nor in Lua itself, so any help you get here will be general Lua syntax help only, especially when you have not even specified what API you're working with. What program will your script be running in? Is there not a forum specific for that API you can post on where people might actually know what you are talking about?

Banknorris 12-04-14 08:52 PM

I bet a grand he is a freshman in some college and this is his homework.

10leej 12-04-14 11:46 PM

Could be the code academy tutorial for Lua which does walk you throgh making a battleship game in python with a lua config.

Phanx 12-05-14 02:48 AM

Well, in any case, (s)he is still posting on the wrong site, and is still not explaining clearly what they are actually doing and why they're asking for help with it on a site for WoW addons.

Gethe 12-05-14 10:56 AM

It looks like it's for Love. If that's the case, you'd be better off going there.

Duugu 12-05-14 11:07 AM

I bet: a Corona mobile app. :)

Cairenn 12-05-14 11:07 AM

Yeah, no, none of that is at all related to WoW AddOns, as others have already said. You might want to look into sites that are directly related to whatever it is you are working on. Good luck!


All times are GMT -6. The time now is 09:32 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI