MCPcopy Create free account
hub / github.com/ActiveState/code / ComputerPlayer

Class ComputerPlayer

recipes/Python/580811_Uno_TextBased/recipe-580811.py:383–514  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

381 random.shuffle(self.deck)
382
383class ComputerPlayer(Player):
384
385 def __init__(self, name):
386 super().__init__(name)
387 self.type = 'Computer'
388 self.begun = False
389 self.colorsInHand = {'red':0, 'blue':0, 'green':0, 'yellow':0, 'wild':0}
390 self.colorsOutHand = {}
391 self.currentColor = ""
392
393 def addCard(self, card):
394 Player.addCard(self, card)
395 color = card.getColor()
396 self.colorsInHand[color] += 1
397
398 def indexCard(self, cardColor, cardValue):
399 for card in self.hand:
400 if card.getValue() == cardValue:
401 if cardValue in ('+4', 'W'):
402 return self.hand.indexCard(card)
403 else:
404 if card.getColor() == cardColor:
405 return self.hand.indexCard(card)
406 raise ValueError("Card Cannot Be Found")
407
408 def think(self, match):
409 card = None
410 self.currentColor = match.currentColor
411 currentValue = match.currentValue
412 zeroChangeRule = match.zeroChange
413 twoPlayers = False
414 previousTurnID = match.getNextTurn(True)
415 nextTurnID = match.getNextTurn(False)
416 previousPlayer = match.getPlayer(previousTurnID)
417 #nextPlayer = match.getPlayer(nextTurnID)
418 if previousTurnID == nextTurnID:
419 twoPlayers = True
420 if self.canSkip == False and self.canReverse == True:
421 self.canSkip = True
422 self.canReverse = False
423
424 self.getLegalCards(self.currentColor, currentValue, zeroChangeRule)
425
426 ### DRAW CASE ###
427
428 if len(self.legalCards) == 0 and len(self.wildCards) == 0:
429 return "d"
430
431 else:
432
433 ### NO LEGAL CARD, USE WILD CARD ###
434
435 if len(self.legalCards) == 0:
436
437 if zeroChangeRule and self.canZeroChange:
438 bestZeroColor = self.getBestColor(self.zeroCards)
439 card = self.getCardByColor(self.zeroCards, bestZeroColor)
440

Callers 1

addComputerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected