# How can I randomly generate arbitrary “rules” for a creative card game

My friends and I in school play this interesting card game, which we call 'the game without rules' (ironically, of course). The game is like this: in the beginning of a school year, we start with one rule: you have to lay a card that is higher. Everything else is not allowed and the one who is the first to have no more cards, wins.

When only one person remains with cards, the game ends. At that point, the winner can define a new rule, such as "on a seven, you have to play a lower card instead of a higher card". This makes the game harder and harder as we play on. Every time a person breaks a rule, he/she is given an extra card.

Now my idea was to make a computer program out of this and let them create new rules, so that would be pretty funny to see what combinations they come up with. Every time a 'computer' defines a new rule, they don't tell the others, so they have to figure out by trial and error.

Well at this point I am stuck. How can I let the computer define new rules and can someone write an example for me? And in what language should I do it?

• "What language should I do it?" will probably be answered with "whichever one you know since none are better" as it usually is. – doppelgreener Oct 23 '11 at 15:08
• I'm first gonna try Game Maker for a 'proof of concept' and to see wheteher this program sucks or not, and then do some real coding :) – user1009013 Oct 23 '11 at 20:12

You're probably going to have to break down what kind of rules you guys have created in the past and just let the computer pick one at random and fill in the specific details. As far as your high level goals this isn't going to make it really "funny" since you'll know all the possible outcomes, but it will make the game possible.

So for example, the "lay a card that is higher" (aside: what happens if somebody puts down a king/ace and everybody has cards left?), you could generalize that to be a rule about ordering and the program could pick between higher cards only or lower cards only. Or you could make a rule about alternating colors, or whatever.

The real interesting part is going to be the conditionals that change the rules. Again, you figure out the base parts and then go from there. Like "on card X (defined by suit/number/color) change the existing rule". Or "after X subsequent cards (same color/suit/number, ascending/descending numbers, etc) change the rule".

I doubt you'll be able to come up with something truly random since how you're going to have to define how you interact with the cards and how to express that to the player in a way that makes sense.

Similar to what Tetrad has said, the rules will likely have to be pre-defined. However, you can go a step further and break the rules into parts which can be combined to form new rules.

For example, you can have a rule grid like the following, where you select one item from each column to form a rule:

Of course you can adapt this to whatever rules you've made up in the past. Keep in mind that this is just one type of "rule sentence" table. You can have multiple tables to choose from when creating a new rule, all with a different sentence format.

Note: For the above table, you can have a rule that says that columns 3 and 5 must be different entities.

• Wasn't expecting an answer so fast, but thanks! gonna try that now. – user1009013 Oct 23 '11 at 19:52
• I'm going with c++. How can I define a card? should I pick an int or some string? or even define numbers between 1 and 52 and set some kind of properties for each card? – user1009013 Oct 23 '11 at 19:59
• Implementation is really up to you. Study up on object oriented programming. C++ is a good choice. It sounds like you're a beginner, so I'd start with plenty of tutorials and some programming methodology courses (free online video lectures are available). Good luck! – MichaelHouse Oct 24 '11 at 1:06

I agree with Byte56 and Tetrad that you should think about elements of rules that can then be combined to form a variety of complex rules.

In terms of implementation, you could give this design a try:

• GameState holds a game state as specified by a stack of cards that were already played (especially the card that lies on top), the cards that each player holds, the remaining cards in the deck, the active player, and the order of players.
• Condition (abstract) represents conditions that the game accounts for. Use polymorphism to define CardCondition (like "heart", "jack", "red", "higher than 9"), DeckCondition ("less than 10 remaining cards in the deck"), and PlayerCondition ("at least one player with only one card left"). Each type of Condition is to be applied to a different part of GameState. Also, choose a data structure similar to data structures that can represent logical or algebraic expressions, so that you can combine atomic conditions to more complex conditions by joining them together with AND, OR, etc.
• Rule represents a game rule by holding a Condition and a Filter. Condition can be checked against a given GameState, indicating whether or not the GameState satisfies the Condition. Filter is a CardCondition that can be applied to a given set of cards, filtering the set so that only cards remain which satisfy the CardCondition. If the Condition for applying the Rule is met by the current GameState, then apply the Filter to the cards of the player to see which ones can be played.
• RuleStack holds all the Rules of the game. It also provides a method that returns the Rule that is relevant for a given GameState. In order to determine which Rule is relevant, RuleStack passes the GameState to each Rule in turn, beginning with the Rule that was created first. If GameState satisfies the Condition of that Rule, that Rule becomes the relevant one to be returned (or to be replaced by the next and younger Rule that also applies to GameState). (Based on your examples, I assume that only one rule is to be applied in any given situation. Return several Rules [with a slightly different algorithm] if several Rules can apply to a given GameState.]

This allows you to

• check if a player did a legitimate move by asking RuleStack for the relevant Rule given GameState and apply the Filter Condition of that Rule to the card that the player wants to play.
• find out a computer player's next move by filtering its available cards through the Filter of the relevant Rule (given GameState) and choosing one of the remaining cards.
• create new Rules by defining a RuleFactory with different sets of Conditions (as in Byte56's answer), which can be combined recursively to create new complex rules.

Regarding your question as to how you can represent cards: Watch the first couple of lectures of this free udacity programming course: http://www.udacity.com/view#Course/cs212/CourseRev/apr2012