Skip to content

Instantly share code, notes, and snippets.

@serjek
Created August 11, 2021 17:32
Show Gist options
  • Save serjek/18bfa3450fd333c8ee4dba71da424c00 to your computer and use it in GitHub Desktop.
Save serjek/18bfa3450fd333c8ee4dba71da424c00 to your computer and use it in GitHub Desktop.
SPP deal service
package ru.smartpref.modules.game;
import ru.smartpref.core.service.ISyncService;
import ru.smartpref.core.object.Card;
import ru.smartpref.core.object.Player;
class DealService implements ISyncService {
public function new() {}
public function newDeal(isOblique:Bool):Array<Array<Int>> {
return isOblique
? obliqueDeal()
:{
var cardsShuffle = shuffle(Card.CARDS);
[
cardsShuffle.slice(0,10),
cardsShuffle.slice(10,20),
cardsShuffle.slice(20,30),
cardsShuffle.slice(30)
];
};
}
function obliqueDeal() {
final cards:Array<Card> = shuffle(Card.CARDS);
final suits:Array<Suit> = shuffle(Suit.ALL);
final OBLIQUE_SUIT_LENGTH = 5;
var usedCards:Array<Card> = [];
function takeCard(?s:Suit = NT) {
for (c in cards)
if (usedCards.indexOf(c) == -1 && (s == NT || c.suit == s)) {
usedCards.push(c);
return c;
}
return null;
}
var hands = [
for (p in Player.ALL) {
var ret = [];
while (ret.length < OBLIQUE_SUIT_LENGTH) {
ret.push(takeCard(suits[p]));
}
ret;
}
];
for (h in hands) {
while (h.length < 10)
h.push(takeCard());
}
final talon = [for (c in cards) if (usedCards.indexOf(c) == -1) c];
hands.push(talon);
return hands;
}
static function shuffle<T>(arr:Array<T>):Array<T> {
if (arr != null) {
for (i in 0...arr.length) {
var j = int(0, arr.length - 1);
var a = arr[i];
var b = arr[j];
arr[i] = b;
arr[j] = a;
}
}
return arr;
}
static inline function int(from:Int, to:Int):Int {
return from + Math.floor(((to - from + 1) * Math.random()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment