Ejemplo n.º 1
0
func (ht *HandTracker) populate(playerid int, hand sdz.Hand) {
	for _, suit := range sdz.Suits() {
		for _, face := range sdz.Faces() {
			card := sdz.CreateCard(suit, face)
			ht.cards[playerid][card] = 0
		}
	}
	for _, card := range hand {
		ht.cards[playerid][card]++
	}
	ht.calculate()
}
Ejemplo n.º 2
0
// Returns a new HandTracker with the initial population and calculation done
func NewHandTracker(playerid int, hand sdz.Hand) *HandTracker {
	ht := new(HandTracker)
	for x := 0; x < 4; x++ {
		ht.cards[x] = make(map[sdz.Card]int)
	}
	ht.playedCards = make(map[sdz.Card]int)
	for _, suit := range sdz.Suits() {
		for _, face := range sdz.Faces() {
			card := sdz.CreateCard(suit, face)
			ht.playedCards[card] = 0
		}
	}
	ht.populate(playerid, hand)
	return ht
}
Ejemplo n.º 3
0
func (ai AI) calculateBid() (amount int, trump sdz.Suit, show sdz.Hand) {
	bids := make(map[sdz.Suit]int)
	for _, suit := range sdz.Suits() {
		bids[suit], show = ai.hand.Meld(suit)
		bids[suit] = bids[suit] + ai.powerBid(suit)
		//		Log("Could bid %d in %s", bids[suit], suit)
		if bids[trump] < bids[suit] {
			trump = suit
		} else if bids[trump] == bids[suit] {
			rand.Seed(time.Now().UnixNano())
			if rand.Intn(2) == 0 { // returns one in the set of [0,2)
				trump = suit
			} // else - stay with trump as it was
		}
	}
	rand.Seed(time.Now().UnixNano())
	bids[trump] += rand.Intn(3) // adds 0, 1, or 2 for a little spontanaeity
	return bids[trump], trump, show
}
Ejemplo n.º 4
0
func (ht *HandTracker) calculate() {
	for _, suit := range sdz.Suits() {
		for _, face := range sdz.Faces() {
			card := sdz.CreateCard(suit, face)
			sum := ht.playedCards[card]
			for x := 0; x < 4; x++ {
				if val, ok := ht.cards[x][card]; ok {
					sum += val
				}
			}
			if sum == 2 {
				for x := 0; x < 4; x++ {
					if _, ok := ht.cards[x][card]; !ok {
						ht.cards[x][card] = 0
					}
				}
			} else {
				unknown := -1
				for x := 0; x < 4; x++ {
					if _, ok := ht.cards[x][card]; !ok {
						if unknown == -1 {
							unknown = x
						} else {
							// at least two unknowns
							unknown = -1
							break
						}
					}
				}
				if unknown != -1 {
					ht.cards[unknown][card] = 2 - sum
				}
			}
		}
	}
}
Ejemplo n.º 5
0
func (ai AI) powerBid(suit sdz.Suit) (count int) {
	count = 5 // your partner's good for at least this right?!?
	suitMap := make(map[sdz.Suit]int)
	for _, card := range *ai.hand {
		suitMap[card.Suit()]++
		if card.Suit() == suit {
			switch card.Face() {
			case ace:
				count += 3
			case ten:
				count += 2
			case king:
				fallthrough
			case queen:
				fallthrough
			case jack:
				fallthrough
			case nine:
				count += 1
			}
		} else if card.Face() == ace {
			count += 2
		} else if card.Face() == jack || card.Face() == nine {
			count -= 1
		}
	}
	for _, x := range sdz.Suits() {
		if x == suit {
			continue
		}
		if suitMap[x] == 0 {
			count++
		}
	}
	return
}