Commits

larry committed 51680a9

Changed command-line processing; you can now run "python3 mille.py dumb"
to play the dumb opponent. Also cleaned up Rosey's output a little.

  • Participants
  • Parent commits 5c656f3

Comments (0)

Files changed (1)

 				if isinstance(card, Hazard):
 					playee = self.other()
 				self.play(card, playee)
-				print("    du-uh, played", card, "on", playee)
+				print("    du-uh, played", card, "on", "herself" if playee == self else "you")
 				if self.won() and self.mileage() == 700:
 					extending = len(self.hand.deck) > (len(self.hand.original_deck) // 2)
 					if extending:
 	return getch(prompt + " [y]es or [n]o>", "yn") == "y"
 
 
-def interactive():
+def pile_repr(pile):
+	s = repr(pile[-1]) if pile else ''
+	return s.rjust(10)
 
-	def pile_repr(pile):
-		s = repr(pile[-1]) if pile else ''
-		return s.rjust(10)
+def safeties_repr(player):
+	array = []
+	for card in sorted(player.safeties, reverse=True):
+		s = "".join([x for x in repr(card) if x.isupper()])
+		if card not in player.coup_fourres:
+			s = s.lower()
+		array.append(s)
+	return ' '.join(array)
 
-	def safeties_repr(player):
-		array = []
-		for card in sorted(player.safeties, reverse=True):
-			s = "".join([x for x in repr(card) if x.isupper()])
-			if card not in player.coup_fourres:
-				s = s.lower()
-			array.append(s)
-		return ' '.join(array)
+def played_so_far():
+	cards = list(game.hand.discard.pile)
+	for p in game.players:
+		cards.extend(p.battle_pile)
+		cards.extend(p.speed_pile)
+		cards.extend(p.mileage_pile)
+		cards.extend(list(p.safeties))
 
-	def played_so_far():
-		cards = list(game.hand.discard.pile)
-		for p in game.players:
-			cards.extend(p.battle_pile)
-			cards.extend(p.speed_pile)
-			cards.extend(p.mileage_pile)
-			cards.extend(list(p.safeties))
+	print()
 
+	width = 3
+	first_line = True
+	for line in (
+		(mileage_25, mileage_50, mileage_75, mileage_100, mileage_200),
+		(accident, repair, driving_ace),
+		(flat_tire, spare_tire, puncture_proof),
+		(out_of_gas, gasoline, extra_tank),
+		(stop, roll, right_of_way),
+		(speed_limit, end_of_limit),
+		):
+		result = []
+		for card in line:
+			count = len([x for x in cards if x == card])
+			total = len([x for x in game.hand.original_deck if x == card])
+			s = repr(card).rjust(width) + " " + str(count).rjust(2)
+			if not isinstance(card, Safety):
+				s += "/" + str(total).ljust(2)
+			result.append(s)
+		s = " :: ".join(result)
+		if first_line:
+			print("-" * len(s))
+		print(s)
+		if first_line:
+			first_line = False
+			print("-" * len(s))
+			width = 10
+
+	game.hand.print_scores()
+
+
+class StdioPlayer(Player):
+
+	def keep_playing(self):
+		return yes_no("Keep playing?")
+
+	def hand_over(self):
 		print()
+		print()
+		getch("Press [space] to continue > ", " ")
 
-		width = 3
-		first_line = True
-		for line in (
-			(mileage_25, mileage_50, mileage_75, mileage_100, mileage_200),
-			(accident, repair, driving_ace),
-			(flat_tire, spare_tire, puncture_proof),
-			(out_of_gas, gasoline, extra_tank),
-			(stop, roll, right_of_way),
-			(speed_limit, end_of_limit),
-			):
-			result = []
-			for card in line:
-				count = len([x for x in cards if x == card])
-				total = len([x for x in game.hand.original_deck if x == card])
-				s = repr(card).rjust(width) + " " + str(count).rjust(2)
-				if not isinstance(card, Safety):
-					s += "/" + str(total).ljust(2)
-				result.append(s)
-			s = " :: ".join(result)
-			if first_line:
-				print("-" * len(s))
-			print(s)
-			if first_line:
-				first_line = False
-				print("-" * len(s))
-				width = 10
+	def turn(self):
+		hand = self.hand
+		other = self.other()
 
-		game.hand.print_scores()
+		while self.cards:
+			print("    deck", str(len(hand.deck)).rjust(3) + "/" + str(len(hand.original_deck)), "   discard |" + pile_repr(hand.discard.pile) + "|")
+			print("             total|hand     miles|battlepile|speed pile|safeties")
+			def stats(player):
+				strings = [player.name.ljust(8)[:8]]
+				add = strings.append
 
+				score = player.score()[0]
 
-	class StdioPlayer(Player):
+				add(" ")
+				add(str(score + player.total_score).rjust(5))
 
-		def keep_playing(self):
-			return yes_no("Keep playing?")
+				add("|")
+				add(str(score).rjust(4))
 
-		def hand_over(self):
+				add("     ")
+				add(str(player.mileage()).rjust(4))
+
+				add(" .:"[player.two_hundreds()])
+
+				add("|")
+				add(pile_repr(player.battle_pile))
+
+				add("|")
+				add(pile_repr(player.speed_pile))
+
+				add("|")
+				add(safeties_repr(player))
+
+				print("   ", "".join(strings))
+
+			stats(other)
+			stats(self)
+
+			line1 = []
+			line2 = []
+			for i, card in enumerate(self.cards, 1):
+				line1.append(("[" + str(i) + "]").rjust(11))
+				line2.append(repr(card).rjust(11))
+
 			print()
-			print()
-			getch("Press [space] to continue > ", " ")
+			print("   ", ''.join(line1))
+			print("   ", ''.join(line2))
 
-		def turn(self):
-			hand = self.hand
-			other = self.other()
+			c = None
+			valid_cards = "123456"[:len(self.cards)]
+			valid = valid_cards + '!' + chr(27)
+			prompt = "    play [1-" + str(len(self.cards)) + "]"
 
-			while self.cards:
-				print("    deck", str(len(hand.deck)).rjust(3) + "/" + str(len(hand.original_deck)), "   discard |" + pile_repr(hand.discard.pile) + "|")
-				print("             total|hand     miles|battlepile|speed pile|safeties")
-				def stats(player):
-					strings = [player.name.ljust(8)[:8]]
-					add = strings.append
+			if hand.deck and (len(self.cards) < 6):
+				prompt += " or [d]raw?> "
+				valid += "d"
+			else:
+				prompt += " or [.]discard?> "
+				valid += "."
+			c = getch(prompt, valid)
 
-					score = player.score()[0]
+			if c == chr(27):
+				sys.exit("ESC")
+			if c == '!':
+				played_so_far()
+				continue
+			if c == 'd':
+				card = hand.deck.pop()
+				self.cards.append(card)
+				self.cards.sort(reverse=True)
+				print()
+				print("    drew", card)
+				print()
+				continue
+			if c == '.':
+				c2 = getch("    Discard which card? " + valid_cards + " >")
+				if c2 not in valid_cards:
+					continue
+				i = int(c2) - 1
+			else:
+				assert '1' <= c <= str(len(self.cards))
+				i = int(c) - 1
 
-					add(" ")
-					add(str(score + player.total_score).rjust(5))
+			card = self.cards[i]
+			player = self
+			if c == '.':
+				playee = hand.discard
+			elif isinstance(card, Hazard):
+				playee = other
+			else:
+				playee = self
 
-					add("|")
-					add(str(score).rjust(4))
+			# print("    Playing", card, "on", playee.name)
+			try:
+				player.play(card, playee)
+			except IllegalMove as response:
+				bangs = "!" * len(str(response))
+				print()
+				print("   ", bangs)
+				print("   ", response)
+				print("   ", bangs)
+				print()
+				c = None
+				continue
+			break
 
-					add("     ")
-					add(str(player.mileage()).rjust(4))
-
-					add(" .:"[player.two_hundreds()])
-
-					add("|")
-					add(pile_repr(player.battle_pile))
-
-					add("|")
-					add(pile_repr(player.speed_pile))
-
-					add("|")
-					add(safeties_repr(player))
-
-					print("   ", "".join(strings))
-
-				stats(other)
-				stats(self)
-
-				line1 = []
-				line2 = []
-				for i, card in enumerate(self.cards, 1):
-					line1.append(("[" + str(i) + "]").rjust(11))
-					line2.append(repr(card).rjust(11))
-
-				print()
-				print("   ", ''.join(line1))
-				print("   ", ''.join(line2))
-
-				c = None
-				valid_cards = "123456"[:len(self.cards)]
-				valid = valid_cards + '!' + chr(27)
-				prompt = "    play [1-" + str(len(self.cards)) + "]"
-
-				if hand.deck and (len(self.cards) < 6):
-					prompt += " or [d]raw?> "
-					valid += "d"
-				else:
-					prompt += " or [.]discard?> "
-					valid += "."
-				c = getch(prompt, valid)
-
-				if c == chr(27):
-					sys.exit("ESC")
+		if (self.mileage() == 700) and not hand.extended:
+			while True:
+				c = getch("    You reached 700!  Extend? >", "yn!")
 				if c == '!':
 					played_so_far()
 					continue
-				if c == 'd':
-					card = hand.deck.pop()
-					self.cards.append(card)
-					self.cards.sort(reverse=True)
-					print()
-					print("    drew", card)
-					print()
-					continue
-				if c == '.':
-					c2 = getch("    Discard which card? " + valid_cards + " >")
-					if c2 not in valid_cards:
-						continue
-					i = int(c2) - 1
-				else:
-					assert '1' <= c <= str(len(self.cards))
-					i = int(c) - 1
+				break
+			hand.extended = c == "y"
 
-				card = self.cards[i]
-				player = self
-				if c == '.':
-					playee = hand.discard
-				elif isinstance(card, Hazard):
-					playee = other
-				else:
-					playee = self
+if __name__ == "__main__":
+	players = [StdioPlayer]
+	if len(sys.argv) > 1:
+		if sys.argv[1] == "test":
+			del sys.argv[1]
+			unittest.main()
+		if sys.argv[1] == "battle":
+			del sys.argv[1]
+			players = []
 
-				# print("    Playing", card, "on", playee.name)
-				try:
-					player.play(card, playee)
-				except IllegalMove as response:
-					bangs = "!" * len(str(response))
-					print()
-					print("   ", bangs)
-					print("   ", response)
-					print("   ", bangs)
-					print()
-					c = None
-					continue
-				break
-
-			if (self.mileage() == 700) and not hand.extended:
-				while True:
-					c = getch("    You reached 700!  Extend? >", "yn!")
-					if c == '!':
-						played_so_far()
-						continue
-					break
-				hand.extended = c == "y"
+		for arg in sys.argv[1:3]:
+			players.append(DumbComputerPlayer
+				if arg == "dumb"
+				else AverageComputerPlayer)
+	
+	for i in range(len(players), 2):
+		players.append(AverageComputerPlayer)
 
 	game = Game()
-	username = getpass.getuser()
-	p1 = StdioPlayer(game, username or "human")
-	# p2 = DumbComputerPlayer(game)
-	p2 = AverageComputerPlayer(game)
-	game.play()	
+	for constructor in players:
+		if constructor == StdioPlayer:
+			username = getpass.getuser()
+			StdioPlayer(game, username or "human")
+		else:
+			constructor(game)
+	game.play()
 
-if __name__ == "__main__":
-	if len(sys.argv) > 1:
-		argv1 = sys.argv[1]
-		del sys.argv[1]
-		if argv1 == "test":
-			unittest.main()
-		if argv1 == "battle":
-			players = [AverageComputerPlayer, AverageComputerPlayer]
-			for i, arg in enumerate(sys.argv[1:]):
-				if i == 2:
-					break
-				if arg == "dumb":
-					players[i] = DumbComputerPlayer
-				elif arg == "average":
-					players[i] = AverageComputerPlayer
-			game = Game()
-			for constructor in players:
-				constructor(game)
-			game.play()
-			sys.exit()
-
-	interactive()