Author Topic: Card Game Functions  (Read 12640 times)

0 Members and 1 Guest are viewing this topic.

Offline erwin

  • Sr. Member
  • ****
  • Posts: 314
    • View Profile
Card Game Functions
« on: October 18, 2015, 04:00:34 pm »
There's been talk about scripting card games (here, a standard deck of 52 cards), and I think it would be nice to consolidate all functions we would need for all possible card games. This means reusable functions for games (not just limited to Poker).

Since there are two main types of games (suit taking games - easier to script), and hand winning games (pairs, two pairs, straights, etc - harder to script), here's what I propose for functions in order to build such games. Feel free to add / suggest better functions, with the idea that "using these functions will allow anyone to script any kind of card game - not just limited to poker / hearts"

1. A shuffle function

with two inputs. These inputs are two (variables) arrays, corresponding to

Code: [Select]
SuitArray:  2 3 4 5 6 7 8 9 10 J K Q A 2 3 4 5 ....
NumArray: C C C ... C D  .. D S .. S  H .. H

The function SHUFFLES these cards, and gives the shuffled output as two arrays:

Code: [Select]
ShuffledSuitArray:  ....
ShuffledNumArray: ....

The purpose of this function is to shuffle cards randomly. Scripts can be used to "divide" the shuffled cards up according to player.

2. An "arrangement" function.

Takes two inputs SuitArray and NumArray (both must be of the same length, but not necessarily 52. These would correspond to cards in a 2 player game (26 each)  4 player game (13 each), poker (5 each), etc).

Gives four outputs, namely:

Code: [Select]
ArrangedBySuitSuits:  C C C D D
ArrangedBySuitNums: 4 5 6 2 8
ArrangedByNumSuits: 2 4 5 6 8
ArrangedByNumNums: D C C C D


The purpose of this function is to cater for different type of games, where arranging by numbers / arranging by suits would be more convenient.

3. A "presentation" function for the above arrangement. Basically, the function takes in any of the above four inputs, and presents the cards to the players in a readable way. This will probably differ based on the type of game


Functions 4-12

With the above three functions, we can now customize our functions. For hand winning games, I suggest scripting the nine functions which correspond to the nine poker hands - Straight Flush, etc ... with the following four inputs - note that this function is NOT restricted to 5 elements in an array. Obviously with checks, eg, can't get a Flush if you had only 3 cards to compare, etc:

Code: [Select]
FirstPlayerSuit   H H H S S
FirstPlayerNum  2 5 6  3 6
SecondPlayerSuit:  D D D D D
SecondPlayerNum: 4 5 6 7 8

These functions would compare the hands of two players, and output a string with the player which has a higher hand. If neither player has a hand of that type, output null (or NA). Furthermore, make these 9 functions in running order - hopefully for some vnum xxx01 to xxx09 (for games which rely on placing a same poker hand, or one with higher value) - so if a person placed a hand of type 05, we can loop through for 06-09.


With the above 12 functions, we can script most games. For example, if we wanted to script a Poker Game, then we write a comparison function, eg below.

13. A comparison function. Given the arrays of X number of players, and an array PlayerName, eg:

Code: [Select]
XerxesSuit   H H H S S
XerxesNum  2 5 6  3 6
TorSuit:  D D D D D
TorNum: 4 5 6 7 8
ErwinSuit: C D H S C
ErwinNum: A 2 3 4 5

PlayerName: Xerxes Tor Erwin


run this function together with the previous 9 functions in order to see which player wins.

The logic would be: Run these functions from highest poker hand to lowest poker hand, until one poker hand is found. Compare with all other players to see who has the highest hand. Output the player name.


Any other functions do you think we need?









« Last Edit: October 18, 2015, 04:19:43 pm by erwin »

Offline erwin

  • Sr. Member
  • ****
  • Posts: 314
    • View Profile
Re: Card Game Functions
« Reply #1 on: October 18, 2015, 04:26:39 pm »
Function 14: Checking if player has these cards

Takes in an input of the player, and checks if he/she has that card in his / her hand, given either the inputs:

Code: [Select]
ArrangedBySuitSuits
ArrangedBySuitNums

or

Code: [Select]
ArrangedByNumSuits
ArrangedByNumNums

and the input

Code: [Select]
maxnum
Using "place" as the command word, possible inputs would be:

place 4D 4H 4H
place 4D 4H
place 4D 8H JS AS
place 4D 7H AA BB

Function will check if the player has these cards (note duplicates, invalid cards, and card placement more than maxnum must not be allowed), and returns an output (string) either yes/no.

Function 15: An extension of Function 14, to REMOVE the valid cards in a player's hand, and returns a smaller Array of cards.

« Last Edit: October 18, 2015, 04:36:27 pm by erwin »

Offline rynald

  • Administrator
  • Newbie
  • *****
  • Posts: 25
    • View Profile
Re: Card Game Functions
« Reply #2 on: October 23, 2015, 03:10:07 pm »
An other way would be to use a command trigger for every game. You can put them on a dealer mob or on the room.
If the game is a global:

Code: [Select]
eval game poker
global game

then each trigger can check if the game is equal to the one they handle:

Code: [Select]
if %self.game% == poker
  if %cmd% == bid
    ...
  elseif %cmd% == fold
    ...
  else
    return 0
else
  return 0

A card can be a two character string: 2C, AS, 1D (10 of diamonds). You can check for the suit and number with dg_letter:

Code: [Select]
eval card 3S
dg_letter num 1 %card%
dg_letter suit 2 %card%

It's probably best to let a function check whether the number of one card is higher than another.

The dealer starts with a deck of 52 cards, that can be made when a player asks to start a game:

"start poker bob mary james"

Code: [Select]
eval numcards 52
eval card1 2H
eval card2 3H
...
eval card52 AS
eval i 1
while %i% < 53
  global card%i%
  eval i %i% + 1
done

The dealer's cards are in context 0, the player cards can be put in their own context when they are dealt at random:

Code: [Select]
eval player 1
while %player% <= 3
  eval i 1
  while %i% <= 5
    # pick random card
    eval c %%random.%numcards%%%
    eval card %%card%c%%%
    # add card to player
    context %player%
    eval numcards %self.numcards% + 1
    global numcards
    eval card%numcards% %card%
    global card%numcards%
    # remove card from dealer
    context 0
    eval card%c% %%card%self.numcards%%%
    global card%c%
    eval numcards %numcards% - 1
    global numcards
    eval i %i% + 1
  done
  eval player %player% + 1
done

I'm not sure about using context, but it makes sense in this case so you can use the same names.

Offline erwin

  • Sr. Member
  • ****
  • Posts: 314
    • View Profile
Re: Card Game Functions
« Reply #3 on: October 24, 2015, 12:47:34 am »
Hmm..is there still the 8-10 page limit for triggers? I'd wager scripting complicated card games would take a lot more pages without using functions :(

Offline Diandra

  • Administrator
  • Full Member
  • *****
  • Posts: 143
    • View Profile
    • Email
Re: Card Game Functions
« Reply #4 on: October 24, 2015, 02:17:45 am »
Rynald recently looked it up:

The trigger size limit is 24576 bytes (or characters).