Commit 41693a0f authored by Vivien Kraus's avatar Vivien Kraus

Implement --impute

parent 2b4a5504
......@@ -21,7 +21,6 @@ fail_tests = \
%reldir%/discard_not_expected.tarot \
%reldir%/discard_should_be_given.tarot \
%reldir%/discard_shown_not_trumps.tarot \
%reldir%/dog.tarot \
%reldir%/handful_forgot.tarot
TESTS += $(fail_tests)
......@@ -31,6 +30,5 @@ EXTRA_DIST += \
%reldir%/discard_not_expected.tarot.expected \
%reldir%/discard_should_be_given.tarot.expected \
%reldir%/discard_shown_not_trumps.tarot.expected \
%reldir%/dog.tarot.expected \
%reldir%/handful_forgot.tarot.expected \
$(fail_tests)
setup 5 with call
deal 21T 20T 13T 9T 8T 4T 1T KH QH CH KD QD 2D AD 8C to P5
bid take
bid pass
bid pass
bid pass
bid push
decl slam
call KC
status
status with imputation
dog AH KH EXC
\ No newline at end of file
+---------+---------+---------+---------+---------+
| P1 | P2 | P3 | P4 | * P5 |
+---------+---------+---------+---------+---------+
| take | pass | pass | pass | push |
| slam! | pass | pass | pass | pass |
+---------+---------+---------+---------+---------+
- Call: KC
+---------+---------+---------+---------+---------+
| P1 | P2 | P3 | P4 | * P5 |
+---------+---------+---------+---------+---------+
- Cards of P5: CH, QH, KH, 8C, AD, 2D, QD, KD, 1T, 4T, 8T, 9T, 13T, 20T, 21T
+---------+---------+---------+---------+---------+
| P1 | P2 | P3 | P4 | * P5 * |
+---------+---------+---------+---------+---------+
| take | pass | pass | pass | push |
| slam! | pass | pass | pass | pass |
+---------+---------+---------+---------+---------+
- Call: KC
+---------+---------+---------+---------+---------+
| P1 | P2 | P3 | P4 | * P5 * |
+---------+---------+---------+---------+---------+
- Cards of P1: 2H, 5H, JH, AC, 2C, JC, 5D, 8D, CD, 8S, 9S, 3T, 6T, 7T, 15T
- Cards of P2: 3H, 8H, 10H, 3C, 4C, 5C, 6C, CC, QC, 9D, 2S, 4S, KS, 16T, 19T
- Cards of P3: 4H, 6H, 7H, 9H, 6D, 10D, 3S, 6S, CS, 10T, 11T, 12T, 14T, 17T, EXC
- Cards of P4: AH, 7C, 9C, 4D, 7D, JD, AS, 5S, 7S, 10S, JS, QS, 2T, 5T, 18T
- Cards of P5: CH, QH, KH, 8C, AD, 2D, QD, KD, 1T, 4T, 8T, 9T, 13T, 20T, 21T
ERROR: this is not possible
......@@ -1724,6 +1724,9 @@ game_assign_missing_card (TarotGameHandle * game, TarotCard card,
}
}
}
/* If we're doing one assignment, then the others will follow; so we
shift from the deal to one player, to the full deal. */
* (game->dealt) = game->n_players;
return 0;
}
......
......@@ -13,6 +13,7 @@ include = "#include <tarot.h>";
include = "#include <string.h>";
include = "#include \"assert.h\"";
include = "int tarot_run_status (char *input, char **output);";
include = "int tarot_run_impute (char *input, const char *seed, int retry, char **output);";
include = "int tarot_run_new_game (size_t n_players, int with_call, char **output);";
include = "int tarot_run_deal_to (char *input, TarotPlayer whom, TarotCardArray *cards, char **output);";
include = "int tarot_run_deal_all (char *input, TarotPlayerArray *owners, char **output);";
......@@ -218,6 +219,10 @@ main = {
input = read_input ();
res = tarot_run_status (input, &output);
break;
case INDEX_OPT_IMPUTE:
input = read_input ();
res = tarot_run_impute (input, OPT_ARG (SEED), OPT_VALUE_RETRY, &output);
break;
case INDEX_OPT_NEW_GAME:
res = tarot_run_new_game (OPT_VALUE_N_PLAYERS, ENABLED_OPT (CALL_ALLOWED) && HAVE_OPT (CALL_ALLOWED), &output);
break;
......@@ -334,6 +339,14 @@ flag = {
doc = 'In this mode, the program deals all cards at random. This operation may fail, since a random deal may lead to a petit sec. To prevent this, use a good combination of @command{--seed}, and @command{--retry}.';
};
flag = {
name = impute;
no-preset;
equivalence = new-game;
descrip = "Impute the game";
doc = 'When we do not know the cards for other players, it is impossible to simulate a game. To guess the cards of the other player, so that it is coherent with the past game actions, you have to @emph{impute} the game. This is a random operation that may fail for no particular reason, but if the game is corrupted then it may never succeed. You can use @command{--seed} and @command{--retry} with this option.';
};
flag = {
name = bid;
no-preset;
......
......@@ -70,7 +70,8 @@ $(srcdir)/%reldir%/tarot_cli.c: %reldir%/tarot_cli.vala src/tarot-@LIBTOOL_CURRE
TAROT_CLI_TESTS = \
%reldir%/test-petit-sec.sh \
%reldir%/test-empty-status.sh \
%reldir%/test-no-slam.sh
%reldir%/test-no-slam.sh \
%reldir%/test-dog-impossible.sh
EXTRA_DIST += %reldir%/tarot-cli.man \
%reldir%/tarot_cli.vala \
......
namespace Tarot {
private static int retry_impute (Tarot.Game game, char[] base_seed, int n_retries, int max_retry) {
char[] seed = base_seed;
string added = "%d".printf (n_retries);
for (long i = 0; i < added.length; i++) {
seed += added[i];
}
var counter = new Tarot.Counter.alloc ();
if (counter.load_from_game (game) != 0) {
stderr.printf ("ERROR: this is not possible.\n");
return 1;
}
if (counter.and_game_impute (game, seed) != 0) {
if (max_retry < 0 || n_retries < max_retry) {
return retry_impute (game, base_seed, n_retries + 1, max_retry);
} else {
stderr.printf ("ERROR: this is not possible.\n");
return 1;
}
}
return 0;
}
public static int run_impute (string input, string seed, int retry, out string? output) {
var game = new Tarot.Game.alloc ();
output = null;
if (game.load_from_xml (input) != 0) {
stderr.printf ("ERROR: this game is not valid.\n");
return 1;
}
char[] seed_bytes = new char[seed.length];
for (long i = 0; i < seed.length; i++) {
seed_bytes[i] = seed[i];
}
var counter = new Tarot.Counter.alloc ();
if (counter.load_from_game (game) != 0) {
stderr.printf ("ERROR: this is not possible.\n");
return 1;
}
if (counter.and_game_impute (game, seed_bytes) != 0) {
if (retry_impute (game, seed_bytes, 1, retry) != 0) {
return 1;
}
}
output = game.save_to_xml ();
return 0;
}
public static int run_new_game (size_t n_players, bool with_call, out string? output) {
var game = new Tarot.Game.alloc ();
var e = new Tarot.GameEvent.alloc ();
......
#!/bin/sh
tarot-cli --help > /dev/null || exit 1
AT_CALL=$(tarot-cli --new-game -n 5 -c \
| tarot-cli --deal-to P5=21T,20T,13T,9T,8T,4T,1T,KH,QH,CH,KD,QD,2D,AD,8C \
| tarot-cli --bid=take \
| tarot-cli --bid=pass \
| tarot-cli --bid=pass \
| tarot-cli --bid=push \
| tarot-cli --decl=slam \
| tarot-cli --call=KC)
STATUS=$(echo "$AT_CALL" | tarot-cli --status)
STATUS_IMPUTED=$(echo "$AT_CALL" | tarot-cli --impute | tarot-cli --status)
STATUS_EXPECTED=$(
echo "+---------+---------+---------+---------+---------+"
echo "| P1 | P2 | P3 | P4 | * P5 |"
echo "+---------+---------+---------+---------+---------+"
echo "| take | pass | pass | pass | push |"
echo "| slam! | pass | pass | pass | pass |"
echo "+---------+---------+---------+---------+---------+"
echo " - Call: KC"
echo "+---------+---------+---------+---------+---------+"
echo "| P1 | P2 | P3 | P4 | * P5 |"
echo "+---------+---------+---------+---------+---------+"
echo " - Cards of P5: CH, QH, KH, 8C, AD, 2D, QD, KD, 1T, 4T, 8T, 9T, 13T, 20T, 21T"
)
STATUS_IMPUTED_EXPECTED=$(
echo "+---------+---------+---------+---------+---------+"
echo "| P1 | P2 | P3 | P4 | * P5 * |"
echo "+---------+---------+---------+---------+---------+"
echo "| take | pass | pass | pass | push |"
echo "| slam! | pass | pass | pass | pass |"
echo "+---------+---------+---------+---------+---------+"
echo " - Call: KC"
echo "+---------+---------+---------+---------+---------+"
echo "| P1 | P2 | P3 | P4 | * P5 * |"
echo "+---------+---------+---------+---------+---------+"
echo " - Cards of P1: 2H, 5H, JH, AC, 2C, JC, 5D, 8D, CD, 8S, 9S, 3T, 6T, 7T, 15T"
echo " - Cards of P2: 3H, 8H, 10H, 3C, 4C, 5C, 6C, CC, QC, 9D, 2S, 4S, KS, 16T, 19T"
echo " - Cards of P3: 4H, 6H, 7H, 9H, 6D, 10D, 3S, 6S, CS, 10T, 11T, 12T, 14T, 17T, EXC"
echo " - Cards of P4: AH, 7C, 9C, 4D, 7D, JD, AS, 5S, 7S, 10S, JS, QS, 2T, 5T, 18T"
echo " - Cards of P5: CH, QH, KH, 8C, AD, 2D, QD, KD, 1T, 4T, 8T, 9T, 13T, 20T, 21T"
)
if test "$STATUS" != "$STATUS_EXPECTED"
then
>&2 echo "FAILED: the status is not correct ('$STATUS' != '$STATUS_EXPECTED')"
fi
if test "$STATUS_IMPUTED" != "$STATUS_IMPUTED_EXPECTED"
then
>&2 echo "FAILED: the status with imputation is not correct ('$STATUS_IMPUTED' != '$STATUS_IMPUTED_EXPECTED')"
fi
if echo "$AT_CALL" | tarot-cli --dog AH,KH,EXC > /dev/null 2>&1
then
>&2 echo "FAILED: I was not supposed to be able to reveal that dog"
exit 1
else
exit 0
fi
......@@ -53,4 +53,11 @@
obj:*/libgobject-2.0*
...
fun:g_type_class_ref
}
{
bashisms
Memcheck:Leak
...
obj:/usr/bin/bash
...
}
\ No newline at end of file
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