Implement XENO in python

The XENO video is really interesting, isn't it? I usually write rails, but sometimes I tried to implement the topic XENO with python as a practice of python.

I'd like to use it as a material for machine learning or post it on youtube.

If you set mannual_flag to True, you can play CPU battles of Texto Logic.

Xeno.py


import random

class Xeno():
	def __init__(self):
		self.winner = "none"

	class Player():
		def __init__(self,teban):
			self.hand = []
			self.field = []
			self.predict_flag = False
			self.defence_flag = False
			self.mannual_flag = False
			if teban == 0:
				self.teban="sente"
			elif teban ==1:
				self.teban="gote"

	def set_deck(self):
		card_list=[1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,10]
		random.shuffle(card_list)
		self.deck = card_list
		#Reincarnation bill processing
		self.reincarnation_card = self.deck.pop(-1)

	#Draw one from the deck
	def draw_top_card(self):
		if len(self.deck) > 0:
			self.drawed_card=self.deck.pop(0)
			return self.drawed_card

	#Sage Execution Look at 3 cards from the deck, make 1 draw card and shuffle the deck
	def do_predict(self):
		if len(self.deck) > 2:
			options = self.deck[0:3]
			if self.player.mannual_flag ==True:
				print("Activate the effect of the wise man")
				print(options)
				print("Please enter the card you want to select")
				ind = options.index(self.inputnumber())
			else:
				ind = options.index(options[1])
			selected_card = self.deck.pop(ind)
			random.shuffle(self.deck)
			self.drawed_card = selected_card
			return self.drawed_card

	#Match setup
	def match_initialize(self):
		self.sente = self.Player(0)
		self.gote = self.Player(1)

		#Create a deck and a reincarnated card, and draw one for each player.
		self.set_deck()
		self.sente.hand.append(self.draw_top_card())
		self.gote.hand.append(self.draw_top_card())

	#Sequence from draw to turn end
	def play_turn(self,_player):
		if _player == self.sente:
			self.player = self.sente
			self.o_player = self.gote
		else:
			self.player = self.gote
			self.o_player = self.sente

		if self.player.predict_flag == True and len(self.deck) > 2:
			self.drawed_card = self.do_predict()
			self.player.predict_flag = False
		else:
			self.drawed_card = self.draw_top_card()


		#Add draw card to hand
		self.player.hand.append(self.drawed_card)
	
		if self.player.mannual_flag ==True:
			print("drawed card:",self.drawed_card)
			print(self.player.teban, self.player.hand)

		#Put a card from your hand into play
		self.play_card = self.decide_play_card()

		#Delete the card from your hand
		self.player.hand.remove(self.play_card)
		#Add to play
		self.player.field.append(self.play_card)
		# print("player hand:", self.player.hand)

		#Play the effect of the issued card
		if self.o_player.defence_flag != True or self.play_card == 7 or self.play_card == 4:
			self.play_effect()
		#If the opponent is guarding, only flag reset
		else:
			self.o_player.defence_flag = False

		print("player field:", self.player.field)
		print("o_player field:", self.o_player.field)

		#If the deck is gone at the end of the turn, hand battle
		print("deck maisuu:", len(self.deck))
		if len(self.deck) == 0:
			self.battle()

	#final battle
	def battle(self):
		print("battle!!!!!!!!!!!!!")
		print("sente:", self.sente.hand[0])
		print("gote:", self.gote.hand[0])
		if self.sente.hand[0] > self.gote.hand[0]:
			self.winner = self.sente.teban
		elif self.gote.hand[0]>self.sente.hand[0]:
			self.winner = self.gote.teban
		else:
			self.winner = "DRAW"

	def decide_play_card(self):
		if self.player.mannual_flag== True:
			print("Please enter the card number to play")
			return self.inputnumber()
		else:
			if self.player.hand.count(10) != 1:
				# random.shuffle(self.player.hand)
				self.player.hand.sort()
				return self.player.hand[0]
			else:
				self.player.hand.sort()
				return self.player.hand[0]

	def play_effect(self):
		pl = self.play_card
		if pl == 1:
			print("play:", pl)
			self.revolution()
		elif pl ==2:
			print("play:", pl)
			self.detect_number()
		elif pl == 3:
			print("play:", pl)
			self.publicate()
		elif pl ==4:
			print("play:", pl)
			self.skip_effect()
		elif pl ==5:
			print("play:", pl)
			self.hide_draw_and_drop()
		elif pl == 6:
			print("play:", pl)
			self.battle_novice()
		elif pl == 7:
			print("play:", pl)
			self.predict()
		elif pl == 8:
			print("play:", pl)
			self.exchange()
		elif pl == 9:
			print("play:", pl)
			self.public_draw_and_drop()

	def inputnumber(self):
		if self.player.mannual_flag==True:
			s = int(input())
			return s
		else:
			return 1

	#1 boy
	def revolution(self):
		all_field = self.sente.field + self.gote.field
		#Public execution only if 1 is already on the field
		print(all_field)
		if all_field.count(1) > 1:
			self.public_draw_and_drop()

	#2 soldiers
	def detect_number(self):
		detection_number = 3
		if self.player.mannual_flag == True:
			print("Activate the effect of soldiers")
			print("Please enter the opponent's hand prediction")
			detection_number = self.inputnumber()
		else:
			detection_number = 5

		if self.o_player.hand[0] == detection_number:
			self.winner = self.player.teban

        #3 fortune teller
	def publicate(self):
		print(self.o_player.hand)
	
	#4 Guardian
	def skip_effect(self):
		self.player.defence_flag = True

	#5 Shinigami
	def hide_draw_and_drop(self):
		if len(self.deck)>0:
			#Opponent draws 1 card and shuffles hand
			self.draw_card = self.draw_top_card()
			self.o_player.hand.append(self.draw_card)
			random.shuffle(self.o_player.hand)
			if self.player.mannual_flag == True:
				print("Please select the position of the hand to be discarded 0:Left 1:right")
				inp = self.inputnumber()
			else:
				inp = 0

			#Drop the 0th card
			drop_target_card = self.o_player.hand.pop(inp)
			self.o_player.field.append(drop_target_card)

			#Reincarnation process if target is 10
			if drop_target_card == 10:
				self.reincarnate()

	#6 aristocrats
	def battle_novice(self):
		self.battle()

	#7 wise man
	def predict(self):
		self.player.predict_flag = True

	#8 Holy Spirit
	def exchange(self):
		tmp = self.player.hand[0]
		self.player.hand[0] = self.o_player.hand[0]
		self.o_player.hand[0] = tmp

	#9 emperor
	def public_draw_and_drop(self):
		if len(self.deck) > 0:
			self.o_player.hand.append(self.draw_top_card())
			#Hand release
			if self.player.mannual_flag ==True:
				print("Click here for the opponent's hand")
				print(self.o_player.hand)
				#Target selection
				print("select target number:")
				drop_target = self.inputnumber()
				ind = self.o_player.hand.index(drop_target)
			else:
				ind = 0

			drop_target_card = self.o_player.hand.pop(ind)
			self.o_player.field.append(drop_target_card)

			#Emperor's card(9)Was played and the target card is a hero(10)If so, player wins
			if drop_target_card == 10:
				if self.play_card == 9:
					self.winner = self.player.teban
				elif self.play_card == 1:
					self.reincarnate()

	#10 heroes
	def reincarnate(self):
		#Discard your hand
		self.o_player.field.append(self.o_player.hand.pop(0))
		#Draw a reincarnation bill
		self.o_player.hand.append(self.reincarnation_card)


def main():
	winner_list = []
	win_process =[]
	for i in range(1,1000):
		xeno = Xeno()
		xeno.match_initialize()
		xeno.sente.mannual_flag = False
		xeno.gote.mannual_flag = False

		while True:
			print("=======================")
			if xeno.winner == "none":
				xeno.play_turn(xeno.sente)
			else:
				break
			print("=======================")
			if xeno.winner == "none":
				xeno.play_turn(xeno.gote)
			else:
				break

		print("winner :", xeno.winner)
		winner_list.append(xeno.winner)


	print("gote",winner_list.count("gote"))
	print("sente",winner_list.count("sente"))
	print("Draw",winner_list.count("DRAW"))

if __name__ == '__main__':
    main()

By the way, it's Logic Texto, but the result of 10,000 games gote 4543 sente 4967 Draw 489

Since there is a possibility that the combination of 6 and 10 will be the first move, the first move is still more advantageous. After that, the guardian card is actually synonymous with "let the opponent choose a hand and discard it + the opponent's turn skip", so I feel that he is a fairly strong attacker.

Recommended Posts

Implement XENO in python
Implement Enigma in python
Implement recommendations in Python
Implement sum in Python
Implement Traceroute in Python 3
Implement ancient ciphers in python
Implement Redis Mutex in Python
Implement extension field in Python
Implement fast RPC in Python
Implement method chain in Python
Implement Dijkstra's Algorithm in python
Implement Slack chatbot in Python
Implement stacking learning in Python [Kaggle]
Implement R's power.prop.test function in python
Implement the Singleton pattern in Python
Quickly implement REST API in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
I tried to implement PLSA in Python
Implement __eq__ etc. generically in Python class