PokerChips Project: Classes and Inheritance

Is there a good reference that will teach me about Classes with Inheritance?

I’m trying to write a Poker Chip program that will help count poker chips during a game of poker. I’ve got pennies, nickels, dimes and quarters, and I want them all to inherit from a class called Coin.

I have a class called Penny which inherits from Coin. If I have an init() function in both my Penny and my Coin class, does just the Penny one get called? Can I have the Penny class call the Coin’s init class using something like super() - a construct I use in ActionScript. Or does the init function simply override the Coin’s init function and that’s that?

Thanks in advance for advice. I’ll post some code when I get a little further.

@interactivenyc I didn’t mean for my code above to be used. I just did that to show you what it could look like. There’s too much math involved using that code to figure out the x,y values of the coins for all the rotations. If you want to use something, try this. I added instances for each coin. There’s no matrix math involved at all. The touch routine is just the regular compare.

displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)

function setup()
    rectMode(CENTER)
    w1,h1=230,300     -- starting positions for the coins
    w2,h2=630,400
    w3,h3=530,800
    w4,h4=130,700
    tab={}
    table.insert(tab,chips(w1,h1,"penny",0,1))
    table.insert(tab,chips(w1+100,h1,"nickel",0,5))
    table.insert(tab,chips(w1+200,h1,"dime",0,10))
    table.insert(tab,chips(w1+300,h1,"quarter",0,25))
    
    table.insert(tab,chips(w2,h2,"penny",90,1))
    table.insert(tab,chips(w2,h2+100,"nickel",90,5))
    table.insert(tab,chips(w2,h2+200,"dime",90,10))
    table.insert(tab,chips(w2,h2+300,"quarter",90,25))
    
    table.insert(tab,chips(w3,h3,"penny",180,1))
    table.insert(tab,chips(w3-100,h3,"nickel",180,5))
    table.insert(tab,chips(w3-200,h3,"dime",180,10))
    table.insert(tab,chips(w3-300,h3,"quarter",180,25))
    
    table.insert(tab,chips(w4,h4,"penny",270,1))
    table.insert(tab,chips(w4,h4-100,"nickel",270,5)) 
    table.insert(tab,chips(w4,h4-200,"dime",270,10))
    table.insert(tab,chips(w4,h4-300,"quarter",270,25))
end

function draw()
    background(40, 40, 50)
    fill(0,255,0)
    rect(380,550,450,450)
    for a,b in pairs(tab) do
        b:draw()
    end
end

function touched(t)
    for a,b in pairs(tab) do
        b:touched(t)
    end    
end

chips=class()

function chips:init(x,y,n,a,v)
    self.x=x
    self.y=y
    self.n=n    -- name
    self.a=a    -- rotate angle
    self.c=20   -- count
    self.v=v*self.c -- coin values
    self.col=color(255) -- coin color
end

function chips:draw()
    fill(self.col)
    translate(self.x,self.y)
    rotate(self.a)
    ellipse(0,0,80)
    fill(255,0,0)
    text(self.n,0,0)
    text(self.c,0,-60)
    text(self.v,0,-80)
    rotate(-self.a)
    translate(-self.x,-self.y)
end

function chips:touched(t)
    if t.state==BEGAN then
        if t.x>self.x-40 and t.x<self.x+40 and t.y>self.y-40 and t.y<self.y+40 then
            self.col=color(0, 27, 255, 255)
        end
    end
    if t.state==ENDED then
        self.col=color(255)
    end
end

Now, that’s a fine bit of magic, the matrix does. Thanks a million!

matrix() with no args will return an identity matrix (with the diagonal line of 1s)

https://coolcodea.wordpress.com/2013/04/15/33-class-inheritance-in-codea/

Here’s my initial commit of the PokerChips repository.

My Pennies are showing properly, but my Nickels aren’t. Working on that now. Beat me to a solution?

What I really want help with is - how can I get the most out of my coins table! I should be able to use this table as kind of a class of it’s own - according to a conversation I had with Ignatz a few days ago. Pointers, please! (pun intended)

I want this app to be a Poker Chip counter, to be played with a deck of real cards. Each player (1-4) will have a stack of coins. They should be able to flick coins towards the center to ante and bet. When a person wins the pot, a four finger swipe towards their side should add the coins in the pot to the coins in front of the winning player. There will be text fields to display how many Pennies, Nickels, Dimes and Quarters you have in front of you.

Another graphical question I have - how can I display a stack of coins so that it looks like it’s increasing in size? I’ll ultimately want to add some physics to this project too. I haven’t done that before, so will be learning that part from scratch.

I’m curious… Would anybody be interested in helping me with this project, in terms of actually contributing code, and committing to our repository? This will be a learning experience - and something I’ll be using to teach my kids some programming, so even if you’re a new coder… Maybe you can contribute something of value. I dunno - this is an experiment to see where this project takes us. Maybe we can release it for free, or even charge a few bucks for it in the App Store. I’m happy to share the wealth, and have contracts in hand to sign away percentages in the profits of the project for anyone interested. I’m serious - I have an LLC, and a friendly lawyer friend of mine helped me draft these contracts to make promises to collaborators. I’ve never used them before, so I’m interested also in seeing how this sort of shared project might work in practice.

First and foremost, this project should be simple and fun. I’m not that interested in simply work for hire. I’d prefer to think of it as a potential merry bande of renegade programmers, trying to disrupt the system. Testing 1-2-3. Is this thing on?

Of course, if you’re younger than 18, I’d have to get your parent’s signature on the contract :wink:

BTW - do a search on Poker Chips in the App Store. Nobody’s done this yet, so I think it might be a unique opportunity. Please post back if you find any similar products. Just because I haven’t found them doesn’t mean they don’t exist.

If this concept breaks any rules of this forum, please let me know. I’ll take it down. I’m just riffing here, with inspiration. I’m not sure this is kosher. Please advise.

I got my nickels to display - the problem was an empty draw routine in Nickel that’s being handled by Coin. I deleted the duplicate function, and the Nickels are now displaying.

Now, having issues trying to translate the coins so they sit in the right place relative to the pennies.

You didn’t link to your repository

You can use a table as a singleton class very simply like this

t={}
t.size=3 --set properties

function t.f1(a)
    t.a=a
    return a*t.size
end
    
function setup()
    print(t.f1(4))
end

You are simply putting variables and functions inside a table. A big advantage is when you share this code between projects, where there is a risk that your variable and function names may clash with something in a project. Embedding everything in a table eliminates this risk.

More here

https://coolcodea.wordpress.com/2013/04/12/31-a-lesson-from-the-roller-coaster-demo/

OOPS:

REPOSITORY LINK:

https://github.com/interactivenyc/PokerChips

Ignatz - thanks for your help. Your first link (#33) helped me figure out inheritance.

I’m not quite grokking the table as class, or table functions - how to use them yet. Do you have any working code to demonstrate? I’m intrigued, but still feel that the lightbulb over my head is DIM.

did you look at my “roller coaster” link above?

to draw a stack of coins, you can simply draw them overlapping each other, and adjust the size of each to be larger/smaller

wrt physics, whatever you do, don’t use the demo physics workshop. It contains a lot of unnecessary code (unless you are doing something very complex) which is very confusing. I’ve written lots of posts about physics, and explained it in my Codea ebook, and also shown how to write a lot of your own physics code (using guidance from other people, of course). I think there is also a physics project in the “step by step” projects link in the wiki above.

wrt creating an app, I don’t think anyone here has too many illusions about making money from apps! Also, what you really need is a knowledgeable collaborator rather than a newbie, and they tend to be busy on their own projects, in my experience.

I have no illusions about making money from apps. I work for an app company, and see how we’ve spent millions of dollars, have a really terrific app (Speakaboos) and still are a long way from turning a profit. I’m just casting a net to see if people are into experimentation - if we made $10 of real money and split it according to a binding agreement, I’d be sort of thrilled :slight_smile:

It’s hard to make money from apps without a marketing budget. Nobody will just stumble across your app, unless you get very lucky. I figure I might be able to convince 10 of my friends to spend $.99 to support my hobby though. And maybe if I was lucky 100, or so people might eventually discover a cool little poker chip app. It would be sort of sweet if you could play your cards on your phone or iPods, and connect the pot of coins with a multiplayer turn based connection. If I got the UI to feel slick, maybe I’d have an interesting product. Poker is one of those games people seem to be able to wrap their heads around. Perhaps, (and I’m in dreamland here now) one of the profitable online gambling companies would like the technology and acquire it with some of their mad money…

One thing that really makes a difference, is a nice design. I have an acquaintance that made the apps The Lost City and The Secret of Grisly Manor. This guy made a fortune from taking the Myst concept, and executing terrific design and decent puzzle creation. He’s I think a two or three man shop these days, and happens to program his games in Lua. He makes sure the games run well on old phones and iPods, as well as cheap and old Android devices. He’s a special breed though, and has qualities I don’t have. And he’s DEDICATED to his craft, like you wouldn’t believe.

I know a few designers that I’m sure I could get to lend me a hand out of the goodness of their hearts. I think most of us that work in the field of making apps for big companies think that we’re actually the ones making stuff happen. Why couldn’t we try to do something on our own?

And then there’s the simple joy of teaching my kids, or even just kids in general how to code. That’s a worthy goal in itself. Who’s to say that a kids motivation towards potentially making a few bucks isn’t a good thing, even if no money is ever made, but they wind up learning a thing or two in the process.

Again, I’m just riffing here. It’s the middle of the night, and I’m having fun coding, knowing that you’re there, Ignatz, to chat with, and that there are a few other people that seem cool on this forum. It’s small, but active, and I feel like this is like my virtual coffee shop, where I’m hanging out with a few folks I’m getting to know, sipping my tea in the middle of the night, rocking out to some cool tunes, while my wife and kids are slumbering around me.

Anyway - apologies for being so chatty this evening. I’m a little punch drunk from our Passover Seder, and feeling warm and fuzzy. Cheers all!

Do you use Working Copy, Ignatz? I’m wondering if my ImageLoader script is working for my PokerChips project yet… I suspect it’s not, but maybe it is and I don’t realize it. It’s similar to the last one I did.

I wrote you a completely different version, with a class for players. I don’t think coins need a class (if the only reason is to rotatable them, you can only see the top coin anyway so there is no need to rotate those underneath).

I also have a table to hold money settings, just to protect the variables from being overwritten by variables somewhere else in the code (you might re-use this code later)

Note how I’m using vec2 for positions. Get used to vectors, it becomes important as your graphics get more complex, and is essential in 3D.

function setup()
    player={} --use a class of players instead of coins
    player[1]=Player({1,2,3,4},1) --see explanation below
    player[2]=Player({2,2,2,2},2)
    player[3]=Player({8,5,3,1},3)
    player[4]=Player({1,1,5,9},4)
end

function draw()
    background(120)
    for i=1,#player do
        player[i]:draw()
    end
end

--define all things to do with money
--these will be the same for all players, so they don't need to be in a class
--but we'll put them in a table so they don't conflict with other variables and functions
Money={}

--image and size of each type of coin
Money.Coins={
    {pic=readImage("Platformer Art:Coin"),size=30},  --penny
    {pic=readImage("Platformer Art:Coin"),size=40},  --nickel
    {pic=readImage("Platformer Art:Coin"),size=50},  --dime
    {pic=readImage("Platformer Art:Coin"),size=60}  --quarter
}

--the positions of the money stacks
Money.gap=100 --gap between each of the 4 types of coins
Money.offset=vec2(-6,6) --overlap between coins on top of each other
--table defining 4 player positions
--pos defines the centre of the first stack
--dir defines which direction to move in for the next stack
Money.position={
    {pos=vec2(150,50),dir=vec2(1,0)},
    {pos=vec2(100,HEIGHT-250),dir=vec2(0,-1)},
    {pos=vec2(150,HEIGHT-100),dir=vec2(1,0)},
    {pos=vec2(WIDTH-400,HEIGHT-250),dir=vec2(0,-1)}
}

Player=class()

--c is table of the number of each type of coin, eg {4,0,2,4}
--side=side of table=1,2,3 or 4, eg 1=bottom,2=top, 3=left, 4=right
function Player:init(c,side) 
    self.coins={}
    for i=1,4 do self.coins[i]=c[i] end
    self.side=side
end

function Player:draw()
    --next line makes subsequent code easier to read
    --paradoxically, code also runs faster if you create local variables like this
   local g,f,m=Money.gap,Money.offset,Money.position[self.side] 
   for i=1,4 do --draw each stack of coins for this player 
        local c=Money.Coins[i]
        for j=1,self.coins[i] do
            local p=m.pos+m.dir*(i-1)*g+f*(j-1)
            sprite(c.pic,p.x,p.y,c.size)
        end
    end
end



Oooh - working code to learn from. You’re THE BEST, Ignatz!

Thank you, thank you, thank you.

@interactivenyc I’m just using a simple class without any inheritance in this code, but this is just an example to give you some ideas.

displayMode(FULLSCREEN)

function setup()
    rectMode(CENTER)
    tab={}    
    table.insert(tab,chips(-150,-250,0,"penny",1))
    table.insert(tab,chips(-50,-250,0,"nickel",5))
    table.insert(tab,chips(50,-250,0,"dime",10))
    table.insert(tab,chips(150,-250,0,"quarter",25))
end

function draw()
    background(0)
    fill(0,255,0)
    rect(WIDTH/2,HEIGHT/2,450,450)
    translate(WIDTH/2,HEIGHT/2)
    for z=1,4 do
        rotate(90)
        for a,b in pairs(tab) do
            b:draw()
        end
    end
end

chips=class()

function chips:init(x,y,r,n,v)
    self.x=x
    self.y=y
    self.r=r    -- rotate angle
    self.n=n    -- name
    self.cnt=10 -- number of chips
    self.value=v*self.cnt   -- value of chips
end

function chips:draw()
    fill(255)
    ellipse(self.x,self.y,80)
    fill(0, 119, 255, 255)
    text(self.n,self.x,self.y)
    text(self.cnt,self.x,self.y-60)
    text(self.value,self.x,self.y-90)
end