Page 2 of 2

Re: How do I change card graphic?

Posted: Mon Jun 16, 2014 7:46 pm
by DrCicero
Oh, yes! The error is in the following lines: (the placement of the spots)

Code: Select all

spots = {}
for i=1,9 do
  spots[i] = Cardspot:new("fancy", 0, 0, 0, 0)
  spots[i].x = 220 + (i%3)*130
  spots[i].y = 120 + math.floor(i/3)*150
end
Do you know the modulo operator %? I used it for the x property.

Code: Select all

0%3 = 0; 1%3 = 1; 2%3 = 2;
3%3 = 0; 4%3 = 1; 5%3 = 2;
6%3 = 0; 7%3 = 1; 8%3 = 2
math.floor (rounding down) is usefull for the y property:

Code: Select all

math.floor(0/3) = 0; math.floor(1/3) = 0; math.floor(2/3) = 0;
math.floor(3/3) = 1; math.floor(4/3) = 1; math.floor(5/3) = 1;
math.floor(6/3) = 2; math.floor(7/3) = 2; math.floor(8/3) = 2
The problem is that lua expects you to index the arrays starting at one, whereas the math i gave you expects you to start at zero. :monocle: The solution is very simple, think a bit about it and then read the following:

SPOILER: The solutions is either going from 0 to 8 and setting spots[i+1], or going from 1 to 9 and using (i-1) in the math. :ultrahappy:

Re: How do I change card graphic?

Posted: Sun Jun 22, 2014 11:04 am
by Lacotemale
Do you know the modulo operator %?
Yeah I know about it but never used it in programming really. Regarding 'math' what is the difference between using math and love.math?

You will be happy to know I figured out the cardspots and the draw of player, enemy cards. :megagrin:

However, I think I will continue on to rewrite the other functions now. I know its gonna take time and be lots of work but I think it will be worthwhile.

Especially in the card setting/card comparison. I am currently setting the card values on a cardspot and comparing cardspots, then trying to figure out what card object to change the graphic.

If I rewrite to not do that but just compare the card values(and keep track of what cards are where on the board) then the changing of the card graphic would be much easier. Lots of silly code will also be removed. :awesome:

Re: How do I change card graphic?

Posted: Sat Jun 28, 2014 11:44 am
by Lacotemale
Although more progress on the rewrite has been made I got a little stuck on the compareValues() function. I wanted to rewrite this to be waaaay more efficient as the old code was very long.

I was thinking of doing 4 loops. One loop for every card side. (wAtk, nAtk, eAtk, sAtk)

So for example the first loop would compare all the right attack values with the left attack values. However, this doesn't seem to work for me at all. Is there a better way to compare the values rather than manually comparing the relevant places?

I have the latest code attached. The function compareValues() has the error.

Re: How do I change card graphic?

Posted: Sat Jun 28, 2014 6:10 pm
by DrCicero
There are exactly four comparison to be done, so you need one loop which runs four times.

Code: Select all

local dirs = {
  nAtk = {"sAtk", 0, -1},
  eAtk = {"wAtk", 1, 0},
  sAtk = {"eAtk", 0, 1},
  wAtk = {"eAtk", -1, 0},
}
function compareCardValues()

    print("COMPARE: cardSelected "..cardSelected.." and ".."spotSelected "..spotSelected)

    local oneSpot = spots[spotSelected]
    local x = spotSelected % 3
    local y = math.floor(spotSelected / 3)

    for dir, params in pairs(dirs) do
      local antidir, dx, dy = unpack(params)
      local otherSpot = spots[x+dx + (y+dy)*3]

      if otherSpot ~= nil
-- from your code {
      and oneSpot[dir] > otherSpot[antidir] then
        if oneSpot.owner==1 and otherSpot.owner==2 then
          playerScore = playerScore + 1
          print("PLAYER wins card {x=" .. (x+dx) +", y=" .. (y+dy))
          updateScore(s[1], playerScore, oneSpot)

        elseif oneSpot.owner==2 and c2.owner==1 then
          enemyScore = enemyScore + 1
          print("ENEMY wins card {x=" .. (x+dx) +", y=" .. (y+dy))
          updateScore(s[2], enemyScore, otherSpot)
        end
      end
-- }
    end
  end
Besides:
* There is no need for parenthesis between if and then.
* What use is the cardSelected variable? ;)