6

A game card

  1. #include "OL.h"
  2.  
  3. extern class Card;
  4.  
  5. extern void defclassCard();
  6.  
  7. #define TWO         0
  8. #define THREE       1
  9. #define FOUR        2
  10. #define FIVE        3
  11. #define SIX         4
  12. #define SEVEN       5
  13. #define EIGHT       6
  14. #define NINE        7
  15. #define TEN         8
  16. #define JACK        9
  17. #define QUEEN       10
  18. #define KING        11
  19. #define ACE         12
  20.  
  21. #define CLUBS       0
  22. #define DIAMONDS    1
  23. #define HEARTS      2
  24. #define SPADES      3

Includes the definitions of the data types and the signatures of the functions of the Object Layer. Declares the class Card and the function defclassCard which builds it. Defines constants for a card rank and color.

  1. #include "So-o.h"
  2.  
  3. #include "Card.h"
  4.  
  5. class Card;

Includes the signatures of the So-o functions. Includes the declarations of the class Card. Defines the class Card.

  1. static instance i_init(instance self, va_list va) {
  2.     int rank = va_arg(va, int);
  3.     int suit = va_arg(va, int);
  4.  
  5.     supersend(Card, self, "init");
  6.  
  7.     sendmsg(self, "set", "rank", rank);
  8.     sendmsg(self, "set", "suit", suit);
  9.  
  10.     return self;
  11. }

init takes 2 arguments: rank and suit, the value and the color of a card. It initializes the properties rank and suit of self with rank and suit.

  1. static int i_rank(instance self) {
  2.     return sendmsg(self, "get", "rank").i;
  3. }

rank returns the rank property of self.

  1. static int i_suit(instance self) {
  2.     return sendmsg(self, "get", "suit").i;
  3. }

suit returns the suit property of self.

  1. static int i_compare(instance self, va_list va) {
  2.     instance card = va_arg(va, instance);
  3.  
  4.     int rank1 = sendmsg(self, "get", "rank").i;
  5.     int rank2 = sendmsg(card, "get", "rank").i;
  6.  
  7.     return rank1 == rank2 ? 0 : rank1 > rank2 ? 1 : -1;
  8. }

compare compares self with card, another instance of a Card. It returns 0 if the rank of self is equal to the rank of card, -1 if it's lower, 1 if it's higher. compare is used to sort an instance of Hand.

  1. static char *i_toString(instance self) {
  2.     static char srank[] = { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' };
  3.     static char ssuit[] = { 'c', 'd', 'h', 's' };
  4.  
  5.     static char s[2+1];
  6.  
  7.     int rank = sendmsg(self, "get", "rank").i;
  8.     int suit = sendmsg(self, "get", "suit").i;
  9.  
  10.     sprintf(s, "%c%c", srank[rank], ssuit[suit]);
  11.  
  12.     return s;
  13. }

toString returns a string containing the rank and the suit of self. This string is formatted in a static area. It must be used before this method is called again.

  1. void defclassCard() {
  2.     property _i_properties[] = {
  3.         "rank",
  4.         "suit",
  5.         0, 0
  6.     };
  7.     selector _i_messages[] = {
  8.         "init",     METHOD(i_init),
  9.         "rank",     METHOD(i_rank),
  10.         "suit",     METHOD(i_suit),
  11.         "compare",  METHOD(i_compare),
  12.         "toString", METHOD(i_toString),
  13.         0, 0
  14.     };
  15.  
  16.     Card = defclass("Card", 0, 1, 0, _i_properties, 0, _i_messages);
  17. }

Initializes the class Card. The class Card inherits from the class Object. An instance has 2 properties: rank, its value, and suit, its color. An instance redefines the messages init and toString. It implements the messages rank, suit and compare.

TEST

Compile and run the unitary test:

$ make test-Card
gcc -g -I.. -Wno-missing-braces -O -fstrength-reduce -finline-functions -fomit-frame-pointer -c test-Card.c
gcc -g -I.. -Wno-missing-braces -O -fstrength-reduce -finline-functions -fomit-frame-pointer -c Card.c
gcc -g test-Card.o Card.o ../libso-o.a -o test-Card
$ test-Card
2c (two of clubs) -> 2c
Td (ten of diamonds) -> Td
Kh (king of hearts) -> Kh
As (ace of spades) -> As
-1 -> -1
 0 ->  0
 1 ->  1
SEE ALSO

Hand

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].