Commit 27fc381f by Blažek, Matěj 💬

### Prvni commit

parents
RandoBot.py 0 → 100644
 import mastermind from random import * class RandoBot: def __init__(self,codelength,numberOfColors): self.codelength = 0 self.turnsPlayed = list() self.rewards = list() self.numberOfColors = numberOfColors self.unplayed = list(range(numberOfColors)) def resetBot(self): self.turnsPlayed.clear() self.rewards.clear() def playMove(self): return choice(self.unplayed), choice(self.unplayed), choice(self.unplayed), choice(self.unplayed) if __name__ == "__main__": mm = mastermind.Mastermind() codelen = mm.getCodelength() clrs = mm.getNumberOfColors() bot = RandoBot(codelen,clrs) cost = 0 playedGames = 0 while playedGames < 10000: for x in range(10): guesses = bot.playMove() white_pins,red_pins = mm.play_turn(guesses) if red_pins == 4: break cost += (x+1) playedGames +=1 if playedGames % 100 == 0: print("Simulated",playedGames,"games") averageCost = cost / playedGames print("Average cost per game is:",averageCost)
mastermind.py 0 → 100644
 from random import * class Mastermind: def __init__(self): self.colors = ["Red","Green","Blue","Yellow","Brown","Orange","Black","White"] self.turns = 10 self.thisturn = 1 self.codelength = 4 self.code = list() self.generateNewCode() def generateNewCode(self): self.code.clear() for x in range(self.codelength): self.code.append(randint(0,len(self.colors)-1)) #print(self.code) def play_turn(self,played): #print("Turn:",self.thisturn) correct_color = 0 correct_placement = 0 codecpy = list(self.code) if len(played) != self.codelength: #print("Wrong length! Your list has to be",self.codelength,"long") return [0,0] else: for x in range(self.codelength): if self.is_inside(played[x]): if played[x] == codecpy[x]: correct_placement += 1 codecpy[x] = -1 else: return [0,0] for x in range(self.codelength): if played[x] == codecpy[x]: continue if played[x] in codecpy: codecpy[codecpy.index(played[x])] = -1 correct_color += 1 if (correct_placement) == self.codelength: #print("You won in",self.thisturn,"turns, generating another code") self.thisturn = 1 self.generateNewCode() return [correct_color, correct_placement] else: if self.turns == self.thisturn: #print("You could not win in",self.thisturn,"turns, generating another code") #print("Correct code was:",self.code) self.thisturn = 0 self.generateNewCode() self.thisturn +=1 return [correct_color, correct_placement] def is_inside(self,number): if isinstance(number,int) and number < len(self.colors) and number >= 0: return True print("Not enough ball colours or not int") return False def getCodelength(self): return self.codelength def getNumberOfColors(self): return len(self.colors) if __name__ == "__main__": mastermind = Mastermind() while True: guesses = list(map(int,input().split())) white_pins,red_pins = mastermind.play_turn(guesses) print(white_pins,red_pins)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment