Card Game

General discussion about LÖVE, Lua, game development, puns, and unicorns.
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Card Game

Post by tdc5013 »

Hey Guys,

I'm looking to make a small card game in Love and was looking for a few suggestions. I'm a pretty competent coder in pure Lua; but I've only really used Love once as part of my job training and have been meaning to come back to it and have a play around.

Basically, I have already coded a basic game framework. I have working Deck, Player, Rulebook, and Card classes (as well as a Constants class to hold all the card info -- the card class simply pulls in this information and creates a new instance of a Card Object. The way the card game works (Shithead - if anyone has ever played this http://en.wikipedia.org/wiki/Shithead_%28card_game%29) is that certain cards have certain properties. For instance, 10 will "burn" the in-game card pile (basically setting a CardPile table to nil). There are other properties like mirroring, forcing a player to go lower, etc. Coding in the effects of these cards is the simple part, the part I'm having trouble figuring out structurally is how to create this part of the game. I was thinking maybe with Enums but I'm not sure how I could link these up with the cards so that, say, if I was dealt a 10 card it would automatically have that "Burn" attribute.

Any ideas would be welcome :)

(1st post w00t)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Card Game

Post by Robin »

I think it depends heavily on the rest of your code. One way to do it would be to give Cards an "attributes" property, like:

Code: Select all

card10 = Card:new(10, 'heart') -- or how it works in your framework
card10.attributes.burn = true
card10.attributes.takeCards = 3
-- or whatever
I can't help you much further without seeing what you have right know.
Help us help you: attach a .love.
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Re: Card Game

Post by tdc5013 »

Basically I'm creating cards (instantiated) using values in tables set up in constants, for instance:

Suit = {};
Suit[1] = Hearts;
Suit[2] = Clubs;
Suit[3] = ...
.
.
.
...etc.

I have tables for the numbers, and for the actual name (so you never go pulling out a 13 of diamonds). Then, in the Deck class, I use a for loop to pull those Constants in and set them up as card values, like so:

Code: Select all

for i = 1, Deck.MaxCards do
	table.insert(Deck.cards, Card.Create(NAME[i], SUIT[i], NUMBER[i]) );
	end
Basically, not every card in the game I'm making has a specific attribute, it's only certain cards. So using this current method wouldn't work. I'll try and upload a version of the code at some point, however there are two incarnations of it. One I was doing for another project (basically top trumps), and one that's more just a classic cards thing, so I need to do a little editing before uploading.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Card Game

Post by Robin »

Can't you have a table ATTRIBUTES then?

I dunno, I guess it still depends on the rest of your code.
Help us help you: attach a .love.
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Re: Card Game

Post by tdc5013 »

I suppose I could, but wouldn't that be a problem for the For Loop? It basically creates a 52 card deck, if I were to include attributes every single card would have one.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Card Game

Post by Robin »

Ehm, I'm not sure why that would be a problem. Is there some special reason that it is bad that I'm missing?
Help us help you: attach a .love.
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Re: Card Game

Post by tdc5013 »

I uploaded an attachment of all the code. There's stuff that needs changing in there from the previous incarnation of the code, particularly in the Rulebook class, but that's the basic framework.

And the reason they can't all have attributes is because only certain cards have them. Some cards are just normal number cards, others are 'magic' cards; like the 10 that burns the card pile.


EDIT: Also, is requiring additional classes the same instruction as it is in regular Lua (require("Example_Class") )?
Attachments
Shithead_Code.zip
(8.1 KiB) Downloaded 318 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Card Game

Post by Robin »

tdc5013 wrote:EDIT: Also, is requiring additional classes the same instruction as it is in regular Lua (require("Example_Class") )?
Yes. It's called modules, not classes. Technically, you don't have any classes in your code. I'll let someone else explain how to use OO in Lua. (Yes, it's possible. Yes, it's simple. No, you're not doing it right now, and it's probably unlike anything you've seen before. At least, it was for me.)

Instead, I'll focus on how to implement those attributes.

My suggestion would be the following:

Code: Select all

function Card.Create(Suit, Number, Colour)
	local card_obj = {};
	--assign any default vars
	card_obj.suit 		= Suit;
	card_obj.number		= Number;
	card_obj.colour 	= Colour;
	card_obj.attributes = {}
	-- what follows is the same

Code: Select all

function Deck.Init()
	--Initialise, and pass through arguments.
	
	--SUIT and NUMBER are tables in Constants
	for i = 1, #SUIT do
		for j = 1, #NUMBER do
			table.insert(Deck.cards, Card.Create(SUIT[i], NUMBER[j] ));
			--Card.Print(Deck.cards[((i-1)*13)+j]);
		end
	end

	Deck.cards[23].attributes.burn = true
	Deck.cards[14].attributes.autokill = 19 -- or whatever, I don't know what other things you have

	--once inited completed, set to true
	Deck.inited = true;
end
You could put those numbers (23 and 14 in this example) in Constants.lua, make a nice loop or something.

As a side note, it looks like you're trying to write Java in Lua. If you start working with the language rather than against it, you can write great things in Lua.
Help us help you: attach a .love.
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Re: Card Game

Post by tdc5013 »

As a side note, it looks like you're trying to write Java in Lua. If you start working with the language rather than against it, you can write great things in Lua.
Yeah, I got trained up by a games company, we use Lua in a really specific way because of the engine we're on (also, occasionally we use other languages depending on what we're working on, the semicolon thing is just a good habit to get into) and it just sticks after a while.

So if I'm understanding you correctly, I'd just nest a table? That's a great idea; I think I was focusing too much on that for loop to see it, pretty obvious now :oops:

Thanks
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Card Game

Post by Robin »

tdc5013 wrote:Yeah, I got trained up by a games company, we use Lua in a really specific way because of the engine we're on (also, occasionally we use other languages depending on what we're working on, the semicolon thing is just a good habit to get into) and it just sticks after a while.
Ah, well, that explains a lot. Also explains the semi-JavaDoc. I would recommend to learn yourself a programming style for Lua that fits with it. Many programming language have their own "classic" style, and following that usually helps to take the greatest advantage of that language.
tdc5013 wrote:So if I'm understanding you correctly, I'd just nest a table? That's a great idea; I think I was focusing too much on that for loop to see it, pretty obvious now :oops:
Pretty much, yeah.
tdc5013 wrote:Thanks
You're welcome. :)

By the way, if you haven't already, you might want to read http://www.lua.org/pil/16.html and the sections that follow it. Tables are really powerful in Lua, and this particular chapter in Programming in Lua will talk about how to use them for actual OOP.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 215 guests