A game card
- #include "OL.h"
- extern class Card;
- extern void defclassCard();
- #define TWO 0
- #define THREE 1
- #define FOUR 2
- #define FIVE 3
- #define SIX 4
- #define SEVEN 5
- #define EIGHT 6
- #define NINE 7
- #define TEN 8
- #define JACK 9
- #define QUEEN 10
- #define KING 11
- #define ACE 12
- #define CLUBS 0
- #define DIAMONDS 1
- #define HEARTS 2
- #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.
- #include "So-o.h"
- #include "Card.h"
- class Card;
Includes the signatures of the So-o functions. Includes the declarations of the class Card. Defines the class Card.
- static instance i_init(instance self, va_list va) {
- int rank = va_arg(va, int);
- int suit = va_arg(va, int);
- supersend(Card, self, "init");
- sendmsg(self, "set", "rank", rank);
- sendmsg(self, "set", "suit", suit);
- return self;
- }
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
.
- static int i_rank(instance self) {
- return sendmsg(self, "get", "rank").i;
- }
rank
returns the rank property of self
.
- static int i_suit(instance self) {
- return sendmsg(self, "get", "suit").i;
- }
suit
returns the suit property of self
.
- static int i_compare(instance self, va_list va) {
- instance card = va_arg(va, instance);
- int rank1 = sendmsg(self, "get", "rank").i;
- int rank2 = sendmsg(card, "get", "rank").i;
- return rank1 == rank2 ? 0 : rank1 > rank2 ? 1 : -1;
- }
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.
- static char *i_toString(instance self) {
- static char srank[] = { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' };
- static char ssuit[] = { 'c', 'd', 'h', 's' };
- static char s[2+1];
- int rank = sendmsg(self, "get", "rank").i;
- int suit = sendmsg(self, "get", "suit").i;
- sprintf(s, "%c%c", srank[rank], ssuit[suit]);
- return s;
- }
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.
- void defclassCard() {
- property _i_properties[] = {
- "rank",
- "suit",
- 0, 0
- };
- selector _i_messages[] = {
- "init", METHOD(i_init),
- "rank", METHOD(i_rank),
- "suit", METHOD(i_suit),
- "compare", METHOD(i_compare),
- "toString", METHOD(i_toString),
- 0, 0
- };
- Card = defclass("Card", 0, 1, 0, _i_properties, 0, _i_messages);
- }
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
Comments