-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.xh
135 lines (105 loc) · 3.37 KB
/
state.xh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <unification.xh>
#include <list.xh>
#include <map.xh>
#include <prolog_utils.xh>
#include <string.xh>
#include <vector.xh>
#include <stdlib.h>
#ifndef _STATE_XH
#define _STATE_XH
#define SECTOR_SIZE 18
#define NUM_PIECES 4
#define FINISH_BACKTRACK_DIST 2
#define DECK_SIZE 54 * 2
#define MIN_HAND 4
#define MAX_HAND 5
#define MAX_PLAYERS 12
typedef unsigned PlayerId;
string showPlayerId(PlayerId p);
#define PLAYER_ID_NONE ((PlayerId)-1)
PlayerId partner(unsigned numPlayers, PlayerId p);
datatype Position {
Out(unsigned ?);
Finish(PlayerId ?, unsigned ?);
};
typedef datatype Position Position;
var_reference datatype Position with GC_malloc prefix gc;
string showPosition(Position p);
show datatype Position with showPosition;
static inline int comparePosition(Position p1, Position p2) {
return match (p1, p2)
(Finish(_, _), Out(_) -> 1;
Out(_), Finish(_, _) -> -1;
Finish(?&a, _), Finish(?&b, _) @when (a != b) -> a - b;
Finish(_, ?&i), Finish(_, ?&j) -> i - j;
Out(?&i), Out(?&j) -> i - j;);
}
static inline int compareUnsigned(unsigned x, unsigned y) {
return x - y;
}
typedef map<Position, PlayerId, comparePosition> Board;
typedef map<PlayerId, unsigned, compareUnsigned> Lot;
datatype State {
St(unsigned ?numPlayers, _Bool ?partners,
Board ?board, Lot ?lot);
};
typedef datatype State State;
string showState(State s);
show datatype State with showState;
unsigned numPlayers(State);
_Bool partners(State);
enum Card {
Joker, A,
J = 11, Q, K,
CARD_MAX
};
typedef enum Card Card;
datatype Move {
MoveOut(PlayerId ?);
MoveDirect(Position ?from, Position ?to);
Swap(Position ?, Position ?);
};
typedef datatype Move Move;
var_reference datatype Move with GC_malloc prefix gc;
string showMove(Move m, PlayerId p1, PlayerId p2);
string showMoves(list<Move ?> ?m, PlayerId p1, PlayerId p2);
typedef unsigned Hand[CARD_MAX];
string showHand(const Hand h);
show Hand with showHand;
show (const Hand) with showHand; // This is a different type since the qualifier attaches on the element type
datatype Action {
Play(Card, list<Move ?> ?);
Burn(Card);
};
typedef datatype Action Action;
string showAction(Action a, PlayerId p1, PlayerId p2);
string showActions(vector<Action> a, PlayerId p1, PlayerId p2);
Card getActionCard(Action);
list<Move ?> ?getActionMoves(Action);
string jsonPosition(Position ?p);
string jsonState(State s);
string jsonHand(const Hand h);
string jsonHands(unsigned numPlayers, const Hand hands[numPlayers]);
string jsonActions(vector<Action> a, PlayerId p1, PlayerId p2);
PlayerId ?copyPlayerId(PlayerId ?);
Position ?copyPosition(Position ?);
Move ?copyMoveDirect(Move ?);
list<Move ?> ?copyMoves(list<Move ?> ?);
void initializeDeck(Hand);
unsigned getDeckSize(const Hand);
unsigned deal(unsigned min, unsigned max, Hand deck, unsigned numPlayers, Hand hands[numPlayers]);
State initialState(unsigned numPlayers, _Bool partners);
State applyMove(Move, State);
State applyMoves(list<Move ?> ?, State);
State applyAction(Action, State, Hand, Hand discard);
vector<list<Move ?> ?> getCardMoves(State s, PlayerId p, Card c);
vector<Action> getActions(State s, PlayerId p, const Hand h);
_Bool actionPossible(State s, PlayerId p, const Hand h, const Hand partnerHand);
_Bool isWon(State s);
PlayerId getWinner(State s);
prolog {
move(State, Move, State ?);
moves(State, list<Move ?>, State ?);
#include "state_utils.pl"
}
#endif