def testMixedHumanAndAIInput(): def aiFunc(vm): if vm.VM.clock < 1000: vm.input(0) if vm.VM.clock >= 1000 and vm.VM.clock < 3000: vm.input(1 << 4) if vm.VM.clock >= 2000: vm.input(0) # record vm1 = chipgr8.init(ROM='pong', aiInputMask=16, display=True) assert vm1.record, "c.record was expected to be True" for _ in range(1000): vm1.act(0) for _ in range(1000): vm1.act(1 << 1) for _ in range(2000): vm1.act(0) for _ in range(1000): vm1.act(1 << 4) for _ in range(1000): vm1.act(0) print(vm1.inputHistory) # playback vm2 = chipgr8.init(ROM='pong', inputHistory=vm1.inputHistory, display=True) assert not vm2.record vm2.go()
def testHumanRecordAndPlayback(): # record vm1 = chipgr8.init(ROM='pong', display=True, aiInputMask=0) assert vm1.record, "c.record was expected to be True" history = vm1.inputHistory vm1.go() # playback vm2 = chipgr8.init(ROM='pong', inputHistory=history, display=True) assert vm2.inputHistory == history, "d.inputHistory was not as expected" assert vm2.record is False, "d.record was expected to be False" vm2.go()
def testPlayback(): history = [ (0, 0), (2, 1314), (0, 1394), (2, 1502), (0, 1541), (16, 1713), (0, 1843), (16, 1974), (0, 2114), (16, 2224), (0, 2333), (2, 2502), (0, 2596), (2, 2862), (0, 2930), (16, 4701), (0, 4811), (2, 4918), (0, 5051), ] vm = chipgr8.init(ROM='pong', inputHistory=history, display=True) assert vm.inputHistory == history, "c.inputHistory was not as expected" assert vm.record is False, "c.record was expected to be False" vm.go()
def testAIRecordAndPlayback(): vm1 = chipgr8.init(ROM='pong') assert vm1.record, "c.record was expected to be True" for _ in range(1000): vm1.act(0) for _ in range(1000): vm1.act(1 << 1) for _ in range(2000): vm1.act(0) for _ in range(1000): vm1.act(1 << 4) for _ in range(1000): vm1.act(0) print(vm1.inputHistory) # playback vm2 = chipgr8.init(ROM='pong', inputHistory=vm1.inputHistory, display=True) assert vm2.record is False, "d.record was expected to be False" vm2.go()
def pongAgent(): global vm vm = chipgr8.init(display=True, ROM=pong.ROM, sampleRate=rate, speed=100) vm.VM.seed = random.randint(0, 255) # wait for the game to load waitToLoad(1000) # Perform AI logic while not vm.done(): vm.doneIf(pong.observe(vm).done) # Get the position of the paddle and the ball setPositions() # Try to stay aligned with the ball if ballAboveMe(): moveUp() elif ballBelowMe(): moveDown() else: stayStill()
def breakoutAgent(): global vm vm = chipgr8.init(display=True, ROM=Breakout.ROM, sampleRate=rate) vm.VM.seed = random.randint(0, 255) # wait for the game to load waitToLoad(1000) # Perform AI logic while not vm.done(): vm.doneIf(Breakout.observe(vm).done) # Get the position of the paddle and the ball setPositions() # Try to stay aligned with the ball if ballLeftOfMe(): moveLeft() elif ballRightOfMe(): moveRight() else: stayStill()
def caveAgent(): global vm, position vm = chipgr8.init(display=True, ROM=Cave.ROM, sampleRate=1) # wait for the title screen to load waitToLoad(1000) # start the game vm.act(Cave.actions.start) # wait for the first playable scene to load waitToLoad(1000) # Reach the first wall by going down and turn left to get a wall on our right side while not vm.done(): obs = Cave.observe(vm) position = getPosition(obs) if canMoveForward(): moveForward() else: turnLeft() break # We are against a wall: begin wall hugging logic while not vm.done(): obs = Cave.observe(vm) position = getPosition(obs) vm.doneIf(obs.done) # If we have a wall on our right we want to move forward if possible if rightSideIsWall(obs): if canMoveForward(): moveForward() else: # We have a wall on our right, but we cant go foward, so we must go left turnLeft() else: # We have lost our wall, turning right and going 1 step forward should find it again turnRight() moveForward()
import chipgr8 import chipgr8.games as games import random import timeit # Choose a gamee with a `score` and `done` observation game = games.Squash # Initialize 100 instances of the game vms = chipgr8.init(instances=100, ROM=game.ROM, sampleRate=12) def action(vm): vm.act(random.choice(game.actions)) # Choose a random action vm.doneIf(game.observe(vm).done) # Mark done if done # Run all the vms in parrallel vms.inParallel(action) # Select the best by score best = vms.maxBy(lambda vm: game.observe(vm).score) # Print the inputHistory so it can be saved print(best.inputHistory) # Show the game chipgr8.init( display=True, ROM=game.ROM, inputHistory=best.inputHistory, ).go()
level=logLevels[min(len(logLevels) - 1, args.verbose)] ) if args.source: result = chipgr8.assemble(inPath=args.source, outPath=args.out) if not args.out: print(result) elif args.binary: result = chipgr8.disassemble( inPath = chipgr8.findROM(args.binary), outPath = args.out, labels = None if args.noLabels else {}, ) if not args.out: print(result) else: foreground, background = chipgr8.themes.get( args.theme, chipgr8.themes['dark'] ) chipgr8.init( smooth = args.smooth, ROM = chipgr8.findROM(args.rom) if args.rom else None, startPaused = not args.rom, display = True, aiInputMask = 0, foreground = ('#' + args.foreground) if args.foreground else foreground, background = ('#' + args.background) if args.background else background, autoScroll = args.noScroll, ).go()
import chipgr8 import chipgr8.games.pong as Pong import random import numpy as np from agent import agent pong = Pong.pong # Creates 100 instances of pong vm = chipgr8.init(display=True, ROM=pong.ROM, instances=1) # Plays 100 games of pong until all games are finished while not vm.done: # Retrieves relevant data from memory observations = pong.observer(vm) #.observe(vm) # Selects a valid key for pong #action = random.choice(pong.actions) #print("{},{},{},{}".format(observations.score, observations.opponent, action)) X = np.array([observations.score, observations.opponent]).reshape(1, -1) #Y.append([ 1 if action == 16 or 2 else 0, 1 if action == 16 or 12 else 0]) #X.append([action]) pred = agent.predict(X) action = 2 if pred[0][0] == 1 else 16 #print((pred[0][0], pred[0][1])) # Performs the selected key vm.act(action) # Determines if the game of pong has ended vm.doneIf(observations.done)
def testHumanRecord(): vm = chipgr8.init(ROM='pong', inputHistory=None, display=True) assert vm.record, "c.record was expected to be True" vm.go() print(vm.inputHistory)
def testSingleInputHistory(): history = [(0, 0)] vm = chipgr8.init(ROM='pong', inputHistory=history, display=True) assert vm.record is False, "c.record was expected to be False" print(vm.inputHistory) vm.go()