Finding sample card game code in Monkey?

Monkey Forums/Monkey Programming/Finding sample card game code in Monkey?

zardon(Posted 2013) [#1]
I am very interested in writing a simple card game with Monkey but I am unsure of how to start a card game using Monkey.

Is there any sample code or projects which I can look at.

I am interested in understanding:

1. How to animate cards, flip them over, should a visual representation of a deck of cards actually have X cards displayed one on top of each other.

2. The logical flow of how a card game actually works, ie: Draw card, player on turn, etc

3. Understanding game states and how they effect the logical flow of the card game

Many thanks


Xaron(Posted 2013) [#2]
I might be able to help. I did a Mau Mau game (crazy 8):

https://play.google.com/store/apps/details?id=com.codeknitters.mau


therevills(Posted 2013) [#3]
@Zardon - Have you tried yourself yet? If so which parts are you stuck at and can you post your code here so we can help you.


Gerry Quinn(Posted 2013) [#4]
I think the principles are the same as with any other strategy game, i.e. each player at every turn has a 'position' based on his hand and any cards on the table or previous scores, then must decide what 'move' to make.

Since the game is turn-based, you have the luxury of not caring too much about graphic efficiency. So write code that's super-clear, rather than code that's super-efficient. And if you want to draw every card in a pile, slightly offset to indicate depth, go for it. You can always change to a more efficient or realistic representation of a pile of cards later.

Animation of events such as playing a card is probably something you can add later once you have the basic mechanisms sorted.


rIKmAN(Posted 2013) [#5]
Just done a quick Google and there are lots of tutorials / articles about this very thing.

ie. http://snipplr.com/view/55221/ should get you started and at the very least give you an idea of how to go about it.


zardon(Posted 2013) [#6]
What I'm stuck with is the following.

1. Lets say a game as 50 cards to draw from. You want to display the deck of 50 cards somewhere on the screen, and animate a card from this pile onto the board.

This seems like wasted memory to me, but I don't know if I should actually put 50 cards one on top of each other like layers and draw from it or not

2. How do I animate the cards from the deck into the board, flipping them over, etc

3. Handling the logic of the game.

a. Shuffle cards
b. Draw X cards from deck to the board, flip them over to show them
c. Player on turn takes a card, or does their actions.
d. Move a card from the board to the player's hand.
e. Move a card from the player's hand to the board.

I have gotten all the models done, I just couldn't wrap my head around the logic .

Thanks for now


Xaron(Posted 2013) [#7]
I will post some code. I'm on vacation atm so please have some patience.


Gerry Quinn(Posted 2013) [#8]
Drawing 52 cards will probably be fast enough, you can add some efficient code for representing big stacks later if you want.

For game purposes (other than animation) cards are always in one place, e.g. they are in the deck, or on the board, or in the hand of player X. These could all be lists or stacks. A card should know also whether it is face up or face down. A game action simply changes these states for one or more cards, and the game rules say which states can permissibly be changed (or are forced to be).

I think I would do it so that the cards also hold information related to drawing/animation. E.g. a card will store its position (coordinates) and also an animation parameter that goes from 0.0 to 1.0 over a series of renders as it engages in an animation such as turning over or changing position. I might give it two position values so it is easy to tell a card to move from A to B, and a separate animation parameter for that.

Then animation is simply a case of updating the parameters of all animating cards and redrawing the game, until all animations are done. Then it is time for a move. Say it is player 2's turn and he gets to play a card to the board. You set up the animations for that players cards (the card he plays will flip over and move to the board, and his other cards will move slightly to give him an even spread). For your first iteration of the game, don't bother with a real AI, just make him play a random legal card. Your game will have code for saying whether a move is legal, so he can select cards at random and call that until one is valid or all have been checked.

The animations happen. When they're done, check whether anybody won the game. If Player 2's turn is over, it's on to Player 3, and so on.

That's the gist of how I would go about it, and there are some small wrinkles that will have to be added (e.g. do we offset the positions of cards slightly according to the number of cards stacked below them), but maybe it gives you an idea of one way to approach things.


zardon(Posted 2013) [#9]
Sorry I was very busy, but I wish to thank you for time. I'm going to have a go of it and try my hardest to have a go of it.

I'm well of aware the rules and how to play the game; I just find it hard to articulate it onto the visual screen.

Anyway, I have now mocked up the graphics for the cards and am going to have a go by using Arrays and simple objects.


Snader(Posted 2013) [#10]
I made my first card game, and although there isn't much animation, it all worked out nicely.

http://www.monkeycoder.co.nz/Apps/app.php?id=263&pic_id=1

Each card in my game is an object with a current X,Y and rotation, and a X and Y coordinate and rotation to 'travel to'. Each card also has an OWNER var. This can be PLAYER_A, _B, _C or _D. Then I simply can render the cards on the right spot, or animate it to the right spot.
Based upon the rules of the game, the AI and the current hands and turn of the players I basically move cards by switching ownership.


Capn Lee(Posted 2013) [#11]
Although I could never recommend anyone look at my code for a positive example, I quickly knocked together a single player version of Set when feeling inspired on a long train journey. If you're not familiar with the game, the rules are here or below is the video instructions I learned about the game from



anyway, this is the complete code, it's very simple and not at all pretty



Hope that helps


zardon(Posted 2013) [#12]
This is exactly the info I was after! Many thanks. The card game I am writing is straight forward; but I could never understand how to transmute my understanding of the game to the screen.

I have got basic graphics (it'll do for now) done, now I just need to do the code which I feel much more confident about now.

Thanks again