-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoves.h
213 lines (173 loc) · 4.98 KB
/
moves.h
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef MOVES_H
#define MOVES_H
#include "types.h"
static inline void Update_pile(PILE* pile) {
if (pile->revealed == pile->cardNumber) {
--pile->revealed;
}
if (pile->revealed < 0) {
pile->revealed = 0;
}
return;
}
static inline bool Turn_stock(STOCK* stock) {
if (stock->cardsLeft == 0) {
// ILLEGAL MOVE
return false;
}
while (true) {
++stock->faceCard;
if (stock->faceCard > STOCK_SIZE) {
stock->faceCard = -1;
return true;
}
if (stock->faceCard == STOCK_SIZE) {
return true;
}
if (stock->stack[stock->faceCard] != NO_CARD) {
return true;
}
}
return true;
};
static inline bool Stock_to_foundation(GAMESTATE* gamestate) {
STOCK* stock = &gamestate->stock;
bool stock_displaying_card = (stock->faceCard != -1 && stock->faceCard != STOCK_SIZE);
if (stock_displaying_card) {
CARDS stock_card = stock->stack[stock->faceCard];
int foundation_index = get_card_suit(stock_card);
FOUNDATION* foundation = &gamestate->foundations[foundation_index];
bool card_is_one_higher = (get_card_rank(stock_card) == foundation->cards);
if (!card_is_one_higher) {
return false;
}
// add foundation card;
foundation->stack[foundation->cards] = stock_card;
++foundation->cards;
// remove stock card
stock->stack[stock->faceCard] = NO_CARD;
Turn_stock(stock);
--stock->cardsLeft;
return true;
}
return false;
}
static inline bool Pile_to_foundation(GAMESTATE* gamestate, int pile_index) {
PILE* pile = &gamestate->piles[pile_index];
if (pile->cardNumber != 0) { // cards exist
CARDS pile_card = pile->pile[pile->cardNumber - 1]; // Top card
int foundation_index = get_card_suit(pile_card);
FOUNDATION* foundation = &gamestate->foundations[foundation_index];
bool card_is_one_higher = (get_card_rank(pile_card) == foundation->cards);
if (!card_is_one_higher) {
return false;
}
// add foundation card;
foundation->stack[foundation->cards] = pile_card;
++foundation->cards;
// remove pile card
--pile->cardNumber;
pile->pile[pile->cardNumber] = NO_CARD;
Update_pile(pile);
return true;
}
return false;
}
static inline bool Foundation_to_pile(PILE* pile, FOUNDATION* foundation, int foundationIndex) {
if (foundation->cards != 0) { // cards exist
CARDS foundation_card = foundation->stack[foundation->cards - 1]; // Top card
if (pile->cardNumber == 0 && get_card_rank(foundation_card) != KING) {
return false;
}
else {
bool is_not_same_suit = ((foundationIndex >> 1) ^ (get_card_suit(pile->pile[pile->cardNumber - 1]) >> 1));
if (is_not_same_suit) {
return false;
}
else {
bool card_is_one_higher = (get_card_rank(foundation_card) + 1) == (get_card_rank(pile->pile[pile->cardNumber - 1]));
if (!card_is_one_higher) {
// ILLEGAL MOVE, not 1 higher
return false;
}
}
}
// add pile card
pile->pile[pile->cardNumber] = foundation_card;
++pile->cardNumber;
Update_pile(pile);
// remove foundation card;
--foundation->cards;
foundation->stack[foundation->cards] = NO_CARD;
return true;
}
return false;
}
static inline bool Pile_to_pile(PILE* pile_from, PILE* pile_to, int card_number) {
if (pile_from->cardNumber > 0) {
int card_from_index = (pile_from->cardNumber - card_number);
CARDS card_from = pile_from->pile[card_from_index];
if ((card_from_index) < pile_from->revealed) {
return false;
}
if (pile_to->cardNumber == 0) {
if (get_card_rank(card_from) != KING) {
return false;
}
}
else {
CARDS card_to = pile_to->pile[pile_to->cardNumber - 1];
if ((get_card_suit(card_from) >> 1) == (get_card_suit(card_to) >> 1)) {
return false;
}
if ((get_card_rank(card_from) + 1) != get_card_rank(card_to)) {
return false;
}
}
int moved_to_index = (pile_to->cardNumber);
for (; card_number > 0; --card_number) {
CARDS card_moved = pile_from->pile[card_from_index];
pile_to->pile[moved_to_index] = card_moved;
++pile_to->cardNumber;
++moved_to_index;
pile_from->pile[card_from_index] = NO_CARD;
--pile_from->cardNumber;
++card_from_index;
}
Update_pile(pile_from);
Update_pile(pile_to);
return true;
}
return false;
}
static inline bool Stock_to_pile(PILE* pile_to, STOCK* stock) {
bool stock_displaying_card = (stock->faceCard != -1 && stock->faceCard != STOCK_SIZE);
if (stock_displaying_card) {
CARDS card_from = stock->stack[stock->faceCard];
if (pile_to->cardNumber == 0) {
if (get_card_rank(card_from) != KING) {
return false;
}
}
else {
CARDS card_to = pile_to->pile[pile_to->cardNumber - 1];
if ((get_card_suit(card_from) >> 1) == (get_card_suit(card_to) >> 1)) {
return false;
}
if ((get_card_rank(card_from) + 1) != get_card_rank(card_to)) {
return false;
}
}
int moved_from_index = (stock->faceCard);
int moved_to_index = (pile_to->cardNumber);
pile_to->pile[moved_to_index] = card_from;
++pile_to->cardNumber;
stock->stack[moved_from_index] = NO_CARD;
Turn_stock(stock);
--stock->cardsLeft;
Update_pile(pile_to);
return true;
}
return false;
}
#endif