3 bool RUFF_Legal(RuffGame game, Player player, int card)
6 if(player.cards[card].kind != none)
9 if(game.start != player.id)
12 bool canSupply = false;
13 Kind kind = game.played[game.start].kind;
15 if(player.cards[c].kind == kind)
20 if(canSupply && player.cards[card].kind != kind)
27 int RUFF_CardScore(RuffGame game, Kind kind, Number number)
29 Round * round = &game.rounds[game.round];
30 Kind setKind = game.played[game.start].kind;
34 else if(kind == round->bet.trump)
39 PlayerPosition RUFF_HandWinner(RuffGame game, int * points, int * maxScore)
41 PlayerPosition winner = none;
48 if(game.played[c].kind != none)
52 switch(game.played[c].number)
54 case _5: *points += 5; break;
55 case _10: case ace: *points += 10; break;
57 score = RUFF_CardScore(game, game.played[c].kind, game.played[c].number);
58 if(score >= *maxScore)
68 static void Server_ShuffleDeck(Card * deck)
73 RandomSeed((int)(GetTime() * 1000));
75 count = GetRandom(1000, 2000);
76 for(t = 0; t<count; t++)
79 Card cuts[2][NUMCARDS];
81 int indexCut[2] = {0,0};
82 int cut = GetRandom(NUMCARDS / 4, NUMCARDS * 3 / 4);
85 for(c = 0; c<cut; c++)
87 for(c = cut; c<NUMCARDS; c++)
88 cuts[1][c - cut] = deck[c];
91 numCut[1] = NUMCARDS - cut;
94 for(c = 0; c<NUMCARDS; c++)
98 if(indexCut[0] < numCut[0] && indexCut[1] < numCut[1])
99 whichCut = GetRandom(0,1);
100 else if(indexCut[0] < numCut[0])
102 else if(indexCut[1] < numCut[1])
105 deck[c] = cuts[whichCut][indexCut[whichCut]++];
110 static int CompareCards(const Card * card1, const Card * card2)
112 if(card1->kind > card2->kind)
114 else if(card1->kind < card2->kind)
116 else if(card1->number < card2->number)
118 else if(card1->number > card2->number)
124 static void Server_Deal(RuffGame game)
127 Round * round = &game.rounds[game.round];
128 static Card deck[NUMCARDS];
130 // Initialize the deck
131 for(c = 0; c<NUMCARDS; c++)
133 deck[c].kind = (Kind)(c / 9);
134 deck[c].number = (Number)(c % 9);
137 Server_ShuffleDeck(deck);
141 // Extra cards for ruff...
142 game.players[c].cards[9].kind = none;
143 game.players[c].cards[10].kind = none;
146 for(c = 0; c<NUMCARDS; c++)
148 Player * player = &game.players[(round->shuffle + c) % 4];
149 player->cards[c / 4] = deck[c];
153 qsort(game.players[c].cards, 9, sizeof(Card), CompareCards);
155 round->bet.howMuch = 0;
156 round->bet.player = none;
157 round->bet.trump = none;
159 game.turn = game.start = (round->shuffle + 1) % 4;
162 round->scores[0] = round->scores[1] = 0;
165 Player_InformNewRound(&game.players[c], game.round, round->shuffle, game.players[c].cards);
167 Player_RequestBet(&game.players[game.turn]);
170 void Server_StartGame(RuffGame game)
175 RandomSeed((int)(GetTime() * 1000));
176 game.rounds[0].shuffle = (PlayerPosition)GetRandom(0, 3);
178 game.gameStarted = true;
180 game.played[c].kind = none;
183 Player_InformStartGame(&game.players[c]);
187 void Server_StopGame(RuffGame game)
192 game.gameStarted = false;
195 Player_InformStopGame(&game.players[c]);
198 void Server_PlaceBet(RuffGame game, Player player, int bet)
200 Round * round = &game.rounds[game.round];
202 (bet >= 40 && bet <= 100 && !(bet%5) && bet >= round->bet.howMuch &&
203 (round->shuffle == player.id || bet > round->bet.howMuch)))
207 if(bet || (!round->bet.howMuch && round->shuffle == player.id))
209 round->bet.howMuch = bet;
210 round->bet.player = game.turn;
212 game.turn = (game.turn + 1) % 4;
215 Player_InformBet(&game.players[c], player.id, bet);
217 if(game.turn == game.start)
219 game.turn = game.start = round->bet.player;
220 Player_RequestTrump(&game.players[game.turn]);
223 Player_RequestBet(&game.players[game.turn]);
226 Player_RequestBet(player);
229 void Server_ChoseTrump(RuffGame game, Player player, Kind trump)
231 Round * round = &game.rounds[game.round];
232 if(trump >= 0 || trump < 4 || trump == none)
236 round->bet.trump = trump;
239 Player_InformTrump(&game.players[c], trump);
242 Player_InformStartHand(&game.players[c], game.start);
244 game.played[c].kind = none;
246 game.betting = false;
247 Player_RequestCard(&game.players[game.turn]);
250 Player_RequestTrump(player);
253 void Server_Ack4Cards(RuffGame game, Player player)
255 Round * round = &game.rounds[game.round];
257 int totalScores[2] = { 0 };
262 if(game.cardSet == 9)
264 for(c = 0; c<game.round; c++)
266 totalScores[0] += game.rounds[c].scores[0];
267 totalScores[1] += game.rounds[c].scores[1];
272 if((int)round->bet.player % 2 == c && round->scores[c] < round->bet.howMuch)
273 round->scores[c] = -round->bet.howMuch;
274 totalScores[c] += round->scores[c];
278 Player_InformEndOfRound(&game.players[c], round->scores[0], round->scores[1]);
283 if((totalScores[0] < 500 && totalScores[1] < 500) || totalScores[0] == totalScores[1])
285 game.rounds[game.round].shuffle = (round->shuffle + 1) % 4;
291 Player_InformNewRound(&game.players[c], game.round, game.rounds[game.round].shuffle,
292 game.players[c].cards);
298 Player_InformStartHand(&game.players[c], game.start);
300 game.played[c].kind = none;
302 Player_RequestCard(&game.players[game.turn]);
306 void Server_PlayCard(RuffGame game, Player player, int card)
308 if(player.id == game.turn && RUFF_Legal(game, player, card))
311 Card theCard = player.cards[card];
314 Player_InformCardPlayed(&game.players[c], player.id,
315 theCard.kind, theCard.number);
317 game.played[player.id] = theCard;
318 player.cards[card].kind = none;
319 game.turn = (player.id + 1) % 4;
321 // End of set of 4 cards
322 if(game.turn == game.start)
324 Round * round = &game.rounds[game.round];
325 int points, maxScore;
326 PlayerPosition winner;
327 winner = RUFF_HandWinner(game, &points, &maxScore);
328 round->scores[winner%2] += points;
331 Player_Request4CardsAck(&game.players[c], winner, round->scores[0], round->scores[1]);
333 game.turn = game.start = winner;
335 Server_Ack4Cards(player.game, &game.players[0]);
338 Player_RequestCard(&game.players[game.turn]);
341 Player_RequestCard(player);
344 void Server_SetName(RuffGame game, Player player, char * name)
347 strcpy(player.name, name);
349 Player_InformPlayerInfo(&game.players[c], player.id, name);