Commit fe48dee5 authored by Vivien Kraus's avatar Vivien Kraus
Browse files

Add c++ support

parent 3ff952b2
......@@ -10,6 +10,7 @@ AC_CONFIG_MACRO_DIRS([m4])
# Checks for programs.
AC_PROG_CC
AC_PROG_CC_STDC
AC_PROG_CXX
gl_EARLY
AM_INIT_AUTOMAKE([subdir-objects])
AM_PROG_AR
......
......@@ -42,12 +42,14 @@ include_HEADERS = tarot.h
TESTS = \
check_composition \
check_game_init \
check_deal
check_deal \
check_deal_cpp
check_PROGRAMS = \
check_composition \
check_game_init \
check_deal
check_deal \
check_deal_cpp
check_composition_SOURCES = check_composition.c
check_composition_CFLAGS = $(ASAN_CFLAGS)
......@@ -64,12 +66,18 @@ check_deal_CFLAGS = $(ASAN_CFLAGS)
check_deal_LDADD = libtarot.la
check_deal_LIBS = $(ASAN_LIBS)
check_deal_cpp_SOURCES = check_deal.cpp
check_deal_cpp_CXXFLAGS = $(ASAN_CFLAGS) # Bof bof
check_deal_cpp_LDADD = libtarot.la
check_deal_cpp_LIBS = $(ASAN_LIBS)
INDENTED_FILES = \
$(include_HEADERS) \
$(libtarot_la_SOURCES) \
$(check_composition_SOURCES) \
$(check_game_init_SOURCES) \
$(check_deal_SOURCES)
$(check_deal_SOURCES) \
$(check_deal_cpp_SOURCES)
../lib/libgnu.la:
(cd ../lib && $(MAKE) libgnu.la)
......
#include <config.h>
#include <stdlib.h>
#include <assert.h>
#include "tarot.h"
#include "gettext.h"
using namespace tarot;
using namespace std;
static void
check ()
{
game g (5, true);
/* First check: there are not enough cards */
{
const vector <card> not_enough =
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
assert (!g.check_deal_one (not_enough));
}
/* Second check: there are too many cards */
{
const vector <card> too_many =
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
assert (!g.check_deal_one (too_many));
}
/* There is an obvious double */
{
const vector <card> double_zero =
{0, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
assert (!g.check_deal_one (double_zero));
}
/* There is a double but the cards are not sorted */
{
const vector <card> double_zero =
{0, 2, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
assert (!g.check_deal_one (double_zero));
}
/* There is a double but the cards are not sorted, other */
{
const vector <card> double_zero =
{0, 0, 3, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
assert (!g.check_deal_one (double_zero));
}
/* There is a petit sec, with sorted cards */
{
const vector <card> petit_sec =
{0, 1, 3, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 56};
assert (!g.check_deal_one (petit_sec));
}
/* There is a petit sec, but the cards are not sorted */
{
const vector <card> petit_sec =
{0, 1, 3, 2, 56, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
assert (!g.check_deal_one (petit_sec));
}
/* OK (not petit sec since there is the excuse) */
{
const vector <card> ok =
{0, 1, 3, 2, 56, 5, 6, 7, 8, 9, 77, 11, 12, 13, 14};
assert (g.check_deal_one (ok));
}
}
int
main ()
{
tarot::init ();
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
check ();
return 0;
}
......@@ -180,6 +180,190 @@ extern "C"
#ifdef __cplusplus
}
#include <string>
#include <vector>
#include <utility>
#include <cassert>
namespace tarot {
typedef TarotCard card;
typedef TarotNumber number;
typedef TarotSuit suit;
typedef TarotError error;
typedef TarotStep step;
typedef TarotBid bid_t;
typedef TarotDeclaration declaration_t;
typedef TarotCall call_t;
typedef TarotDog dog_t;
typedef TarotDiscard discard_t;
typedef TarotHandful handful_t;
typedef TarotTeam team;
typedef TarotHint hint;
typedef TarotPlayer player;
inline int
init ()
{
return tarot_init ();
}
inline std::string
libtarot_version ()
{
return std::string (tarot_libtarot_version ());
}
inline std::string
strerror (error err)
{
return std::string (tarot_strerror (err));
}
inline card
of (number n, suit s)
{
card ret;
error err = tarot_of (n, s, &ret);
if (err == TAROT_OK)
{
return ret;
}
throw err;
}
inline std::pair <number, suit>
decompose (card c)
{
number n;
suit s;
error err = tarot_decompose (c, &n, &s);
if (err == TAROT_OK)
{
return std::pair <number, suit> (n, s);
}
throw err;
}
class game
{
public:
game (unsigned int n_players, bool with_call)
{
const size_t required = tarot_game_init (n_players, with_call, 0, NULL);
char *memory = new char[required];
assert (memory != nullptr);
assert (tarot_game_init (n_players, with_call, required, memory) == required);
m_addr = (TarotGame *) memory;
}
virtual ~game ()
{
char *memory = (char *) m_addr;
delete[] memory;
}
void
options (unsigned int &n_players, bool &with_call) const
{
int w;
tarot_game_options (m_addr, &n_players, &w);
with_call = (w != 0);
}
void
state (step &step, unsigned int &i_trick, unsigned int &n_cards_in_trick) const
{
tarot_game_state (m_addr, &step, &i_trick, &n_cards_in_trick);
}
std::vector <card>
cards (player p) const
{
std::vector<card> ret;
unsigned int n;
const card *c_mem = tarot_game_cards (m_addr, p, &n);
ret.resize (n);
for (unsigned int i = 0; i < n; ++i)
{
ret[i] = c_mem[i];
}
return ret;
}
std::vector <bid_t>
bids (player &taker) const
{
std::vector <bid_t> ret;
unsigned int n;
const bid_t *bids = tarot_game_bids (m_addr, &n, &taker);
ret.resize (n);
for (unsigned int i = 0; i < n; ++i)
{
ret[i] = bids[i];
}
return ret;
}
bool
decl (player &p) const
{
return (tarot_game_decl (m_addr, &p) != 0);
}
call_t
call (card &call, player &partner) const
{
return tarot_game_call (m_addr, &call, &partner);
}
dog_t
dog (std::vector <card> &vec_cards) const
{
const card *cards = NULL;
unsigned int n;
dog_t ret = tarot_game_dog (m_addr, (const card **) (&cards), &n);
vec_cards.resize (n);
for (unsigned int i = 0; i < n; ++i)
{
vec_cards[i] = cards[i];
}
return ret;
}
discard_t
discard (std::vector <card> &vec_cards) const
{
const card *cards = NULL;
unsigned int n;
discard_t ret = tarot_game_discard (m_addr, (const card **) (&cards), &n);
vec_cards.resize (n);
for (unsigned int i = 0; i < n; ++i)
{
vec_cards[i] = cards[i];
}
return ret;
}
handful_t
handful (player p, std::vector <card> &vec_cards) const
{
unsigned int n;
card data[22];
handful_t ret = tarot_game_handful (m_addr, p, &n, data, 22);
assert (n <= 22);
vec_cards.resize (n);
for (unsigned int i = 0; i < n; ++i)
{
vec_cards[i] = data[i];
}
return ret;
}
void
trick (unsigned int i, player &leader, bool &has_lead_suit, suit &suit, unsigned int &n_played, player &taker, team &team) const
{
int hls;
tarot_game_trick (m_addr, i, &leader, &hls, &suit, &n_played, &taker, &team);
has_lead_suit = (hls != 0);
}
card
trick_card (unsigned int i, player p, hint *h) const
{
return tarot_game_trick_card (m_addr, i, p, h);
}
bool
check_deal_one (const std::vector<card> &cards)
{
return (tarot_game_check_deal_one (m_addr, cards.size (), cards.data ()) != 0);
}
private:
TarotGame *m_addr;
};
}
#endif /* cplusplus */
#endif /* not H_TAROT_INCLUDED */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment