From 2e41fac4ddf7a91be108cccd3525788acafd0bc8 Mon Sep 17 00:00:00 2001 From: mrsteveson Date: Mon, 18 Nov 2019 17:07:37 -0600 Subject: [PATCH 01/32] initial setup --- Pipfile | 11 ++ Pipfile.lock | 20 +++ cpu.py | 350 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 9 ++ miner.py | 34 +++++ 5 files changed, 424 insertions(+) create mode 100644 Pipfile create mode 100644 Pipfile.lock create mode 100644 cpu.py create mode 100644 main.py create mode 100644 miner.py diff --git a/Pipfile b/Pipfile new file mode 100644 index 00000000..b723d019 --- /dev/null +++ b/Pipfile @@ -0,0 +1,11 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] + +[requires] +python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 00000000..9a51a282 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,20 @@ +{ + "_meta": { + "hash": { + "sha256": "7e7ef69da7248742e869378f8421880cf8f0017f96d94d086813baa518a65489" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.7" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": {}, + "develop": {} +} diff --git a/cpu.py b/cpu.py new file mode 100644 index 00000000..9a48b989 --- /dev/null +++ b/cpu.py @@ -0,0 +1,350 @@ +"""CPU functionality.""" +​ +import sys +from datetime import datetime # for timer interrupt +​ +# Opcodes: +​ +ADD = 0b10100000 +AND = 0b10101000 +CALL = 0b01010000 +CMP = 0b10100111 +DEC = 0b01100110 +DIV = 0b10100011 +HLT = 0b00000001 +INC = 0b01100101 +IRET = 0b00010011 +JEQ = 0b01010101 +JLE = 0b01011001 +JLT = 0b01011000 +JMP = 0b01010100 +JNE = 0b01010110 +LD = 0b10000011 +LDI = 0b10000010 +MUL = 0b10100010 +OR = 0b10101010 +POP = 0b01000110 +PRA = 0b01001000 +PRN = 0b01000111 +PUSH = 0b01000101 +RET = 0b00010001 +SHL = 0b10101100 +ST = 0b10000100 +SUB = 0b10100001 +XOR = 0b10101011 +# Reserved general-purpose register numbers: +​ +IM = 5 +IS = 6 +SP = 7 +​ +# CMP flags: +​ +FL_LT = 0b100 +FL_GT = 0b010 +FL_EQ = 0b001 +​ +# IS flags +​ +IS_TIMER = 0b00000001 +IS_KEYBOARD = 0b00000010 +​ +class CPU: + """Main CPU class.""" +​ + def __init__(self): + """Construct a new CPU.""" +​ + self.pc = 0 # program counter + self.fl = 0 # flags + self.ie = 1 # interrupts enabled +​ + self.halted = False +​ + self.last_timer_int = None +​ + self.inst_set_pc = False # True if this instruction set the PC +​ + self.ram = [0] * 256 +​ + self.reg = [0] * 8 + self.reg[SP] = 0xf4 +​ + self.bt = { # branch table + ADD: self.op_add, + AND: self.op_and, + CALL: self.op_call, + CMP: self.op_cmp, + DEC: self.op_dec, + DIV: self.op_div, + HLT: self.op_hlt, + INC: self.op_inc, + IRET: self.op_iret, + JEQ: self.op_jeq, + JLE: self.op_jle, + JLT: self.op_jlt, + JMP: self.op_jmp, + JNE: self.op_jne, + LD: self.op_ld, + LDI: self.op_ldi, + MUL: self.op_mul, + OR: self.op_or, + POP: self.op_pop, + PRA: self.op_pra, + PRN: self.op_prn, + PUSH: self.op_push, + RET: self.op_ret, + SHL: self.op_shl, + ST: self.op_st, + SUB: self.op_sub, + XOR: self.op_xor, + } +​ + def load(self, filename): + """Load a file from disk into memory.""" +​ + address = 0 + with open(filename) as fp: + for line in fp: + comment_split = line.split("#") + num = comment_split[0].strip() + if num == '': # ignore blanks + continue + val = int(num, 2) +​ + self.ram[address] = val + address += 1 +​ + def ram_write(self, mdr, mar): + self.ram[mar] = mdr +​ + def ram_read(self, mar): + return self.ram[mar] +​ + def push_val(self, val): + self.reg[SP] -= 1 + self.ram_write(val, self.reg[7]) +​ + def pop_val(self): + val = self.ram_read(self.reg[7]) + self.reg[SP] += 1 +​ + return val +​ + def alu(self, op, reg_a, reg_b): +​ + if op == "ADD": + self.reg[reg_a] += self.reg[reg_b] + elif op == "AND": + self.reg[reg_a] &= self.reg[reg_b] + elif op == "SUB": + self.reg[reg_a] -= self.reg[reg_b] + elif op == "MUL": + self.reg[reg_a] *= self.reg[reg_b] + elif op == "DIV": + self.reg[reg_a] /= self.reg[reg_b] + elif op == "DEC": + self.reg[reg_a] -= 1 + elif op == "INC": + self.reg[reg_a] += 1 + elif op == "CMP": + self.fl &= 0x11111000 # clear all CMP flags + if self.reg[reg_a] < self.reg[reg_b]: + self.fl |= FL_LT + elif self.reg[reg_a] > self.reg[reg_b]: + self.fl |= FL_GT + else: + self.fl |= FL_EQ + elif op == "OR": + self.reg[reg_a] |= self.reg[reg_b] + elif op == "SHL": + self.reg[reg_a] <<= self.reg[reg_b] + elif op == "XOR": + self.reg[reg_a] ^= self.reg[reg_b] + else: + raise Exception("Unsupported ALU operation") +​ + def check_for_timer_int(self): + """Check the time to see if a timer interrupt should fire.""" + if self.last_timer_int == None: + self.last_timer_int = datetime.now() +​ + now = datetime.now() +​ + diff = now - self.last_timer_int +​ + if diff.seconds >= 1: # OK, fire! + self.last_timer_int = now + self.reg[IS] |= IS_TIMER +​ + def handle_ints(self): + if not self.ie: # see if interrupts enabled + return +​ + # Mask out interrupts + masked_ints = self.reg[IM] & self.reg[IS] +​ + for i in range(8): + # See if the interrupt triggered + if masked_ints & (1 << i): + self.ie = 0 # disable interrupts + self.reg[IS] &= ~(1 << i) # clear bit for this interrupt +​ + # Save all the work on the stack + self.push_val(self.pc) + self.push_val(self.fl) + for r in range(7): + self.push_val(self.reg[r]) +​ + # Look up the address vector and jump to it + self.pc = self.ram_read(0xf8 + i) +​ + break # no more processing +​ + def trace(self): + print(f"TRACE: %02X | %02X | %d | %02X %02X %02X |" % ( + self.pc, + self.fl, + self.ie, + self.ram_read(self.pc), + self.ram_read(self.pc + 1), + self.ram_read(self.pc + 2) + ), end='') +​ + for i in range(8): + print(" %02X" % self.reg[i], end='') +​ + print() +​ + def run(self): + """Run the CPU.""" +​ + while not self.halted: + # Interrupt code +​ + self.check_for_timer_int() # timer interrupt check + #self.check_for_keyboard_int() # keyboard interrupt check + self.handle_ints() # see if any interrupts occurred +​ + # Normal instruction processing +​ + #self.trace() +​ + ir = self.ram[self.pc] + operand_a = self.ram_read(self.pc + 1) + operand_b = self.ram_read(self.pc + 2) +​ + inst_size = ((ir >> 6) & 0b11) + 1 + self.inst_set_pc = ((ir >> 4) & 0b1) == 1 +​ + if ir in self.bt: + self.bt[ir](operand_a, operand_b) + else: + raise Exception(f"Invalid instruction {hex(ir)} at address {hex(self.pc)}") +​ + # If the instruction didn't set the PC, just move to the next instruction + if not self.inst_set_pc: + self.pc += inst_size +​ +​ + def op_ldi(self, operand_a, operand_b): + self.reg[operand_a] = operand_b +​ + def op_prn(self, operand_a, operand_b): + print(self.reg[operand_a]) +​ + def op_pra(self, operand_a, operand_b): + print(chr(self.reg[operand_a]), end='') + sys.stdout.flush() +​ + def op_add(self, operand_a, operand_b): + self.alu("ADD", operand_a, operand_b) +​ + def op_and(self, operand_a, operand_b): + self.alu("AND", operand_a, operand_b) +​ + def op_sub(self, operand_a, operand_b): + self.alu("SUB", operand_a, operand_b) +​ + def op_mul(self, operand_a, operand_b): + self.alu("MUL", operand_a, operand_b) +​ + def op_div(self, operand_a, operand_b): + self.alu("DIV", operand_a, operand_b) +​ + def op_dec(self, operand_a, operand_b): + self.alu("DEC", operand_a, None) +​ + def op_inc(self, operand_a, operand_b): + self.alu("INC", operand_a, None) +​ + def op_or(self, operand_a, operand_b): + self.alu("OR", operand_a, operand_b) +​ + def op_xor(self, operand_a, operand_b): + self.alu("XOR", operand_a, operand_b) +​ + def op_pop(self, operand_a, operand_b): + self.reg[operand_a] = self.pop_val() +​ + def op_push(self, operand_a, operand_b): + self.push_val(self.reg[operand_a]) +​ + def op_call(self, operand_a, operand_b): + self.push_val(self.pc + 2) + self.pc = self.reg[operand_a] +​ + def op_ret(self, operand_a, operand_b): + self.pc = self.pop_val() +​ + def op_ld(self, operand_a, operand_b): + self.reg[operand_a] = self.ram_read(self.reg[operand_b]) +​ + def op_shl(self, operand_a, operand_b): + self.alu("SHL", operand_a, operand_b) +​ + def op_st(self, operand_a, operand_b): + self.ram_write(self.reg[operand_b], self.reg[operand_a]) +​ + def op_jmp(self, operand_a, operand_b): + self.pc = self.reg[operand_a] +​ + def op_jeq(self, operand_a, operand_b): + if self.fl & FL_EQ: + self.pc = self.reg[operand_a] + else: + self.inst_set_pc = False +​ + def op_jle(self, operand_a, operand_b): + if self.fl & FL_LT or self.fl & FL_EQ: + self.pc = self.reg[operand_a] + else: + self.inst_set_pc = False +​ + def op_jlt(self, operand_a, operand_b): + if self.fl & FL_LT: + self.pc = self.reg[operand_a] + else: + self.inst_set_pc = False +​ + def op_jne(self, operand_a, operand_b): + if not self.fl & FL_EQ: + self.pc = self.reg[operand_a] + else: + self.inst_set_pc = False +​ + def op_cmp(self, operand_a, operand_b): + self.alu("CMP", operand_a, operand_b) +​ + def op_iret(self, operand_a, operand_b): + # restore work from stack + for i in range(6, -1, -1): + self.reg[i] = self.pop_val() + self.fl = self.pop_val() + self.pc = self.pop_val() +​ + # enable interrupts + self.ie = 1 +​ + def op_hlt(self, operand_a, operand_b): + self.halted = True \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 00000000..2510d2d7 --- /dev/null +++ b/main.py @@ -0,0 +1,9 @@ +import sys +from cpu import * + +print("This is the Treasure Map") +api_key = input("Please enter your provided API Key: ") + +# cpu = CPU() +# cpu.load() +# cpu.run() \ No newline at end of file diff --git a/miner.py b/miner.py new file mode 100644 index 00000000..ff52b3ba --- /dev/null +++ b/miner.py @@ -0,0 +1,34 @@ +import hashlib +import requests + +import sys +from timeit import default_timer as timer +import random + + +def proof_of_work(last_proof, difficulty): + start = timer() + + print("Searching for next proof") + proof = 0 + total_tries = 0 + prev_proof = f'{last_proof}'.encode() + last_hash = hashlib.sha256(prev_proof).hexdigest() + while valid_proof(last_hash, proof, difficulty) is False: + proof = random.randint(0, 10000) + total_tries += 1 + + print("Proof found: " + str(proof) + " in " + str(timer() - start)) + return proof + + +def valid_proof(last_hash, proof, difficulty): + guess = f'{proof}'.encode() + guess_hash = hashlib.sha256(guess).hexdigest() + + if difficulty is not None: + leading_zeros = "0" * difficulty + else: + leading_zeros = "0" * 6 + + return guess_hash[0:difficulty] == leading_zeros \ No newline at end of file From 5f6f1246df4a2c6a6a5b6a7cdcb7ce40bf0503f3 Mon Sep 17 00:00:00 2001 From: danielhorsley Date: Tue, 19 Nov 2019 13:20:47 +0000 Subject: [PATCH 02/32] basic utils and new exploring utils --- .gitignore | 110 +++++++++++++++++++++++++++ basic_utils.py | 169 +++++++++++++++++++++++++++++++++++++++++ utils.py | 200 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 479 insertions(+) create mode 100644 .gitignore create mode 100644 basic_utils.py create mode 100644 utils.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..dc1502ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,110 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +#csv files +*.csv + +#png files +# *.png + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ diff --git a/basic_utils.py b/basic_utils.py new file mode 100644 index 00000000..277c3b5c --- /dev/null +++ b/basic_utils.py @@ -0,0 +1,169 @@ +class Queue(): + def __init__(self): + self.queue = [] + def enqueue(self, value): + self.queue.append(value) + def dequeue(self): + if self.size() > 0: + return self.queue.pop(0) + else: + return None + def size(self): + return len(self.queue) + +class Stack(): + def __init__(self): + self.stack = [] + def push(self, value): + self.stack.append(value) + def pop(self): + if self.size() > 0: + return self.stack.pop() + else: + return None + def size(self): + return len(self.stack) + +class Graph: + """Represent a graph as a dictionary of vertices mapping labels to edges.""" + def __init__(self): + self.vertices = {} + def add_vertex(self, vertex): + """ + Add a vertex to the graph. + """ + if vertex not in self.vertices.keys(): + self.vertices[vertex] = set() + else: + pass + def add_edge(self, v1, v2): + """ + Add a directed edge to the graph. + """ + if v1 and v2 in self.vertices.keys(): + #self.vertices[v2].add(v1) + self.vertices[v1].add(v2) + + def bft(self, starting_vertex): + """ + Print each vertex in breadth-first order + beginning from starting_vertex. + """ + ret_list = [] + if starting_vertex is None: + return None + my_q = Queue() + visited = [starting_vertex] + my_q.enqueue(starting_vertex) + while len(my_q.queue) > 0: + point = my_q.queue[0] + joins = self.vertices[point] + for j in joins: + if j not in visited: + my_q.enqueue(j) + visited.append(j) + #print(my_q.dequeue()) + ret = my_q.dequeue() + ret_list.append(ret) + return ret_list + + + + def dft(self, starting_vertex, chooser=None): + """ + Print each vertex in depth-first order + beginning from starting_vertex. + """ + ret_list = [] + if starting_vertex is None: + return None + my_s = Stack() + visited = [starting_vertex] + my_s.push(starting_vertex) + while len(my_s.stack) > 0: + point = my_s.stack[-1] + joins = self.vertices[point] + r = my_s.pop() ##new code + ret_list.append(r) ##new code + #print(r) ##changed to r from pop + if chooser is None: + pass + elif chooser == 'random': + joins = random.sample(joins,len(joins)) + elif chooser == 'shortest': + joins = find_longest_clique(point,self,visited) + for j in joins: + if j not in visited: + my_s.push(j) + visited.append(j) + return ret_list + + + def dft_recursive(self, starting_vertex, visited = []): + """ + Print each vertex in depth-first order + beginning from starting_vertex. + This should be done using recursion. + """ + print(starting_vertex) + visited.append(starting_vertex) + joins = self.vertices[starting_vertex] + if joins is None: + return None + for j in joins: + if j in visited: + pass + else: + self.dft_recursive(j,visited) + + + + def bfs(self, starting_vertex, destination_vertex): + """ + Return a list containing the shortest path from + starting_vertex to destination_vertex in + breath-first order. + """ + q = Queue() + q.enqueue([starting_vertex]) + + while destination_vertex not in q.queue[0]: + current_point = q.queue[0][-1] + joins = self.vertices[current_point] + for j in joins: + _ = [x for x in q.queue[0]] + _.append(j) + q.enqueue(_) + q.dequeue() + + return q.queue[0] + + + + + def dfs(self, starting_vertex, destination_vertex): + """ + Return a list containing a path from + starting_vertex to destination_vertex in + depth-first order. + """ + s = Stack() + s.push([starting_vertex]) + + while destination_vertex not in s.stack[-1]: + current_point = s.stack[-1][-1] + + joins = self.vertices[current_point] + if joins is None: + s.pop() + else: + temp_list = [] + for j in joins: + _ = [x for x in s.stack[-1]] + _.append(j) + temp_list.append(_) + for tl in temp_list: + s.push(tl) + #s.pop() + + return s.stack[-1] diff --git a/utils.py b/utils.py new file mode 100644 index 00000000..f5fac373 --- /dev/null +++ b/utils.py @@ -0,0 +1,200 @@ +from decouple import config +from basic_utils import * +import requests +import json +import time +import random + +auth_key = config('AUTH_KEY') +my_url = config('LAMBDA_URL') + +class Player: + def __init__(self, name, startingRoom): + self.name = name + self.currentRoom = startingRoom + +class mapper: + def __init__(self,auth ='827d98231059f187c4203da53476090d1c83a2b9'): + self.auth = auth #the auth token + self.header = {'Authorization':f'Token {self.auth}'} #the header for post and get + self.wait = 18 # the current sleep length + self.info = {} #the last status json from post or get + self.accumulate = False #whether player picks up items or not - it ise very easy to get overencumbered + + def get_info(self,what='init',direction=None,backtrack=None): + """multi purpose move & init function""" + #info = !curl -X GET -H 'Authorization: Token 827d98231059f187c4203da53476090d1c83a2b9' https://lambda-treasure-hunt.herokuapp.com/api/adv/init/ + if what=='init': + response = requests.get(f'{my_url}{what}/',headers=self.header) + + elif what=='move': + response = requests.post(f'{my_url}move/',headers=self.header,json = {"direction":direction}) + + elif what=='backtrack': + response = requests.post(f'{my_url}move/',headers=self.header,json = {"direction":direction,"next_room_id": backtrack}) + + if response.status_code==200: + self.info = json.loads(response.content) + if self.info['terrain'] == 'TRAP': + time.sleep(30) + self.room_check() + return self.info + else: + print('cooldown triggered - waiting 20 seconds') + time.sleep(20) + self.get_info(what=what,direction=direction,backtrack=backtrack) + + def action(self,what='take',treasure=None): + """another multi purpose request function""" + + if what in ['take','drop','sell','examine']: + response = requests.post(f'{my_url}{what}/',headers=self.header,json = {"name":treasure}) + + if what in ['status','pray']: + response = requests.post(f'{my_url}{what}/',headers=self.header) + + if what == 'confirm_sell': + response = requests.post(f'{my_url}{what}/',headers=self.header,json = {"name":treasure, "confirm" : "yes"}) + + if response.status_code==200: + self.info = json.loads(response.content) + return self.info + else: + print('error',what,treasure,response.status_code) + + def room_check(self): + print('room check triggered. info: ',self.info) + if self.info['items']!=[] and self.accumulate: + for item in self.info['items']: + time.sleep(self.wait) + self.info = self.action('take',item) + print(self.info) + time.sleep(self.info['cooldown']) + + if self.info['title'] == 'Shrine': + self.info = self.action('pray') + + def create_starting_map(self): + """"initiates your starting map which is stored under the vertices of a graph class""" + info_dict = self.get_info() + print(info_dict) #this can be deactivated - just helpful at first + self.my_map = Graph() + self.player = Player("scooby_doo",info_dict['room_id']) + exits = info_dict['exits'] + exit_dict = {} + for e in exits: + exit_dict[e] = '?' + self.my_map.vertices[self.player.currentRoom] = exit_dict + return self.my_map,self.player + + def pop_map_on_move(self,move): + """fills in the map while moving in the direction specified""" + reverse_dir ={'n':'s','s':'n','w':'e','e':'w'} + old_room = self.player.currentRoom + info = self.get_info('move',move) + self.player.currentRoom = info['room_id'] + print(info) #another debugger + new_room = info['room_id'] + if new_room not in self.my_map.vertices: + exit_dict = {} + for exits in info['exits']: + for e in exits: + exit_dict[e] = '?' + self.my_map.vertices[new_room] = exit_dict + self.my_map.vertices[old_room][move] = new_room + reverse_move = reverse_dir[move] + self.my_map.vertices[new_room][reverse_move] = old_room + + def count_unmapped(self): + """counts all the unmapped rooms""" + counter = 0 + for val1 in self.my_map.vertices.values(): + for val2 in val1.values(): + if val2=='?': + counter += 1 + return counter + + def get_dirs(self,traversal): + """gets the direction of travel given a room traversal list""" + point = traversal[0] + dir_list = [] + for t in traversal[1:]: + for key in self.my_map.vertices[point]: + if self.my_map.vertices[point][key]==t: + dir_list.append(key) + point = t + return dir_list + + def bfs_for_q(self): + """breadth first search for last ?""" + room = self.player.currentRoom + q = Queue() + q.enqueue([room]) + + while '?' not in self.my_map.vertices[room].values(): + + joins = self.my_map.vertices[room] + for j in joins.values(): + if j in q.queue[0]: + pass + else: + _ = [x for x in q.queue[0]] + _.append(j) + q.enqueue(_) + q.dequeue() + room = q.queue[0][-1] + + return q.queue[0] + + def explore_random(self,counter=5): + """explores the map choosing random ? and backtracks using bfs + counter is the number of times you want it to explore unkown rooms""" + unmapped_number = self.count_unmapped() + moves = [] + c=0 + while unmapped_number > 0 and c <= counter: + print(self.my_map.vertices) + + room = self.player.currentRoom + unvisited_exits = [x for x in self.my_map.vertices[room] if self.my_map.vertices[room][x]=='?'] + if unvisited_exits !=[]: + print('exit checker',unvisited_exits) + move = random.choice(unvisited_exits) + moves.append(move) + self.pop_map_on_move(move) + unmapped_number = self.count_unmapped() + time.sleep(self.wait) + else: + print('back track on') + backtrack = self.bfs_for_q() + #print('backtrack is', backtrack) + backtrack_dirs = self.get_dirs(backtrack) + print('backtrack details',backtrack,backtrack_dirs) + for i in range(len(backtrack_dirs)): + b_info = self.get_info('backtrack',backtrack_dirs[i],str(backtrack[i+1])) + self.player.currentRoom = b_info['room_id'] + time.sleep(self.wait) + c+=1 + + def go_to_room(self,destination): + """depth first traversal to particular room in shortest route""" + s = Stack() + s.push([self.player.currentRoom]) + + while destination not in s.stack[-1]: + current_point = s.stack[-1][-1] + + joins = self.my_map.vertices[current_point] + if joins is None: + s.pop() + else: + temp_list = [] + for j in joins: + _ = [x for x in s.stack[-1]] + _.append(j) + temp_list.append(_) + for tl in temp_list: + s.push(tl) + + return s.stack[-1] + From d781ff44b218ba0ac5b34f5a930f45748c9ec132 Mon Sep 17 00:00:00 2001 From: danielhorsley Date: Tue, 19 Nov 2019 13:25:16 +0000 Subject: [PATCH 03/32] some readme isntructions in explorer guide --- explorer_guide.md | 20 ++++++++++++++++++++ utils.py | 6 ++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 explorer_guide.md diff --git a/explorer_guide.md b/explorer_guide.md new file mode 100644 index 00000000..3751aa93 --- /dev/null +++ b/explorer_guide.md @@ -0,0 +1,20 @@ +# Explorer guide + + +## to initialize + +```mappy = mapper() +mappy.create_starting_map()``` + +## randomly explore + +```mappy.explore_random(50)``` + +## take action in current room given lastest info from server + +```mappy.room_check()``` + +## examples of workign commands +```mappy.action('take','tiny treasure')``` +```mappy.action('status')``` +```mappy.get_info('backtrack','e','254')``` \ No newline at end of file diff --git a/utils.py b/utils.py index f5fac373..ef85c888 100644 --- a/utils.py +++ b/utils.py @@ -22,7 +22,8 @@ def __init__(self,auth ='827d98231059f187c4203da53476090d1c83a2b9'): self.accumulate = False #whether player picks up items or not - it ise very easy to get overencumbered def get_info(self,what='init',direction=None,backtrack=None): - """multi purpose move & init function""" + """multi purpose move & init function - this is used + for the most common actions""" #info = !curl -X GET -H 'Authorization: Token 827d98231059f187c4203da53476090d1c83a2b9' https://lambda-treasure-hunt.herokuapp.com/api/adv/init/ if what=='init': response = requests.get(f'{my_url}{what}/',headers=self.header) @@ -45,7 +46,8 @@ def get_info(self,what='init',direction=None,backtrack=None): self.get_info(what=what,direction=direction,backtrack=backtrack) def action(self,what='take',treasure=None): - """another multi purpose request function""" + """another multi purpose request function + this one focuses on less common actions""" if what in ['take','drop','sell','examine']: response = requests.post(f'{my_url}{what}/',headers=self.header,json = {"name":treasure}) From 9f411762f7b9e01a9b7d9560e8da8ca224056044 Mon Sep 17 00:00:00 2001 From: danielhorsley Date: Tue, 19 Nov 2019 13:28:39 +0000 Subject: [PATCH 04/32] formatting improvement --- explorer_guide.md | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/explorer_guide.md b/explorer_guide.md index 3751aa93..5845de3f 100644 --- a/explorer_guide.md +++ b/explorer_guide.md @@ -3,18 +3,26 @@ ## to initialize -```mappy = mapper() -mappy.create_starting_map()``` +```python +mappy = mapper() +mappy.create_starting_map() +``` ## randomly explore -```mappy.explore_random(50)``` +```python +mappy.explore_random(50) +``` ## take action in current room given lastest info from server -```mappy.room_check()``` +```python +mappy.room_check() +``` ## examples of workign commands -```mappy.action('take','tiny treasure')``` -```mappy.action('status')``` -```mappy.get_info('backtrack','e','254')``` \ No newline at end of file +```python +mappy.action('take','tiny treasure') #pick up some treasure +mappy.action('status') # get status update +mappy.get_info('backtrack','e','254') # back track specifying room and direction +``` \ No newline at end of file From 92c48cabc9f166be4250ec3825585cd53abc2d5e Mon Sep 17 00:00:00 2001 From: danielhorsley Date: Tue, 19 Nov 2019 14:11:58 +0000 Subject: [PATCH 05/32] improved logic on cooldown --- cpu.py | 350 ---------------------------------------------- explore.py | 9 ++ explorer_guide.md | 4 +- utils.py | 30 ++-- 4 files changed, 28 insertions(+), 365 deletions(-) delete mode 100644 cpu.py create mode 100644 explore.py diff --git a/cpu.py b/cpu.py deleted file mode 100644 index 9a48b989..00000000 --- a/cpu.py +++ /dev/null @@ -1,350 +0,0 @@ -"""CPU functionality.""" -​ -import sys -from datetime import datetime # for timer interrupt -​ -# Opcodes: -​ -ADD = 0b10100000 -AND = 0b10101000 -CALL = 0b01010000 -CMP = 0b10100111 -DEC = 0b01100110 -DIV = 0b10100011 -HLT = 0b00000001 -INC = 0b01100101 -IRET = 0b00010011 -JEQ = 0b01010101 -JLE = 0b01011001 -JLT = 0b01011000 -JMP = 0b01010100 -JNE = 0b01010110 -LD = 0b10000011 -LDI = 0b10000010 -MUL = 0b10100010 -OR = 0b10101010 -POP = 0b01000110 -PRA = 0b01001000 -PRN = 0b01000111 -PUSH = 0b01000101 -RET = 0b00010001 -SHL = 0b10101100 -ST = 0b10000100 -SUB = 0b10100001 -XOR = 0b10101011 -# Reserved general-purpose register numbers: -​ -IM = 5 -IS = 6 -SP = 7 -​ -# CMP flags: -​ -FL_LT = 0b100 -FL_GT = 0b010 -FL_EQ = 0b001 -​ -# IS flags -​ -IS_TIMER = 0b00000001 -IS_KEYBOARD = 0b00000010 -​ -class CPU: - """Main CPU class.""" -​ - def __init__(self): - """Construct a new CPU.""" -​ - self.pc = 0 # program counter - self.fl = 0 # flags - self.ie = 1 # interrupts enabled -​ - self.halted = False -​ - self.last_timer_int = None -​ - self.inst_set_pc = False # True if this instruction set the PC -​ - self.ram = [0] * 256 -​ - self.reg = [0] * 8 - self.reg[SP] = 0xf4 -​ - self.bt = { # branch table - ADD: self.op_add, - AND: self.op_and, - CALL: self.op_call, - CMP: self.op_cmp, - DEC: self.op_dec, - DIV: self.op_div, - HLT: self.op_hlt, - INC: self.op_inc, - IRET: self.op_iret, - JEQ: self.op_jeq, - JLE: self.op_jle, - JLT: self.op_jlt, - JMP: self.op_jmp, - JNE: self.op_jne, - LD: self.op_ld, - LDI: self.op_ldi, - MUL: self.op_mul, - OR: self.op_or, - POP: self.op_pop, - PRA: self.op_pra, - PRN: self.op_prn, - PUSH: self.op_push, - RET: self.op_ret, - SHL: self.op_shl, - ST: self.op_st, - SUB: self.op_sub, - XOR: self.op_xor, - } -​ - def load(self, filename): - """Load a file from disk into memory.""" -​ - address = 0 - with open(filename) as fp: - for line in fp: - comment_split = line.split("#") - num = comment_split[0].strip() - if num == '': # ignore blanks - continue - val = int(num, 2) -​ - self.ram[address] = val - address += 1 -​ - def ram_write(self, mdr, mar): - self.ram[mar] = mdr -​ - def ram_read(self, mar): - return self.ram[mar] -​ - def push_val(self, val): - self.reg[SP] -= 1 - self.ram_write(val, self.reg[7]) -​ - def pop_val(self): - val = self.ram_read(self.reg[7]) - self.reg[SP] += 1 -​ - return val -​ - def alu(self, op, reg_a, reg_b): -​ - if op == "ADD": - self.reg[reg_a] += self.reg[reg_b] - elif op == "AND": - self.reg[reg_a] &= self.reg[reg_b] - elif op == "SUB": - self.reg[reg_a] -= self.reg[reg_b] - elif op == "MUL": - self.reg[reg_a] *= self.reg[reg_b] - elif op == "DIV": - self.reg[reg_a] /= self.reg[reg_b] - elif op == "DEC": - self.reg[reg_a] -= 1 - elif op == "INC": - self.reg[reg_a] += 1 - elif op == "CMP": - self.fl &= 0x11111000 # clear all CMP flags - if self.reg[reg_a] < self.reg[reg_b]: - self.fl |= FL_LT - elif self.reg[reg_a] > self.reg[reg_b]: - self.fl |= FL_GT - else: - self.fl |= FL_EQ - elif op == "OR": - self.reg[reg_a] |= self.reg[reg_b] - elif op == "SHL": - self.reg[reg_a] <<= self.reg[reg_b] - elif op == "XOR": - self.reg[reg_a] ^= self.reg[reg_b] - else: - raise Exception("Unsupported ALU operation") -​ - def check_for_timer_int(self): - """Check the time to see if a timer interrupt should fire.""" - if self.last_timer_int == None: - self.last_timer_int = datetime.now() -​ - now = datetime.now() -​ - diff = now - self.last_timer_int -​ - if diff.seconds >= 1: # OK, fire! - self.last_timer_int = now - self.reg[IS] |= IS_TIMER -​ - def handle_ints(self): - if not self.ie: # see if interrupts enabled - return -​ - # Mask out interrupts - masked_ints = self.reg[IM] & self.reg[IS] -​ - for i in range(8): - # See if the interrupt triggered - if masked_ints & (1 << i): - self.ie = 0 # disable interrupts - self.reg[IS] &= ~(1 << i) # clear bit for this interrupt -​ - # Save all the work on the stack - self.push_val(self.pc) - self.push_val(self.fl) - for r in range(7): - self.push_val(self.reg[r]) -​ - # Look up the address vector and jump to it - self.pc = self.ram_read(0xf8 + i) -​ - break # no more processing -​ - def trace(self): - print(f"TRACE: %02X | %02X | %d | %02X %02X %02X |" % ( - self.pc, - self.fl, - self.ie, - self.ram_read(self.pc), - self.ram_read(self.pc + 1), - self.ram_read(self.pc + 2) - ), end='') -​ - for i in range(8): - print(" %02X" % self.reg[i], end='') -​ - print() -​ - def run(self): - """Run the CPU.""" -​ - while not self.halted: - # Interrupt code -​ - self.check_for_timer_int() # timer interrupt check - #self.check_for_keyboard_int() # keyboard interrupt check - self.handle_ints() # see if any interrupts occurred -​ - # Normal instruction processing -​ - #self.trace() -​ - ir = self.ram[self.pc] - operand_a = self.ram_read(self.pc + 1) - operand_b = self.ram_read(self.pc + 2) -​ - inst_size = ((ir >> 6) & 0b11) + 1 - self.inst_set_pc = ((ir >> 4) & 0b1) == 1 -​ - if ir in self.bt: - self.bt[ir](operand_a, operand_b) - else: - raise Exception(f"Invalid instruction {hex(ir)} at address {hex(self.pc)}") -​ - # If the instruction didn't set the PC, just move to the next instruction - if not self.inst_set_pc: - self.pc += inst_size -​ -​ - def op_ldi(self, operand_a, operand_b): - self.reg[operand_a] = operand_b -​ - def op_prn(self, operand_a, operand_b): - print(self.reg[operand_a]) -​ - def op_pra(self, operand_a, operand_b): - print(chr(self.reg[operand_a]), end='') - sys.stdout.flush() -​ - def op_add(self, operand_a, operand_b): - self.alu("ADD", operand_a, operand_b) -​ - def op_and(self, operand_a, operand_b): - self.alu("AND", operand_a, operand_b) -​ - def op_sub(self, operand_a, operand_b): - self.alu("SUB", operand_a, operand_b) -​ - def op_mul(self, operand_a, operand_b): - self.alu("MUL", operand_a, operand_b) -​ - def op_div(self, operand_a, operand_b): - self.alu("DIV", operand_a, operand_b) -​ - def op_dec(self, operand_a, operand_b): - self.alu("DEC", operand_a, None) -​ - def op_inc(self, operand_a, operand_b): - self.alu("INC", operand_a, None) -​ - def op_or(self, operand_a, operand_b): - self.alu("OR", operand_a, operand_b) -​ - def op_xor(self, operand_a, operand_b): - self.alu("XOR", operand_a, operand_b) -​ - def op_pop(self, operand_a, operand_b): - self.reg[operand_a] = self.pop_val() -​ - def op_push(self, operand_a, operand_b): - self.push_val(self.reg[operand_a]) -​ - def op_call(self, operand_a, operand_b): - self.push_val(self.pc + 2) - self.pc = self.reg[operand_a] -​ - def op_ret(self, operand_a, operand_b): - self.pc = self.pop_val() -​ - def op_ld(self, operand_a, operand_b): - self.reg[operand_a] = self.ram_read(self.reg[operand_b]) -​ - def op_shl(self, operand_a, operand_b): - self.alu("SHL", operand_a, operand_b) -​ - def op_st(self, operand_a, operand_b): - self.ram_write(self.reg[operand_b], self.reg[operand_a]) -​ - def op_jmp(self, operand_a, operand_b): - self.pc = self.reg[operand_a] -​ - def op_jeq(self, operand_a, operand_b): - if self.fl & FL_EQ: - self.pc = self.reg[operand_a] - else: - self.inst_set_pc = False -​ - def op_jle(self, operand_a, operand_b): - if self.fl & FL_LT or self.fl & FL_EQ: - self.pc = self.reg[operand_a] - else: - self.inst_set_pc = False -​ - def op_jlt(self, operand_a, operand_b): - if self.fl & FL_LT: - self.pc = self.reg[operand_a] - else: - self.inst_set_pc = False -​ - def op_jne(self, operand_a, operand_b): - if not self.fl & FL_EQ: - self.pc = self.reg[operand_a] - else: - self.inst_set_pc = False -​ - def op_cmp(self, operand_a, operand_b): - self.alu("CMP", operand_a, operand_b) -​ - def op_iret(self, operand_a, operand_b): - # restore work from stack - for i in range(6, -1, -1): - self.reg[i] = self.pop_val() - self.fl = self.pop_val() - self.pc = self.pop_val() -​ - # enable interrupts - self.ie = 1 -​ - def op_hlt(self, operand_a, operand_b): - self.halted = True \ No newline at end of file diff --git a/explore.py b/explore.py new file mode 100644 index 00000000..b6d48ae3 --- /dev/null +++ b/explore.py @@ -0,0 +1,9 @@ +from utils import * + +mappy = mapper() +mappy.accumulate=False #set pick up items to false + +mappy.create_starting_map() +time.sleep(5) + +mappy.explore_random(50) \ No newline at end of file diff --git a/explorer_guide.md b/explorer_guide.md index 5845de3f..0e918fb5 100644 --- a/explorer_guide.md +++ b/explorer_guide.md @@ -20,9 +20,9 @@ mappy.explore_random(50) mappy.room_check() ``` -## examples of workign commands +## examples of working commands ```python -mappy.action('take','tiny treasure') #pick up some treasure +mappy.action('take','tiny treasure') # pick up some treasure mappy.action('status') # get status update mappy.get_info('backtrack','e','254') # back track specifying room and direction ``` \ No newline at end of file diff --git a/utils.py b/utils.py index ef85c888..2f21a151 100644 --- a/utils.py +++ b/utils.py @@ -5,8 +5,8 @@ import time import random -auth_key = config('AUTH_KEY') -my_url = config('LAMBDA_URL') +auth_key = config('AUTH_KEY') #MAKE SURE YPU HAVE .ENV SET UP +my_url = config('LAMBDA_URL') # AND PYTHON DECOUPLE INSTALLED class Player: def __init__(self, name, startingRoom): @@ -14,12 +14,13 @@ def __init__(self, name, startingRoom): self.currentRoom = startingRoom class mapper: - def __init__(self,auth ='827d98231059f187c4203da53476090d1c83a2b9'): + def __init__(self,auth =auth_key): self.auth = auth #the auth token self.header = {'Authorization':f'Token {self.auth}'} #the header for post and get self.wait = 18 # the current sleep length self.info = {} #the last status json from post or get self.accumulate = False #whether player picks up items or not - it ise very easy to get overencumbered + self.pray = False #can't pray without a name unfortunately def get_info(self,what='init',direction=None,backtrack=None): """multi purpose move & init function - this is used @@ -36,8 +37,9 @@ def get_info(self,what='init',direction=None,backtrack=None): if response.status_code==200: self.info = json.loads(response.content) - if self.info['terrain'] == 'TRAP': - time.sleep(30) + #if self.info['terrain'] == 'TRAP': + if 'cooldown' in self.info.keys(): + time.sleep(self.info['cooldown']) self.room_check() return self.info else: @@ -60,20 +62,22 @@ def action(self,what='take',treasure=None): if response.status_code==200: self.info = json.loads(response.content) + if 'cooldown' in self.info.keys(): + time.sleep(self.info['cooldown']) return self.info else: print('error',what,treasure,response.status_code) def room_check(self): - print('room check triggered. info: ',self.info) + #print('room check triggered. info: ',self.info) if self.info['items']!=[] and self.accumulate: for item in self.info['items']: - time.sleep(self.wait) + #time.sleep(self.wait) self.info = self.action('take',item) print(self.info) - time.sleep(self.info['cooldown']) + #time.sleep(self.info['cooldown']) - if self.info['title'] == 'Shrine': + if self.info['title'] == "Linh's Shrine" and self.pray: self.info = self.action('pray') def create_starting_map(self): @@ -95,7 +99,7 @@ def pop_map_on_move(self,move): old_room = self.player.currentRoom info = self.get_info('move',move) self.player.currentRoom = info['room_id'] - print(info) #another debugger + print(info) # leave this line in to get movement updates new_room = info['room_id'] if new_room not in self.my_map.vertices: exit_dict = {} @@ -167,15 +171,15 @@ def explore_random(self,counter=5): unmapped_number = self.count_unmapped() time.sleep(self.wait) else: - print('back track on') + print('back track on') #leave this line in to show you when you are backtracking backtrack = self.bfs_for_q() #print('backtrack is', backtrack) backtrack_dirs = self.get_dirs(backtrack) - print('backtrack details',backtrack,backtrack_dirs) + print('backtrack details',backtrack,backtrack_dirs) #this line shows details of backtrack for i in range(len(backtrack_dirs)): b_info = self.get_info('backtrack',backtrack_dirs[i],str(backtrack[i+1])) self.player.currentRoom = b_info['room_id'] - time.sleep(self.wait) + #time.sleep(self.wait) #waiting logic has been moved to action functions c+=1 def go_to_room(self,destination): From 563a92cd9dde70fda00e303dedb0c70e39465600 Mon Sep 17 00:00:00 2001 From: danielhorsley Date: Tue, 19 Nov 2019 14:58:10 +0000 Subject: [PATCH 06/32] map included --- map.txt | 1 + utils.py | 32 ++++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 map.txt diff --git a/map.txt b/map.txt new file mode 100644 index 00000000..7b605ec5 --- /dev/null +++ b/map.txt @@ -0,0 +1 @@ +{"203": {"n": 268, "s": 165, "e": "?"}, "268": {"s": 203, "e": 411, "w": 312}, "312": {"n": 328, "e": 268}, "328": {"n": 332, "s": 312, "e": 357, "w": 363}, "363": {"n": 372, "e": 328}, "372": {"n": 441, "s": 363}, "441": {"s": 372}, "332": {"n": 350, "s": 328}, "350": {"n": 436, "s": 332, "e": 404}, "436": {"s": 350}, "404": {"n": 481, "w": 350}, "481": {"s": 404}, "357": {"w": 328}, "411": {"w": 268}, "165": {"n": 203, "s": 125, "w": "?"}, "125": {"n": 165, "e": 83, "w": "?"}, "83": {"s": 76, "e": 130, "w": 125}, "130": {"w": 83}, "76": {"n": 83, "e": 72, "w": 110}, "110": {"e": 76}, "72": {"s": 63, "w": 76}, "63": {"n": 72, "s": 20, "w": 73}, "73": {"e": 63}, "20": {"n": 63, "s": 19, "e": "?", "w": "?"}, "19": {"n": 20, "s": 10, "w": "?"}, "10": {"n": 19, "s": 0, "w": 43}, "43": {"e": 10, "w": 47}, "47": {"n": 71, "e": 43}, "71": {"s": 47}, "0": {"n": 10, "s": "?", "e": 4, "w": "?"}, "4": {"n": "?", "e": 13, "w": 0}, "13": {"e": 15, "w": 4}, "15": {"w": 1}, "1": {"e": 15}} \ No newline at end of file diff --git a/utils.py b/utils.py index 2f21a151..4dac53db 100644 --- a/utils.py +++ b/utils.py @@ -4,6 +4,7 @@ import json import time import random +import os auth_key = config('AUTH_KEY') #MAKE SURE YPU HAVE .ENV SET UP my_url = config('LAMBDA_URL') # AND PYTHON DECOUPLE INSTALLED @@ -14,18 +15,20 @@ def __init__(self, name, startingRoom): self.currentRoom = startingRoom class mapper: - def __init__(self,auth =auth_key): + def __init__(self,auth =auth_key,save = True, load_map= True): self.auth = auth #the auth token self.header = {'Authorization':f'Token {self.auth}'} #the header for post and get self.wait = 18 # the current sleep length self.info = {} #the last status json from post or get self.accumulate = False #whether player picks up items or not - it ise very easy to get overencumbered self.pray = False #can't pray without a name unfortunately + self.save_map_to_text = save #save latest map to a text file + self.import_text_map = load_map #import map so far - setting to false starts from scratch def get_info(self,what='init',direction=None,backtrack=None): """multi purpose move & init function - this is used for the most common actions""" - #info = !curl -X GET -H 'Authorization: Token 827d98231059f187c4203da53476090d1c83a2b9' https://lambda-treasure-hunt.herokuapp.com/api/adv/init/ + if what=='init': response = requests.get(f'{my_url}{what}/',headers=self.header) @@ -37,9 +40,10 @@ def get_info(self,what='init',direction=None,backtrack=None): if response.status_code==200: self.info = json.loads(response.content) - #if self.info['terrain'] == 'TRAP': - if 'cooldown' in self.info.keys(): + + if 'cooldown' in self.info.keys(): #there are a lot of TRAPS which require extra cooldown time.sleep(self.info['cooldown']) + self.room_check() return self.info else: @@ -69,15 +73,15 @@ def action(self,what='take',treasure=None): print('error',what,treasure,response.status_code) def room_check(self): + """checks for items in teh room or special rooms""" #print('room check triggered. info: ',self.info) if self.info['items']!=[] and self.accumulate: for item in self.info['items']: - #time.sleep(self.wait) + self.info = self.action('take',item) print(self.info) - #time.sleep(self.info['cooldown']) - if self.info['title'] == "Linh's Shrine" and self.pray: + if self.info['title'] == "Linh's Shrine" and self.pray: #there may be other shrines self.info = self.action('pray') def create_starting_map(self): @@ -90,7 +94,14 @@ def create_starting_map(self): exit_dict = {} for e in exits: exit_dict[e] = '?' - self.my_map.vertices[self.player.currentRoom] = exit_dict + if self.import_text_map: + print("load map triggered") + with open('map.txt','r') as file: + self.my_map.vertices = json.loads(file.read()) + else: + print("fresh map triggered") + self.my_map.vertices[self.player.currentRoom] = exit_dict + return self.my_map,self.player def pop_map_on_move(self,move): @@ -110,6 +121,9 @@ def pop_map_on_move(self,move): self.my_map.vertices[old_room][move] = new_room reverse_move = reverse_dir[move] self.my_map.vertices[new_room][reverse_move] = old_room + if self.save_map_to_text: + with open('map.txt','w') as file: + file.write(json.dumps(self.my_map.vertices)) def count_unmapped(self): """counts all the unmapped rooms""" @@ -173,13 +187,11 @@ def explore_random(self,counter=5): else: print('back track on') #leave this line in to show you when you are backtracking backtrack = self.bfs_for_q() - #print('backtrack is', backtrack) backtrack_dirs = self.get_dirs(backtrack) print('backtrack details',backtrack,backtrack_dirs) #this line shows details of backtrack for i in range(len(backtrack_dirs)): b_info = self.get_info('backtrack',backtrack_dirs[i],str(backtrack[i+1])) self.player.currentRoom = b_info['room_id'] - #time.sleep(self.wait) #waiting logic has been moved to action functions c+=1 def go_to_room(self,destination): From e23d6587d471442203264a49d68dc3177797caba Mon Sep 17 00:00:00 2001 From: danielhorsley Date: Tue, 19 Nov 2019 15:03:58 +0000 Subject: [PATCH 07/32] more detail on explore.md --- explorer_guide.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/explorer_guide.md b/explorer_guide.md index 0e918fb5..66e2451b 100644 --- a/explorer_guide.md +++ b/explorer_guide.md @@ -8,10 +8,17 @@ mappy = mapper() mappy.create_starting_map() ``` +## set your custom variables +```python +mappy.accumulate = True +mappy.save_map_to_text = True #overwrite current map.txt with fresh map.txt +mappy.import_text_map = True #load current map.txt as starting map #MUST BE DONE BEFORE create_starting_map + #if you don't want fresh map +``` ## randomly explore ```python -mappy.explore_random(50) +mappy.explore_random(50) #exploring 50 unkown rooms (not including backtracking) ``` ## take action in current room given lastest info from server @@ -23,6 +30,10 @@ mappy.room_check() ## examples of working commands ```python mappy.action('take','tiny treasure') # pick up some treasure +mappy.action('drop','tiny treasure') # pick up some treasure +mappy.action('sell','tiny treasure') # pick up some treasure +mappy.action('sell_confirm','tiny treasure') # pick up some treasure mappy.action('status') # get status update +mappy.action('pray') #pray in shrine room mappy.get_info('backtrack','e','254') # back track specifying room and direction ``` \ No newline at end of file From c49a93a8285ab8c544fcf9d010e5229cdcaaa81a Mon Sep 17 00:00:00 2001 From: danielhorsley Date: Tue, 19 Nov 2019 15:13:34 +0000 Subject: [PATCH 08/32] more markdown --- explore.py | 3 +-- explorer_guide.md | 11 ++++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/explore.py b/explore.py index b6d48ae3..29c05143 100644 --- a/explore.py +++ b/explore.py @@ -1,9 +1,8 @@ from utils import * mappy = mapper() -mappy.accumulate=False #set pick up items to false +#mappy.accumulate=False #set pick up items to false mappy.create_starting_map() -time.sleep(5) mappy.explore_random(50) \ No newline at end of file diff --git a/explorer_guide.md b/explorer_guide.md index 66e2451b..a8c73512 100644 --- a/explorer_guide.md +++ b/explorer_guide.md @@ -1,7 +1,16 @@ # Explorer guide +either run from command line with -## to initialize +``` +python explore.py +``` + +or load up python from command line and import * from utils + +basic_utils.py is just all the old graphs, stack, queue and search funtions from our assignments + +## to initialize from python ```python mappy = mapper() From b0d56afb3ffa24f5095fe854c7683c203f17822c Mon Sep 17 00:00:00 2001 From: danielhorsley Date: Tue, 19 Nov 2019 15:15:47 +0000 Subject: [PATCH 09/32] more markdown --- explorer_guide.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/explorer_guide.md b/explorer_guide.md index a8c73512..99baff14 100644 --- a/explorer_guide.md +++ b/explorer_guide.md @@ -19,7 +19,7 @@ mappy.create_starting_map() ## set your custom variables ```python -mappy.accumulate = True +mappy.accumulate = True # auto pick up items mappy.save_map_to_text = True #overwrite current map.txt with fresh map.txt mappy.import_text_map = True #load current map.txt as starting map #MUST BE DONE BEFORE create_starting_map #if you don't want fresh map @@ -33,7 +33,8 @@ mappy.explore_random(50) #exploring 50 unkown rooms (not including backtrackin ## take action in current room given lastest info from server ```python -mappy.room_check() +mappy.room_check() #given how you have set certain variables like pray oraccumulate will uatomate actions + # given certain cues from the server json responses ``` ## examples of working commands From acc189dcd092ada00ae5b5aaf6bf165fb0333cf0 Mon Sep 17 00:00:00 2001 From: danielhorsley Date: Tue, 19 Nov 2019 15:19:23 +0000 Subject: [PATCH 10/32] more markdown --- explorer_guide.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/explorer_guide.md b/explorer_guide.md index 99baff14..a39b4a04 100644 --- a/explorer_guide.md +++ b/explorer_guide.md @@ -39,11 +39,16 @@ mappy.room_check() #given how you have set certain variables like pray oraccum ## examples of working commands ```python +mappy.get_info('init') # initialize and get current room details +mappy.get_info('move','n') # move north +mappy.get_info('backtrack','e','254') # back track specifying room and direction + mappy.action('take','tiny treasure') # pick up some treasure -mappy.action('drop','tiny treasure') # pick up some treasure -mappy.action('sell','tiny treasure') # pick up some treasure -mappy.action('sell_confirm','tiny treasure') # pick up some treasure +mappy.action('drop','tiny treasure') # drop some treasure in your inventory +mappy.action('sell','tiny treasure') # sell some treasure in your inventory +mappy.action('sell_confirm','tiny treasure') # confirm sell some treasure in your inventory +mappy.action('examine','tiny treasure') # examine tiny treasure in your inventory mappy.action('status') # get status update -mappy.action('pray') #pray in shrine room -mappy.get_info('backtrack','e','254') # back track specifying room and direction +mappy.action('pray') # pray in shrine room + ``` \ No newline at end of file From 03be80c8b0579e02ab077399d8160d1b9d4c996e Mon Sep 17 00:00:00 2001 From: danielhorsley Date: Tue, 19 Nov 2019 16:28:19 +0000 Subject: [PATCH 11/32] fixing of map key bug and non update room bug --- explorer_guide.md | 7 ++++++- map.txt | 2 +- utils.py | 18 ++++++++++++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/explorer_guide.md b/explorer_guide.md index a39b4a04..046fb2b3 100644 --- a/explorer_guide.md +++ b/explorer_guide.md @@ -24,6 +24,11 @@ mappy.save_map_to_text = True #overwrite current map.txt with fresh map.txt mappy.import_text_map = True #load current map.txt as starting map #MUST BE DONE BEFORE create_starting_map #if you don't want fresh map ``` +## show your latest map +```python +mappy.my_map.vertices +``` + ## randomly explore ```python @@ -33,7 +38,7 @@ mappy.explore_random(50) #exploring 50 unkown rooms (not including backtrackin ## take action in current room given lastest info from server ```python -mappy.room_check() #given how you have set certain variables like pray oraccumulate will uatomate actions +mappy.room_check() #given how you have set certain variables like pray or accumulate will automate actions # given certain cues from the server json responses ``` diff --git a/map.txt b/map.txt index 7b605ec5..caf2ca42 100644 --- a/map.txt +++ b/map.txt @@ -1 +1 @@ -{"203": {"n": 268, "s": 165, "e": "?"}, "268": {"s": 203, "e": 411, "w": 312}, "312": {"n": 328, "e": 268}, "328": {"n": 332, "s": 312, "e": 357, "w": 363}, "363": {"n": 372, "e": 328}, "372": {"n": 441, "s": 363}, "441": {"s": 372}, "332": {"n": 350, "s": 328}, "350": {"n": 436, "s": 332, "e": 404}, "436": {"s": 350}, "404": {"n": 481, "w": 350}, "481": {"s": 404}, "357": {"w": 328}, "411": {"w": 268}, "165": {"n": 203, "s": 125, "w": "?"}, "125": {"n": 165, "e": 83, "w": "?"}, "83": {"s": 76, "e": 130, "w": 125}, "130": {"w": 83}, "76": {"n": 83, "e": 72, "w": 110}, "110": {"e": 76}, "72": {"s": 63, "w": 76}, "63": {"n": 72, "s": 20, "w": 73}, "73": {"e": 63}, "20": {"n": 63, "s": 19, "e": "?", "w": "?"}, "19": {"n": 20, "s": 10, "w": "?"}, "10": {"n": 19, "s": 0, "w": 43}, "43": {"e": 10, "w": 47}, "47": {"n": 71, "e": 43}, "71": {"s": 47}, "0": {"n": 10, "s": "?", "e": 4, "w": "?"}, "4": {"n": "?", "e": 13, "w": 0}, "13": {"e": 15, "w": 4}, "15": {"w": 1}, "1": {"e": 15}} \ No newline at end of file +{"59": {"n": 38, "s": "?", "e": "?"}, "38": {"s": 59, "e": "?", "w": 33}, "33": {"e": 38, "w": 31}, "31": {"n": 30, "e": 33}, "30": {"s": 31, "e": 32, "w": "?"}, "32": {"n": "?", "e": "?", "w": 30}} \ No newline at end of file diff --git a/utils.py b/utils.py index 4dac53db..6672ba57 100644 --- a/utils.py +++ b/utils.py @@ -9,6 +9,10 @@ auth_key = config('AUTH_KEY') #MAKE SURE YPU HAVE .ENV SET UP my_url = config('LAMBDA_URL') # AND PYTHON DECOUPLE INSTALLED +def keystoint(x): + "function to change json dictionary keys to ints - used for map load" + return {int(k): v for k, v in x.items()} + class Player: def __init__(self, name, startingRoom): self.name = name @@ -18,12 +22,13 @@ class mapper: def __init__(self,auth =auth_key,save = True, load_map= True): self.auth = auth #the auth token self.header = {'Authorization':f'Token {self.auth}'} #the header for post and get - self.wait = 18 # the current sleep length + self.wait = 18 # the current sleep length - this is no longer required as wait always points to cooldown self.info = {} #the last status json from post or get - self.accumulate = False #whether player picks up items or not - it ise very easy to get overencumbered + self.accumulate = False #whether player picks up items or not - it is very easy to get overencumbered self.pray = False #can't pray without a name unfortunately self.save_map_to_text = save #save latest map to a text file self.import_text_map = load_map #import map so far - setting to false starts from scratch + self.player = None def get_info(self,what='init',direction=None,backtrack=None): """multi purpose move & init function - this is used @@ -40,6 +45,8 @@ def get_info(self,what='init',direction=None,backtrack=None): if response.status_code==200: self.info = json.loads(response.content) + if self.player is not None: + self.player.currentRoom = self.info['room_id'] if 'cooldown' in self.info.keys(): #there are a lot of TRAPS which require extra cooldown time.sleep(self.info['cooldown']) @@ -97,7 +104,9 @@ def create_starting_map(self): if self.import_text_map: print("load map triggered") with open('map.txt','r') as file: - self.my_map.vertices = json.loads(file.read()) + string_dict = json.loads(file.read()) + for key in string_dict: + self.my_map.vertices[int(key)] = string_dict[key] else: print("fresh map triggered") self.my_map.vertices[self.player.currentRoom] = exit_dict @@ -195,7 +204,8 @@ def explore_random(self,counter=5): c+=1 def go_to_room(self,destination): - """depth first traversal to particular room in shortest route""" + """depth first traversal to particular room in shortest route + NOT OPERATIONAL YET""" s = Stack() s.push([self.player.currentRoom]) From 37ccc96949d08d6e2771e9a39d014833725559b0 Mon Sep 17 00:00:00 2001 From: PWalis Date: Tue, 19 Nov 2019 13:00:46 -0700 Subject: [PATCH 12/32] Current progress on go_to_room function --- Pipfile | 2 ++ Pipfile.lock | 48 +++++++++++++++++++++++++-- basic_utils.py | 19 +++++++---- explore.py | 8 +++-- map.txt | 2 +- test_graph.py | 4 +++ utils.py | 89 ++++++++++++++++++++++++++++++++------------------ 7 files changed, 129 insertions(+), 43 deletions(-) create mode 100644 test_graph.py diff --git a/Pipfile b/Pipfile index b723d019..637e1c4f 100644 --- a/Pipfile +++ b/Pipfile @@ -6,6 +6,8 @@ verify_ssl = true [dev-packages] [packages] +python-decouple = "*" +requests = "*" [requires] python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock index 9a51a282..c1b96a73 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "7e7ef69da7248742e869378f8421880cf8f0017f96d94d086813baa518a65489" + "sha256": "40ca981d597ce7f3965016abfbb4f4db88df55b1f70a8c5f6fc1c1992fdba2f3" }, "pipfile-spec": 6, "requires": { @@ -15,6 +15,50 @@ } ] }, - "default": {}, + "default": { + "certifi": { + "hashes": [ + "sha256:e4f3620cfea4f83eedc95b24abd9cd56f3c4b146dd0177e83a21b4eb49e21e50", + "sha256:fd7c7c74727ddcf00e9acd26bba8da604ffec95bf1c2144e67aff7a8b50e6cef" + ], + "version": "==2019.9.11" + }, + "chardet": { + "hashes": [ + "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae", + "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691" + ], + "version": "==3.0.4" + }, + "idna": { + "hashes": [ + "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407", + "sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c" + ], + "version": "==2.8" + }, + "python-decouple": { + "hashes": [ + "sha256:55c546b85b0c47a15a47a4312d451a437f7344a9be3e001660bccd93b637de95" + ], + "index": "pypi", + "version": "==3.3" + }, + "requests": { + "hashes": [ + "sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4", + "sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31" + ], + "index": "pypi", + "version": "==2.22.0" + }, + "urllib3": { + "hashes": [ + "sha256:a8a318824cc77d1fd4b2bec2ded92646630d7fe8619497b142c84a9e6f5a7293", + "sha256:f3c5fd51747d450d4dcf6f923c81f78f811aab8205fda64b0aba34a4e48b0745" + ], + "version": "==1.25.7" + } + }, "develop": {} } diff --git a/basic_utils.py b/basic_utils.py index 277c3b5c..1e543798 100644 --- a/basic_utils.py +++ b/basic_utils.py @@ -124,16 +124,23 @@ def bfs(self, starting_vertex, destination_vertex): starting_vertex to destination_vertex in breath-first order. """ + print('Starting BFS') q = Queue() + visited = {} q.enqueue([starting_vertex]) - - while destination_vertex not in q.queue[0]: + print(f'Starting vertex: {starting_vertex}') + # while destination_vertex not in q.queue[0]: + while q.queue[0][-1] != destination_vertex: current_point = q.queue[0][-1] - joins = self.vertices[current_point] + print(f'current point: {current_point}') + joins = self.vertices[current_point].values() + # print(joins) for j in joins: - _ = [x for x in q.queue[0]] - _.append(j) - q.enqueue(_) + # print(f'J: {j}') + if j != '?': + _ = [x for x in q.queue[0]] + _.append(j) + q.enqueue(_) q.dequeue() return q.queue[0] diff --git a/explore.py b/explore.py index 29c05143..a544f602 100644 --- a/explore.py +++ b/explore.py @@ -1,8 +1,10 @@ from utils import * mappy = mapper() -#mappy.accumulate=False #set pick up items to false +mappy.accumulate=False #set pick up items to false +mappy.import_text_map=True mappy.create_starting_map() - -mappy.explore_random(50) \ No newline at end of file +mappy.go_to_room('10') +print(mappy.player.currentRoom) +# mappy.explore_random(50) \ No newline at end of file diff --git a/map.txt b/map.txt index caf2ca42..7201b461 100644 --- a/map.txt +++ b/map.txt @@ -1 +1 @@ -{"59": {"n": 38, "s": "?", "e": "?"}, "38": {"s": 59, "e": "?", "w": 33}, "33": {"e": 38, "w": 31}, "31": {"n": 30, "e": 33}, "30": {"s": 31, "e": 32, "w": "?"}, "32": {"n": "?", "e": "?", "w": 30}} \ No newline at end of file +{"10": {"n": 19, "s": "?", "w": "?"}, "19": {"n": 20, "s": 10, "w": "?"}, "20": {"n": "?", "s": 19, "e": 27, "w": "?"}, "27": {"n": "?", "s": "?", "e": 30, "w": 20}, "30": {"s": "?", "e": 32, "w": 27}, "32": {"n": 39, "e": 54, "w": 30}, "54": {"w": 32}, "39": {"n": "?", "s": 32, "e": "?", "w": "?"}} \ No newline at end of file diff --git a/test_graph.py b/test_graph.py new file mode 100644 index 00000000..f5468d80 --- /dev/null +++ b/test_graph.py @@ -0,0 +1,4 @@ +from basic_utils import Graph +graph = Graph() +graph.vertices = {'1':{'n':'2','e':'3','w':'4','s':'5'},'2':{'s':'1'},'3':{'w':'1'},'4':{'e':'1'},'5':{'n':'1'}} +print(graph.bfs('5','3')) \ No newline at end of file diff --git a/utils.py b/utils.py index 6672ba57..272dfcab 100644 --- a/utils.py +++ b/utils.py @@ -17,9 +17,20 @@ class Player: def __init__(self, name, startingRoom): self.name = name self.currentRoom = startingRoom + self.player_cooldown = 1, + self.player_encumbrance = 0, + self.player_strength = 0, + self.player_speed = 0, + self.player_gold = 0, + self.player_inventory = [], + self.player_status = [], + self.player_errors = [], + self.player_messages = [] + self.player_mine = '' + class mapper: - def __init__(self,auth =auth_key,save = True, load_map= True): + def __init__(self, auth=auth_key, save=True, load_map=True): self.auth = auth #the auth token self.header = {'Authorization':f'Token {self.auth}'} #the header for post and get self.wait = 18 # the current sleep length - this is no longer required as wait always points to cooldown @@ -30,7 +41,7 @@ def __init__(self,auth =auth_key,save = True, load_map= True): self.import_text_map = load_map #import map so far - setting to false starts from scratch self.player = None - def get_info(self,what='init',direction=None,backtrack=None): + def get_info(self, what='init', direction=None, backtrack=None): """multi purpose move & init function - this is used for the most common actions""" @@ -56,20 +67,20 @@ def get_info(self,what='init',direction=None,backtrack=None): else: print('cooldown triggered - waiting 20 seconds') time.sleep(20) - self.get_info(what=what,direction=direction,backtrack=backtrack) + self.get_info(what=what, direction=direction, backtrack=backtrack) - def action(self,what='take',treasure=None): + def action(self, what='take', treasure=None): """another multi purpose request function this one focuses on less common actions""" if what in ['take','drop','sell','examine']: - response = requests.post(f'{my_url}{what}/',headers=self.header,json = {"name":treasure}) + response = requests.post(f'{my_url}{what}/', headers=self.header, json={"name":treasure}) if what in ['status','pray']: - response = requests.post(f'{my_url}{what}/',headers=self.header) + response = requests.post(f'{my_url}{what}/', headers=self.header) if what == 'confirm_sell': - response = requests.post(f'{my_url}{what}/',headers=self.header,json = {"name":treasure, "confirm" : "yes"}) + response = requests.post(f'{my_url}{what}/', headers=self.header, json={"name":treasure, "confirm" : "yes"}) if response.status_code==200: self.info = json.loads(response.content) @@ -113,11 +124,11 @@ def create_starting_map(self): return self.my_map,self.player - def pop_map_on_move(self,move): + def pop_map_on_move(self, move): """fills in the map while moving in the direction specified""" reverse_dir ={'n':'s','s':'n','w':'e','e':'w'} old_room = self.player.currentRoom - info = self.get_info('move',move) + info = self.get_info('move', move) self.player.currentRoom = info['room_id'] print(info) # leave this line in to get movement updates new_room = info['room_id'] @@ -143,7 +154,7 @@ def count_unmapped(self): counter += 1 return counter - def get_dirs(self,traversal): + def get_dirs(self, traversal): """gets the direction of travel given a room traversal list""" point = traversal[0] dir_list = [] @@ -175,7 +186,7 @@ def bfs_for_q(self): return q.queue[0] - def explore_random(self,counter=5): + def explore_random(self, counter=5): """explores the map choosing random ? and backtracks using bfs counter is the number of times you want it to explore unkown rooms""" unmapped_number = self.count_unmapped() @@ -203,26 +214,42 @@ def explore_random(self,counter=5): self.player.currentRoom = b_info['room_id'] c+=1 - def go_to_room(self,destination): - """depth first traversal to particular room in shortest route - NOT OPERATIONAL YET""" - s = Stack() - s.push([self.player.currentRoom]) + def go_to_room(self, destination): + """depth first traversal to particular room in shortest route + NOT OPERATIONAL YET""" + self.accumulate = False + print('moving') + path = self.my_map.bfs(self.player.currentRoom, destination) + print(f'Path: {path}') + for m in path: + room = self.player.currentRoom + print(f'Room: {room}') + exits = self.my_map[room] + print(f'Room: {room}') + for direction in exits: + if self.my_map[room][direction] == m: + self.get_info(what='move', direction=direction) + else: + return f'go_to_room FAILED: Invalid Direction[{direction}], Room[{room}]' + self.accumulate = True + + # s = Stack() + # s.push([self.player.currentRoom]) - while destination not in s.stack[-1]: - current_point = s.stack[-1][-1] + # while destination not in s.stack[-1]: + # current_point = s.stack[-1][-1] - joins = self.my_map.vertices[current_point] - if joins is None: - s.pop() - else: - temp_list = [] - for j in joins: - _ = [x for x in s.stack[-1]] - _.append(j) - temp_list.append(_) - for tl in temp_list: - s.push(tl) - - return s.stack[-1] + # joins = self.my_map.vertices[current_point] + # if joins is None: + # s.pop() + # else: + # temp_list = [] + # for j in joins: + # _ = [x for x in s.stack[-1]] + # _.append(j) + # temp_list.append(_) + # for tl in temp_list: + # s.push(tl) + + # return s.stack[-1] From 1b658bac1232c7460fe3aa5b88bf0a95486cd535 Mon Sep 17 00:00:00 2001 From: PWalis Date: Tue, 19 Nov 2019 14:43:03 -0700 Subject: [PATCH 13/32] Go_to_room is working! Just give it the room number as an int. --- basic_utils.py | 13 +++++++++---- utils.py | 14 ++++++++------ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/basic_utils.py b/basic_utils.py index 1e543798..e5f06800 100644 --- a/basic_utils.py +++ b/basic_utils.py @@ -1,6 +1,8 @@ class Queue(): def __init__(self): self.queue = [] + def __repr__(self): + return f'{self.queue}' def enqueue(self, value): self.queue.append(value) def dequeue(self): @@ -126,18 +128,21 @@ def bfs(self, starting_vertex, destination_vertex): """ print('Starting BFS') q = Queue() - visited = {} + visited = set() q.enqueue([starting_vertex]) print(f'Starting vertex: {starting_vertex}') - # while destination_vertex not in q.queue[0]: - while q.queue[0][-1] != destination_vertex: + + while destination_vertex not in q.queue[0]: + # while q.queue[0][-1] != destination_vertex: + print(q) current_point = q.queue[0][-1] print(f'current point: {current_point}') joins = self.vertices[current_point].values() # print(joins) for j in joins: # print(f'J: {j}') - if j != '?': + if j != '?' and j not in visited: + visited.add(j) _ = [x for x in q.queue[0]] _.append(j) q.enqueue(_) diff --git a/utils.py b/utils.py index 272dfcab..b9eb2bce 100644 --- a/utils.py +++ b/utils.py @@ -220,17 +220,19 @@ def go_to_room(self, destination): self.accumulate = False print('moving') path = self.my_map.bfs(self.player.currentRoom, destination) - print(f'Path: {path}') + # print(f'Path: {path}') for m in path: room = self.player.currentRoom - print(f'Room: {room}') - exits = self.my_map[room] - print(f'Room: {room}') + # print(f'Room: {type(room)}') + exits = self.my_map.vertices[room] + # print(f'Room: {room}') for direction in exits: - if self.my_map[room][direction] == m: + if self.my_map.vertices[room][direction] == m: + # print(f'direction: {direction}') self.get_info(what='move', direction=direction) else: - return f'go_to_room FAILED: Invalid Direction[{direction}], Room[{room}]' + # print(f'go_to_room FAILED: Invalid Direction[{direction}], Room[{room}]') + continue self.accumulate = True # s = Stack() From 654176b8c374b6498562d341f98cef2c6a0acb3f Mon Sep 17 00:00:00 2001 From: mrsteveson Date: Tue, 19 Nov 2019 15:46:24 -0600 Subject: [PATCH 14/32] get_treasure --- .vscode/settings.json | 3 ++ main.py | 9 ----- utils.py | 83 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 .vscode/settings.json delete mode 100644 main.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..d46cd3e9 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "C:\\Users\\mrste\\.virtualenvs\\CS-Build-Week-2-_lfTzCd4\\Scripts\\python.exe" +} \ No newline at end of file diff --git a/main.py b/main.py deleted file mode 100644 index 2510d2d7..00000000 --- a/main.py +++ /dev/null @@ -1,9 +0,0 @@ -import sys -from cpu import * - -print("This is the Treasure Map") -api_key = input("Please enter your provided API Key: ") - -# cpu = CPU() -# cpu.load() -# cpu.run() \ No newline at end of file diff --git a/utils.py b/utils.py index 272dfcab..d38ebf9b 100644 --- a/utils.py +++ b/utils.py @@ -82,6 +82,13 @@ def action(self, what='take', treasure=None): if what == 'confirm_sell': response = requests.post(f'{my_url}{what}/', headers=self.header, json={"name":treasure, "confirm" : "yes"}) + # Change Name +++++++ + if what == 'change_name': + response = request.post(f'{my_url}{what}/', headers=self.headers, json={"name":new}) + # Confirm Name +++++++ + if what == 'confirm_name': + response = request.post(f'{my_url}{what}/', headers=self.headers, json={"confirm": "aye"}) + if response.status_code==200: self.info = json.loads(response.content) if 'cooldown' in self.info.keys(): @@ -101,6 +108,16 @@ def room_check(self): if self.info['title'] == "Linh's Shrine" and self.pray: #there may be other shrines self.info = self.action('pray') + + # Would this sell? ++++++ + if self.info['title'] == "shop" and self.sell: # This could sell? + self.info = self.action('sell',item) + self.info = self.action('confirm_sell') + + # Could this do the name change? +++++++ + if self.info['title'] == "pirate ry" and self.change_name: + self.info = self.action('change_name', "name":"Spongebubba") + self.info = self.action('confirm_name') def create_starting_map(self): """"initiates your starting map which is stored under the vertices of a graph class""" @@ -253,3 +270,69 @@ def go_to_room(self, destination): # return s.stack[-1] + def pirate(self): + # Goes directly to pirate ry + # self.go_to_room() + # time.sleep(self.wait) + pass + + def wishing_well(self): + # Goes directly to pirate ry + # self.go_to_room() + # time.sleep(self.wait) + pass + + def vendor(self): + # Goes directly to the shop + self.go_to_room(1) + time.sleep(self.wait) + + # Method to get treasure + # BFS Randomly to travel the maze, looting + # Once you get enough treasure, go sell + # Once you reach 1000 gold, buy a name + # Change name to something unique, that doesnt contain player + # Keep looting and selling until stopped. + def get_treasure(self): + while True: + if self.player.name.contains('player') and self.player.gold > 1000: # get a name + # Go to name changer (pirate ry) + print('Time to Buy a Name') + name_changer = self.go_to_room('pirate ry') + time.sleep(self.wait) + # Buy name + self.action('change_name') + time.sleep(self.wait) + # confirm_name + self.action('confirm_name') + print('Got a name! Time to get a COIN.', {self.player.name}) + time.sleep(self.wait) + # self.action('status') #Check new name + + elif player.encumbered <= player.strength - 2: + # If encumbered is str-2 (at base = 8) + # Travel the room bfs style at random + # Loot as you go with room_check + print('Looting..') + # self.explore_random(500) + self.go_to_room(str(random.choice(range(0,499)))) + time.sleep(self.wait) + + # Could potentially add a section to manage miner + + else: + # else go directly to the shop + # loop through inventory and sell + # Go back to looting + print('Need to offload my loot.') + self.go_to_room(1) # room 1 = shop + time.sleep(self.wait) + print('At the shop, time to sell.') + for count in range(treasures/inventory): + print('Selling item..') + self.action('sell', item) + time.sleep(self.wait) + self.action('confirm_sell') + print("{self.player.gold}") + time.sleep(self.wait) + print('Back to Looting', {player.inventory}) \ No newline at end of file From a8f3a1916a10a881086e424a61a75138f5947c6b Mon Sep 17 00:00:00 2001 From: mrsteveson Date: Tue, 19 Nov 2019 16:51:00 -0600 Subject: [PATCH 15/32] building maze --- explore.py | 8 +++---- map.txt | 2 +- rooms.txt | 1 + utils.py | 61 ++++++++++++++++++++---------------------------------- 4 files changed, 29 insertions(+), 43 deletions(-) create mode 100644 rooms.txt diff --git a/explore.py b/explore.py index a544f602..a7a5c0c6 100644 --- a/explore.py +++ b/explore.py @@ -2,9 +2,9 @@ mappy = mapper() mappy.accumulate=False #set pick up items to false -mappy.import_text_map=True +# mappy.import_text_map=False mappy.create_starting_map() -mappy.go_to_room('10') -print(mappy.player.currentRoom) -# mappy.explore_random(50) \ No newline at end of file +# mappy.go_to_room('10') +# print(mappy.player.currentRoom) +mappy.explore_random(50) \ No newline at end of file diff --git a/map.txt b/map.txt index 7201b461..3258aff9 100644 --- a/map.txt +++ b/map.txt @@ -1 +1 @@ -{"10": {"n": 19, "s": "?", "w": "?"}, "19": {"n": 20, "s": 10, "w": "?"}, "20": {"n": "?", "s": 19, "e": 27, "w": "?"}, "27": {"n": "?", "s": "?", "e": 30, "w": 20}, "30": {"s": "?", "e": 32, "w": 27}, "32": {"n": 39, "e": 54, "w": 30}, "54": {"w": 32}, "39": {"n": "?", "s": 32, "e": "?", "w": "?"}} \ No newline at end of file +{"483": {"w": 477}, "477": {"e": 483, "w": 388}, "388": {"e": 477, "w": 382}, "382": {"s": 322, "e": 388}, "322": {"n": 382, "e": "?", "w": 261}, "261": {"s": "?", "e": 322, "w": 247}, "247": {"e": 261, "w": 217}, "217": {"s": 164, "e": 247}, "164": {"n": 217, "e": "?", "w": 156}, "156": {"s": "?", "e": 164, "w": 141}, "141": {"n": 112, "e": 156}, "112": {"s": 141, "e": "?", "w": 100}, "100": {"s": "?", "e": 112, "w": 68}, "68": {"n": 52, "e": 100}, "52": {"n": 35, "s": 68, "e": 75}, "75": {"e": 85, "w": 52}, "85": {"e": 154, "w": 75}, "154": {"e": 193, "w": 85}, "193": {"e": 251, "w": 154}, "251": {"e": 315, "w": 193}, "315": {"w": 251}, "35": {"s": 52, "w": 34}, "34": {"n": "?", "s": 50, "e": 35}, "50": {"n": 34, "s": 89}, "89": {"n": 50, "s": 93}, "93": {"n": 89, "w": 108}, "108": {"n": "?", "s": 117, "e": 93}, "117": {"n": 108, "s": 131, "e": "?", "w": "?"}, "131": {"n": 117, "s": "?", "w": 138}, "138": {"s": "?", "e": 131, "w": 195}, "195": {"s": 228, "e": 138, "w": 225}, "225": {"s": 278, "e": 195}, "278": {"n": 225}, "228": {"n": 195, "s": 281}, "281": {"n": 228, "s": "?", "e": 309, "w": 317}, "309": {"s": 333, "e": 326, "w": 281}, "333": {"n": 309, "s": 378}, "378": {"n": 333}, "326": {"s": 342, "w": 309}, "342": {"n": 326, "s": 432}, "432": {"n": 342}, "317": {"s": 387, "e": 281, "w": 409}, "409": {"e": 317}, "387": {"n": 317, "s": 417, "w": 431}, "431": {"e": 387, "w": 492}, "492": {"e": 431}, "417": {"n": 387}} \ No newline at end of file diff --git a/rooms.txt b/rooms.txt new file mode 100644 index 00000000..99d295be --- /dev/null +++ b/rooms.txt @@ -0,0 +1 @@ +{"A misty room": 417, "Mt. Holloway": 131} \ No newline at end of file diff --git a/utils.py b/utils.py index 242add1a..7f3d03f3 100644 --- a/utils.py +++ b/utils.py @@ -40,6 +40,7 @@ def __init__(self, auth=auth_key, save=True, load_map=True): self.save_map_to_text = save #save latest map to a text file self.import_text_map = load_map #import map so far - setting to false starts from scratch self.player = None + self.important_rooms = {} def get_info(self, what='init', direction=None, backtrack=None): """multi purpose move & init function - this is used @@ -103,20 +104,20 @@ def room_check(self): if self.info['items']!=[] and self.accumulate: for item in self.info['items']: - self.info = self.action('take',item) + self.info = self.action('take', item) print(self.info) if self.info['title'] == "Linh's Shrine" and self.pray: #there may be other shrines self.info = self.action('pray') # Would this sell? ++++++ - if self.info['title'] == "shop" and self.sell: # This could sell? - self.info = self.action('sell',item) + if self.info['title'] == "shop": + self.info = self.action('sell', item) self.info = self.action('confirm_sell') # Could this do the name change? +++++++ - if self.info['title'] == "pirate ry" and self.change_name: - self.info = self.action('change_name', "name":"Spongebubba") + if self.info['title'] == "pirate ry": + self.info = self.action('change_name', name) self.info = self.action('confirm_name') def create_starting_map(self): @@ -135,6 +136,10 @@ def create_starting_map(self): string_dict = json.loads(file.read()) for key in string_dict: self.my_map.vertices[int(key)] = string_dict[key] + with open('rooms.txt', 'r') as file: + string_dict = json.loads(file.read()) + for key in string_dict: + self.important_rooms[key] = string_dict[key] else: print("fresh map triggered") self.my_map.vertices[self.player.currentRoom] = exit_dict @@ -161,6 +166,11 @@ def pop_map_on_move(self, move): if self.save_map_to_text: with open('map.txt','w') as file: file.write(json.dumps(self.my_map.vertices)) + + self.important_rooms.update({info['title']: info['room_id']}) + if self.save_map_to_text: + with open('rooms.txt', 'w') as file: + file.write(json.dumps(self.important_rooms)) def count_unmapped(self): """counts all the unmapped rooms""" @@ -232,8 +242,7 @@ def explore_random(self, counter=5): c+=1 def go_to_room(self, destination): - """depth first traversal to particular room in shortest route - NOT OPERATIONAL YET""" + """depth first traversal to particular room in shortest route""" self.accumulate = False print('moving') path = self.my_map.bfs(self.player.currentRoom, destination) @@ -252,35 +261,15 @@ def go_to_room(self, destination): continue self.accumulate = True - # s = Stack() - # s.push([self.player.currentRoom]) - - # while destination not in s.stack[-1]: - # current_point = s.stack[-1][-1] - - # joins = self.my_map.vertices[current_point] - # if joins is None: - # s.pop() - # else: - # temp_list = [] - # for j in joins: - # _ = [x for x in s.stack[-1]] - # _.append(j) - # temp_list.append(_) - # for tl in temp_list: - # s.push(tl) - - # return s.stack[-1] - def pirate(self): # Goes directly to pirate ry - # self.go_to_room() + # self.go_to_room(self.important_rooms['pirate ry']) # time.sleep(self.wait) pass def wishing_well(self): # Goes directly to pirate ry - # self.go_to_room() + # self.go_to_room(self.important_rooms['wishing well']) # time.sleep(self.wait) pass @@ -300,7 +289,7 @@ def get_treasure(self): if self.player.name.contains('player') and self.player.gold > 1000: # get a name # Go to name changer (pirate ry) print('Time to Buy a Name') - name_changer = self.go_to_room('pirate ry') + self.go_to_room(self.important_rooms['pirate ry']) time.sleep(self.wait) # Buy name self.action('change_name') @@ -310,7 +299,6 @@ def get_treasure(self): print('Got a name! Time to get a COIN.', {self.player.name}) time.sleep(self.wait) # self.action('status') #Check new name - elif player.encumbered <= player.strength - 2: # If encumbered is str-2 (at base = 8) # Travel the room bfs style at random @@ -319,22 +307,19 @@ def get_treasure(self): # self.explore_random(500) self.go_to_room(str(random.choice(range(0,499)))) time.sleep(self.wait) - # Could potentially add a section to manage miner - else: # else go directly to the shop # loop through inventory and sell # Go back to looting print('Need to offload my loot.') - self.go_to_room(1) # room 1 = shop - time.sleep(self.wait) + self.vendor() print('At the shop, time to sell.') - for count in range(treasures/inventory): + for count in range(self.player.inventory): print('Selling item..') self.action('sell', item) time.sleep(self.wait) self.action('confirm_sell') - print("{self.player.gold}") + print({self.player.gold}) time.sleep(self.wait) - print('Back to Looting', {player.inventory}) \ No newline at end of file + print('Back to Looting', {self.player.inventory}) \ No newline at end of file From 37293efb4b51c4e1b018e60233ad72bbe72861b5 Mon Sep 17 00:00:00 2001 From: micahjones13 Date: Tue, 19 Nov 2019 16:16:06 -0700 Subject: [PATCH 16/32] map fully mapped --- Pipfile | 3 +++ explore.py | 6 +++--- find.py | 2 ++ map.txt | 2 +- 4 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 find.py diff --git a/Pipfile b/Pipfile index b723d019..d6bbe283 100644 --- a/Pipfile +++ b/Pipfile @@ -6,6 +6,9 @@ verify_ssl = true [dev-packages] [packages] +decouple = "*" +requests = "*" +python-decouple = "*" [requires] python_version = "3.7" diff --git a/explore.py b/explore.py index 29c05143..1cd79ac2 100644 --- a/explore.py +++ b/explore.py @@ -1,8 +1,8 @@ from utils import * mappy = mapper() -#mappy.accumulate=False #set pick up items to false - +# mappy.accumulate=False #set pick up items to false +# mappy.import_text_map = False mappy.create_starting_map() -mappy.explore_random(50) \ No newline at end of file +mappy.explore_random(450) diff --git a/find.py b/find.py new file mode 100644 index 00000000..220a5216 --- /dev/null +++ b/find.py @@ -0,0 +1,2 @@ +my_dict ={"0": {"n": 10, "s": 2, "e": 4, "w": 1}, "1": {"e": 0}, "2": {"n": 0, "s": 6, "e": 3}, "3": {"s": 9, "e": 5, "w": 2}, "9": {"n": 3, "s": 12, "e": 11}, "11": {"e": 17, "w": 9}, "17": {"n": 24, "e": 42, "w": 11}, "24": {"s": 17}, "42": {"n": 44, "s": 80, "e": 118, "w": 17}, "44": {"s": 42}, "80": {"n": 42, "s": 81, "e": 86}, "81": {"n": 80}, "86": {"s": 96, "e": 90, "w": 80}, "90": {"e": 178, "w": 86}, "178": {"n": 209, "e": 243, "w": 90}, "243": {"s": 293, "e": 256, "w": 178}, "293": {"n": 243}, "256": {"s": 360, "e": 327, "w": 243}, "327": {"e": 427, "w": 256}, "427": {"e": 430, "w": 327}, "430": {"n": 443, "e": 439, "w": 427}, "443": {"s": 430, "e": 471}, "471": {"w": 443}, "439": {"w": 430}, "360": {"n": 256, "e": 398}, "398": {"e": 438, "w": 360}, "438": {"e": 465, "w": 398}, "465": {"e": 498, "w": 438}, "498": {"w": 465}, "209": {"s": 178}, "96": {"n": 86, "e": 97}, "97": {"e": 181, "w": 96}, "181": {"w": 97}, "118": {"e": 137, "w": 42}, "137": {"w": 118}, "12": {"n": 9, "s": 18, "e": 14, "w": 21}, "14": {"s": 34, "e": 37, "w": 12}, "37": {"w": 14}, "34": {"n": 14, "s": 50, "e": 35}, "50": {"n": 34, "s": 89}, "35": {"s": 52, "w": 34}, "52": {"n": 35, "s": 68, "e": 75}, "68": {"n": 52, "e": 100}, "100": {"s": 106, "e": 112, "w": 68}, "106": {"n": 100, "s": 111, "w": 135}, "111": {"n": 106, "s": 367, "e": 158}, "367": {"n": 111}, "158": {"s": 167, "w": 111}, "167": {"n": 158, "s": 262, "e": 260}, "260": {"w": 167}, "262": {"n": 167, "s": 370, "e": 358}, "358": {"e": 401, "w": 262}, "401": {"w": 358}, "370": {"n": 262, "s": 434, "e": 407}, "434": {"n": 370}, "407": {"s": 496, "w": 370}, "496": {"n": 407}, "135": {"s": 150, "e": 106}, "150": {"n": 135, "w": 166}, "166": {"s": 198, "e": 150, "w": 117}, "198": {"n": 166, "s": 239, "e": 199}, "239": {"n": 198, "w": 244}, "244": {"n": 131, "e": 239}, "131": {"n": 117, "s": 244, "w": 138}, "138": {"s": 211, "e": 131, "w": 195}, "195": {"s": 228, "e": 138, "w": 225}, "225": {"s": 278, "e": 195}, "278": {"n": 225}, "228": {"n": 195, "s": 281}, "281": {"n": 228, "s": 318, "e": 309, "w": 317}, "318": {"n": 281, "s": 487}, "487": {"n": 318, "s": 489}, "489": {"n": 487}, "309": {"s": 333, "e": 326, "w": 281}, "333": {"n": 309, "s": 378}, "378": {"n": 333}, "326": {"s": 342, "w": 309}, "342": {"n": 326, "s": 432}, "432": {"n": 342}, "317": {"s": 387, "e": 281, "w": 409}, "387": {"n": 317, "s": 417, "w": 431}, "431": {"e": 387, "w": 492}, "492": {"e": 431}, "417": {"n": 387}, "409": {"e": 317}, "211": {"n": 138}, "117": {"n": 108, "s": 131, "e": 166, "w": 133}, "133": {"e": 117, "w": 173}, "173": {"e": 133, "w": 214}, "214": {"n": 194, "e": 173, "w": 226}, "194": {"s": 214, "w": 129}, "129": {"n": 126, "e": 194, "w": 170}, "170": {"e": 129}, "126": {"n": 98, "s": 129}, "98": {"n": 102, "s": 126, "e": 70, "w": 109}, "109": {"s": 185, "e": 98, "w": 175}, "175": {"s": 183, "e": 109, "w": 179}, "179": {"s": 233, "e": 175, "w": 213}, "213": {"e": 179, "w": 420}, "420": {"s": 444, "e": 213, "w": 437}, "437": {"e": 420, "w": 497}, "497": {"e": 437}, "444": {"n": 420, "w": 490}, "490": {"e": 444, "w": 493}, "493": {"e": 490}, "233": {"n": 179, "w": 238}, "238": {"e": 233}, "183": {"n": 175, "s": 229}, "229": {"n": 183, "s": 250, "w": 236}, "236": {"s": 264, "e": 229}, "264": {"n": 236, "s": 274, "w": 273}, "273": {"n": 343, "e": 264}, "343": {"s": 273, "w": 351}, "351": {"s": 491, "e": 343, "w": 478}, "478": {"e": 351}, "491": {"n": 351}, "274": {"n": 264, "w": 308}, "308": {"e": 274}, "250": {"n": 229, "s": 294, "e": 289}, "294": {"n": 250, "s": 334}, "334": {"n": 294, "s": 393, "e": 341, "w": 391}, "341": {"s": 449, "w": 334}, "449": {"n": 341}, "393": {"n": 334, "s": 482}, "482": {"n": 393}, "391": {"s": 396, "e": 334, "w": 428}, "396": {"n": 391}, "428": {"e": 391}, "289": {"w": 250}, "185": {"n": 109}, "102": {"s": 98, "w": 142}, "142": {"e": 102, "w": 159}, "159": {"e": 142, "w": 196}, "196": {"n": 222, "e": 159, "w": 197}, "197": {"n": 232, "e": 196, "w": 276}, "232": {"n": 272, "s": 197, "w": 235}, "235": {"n": 330, "e": 232, "w": 355}, "330": {"n": 369, "s": 235, "w": 383}, "383": {"e": 330, "w": 495}, "495": {"e": 383}, "369": {"n": 400, "s": 330, "w": 376}, "400": {"s": 369}, "376": {"e": 369}, "355": {"e": 235}, "272": {"n": 295, "s": 232}, "295": {"s": 272}, "276": {"e": 197, "w": 419}, "419": {"e": 276}, "222": {"n": 305, "s": 196}, "305": {"n": 365, "s": 222}, "365": {"s": 305}, "70": {"s": 163, "e": 60, "w": 98}, "60": {"n": 45, "e": 36, "w": 70}, "36": {"s": 48, "e": 22, "w": 60}, "22": {"n": 18, "s": 78, "w": 36}, "18": {"n": 12, "s": 22, "w": 25}, "21": {"e": 12, "w": 29}, "29": {"s": 45, "e": 21, "w": 49}, "49": {"s": 79, "e": 29, "w": 136}, "136": {"e": 49, "w": 148}, "148": {"e": 136, "w": 292}, "292": {"n": 301, "e": 148}, "301": {"n": 304, "s": 292}, "304": {"s": 301}, "79": {"n": 49}, "45": {"n": 29, "s": 60}, "48": {"n": 36, "s": 105, "w": 149}, "149": {"e": 48}, "105": {"n": 48, "w": 202}, "202": {"e": 105}, "78": {"n": 22, "s": 108}, "108": {"n": 78, "s": 117, "e": 93}, "93": {"n": 89, "w": 108}, "89": {"n": 50, "s": 93}, "75": {"e": 85, "w": 52}, "85": {"e": 154, "w": 75}, "154": {"e": 193, "w": 85}, "193": {"e": 251, "w": 154}, "251": {"e": 315, "w": 193}, "315": {"w": 251}, "112": {"s": 141, "e": 140, "w": 100}, "140": {"w": 112}, "141": {"n": 112, "e": 156}, "156": {"s": 168, "e": 164, "w": 141}, "168": {"n": 156, "e": 340}, "340": {"w": 168}, "164": {"n": 217, "e": 298, "w": 156}, "217": {"s": 164, "e": 247}, "247": {"e": 261, "w": 217}, "261": {"s": 277, "e": 322, "w": 247}, "322": {"n": 382, "e": 435, "w": 261}, "382": {"s": 322, "e": 388}, "388": {"e": 477, "w": 382}, "477": {"e": 483, "w": 388}, "483": {"w": 477}, "435": {"w": 322}, "277": {"n": 261, "e": 323}, "323": {"e": 433, "w": 277}, "433": {"s": 455, "e": 460, "w": 323}, "455": {"n": 433}, "460": {"w": 433}, "298": {"s": 324, "w": 164}, "324": {"n": 298, "s": 349, "e": 354}, "349": {"n": 324, "s": 352, "e": 384, "w": 356}, "356": {"e": 349}, "352": {"n": 349, "s": 362, "e": 485}, "485": {"w": 352}, "362": {"n": 352, "s": 399, "w": 463}, "399": {"n": 362, "s": 467}, "467": {"n": 399}, "463": {"s": 468, "e": 362}, "468": {"n": 463}, "384": {"w": 349}, "354": {"w": 324}, "25": {"e": 18}, "5": {"w": 3}, "6": {"n": 2, "w": 7}, "7": {"n": 8, "e": 6, "w": 56}, "8": {"s": 7, "w": 16}, "16": {"n": 58, "e": 8, "w": 67}, "58": {"s": 16, "w": 65}, "65": {"n": 74, "e": 58, "w": 139}, "139": {"e": 65, "w": 188}, "188": {"e": 139, "w": 335}, "335": {"e": 188, "w": 366}, "366": {"e": 335}, "74": {"n": 87, "s": 65, "w": 161}, "161": {"e": 74}, "87": {"s": 74}, "67": {"e": 16, "w": 162}, "162": {"e": 67}, "56": {"e": 7, "w": 61}, "61": {"e": 56, "w": 171}, "171": {"e": 61}, "10": {"n": 19, "s": 0, "w": 43}, "19": {"n": 20, "s": 10, "w": 77}, "20": {"n": 63, "s": 19, "e": 27, "w": 46}, "63": {"n": 72, "s": 20, "w": 73}, "72": {"s": 63, "w": 76}, "76": {"n": 83, "e": 72, "w": 110}, "110": {"e": 76}, "83": {"s": 76, "e": 130, "w": 125}, "125": {"n": 165, "e": 83, "w": 237}, "165": {"n": 203, "s": 125, "w": 204}, "203": {"n": 268, "s": 165, "e": 299}, "268": {"s": 203, "e": 411, "w": 312}, "411": {"w": 268}, "312": {"n": 328, "e": 268}, "328": {"n": 332, "s": 312, "e": 357, "w": 363}, "363": {"n": 372, "e": 328}, "372": {"n": 441, "s": 363}, "441": {"s": 372}, "332": {"n": 350, "s": 328}, "350": {"n": 436, "s": 332, "e": 404}, "436": {"s": 350}, "404": {"n": 481, "w": 350}, "481": {"s": 404}, "357": {"w": 328}, "299": {"e": 311, "w": 203}, "311": {"w": 299}, "204": {"n": 219, "e": 165, "w": 216}, "219": {"s": 204}, "216": {"n": 234, "e": 204, "w": 218}, "234": {"n": 368, "s": 216, "w": 252}, "252": {"n": 284, "e": 234}, "284": {"n": 302, "s": 252, "w": 303}, "303": {"n": 361, "e": 284, "w": 405}, "405": {"n": 406, "e": 303}, "406": {"s": 405, "w": 415}, "415": {"e": 406, "w": 418}, "418": {"n": 425, "s": 474, "e": 415}, "425": {"s": 418, "w": 469}, "469": {"e": 425}, "474": {"n": 418}, "361": {"n": 408, "s": 303}, "408": {"n": 458, "s": 361, "w": 423}, "423": {"e": 408, "w": 454}, "454": {"n": 470, "e": 423}, "470": {"s": 454}, "458": {"s": 408, "w": 459}, "459": {"e": 458}, "302": {"n": 422, "s": 284}, "422": {"n": 426, "s": 302}, "426": {"n": 457, "s": 422}, "457": {"n": 461, "s": 426}, "461": {"s": 457}, "368": {"s": 234}, "218": {"s": 263, "e": 216, "w": 242}, "242": {"n": 287, "s": 259, "e": 218, "w": 275}, "287": {"s": 242, "w": 339}, "339": {"e": 287, "w": 445}, "445": {"n": 447, "e": 339, "w": 450}, "450": {"e": 445}, "447": {"s": 445}, "275": {"e": 242, "w": 456}, "456": {"e": 275, "w": 499}, "499": {"e": 456}, "259": {"n": 242, "w": 310}, "310": {"e": 259, "w": 412}, "412": {"s": 488, "e": 310}, "488": {"n": 412}, "263": {"n": 218}, "237": {"e": 125, "w": 245}, "245": {"s": 254, "e": 237}, "254": {"n": 245, "w": 314}, "314": {"e": 254}, "130": {"w": 83}, "73": {"e": 63}, "27": {"n": 40, "s": 28, "e": 30, "w": 20}, "40": {"s": 27}, "28": {"n": 27}, "30": {"s": 31, "e": 32, "w": 27}, "32": {"n": 39, "e": 54, "w": 30}, "39": {"n": 53, "s": 32, "e": 51, "w": 41}, "53": {"n": 95, "s": 39, "w": 88}, "88": {"e": 53, "w": 122}, "122": {"n": 124, "e": 88}, "124": {"n": 157, "s": 122}, "157": {"n": 210, "s": 124, "w": 182}, "210": {"s": 157}, "182": {"e": 157, "w": 208}, "208": {"e": 182}, "95": {"n": 119, "s": 53, "w": 115}, "115": {"n": 116, "e": 95}, "116": {"n": 132, "s": 115}, "132": {"s": 116}, "119": {"n": 134, "s": 95}, "134": {"n": 147, "s": 119, "e": 144}, "144": {"e": 155, "w": 134}, "155": {"s": 187, "e": 316, "w": 144}, "187": {"n": 155}, "316": {"n": 344, "w": 155}, "344": {"n": 392, "s": 316, "e": 390}, "390": {"w": 344}, "392": {"s": 344, "e": 462}, "462": {"w": 392}, "147": {"n": 200, "s": 134, "e": 153, "w": 151}, "153": {"e": 329, "w": 147}, "329": {"w": 153}, "151": {"n": 172, "e": 147, "w": 207}, "207": {"n": 231, "e": 151, "w": 290}, "231": {"s": 207, "w": 248}, "248": {"n": 296, "e": 231, "w": 280}, "280": {"n": 325, "e": 248}, "325": {"n": 353, "s": 280, "w": 374}, "374": {"e": 325}, "353": {"s": 325}, "296": {"s": 248}, "290": {"e": 207}, "172": {"n": 267, "s": 151}, "267": {"n": 285, "s": 172, "w": 271}, "285": {"n": 286, "s": 267}, "286": {"n": 336, "s": 285, "w": 291}, "336": {"s": 286}, "291": {"n": 410, "e": 286, "w": 347}, "347": {"n": 452, "s": 442, "e": 291}, "452": {"s": 347}, "442": {"n": 347}, "410": {"s": 291}, "271": {"n": 337, "e": 267}, "337": {"s": 271}, "200": {"n": 227, "s": 147, "e": 206}, "227": {"n": 269, "s": 200}, "269": {"n": 319, "s": 227}, "319": {"n": 359, "s": 269, "e": 345}, "359": {"s": 319}, "345": {"s": 375, "w": 319}, "375": {"n": 345, "e": 385}, "385": {"w": 375}, "206": {"n": 288, "e": 380, "w": 200}, "288": {"s": 206}, "380": {"n": 424, "w": 206}, "424": {"s": 380, "e": 473}, "473": {"e": 494, "w": 424}, "494": {"w": 473}, "51": {"n": 69, "e": 57, "w": 39}, "57": {"e": 145, "w": 51}, "145": {"n": 174, "e": 220, "w": 57}, "220": {"w": 145}, "174": {"n": 192, "s": 145, "e": 224}, "224": {"w": 174}, "192": {"n": 201, "s": 174, "e": 223}, "201": {"s": 192}, "223": {"n": 283, "w": 192}, "283": {"n": 331, "s": 223, "e": 313}, "331": {"s": 283, "e": 446}, "446": {"e": 466, "w": 331}, "466": {"s": 486, "e": 472, "w": 446}, "486": {"n": 466}, "472": {"w": 466}, "313": {"w": 283}, "69": {"n": 94, "s": 51, "e": 103}, "94": {"n": 152, "s": 69}, "152": {"s": 94}, "103": {"n": 160, "w": 69}, "160": {"s": 103}, "41": {"e": 39}, "54": {"w": 32}, "31": {"n": 30, "e": 33}, "33": {"e": 38, "w": 31}, "38": {"s": 59, "e": 66, "w": 33}, "59": {"n": 38, "s": 104, "e": 92}, "92": {"w": 59}, "104": {"n": 59, "e": 107}, "107": {"s": 120, "e": 121, "w": 104}, "120": {"n": 107, "e": 127}, "127": {"e": 184, "w": 120}, "184": {"e": 221, "w": 127}, "221": {"s": 253, "e": 240, "w": 184}, "240": {"n": 249, "e": 386, "w": 221}, "249": {"n": 265, "s": 240, "e": 282}, "282": {"w": 249}, "265": {"n": 279, "s": 249, "e": 270}, "279": {"s": 265}, "270": {"n": 416, "e": 338, "w": 265}, "338": {"s": 379, "w": 270}, "379": {"n": 338, "e": 395}, "395": {"s": 403, "e": 421, "w": 379}, "421": {"n": 440, "w": 395}, "440": {"s": 421, "w": 476}, "476": {"e": 440}, "403": {"n": 395}, "416": {"s": 270}, "386": {"e": 414, "w": 240}, "414": {"w": 386}, "253": {"n": 221, "e": 258}, "258": {"e": 306, "w": 253}, "306": {"e": 397, "w": 258}, "397": {"w": 306}, "121": {"n": 128, "e": 143, "w": 107}, "143": {"e": 212, "w": 121}, "212": {"w": 143}, "128": {"s": 121, "e": 189}, "189": {"e": 255, "w": 128}, "255": {"w": 189}, "66": {"n": 169, "e": 123, "w": 38}, "169": {"s": 66, "e": 186}, "186": {"e": 205, "w": 169}, "205": {"s": 241, "e": 479, "w": 186}, "479": {"w": 205}, "241": {"n": 205, "e": 266}, "266": {"w": 241}, "123": {"w": 66}, "46": {"e": 20, "w": 62}, "62": {"n": 64, "e": 46, "w": 84}, "84": {"e": 62, "w": 91}, "91": {"n": 180, "s": 101, "e": 84, "w": 99}, "180": {"s": 91}, "99": {"n": 190, "e": 91, "w": 146}, "190": {"s": 99}, "146": {"n": 215, "s": 177, "e": 99, "w": 257}, "257": {"n": 320, "e": 146, "w": 364}, "364": {"n": 429, "s": 381, "e": 257, "w": 448}, "429": {"s": 364}, "448": {"e": 364}, "381": {"n": 364, "w": 394}, "394": {"e": 381}, "320": {"n": 348, "s": 257}, "348": {"s": 320}, "177": {"n": 146, "w": 346}, "346": {"e": 177}, "215": {"n": 246, "s": 146}, "246": {"s": 215}, "101": {"n": 91, "w": 113}, "113": {"s": 114, "e": 101}, "114": {"n": 113, "w": 176}, "176": {"e": 114, "w": 402}, "402": {"e": 176, "w": 451}, "451": {"e": 402, "w": 453}, "453": {"s": 464, "e": 451}, "464": {"n": 453}, "64": {"s": 62, "w": 82}, "82": {"n": 191, "e": 64}, "191": {"s": 82}, "77": {"e": 19}, "43": {"e": 10, "w": 47}, "47": {"n": 71, "e": 43}, "71": {"s": 47}, "4": {"n": 23, "e": 13, "w": 0}, "13": {"e": 15, "w": 4}, "15": {"w": 13}, "23": {"s": 4, "e": 26}, "26": {"e": 55, "w": 23}, "55": {"w": 26}, "163": {"n": 70}, "226": {"s": 300, "e": 214}, "300": {"n": 226, "s": 377, "w": 389}, "389": {"e": 300}, "377": {"n": 300}, "199": {"s": 230, "w": 198}, "230": {"n": 199, "s": 307, "e": 297}, "307": {"n": 230, "s": 373, "e": 371, "w": 321}, "373": {"n": 307, "s": 480}, "480": {"n": 373}, "371": {"s": 475, "w": 307}, "475": {"n": 371, "s": 484}, "484": {"n": 475}, "321": {"s": 413, "e": 307}, "413": {"n": 321}, "297": {"w": 230}} +print(len(my_dict), "LENGTH") diff --git a/map.txt b/map.txt index caf2ca42..24986eb4 100644 --- a/map.txt +++ b/map.txt @@ -1 +1 @@ -{"59": {"n": 38, "s": "?", "e": "?"}, "38": {"s": 59, "e": "?", "w": 33}, "33": {"e": 38, "w": 31}, "31": {"n": 30, "e": 33}, "30": {"s": 31, "e": 32, "w": "?"}, "32": {"n": "?", "e": "?", "w": 30}} \ No newline at end of file +{"0": {"n": 10, "s": 2, "e": 4, "w": 1}, "1": {"e": 0}, "2": {"n": 0, "s": 6, "e": 3}, "3": {"s": 9, "e": 5, "w": 2}, "9": {"n": 3, "s": 12, "e": 11}, "11": {"e": 17, "w": 9}, "17": {"n": 24, "e": 42, "w": 11}, "24": {"s": 17}, "42": {"n": 44, "s": 80, "e": 118, "w": 17}, "44": {"s": 42}, "80": {"n": 42, "s": 81, "e": 86}, "81": {"n": 80}, "86": {"s": 96, "e": 90, "w": 80}, "90": {"e": 178, "w": 86}, "178": {"n": 209, "e": 243, "w": 90}, "243": {"s": 293, "e": 256, "w": 178}, "293": {"n": 243}, "256": {"s": 360, "e": 327, "w": 243}, "327": {"e": 427, "w": 256}, "427": {"e": 430, "w": 327}, "430": {"n": 443, "e": 439, "w": 427}, "443": {"s": 430, "e": 471}, "471": {"w": 443}, "439": {"w": 430}, "360": {"n": 256, "e": 398}, "398": {"e": 438, "w": 360}, "438": {"e": 465, "w": 398}, "465": {"e": 498, "w": 438}, "498": {"w": 465}, "209": {"s": 178}, "96": {"n": 86, "e": 97}, "97": {"e": 181, "w": 96}, "181": {"w": 97}, "118": {"e": 137, "w": 42}, "137": {"w": 118}, "12": {"n": 9, "s": 18, "e": 14, "w": 21}, "14": {"s": 34, "e": 37, "w": 12}, "37": {"w": 14}, "34": {"n": 14, "s": 50, "e": 35}, "50": {"n": 34, "s": 89}, "35": {"s": 52, "w": 34}, "52": {"n": 35, "s": 68, "e": 75}, "68": {"n": 52, "e": 100}, "100": {"s": 106, "e": 112, "w": 68}, "106": {"n": 100, "s": 111, "w": 135}, "111": {"n": 106, "s": 367, "e": 158}, "367": {"n": 111}, "158": {"s": 167, "w": 111}, "167": {"n": 158, "s": 262, "e": 260}, "260": {"w": 167}, "262": {"n": 167, "s": 370, "e": 358}, "358": {"e": 401, "w": 262}, "401": {"w": 358}, "370": {"n": 262, "s": 434, "e": 407}, "434": {"n": 370}, "407": {"s": 496, "w": 370}, "496": {"n": 407}, "135": {"s": 150, "e": 106}, "150": {"n": 135, "w": 166}, "166": {"s": 198, "e": 150, "w": 117}, "198": {"n": 166, "s": 239, "e": 199}, "239": {"n": 198, "w": 244}, "244": {"n": 131, "e": 239}, "131": {"n": 117, "s": 244, "w": 138}, "138": {"s": 211, "e": 131, "w": 195}, "195": {"s": 228, "e": 138, "w": 225}, "225": {"s": 278, "e": 195}, "278": {"n": 225}, "228": {"n": 195, "s": 281}, "281": {"n": 228, "s": 318, "e": 309, "w": 317}, "318": {"n": 281, "s": 487}, "487": {"n": 318, "s": 489}, "489": {"n": 487}, "309": {"s": 333, "e": 326, "w": 281}, "333": {"n": 309, "s": 378}, "378": {"n": 333}, "326": {"s": 342, "w": 309}, "342": {"n": 326, "s": 432}, "432": {"n": 342}, "317": {"s": 387, "e": 281, "w": 409}, "387": {"n": 317, "s": 417, "w": 431}, "431": {"e": 387, "w": 492}, "492": {"e": 431}, "417": {"n": 387}, "409": {"e": 317}, "211": {"n": 138}, "117": {"n": 108, "s": 131, "e": 166, "w": 133}, "133": {"e": 117, "w": 173}, "173": {"e": 133, "w": 214}, "214": {"n": 194, "e": 173, "w": 226}, "194": {"s": 214, "w": 129}, "129": {"n": 126, "e": 194, "w": 170}, "170": {"e": 129}, "126": {"n": 98, "s": 129}, "98": {"n": 102, "s": 126, "e": 70, "w": 109}, "109": {"s": 185, "e": 98, "w": 175}, "175": {"s": 183, "e": 109, "w": 179}, "179": {"s": 233, "e": 175, "w": 213}, "213": {"e": 179, "w": 420}, "420": {"s": 444, "e": 213, "w": 437}, "437": {"e": 420, "w": 497}, "497": {"e": 437}, "444": {"n": 420, "w": 490}, "490": {"e": 444, "w": 493}, "493": {"e": 490}, "233": {"n": 179, "w": 238}, "238": {"e": 233}, "183": {"n": 175, "s": 229}, "229": {"n": 183, "s": 250, "w": 236}, "236": {"s": 264, "e": 229}, "264": {"n": 236, "s": 274, "w": 273}, "273": {"n": 343, "e": 264}, "343": {"s": 273, "w": 351}, "351": {"s": 491, "e": 343, "w": 478}, "478": {"e": 351}, "491": {"n": 351}, "274": {"n": 264, "w": 308}, "308": {"e": 274}, "250": {"n": 229, "s": 294, "e": 289}, "294": {"n": 250, "s": 334}, "334": {"n": 294, "s": 393, "e": 341, "w": 391}, "341": {"s": 449, "w": 334}, "449": {"n": 341}, "393": {"n": 334, "s": 482}, "482": {"n": 393}, "391": {"s": 396, "e": 334, "w": 428}, "396": {"n": 391}, "428": {"e": 391}, "289": {"w": 250}, "185": {"n": 109}, "102": {"s": 98, "w": 142}, "142": {"e": 102, "w": 159}, "159": {"e": 142, "w": 196}, "196": {"n": 222, "e": 159, "w": 197}, "197": {"n": 232, "e": 196, "w": 276}, "232": {"n": 272, "s": 197, "w": 235}, "235": {"n": 330, "e": 232, "w": 355}, "330": {"n": 369, "s": 235, "w": 383}, "383": {"e": 330, "w": 495}, "495": {"e": 383}, "369": {"n": 400, "s": 330, "w": 376}, "400": {"s": 369}, "376": {"e": 369}, "355": {"e": 235}, "272": {"n": 295, "s": 232}, "295": {"s": 272}, "276": {"e": 197, "w": 419}, "419": {"e": 276}, "222": {"n": 305, "s": 196}, "305": {"n": 365, "s": 222}, "365": {"s": 305}, "70": {"s": 163, "e": 60, "w": 98}, "60": {"n": 45, "e": 36, "w": 70}, "36": {"s": 48, "e": 22, "w": 60}, "22": {"n": 18, "s": 78, "w": 36}, "18": {"n": 12, "s": 22, "w": 25}, "21": {"e": 12, "w": 29}, "29": {"s": 45, "e": 21, "w": 49}, "49": {"s": 79, "e": 29, "w": 136}, "136": {"e": 49, "w": 148}, "148": {"e": 136, "w": 292}, "292": {"n": 301, "e": 148}, "301": {"n": 304, "s": 292}, "304": {"s": 301}, "79": {"n": 49}, "45": {"n": 29, "s": 60}, "48": {"n": 36, "s": 105, "w": 149}, "149": {"e": 48}, "105": {"n": 48, "w": 202}, "202": {"e": 105}, "78": {"n": 22, "s": 108}, "108": {"n": 78, "s": 117, "e": 93}, "93": {"n": 89, "w": 108}, "89": {"n": 50, "s": 93}, "75": {"e": 85, "w": 52}, "85": {"e": 154, "w": 75}, "154": {"e": 193, "w": 85}, "193": {"e": 251, "w": 154}, "251": {"e": 315, "w": 193}, "315": {"w": 251}, "112": {"s": 141, "e": 140, "w": 100}, "140": {"w": 112}, "141": {"n": 112, "e": 156}, "156": {"s": 168, "e": 164, "w": 141}, "168": {"n": 156, "e": 340}, "340": {"w": 168}, "164": {"n": 217, "e": 298, "w": 156}, "217": {"s": 164, "e": 247}, "247": {"e": 261, "w": 217}, "261": {"s": 277, "e": 322, "w": 247}, "322": {"n": 382, "e": 435, "w": 261}, "382": {"s": 322, "e": 388}, "388": {"e": 477, "w": 382}, "477": {"e": 483, "w": 388}, "483": {"w": 477}, "435": {"w": 322}, "277": {"n": 261, "e": 323}, "323": {"e": 433, "w": 277}, "433": {"s": 455, "e": 460, "w": 323}, "455": {"n": 433}, "460": {"w": 433}, "298": {"s": 324, "w": 164}, "324": {"n": 298, "s": 349, "e": 354}, "349": {"n": 324, "s": 352, "e": 384, "w": 356}, "356": {"e": 349}, "352": {"n": 349, "s": 362, "e": 485}, "485": {"w": 352}, "362": {"n": 352, "s": 399, "w": 463}, "399": {"n": 362, "s": 467}, "467": {"n": 399}, "463": {"s": 468, "e": 362}, "468": {"n": 463}, "384": {"w": 349}, "354": {"w": 324}, "25": {"e": 18}, "5": {"w": 3}, "6": {"n": 2, "w": 7}, "7": {"n": 8, "e": 6, "w": 56}, "8": {"s": 7, "w": 16}, "16": {"n": 58, "e": 8, "w": 67}, "58": {"s": 16, "w": 65}, "65": {"n": 74, "e": 58, "w": 139}, "139": {"e": 65, "w": 188}, "188": {"e": 139, "w": 335}, "335": {"e": 188, "w": 366}, "366": {"e": 335}, "74": {"n": 87, "s": 65, "w": 161}, "161": {"e": 74}, "87": {"s": 74}, "67": {"e": 16, "w": 162}, "162": {"e": 67}, "56": {"e": 7, "w": 61}, "61": {"e": 56, "w": 171}, "171": {"e": 61}, "10": {"n": 19, "s": 0, "w": 43}, "19": {"n": 20, "s": 10, "w": 77}, "20": {"n": 63, "s": 19, "e": 27, "w": 46}, "63": {"n": 72, "s": 20, "w": 73}, "72": {"s": 63, "w": 76}, "76": {"n": 83, "e": 72, "w": 110}, "110": {"e": 76}, "83": {"s": 76, "e": 130, "w": 125}, "125": {"n": 165, "e": 83, "w": 237}, "165": {"n": 203, "s": 125, "w": 204}, "203": {"n": 268, "s": 165, "e": 299}, "268": {"s": 203, "e": 411, "w": 312}, "411": {"w": 268}, "312": {"n": 328, "e": 268}, "328": {"n": 332, "s": 312, "e": 357, "w": 363}, "363": {"n": 372, "e": 328}, "372": {"n": 441, "s": 363}, "441": {"s": 372}, "332": {"n": 350, "s": 328}, "350": {"n": 436, "s": 332, "e": 404}, "436": {"s": 350}, "404": {"n": 481, "w": 350}, "481": {"s": 404}, "357": {"w": 328}, "299": {"e": 311, "w": 203}, "311": {"w": 299}, "204": {"n": 219, "e": 165, "w": 216}, "219": {"s": 204}, "216": {"n": 234, "e": 204, "w": 218}, "234": {"n": 368, "s": 216, "w": 252}, "252": {"n": 284, "e": 234}, "284": {"n": 302, "s": 252, "w": 303}, "303": {"n": 361, "e": 284, "w": 405}, "405": {"n": 406, "e": 303}, "406": {"s": 405, "w": 415}, "415": {"e": 406, "w": 418}, "418": {"n": 425, "s": 474, "e": 415}, "425": {"s": 418, "w": 469}, "469": {"e": 425}, "474": {"n": 418}, "361": {"n": 408, "s": 303}, "408": {"n": 458, "s": 361, "w": 423}, "423": {"e": 408, "w": 454}, "454": {"n": 470, "e": 423}, "470": {"s": 454}, "458": {"s": 408, "w": 459}, "459": {"e": 458}, "302": {"n": 422, "s": 284}, "422": {"n": 426, "s": 302}, "426": {"n": 457, "s": 422}, "457": {"n": 461, "s": 426}, "461": {"s": 457}, "368": {"s": 234}, "218": {"s": 263, "e": 216, "w": 242}, "242": {"n": 287, "s": 259, "e": 218, "w": 275}, "287": {"s": 242, "w": 339}, "339": {"e": 287, "w": 445}, "445": {"n": 447, "e": 339, "w": 450}, "450": {"e": 445}, "447": {"s": 445}, "275": {"e": 242, "w": 456}, "456": {"e": 275, "w": 499}, "499": {"e": 456}, "259": {"n": 242, "w": 310}, "310": {"e": 259, "w": 412}, "412": {"s": 488, "e": 310}, "488": {"n": 412}, "263": {"n": 218}, "237": {"e": 125, "w": 245}, "245": {"s": 254, "e": 237}, "254": {"n": 245, "w": 314}, "314": {"e": 254}, "130": {"w": 83}, "73": {"e": 63}, "27": {"n": 40, "s": 28, "e": 30, "w": 20}, "40": {"s": 27}, "28": {"n": 27}, "30": {"s": 31, "e": 32, "w": 27}, "32": {"n": 39, "e": 54, "w": 30}, "39": {"n": 53, "s": 32, "e": 51, "w": 41}, "53": {"n": 95, "s": 39, "w": 88}, "88": {"e": 53, "w": 122}, "122": {"n": 124, "e": 88}, "124": {"n": 157, "s": 122}, "157": {"n": 210, "s": 124, "w": 182}, "210": {"s": 157}, "182": {"e": 157, "w": 208}, "208": {"e": 182}, "95": {"n": 119, "s": 53, "w": 115}, "115": {"n": 116, "e": 95}, "116": {"n": 132, "s": 115}, "132": {"s": 116}, "119": {"n": 134, "s": 95}, "134": {"n": 147, "s": 119, "e": 144}, "144": {"e": 155, "w": 134}, "155": {"s": 187, "e": 316, "w": 144}, "187": {"n": 155}, "316": {"n": 344, "w": 155}, "344": {"n": 392, "s": 316, "e": 390}, "390": {"w": 344}, "392": {"s": 344, "e": 462}, "462": {"w": 392}, "147": {"n": 200, "s": 134, "e": 153, "w": 151}, "153": {"e": 329, "w": 147}, "329": {"w": 153}, "151": {"n": 172, "e": 147, "w": 207}, "207": {"n": 231, "e": 151, "w": 290}, "231": {"s": 207, "w": 248}, "248": {"n": 296, "e": 231, "w": 280}, "280": {"n": 325, "e": 248}, "325": {"n": 353, "s": 280, "w": 374}, "374": {"e": 325}, "353": {"s": 325}, "296": {"s": 248}, "290": {"e": 207}, "172": {"n": 267, "s": 151}, "267": {"n": 285, "s": 172, "w": 271}, "285": {"n": 286, "s": 267}, "286": {"n": 336, "s": 285, "w": 291}, "336": {"s": 286}, "291": {"n": 410, "e": 286, "w": 347}, "347": {"n": 452, "s": 442, "e": 291}, "452": {"s": 347}, "442": {"n": 347}, "410": {"s": 291}, "271": {"n": 337, "e": 267}, "337": {"s": 271}, "200": {"n": 227, "s": 147, "e": 206}, "227": {"n": 269, "s": 200}, "269": {"n": 319, "s": 227}, "319": {"n": 359, "s": 269, "e": 345}, "359": {"s": 319}, "345": {"s": 375, "w": 319}, "375": {"n": 345, "e": 385}, "385": {"w": 375}, "206": {"n": 288, "e": 380, "w": 200}, "288": {"s": 206}, "380": {"n": 424, "w": 206}, "424": {"s": 380, "e": 473}, "473": {"e": 494, "w": 424}, "494": {"w": 473}, "51": {"n": 69, "e": 57, "w": 39}, "57": {"e": 145, "w": 51}, "145": {"n": 174, "e": 220, "w": 57}, "220": {"w": 145}, "174": {"n": 192, "s": 145, "e": 224}, "224": {"w": 174}, "192": {"n": 201, "s": 174, "e": 223}, "201": {"s": 192}, "223": {"n": 283, "w": 192}, "283": {"n": 331, "s": 223, "e": 313}, "331": {"s": 283, "e": 446}, "446": {"e": 466, "w": 331}, "466": {"s": 486, "e": 472, "w": 446}, "486": {"n": 466}, "472": {"w": 466}, "313": {"w": 283}, "69": {"n": 94, "s": 51, "e": 103}, "94": {"n": 152, "s": 69}, "152": {"s": 94}, "103": {"n": 160, "w": 69}, "160": {"s": 103}, "41": {"e": 39}, "54": {"w": 32}, "31": {"n": 30, "e": 33}, "33": {"e": 38, "w": 31}, "38": {"s": 59, "e": 66, "w": 33}, "59": {"n": 38, "s": 104, "e": 92}, "92": {"w": 59}, "104": {"n": 59, "e": 107}, "107": {"s": 120, "e": 121, "w": 104}, "120": {"n": 107, "e": 127}, "127": {"e": 184, "w": 120}, "184": {"e": 221, "w": 127}, "221": {"s": 253, "e": 240, "w": 184}, "240": {"n": 249, "e": 386, "w": 221}, "249": {"n": 265, "s": 240, "e": 282}, "282": {"w": 249}, "265": {"n": 279, "s": 249, "e": 270}, "279": {"s": 265}, "270": {"n": 416, "e": 338, "w": 265}, "338": {"s": 379, "w": 270}, "379": {"n": 338, "e": 395}, "395": {"s": 403, "e": 421, "w": 379}, "421": {"n": 440, "w": 395}, "440": {"s": 421, "w": 476}, "476": {"e": 440}, "403": {"n": 395}, "416": {"s": 270}, "386": {"e": 414, "w": 240}, "414": {"w": 386}, "253": {"n": 221, "e": 258}, "258": {"e": 306, "w": 253}, "306": {"e": 397, "w": 258}, "397": {"w": 306}, "121": {"n": 128, "e": 143, "w": 107}, "143": {"e": 212, "w": 121}, "212": {"w": 143}, "128": {"s": 121, "e": 189}, "189": {"e": 255, "w": 128}, "255": {"w": 189}, "66": {"n": 169, "e": 123, "w": 38}, "169": {"s": 66, "e": 186}, "186": {"e": 205, "w": 169}, "205": {"s": 241, "e": 479, "w": 186}, "479": {"w": 205}, "241": {"n": 205, "e": 266}, "266": {"w": 241}, "123": {"w": 66}, "46": {"e": 20, "w": 62}, "62": {"n": 64, "e": 46, "w": 84}, "84": {"e": 62, "w": 91}, "91": {"n": 180, "s": 101, "e": 84, "w": 99}, "180": {"s": 91}, "99": {"n": 190, "e": 91, "w": 146}, "190": {"s": 99}, "146": {"n": 215, "s": 177, "e": 99, "w": 257}, "257": {"n": 320, "e": 146, "w": 364}, "364": {"n": 429, "s": 381, "e": 257, "w": 448}, "429": {"s": 364}, "448": {"e": 364}, "381": {"n": 364, "w": 394}, "394": {"e": 381}, "320": {"n": 348, "s": 257}, "348": {"s": 320}, "177": {"n": 146, "w": 346}, "346": {"e": 177}, "215": {"n": 246, "s": 146}, "246": {"s": 215}, "101": {"n": 91, "w": 113}, "113": {"s": 114, "e": 101}, "114": {"n": 113, "w": 176}, "176": {"e": 114, "w": 402}, "402": {"e": 176, "w": 451}, "451": {"e": 402, "w": 453}, "453": {"s": 464, "e": 451}, "464": {"n": 453}, "64": {"s": 62, "w": 82}, "82": {"n": 191, "e": 64}, "191": {"s": 82}, "77": {"e": 19}, "43": {"e": 10, "w": 47}, "47": {"n": 71, "e": 43}, "71": {"s": 47}, "4": {"n": 23, "e": 13, "w": 0}, "13": {"e": 15, "w": 4}, "15": {"w": 13}, "23": {"s": 4, "e": 26}, "26": {"e": 55, "w": 23}, "55": {"w": 26}, "163": {"n": 70}, "226": {"s": 300, "e": 214}, "300": {"n": 226, "s": 377, "w": 389}, "389": {"e": 300}, "377": {"n": 300}, "199": {"s": 230, "w": 198}, "230": {"n": 199, "s": 307, "e": 297}, "307": {"n": 230, "s": 373, "e": 371, "w": 321}, "373": {"n": 307, "s": 480}, "480": {"n": 373}, "371": {"s": 475, "w": 307}, "475": {"n": 371, "s": 484}, "484": {"n": 475}, "321": {"s": 413, "e": 307}, "413": {"n": 321}, "297": {"w": 230}} \ No newline at end of file From 7d30eeb0ae95c661da3ebe41cf391463e274e754 Mon Sep 17 00:00:00 2001 From: micahjones13 Date: Tue, 19 Nov 2019 19:15:02 -0700 Subject: [PATCH 17/32] can now find and sell treasure --- basic_utils.py | 151 ++++++++------ explore.py | 9 +- utils.py | 543 +++++++++++++++++++++++++++++++------------------ 3 files changed, 431 insertions(+), 272 deletions(-) diff --git a/basic_utils.py b/basic_utils.py index 277c3b5c..5276f57f 100644 --- a/basic_utils.py +++ b/basic_utils.py @@ -1,48 +1,62 @@ class Queue(): def __init__(self): self.queue = [] + + def __repr__(self): + return f'{self.queue}' + def enqueue(self, value): self.queue.append(value) + def dequeue(self): if self.size() > 0: return self.queue.pop(0) else: return None + def size(self): return len(self.queue) + class Stack(): def __init__(self): self.stack = [] + def push(self, value): self.stack.append(value) + def pop(self): if self.size() > 0: return self.stack.pop() else: return None + def size(self): return len(self.stack) + class Graph: """Represent a graph as a dictionary of vertices mapping labels to edges.""" + def __init__(self): self.vertices = {} + def add_vertex(self, vertex): """ Add a vertex to the graph. """ if vertex not in self.vertices.keys(): - self.vertices[vertex] = set() + self.vertices[vertex] = set() else: - pass + pass + def add_edge(self, v1, v2): """ Add a directed edge to the graph. """ if v1 and v2 in self.vertices.keys(): - #self.vertices[v2].add(v1) - self.vertices[v1].add(v2) + # self.vertices[v2].add(v1) + self.vertices[v1].add(v2) def bft(self, starting_vertex): """ @@ -51,23 +65,21 @@ def bft(self, starting_vertex): """ ret_list = [] if starting_vertex is None: - return None + return None my_q = Queue() visited = [starting_vertex] my_q.enqueue(starting_vertex) while len(my_q.queue) > 0: - point = my_q.queue[0] - joins = self.vertices[point] - for j in joins: - if j not in visited: - my_q.enqueue(j) - visited.append(j) - #print(my_q.dequeue()) - ret = my_q.dequeue() - ret_list.append(ret) + point = my_q.queue[0] + joins = self.vertices[point] + for j in joins: + if j not in visited: + my_q.enqueue(j) + visited.append(j) + # print(my_q.dequeue()) + ret = my_q.dequeue() + ret_list.append(ret) return ret_list - - def dft(self, starting_vertex, chooser=None): """ @@ -76,30 +88,29 @@ def dft(self, starting_vertex, chooser=None): """ ret_list = [] if starting_vertex is None: - return None + return None my_s = Stack() visited = [starting_vertex] my_s.push(starting_vertex) while len(my_s.stack) > 0: - point = my_s.stack[-1] - joins = self.vertices[point] - r = my_s.pop() ##new code - ret_list.append(r) ##new code - #print(r) ##changed to r from pop - if chooser is None: - pass - elif chooser == 'random': - joins = random.sample(joins,len(joins)) - elif chooser == 'shortest': - joins = find_longest_clique(point,self,visited) - for j in joins: - if j not in visited: - my_s.push(j) - visited.append(j) + point = my_s.stack[-1] + joins = self.vertices[point] + r = my_s.pop() # new code + ret_list.append(r) # new code + # print(r) ##changed to r from pop + if chooser is None: + pass + elif chooser == 'random': + joins = random.sample(joins, len(joins)) + elif chooser == 'shortest': + joins = find_longest_clique(point, self, visited) + for j in joins: + if j not in visited: + my_s.push(j) + visited.append(j) return ret_list - - def dft_recursive(self, starting_vertex, visited = []): + def dft_recursive(self, starting_vertex, visited=[]): """ Print each vertex in depth-first order beginning from starting_vertex. @@ -109,14 +120,12 @@ def dft_recursive(self, starting_vertex, visited = []): visited.append(starting_vertex) joins = self.vertices[starting_vertex] if joins is None: - return None + return None for j in joins: - if j in visited: - pass - else: - self.dft_recursive(j,visited) - - + if j in visited: + pass + else: + self.dft_recursive(j, visited) def bfs(self, starting_vertex, destination_vertex): """ @@ -124,23 +133,31 @@ def bfs(self, starting_vertex, destination_vertex): starting_vertex to destination_vertex in breath-first order. """ + print('Starting BFS') q = Queue() + visited = set() q.enqueue([starting_vertex]) - + print(f'Starting vertex: {starting_vertex}') + print(f'End Room: {destination_vertex}') + while destination_vertex not in q.queue[0]: - current_point = q.queue[0][-1] - joins = self.vertices[current_point] - for j in joins: - _ = [x for x in q.queue[0]] - _.append(j) - q.enqueue(_) - q.dequeue() + # while q.queue[0][-1] != destination_vertex: + # print(q) + current_point = q.queue[0][-1] + # print(f'current point: {current_point}') + joins = self.vertices[current_point].values() + # print(joins) + for j in joins: + # print(f'J: {j}') + if j != '?' and j not in visited: + visited.add(j) + _ = [x for x in q.queue[0]] + _.append(j) + q.enqueue(_) + q.dequeue() return q.queue[0] - - - def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from @@ -149,21 +166,21 @@ def dfs(self, starting_vertex, destination_vertex): """ s = Stack() s.push([starting_vertex]) - + while destination_vertex not in s.stack[-1]: - current_point = s.stack[-1][-1] - - joins = self.vertices[current_point] - if joins is None: - s.pop() - else: - temp_list = [] - for j in joins: - _ = [x for x in s.stack[-1]] - _.append(j) - temp_list.append(_) - for tl in temp_list: - s.push(tl) - #s.pop() + current_point = s.stack[-1][-1] + + joins = self.vertices[current_point] + if joins is None: + s.pop() + else: + temp_list = [] + for j in joins: + _ = [x for x in s.stack[-1]] + _.append(j) + temp_list.append(_) + for tl in temp_list: + s.push(tl) + # s.pop() return s.stack[-1] diff --git a/explore.py b/explore.py index 1cd79ac2..38f80856 100644 --- a/explore.py +++ b/explore.py @@ -1,8 +1,11 @@ from utils import * mappy = mapper() -# mappy.accumulate=False #set pick up items to false -# mappy.import_text_map = False +mappy.accumulate = True # set pick up items to false +mappy.import_text_map = True mappy.create_starting_map() +# mappy.go_to_room(1) +mappy.get_treasure() +# mappy.get_info() -mappy.explore_random(450) +# mappy.explore_random(118) diff --git a/utils.py b/utils.py index 6672ba57..9baf2281 100644 --- a/utils.py +++ b/utils.py @@ -6,223 +6,362 @@ import random import os -auth_key = config('AUTH_KEY') #MAKE SURE YPU HAVE .ENV SET UP +auth_key = config('AUTH_KEY') # MAKE SURE YPU HAVE .ENV SET UP my_url = config('LAMBDA_URL') # AND PYTHON DECOUPLE INSTALLED +my_name = config('NAME') # when to change name + def keystoint(x): "function to change json dictionary keys to ints - used for map load" return {int(k): v for k, v in x.items()} + class Player: def __init__(self, name, startingRoom): self.name = name self.currentRoom = startingRoom + self.player_cooldown = 1, + self.player_encumbrance = 0, + self.player_strength = 0, + self.player_speed = 0, + self.player_gold = 0, + self.player_inventory = [], + self.player_status = [], + self.player_errors = [], + self.player_messages = [] + self.player_mine = '' + class mapper: - def __init__(self,auth =auth_key,save = True, load_map= True): - self.auth = auth #the auth token - self.header = {'Authorization':f'Token {self.auth}'} #the header for post and get - self.wait = 18 # the current sleep length - this is no longer required as wait always points to cooldown - self.info = {} #the last status json from post or get - self.accumulate = False #whether player picks up items or not - it is very easy to get overencumbered - self.pray = False #can't pray without a name unfortunately - self.save_map_to_text = save #save latest map to a text file - self.import_text_map = load_map #import map so far - setting to false starts from scratch - self.player = None - - def get_info(self,what='init',direction=None,backtrack=None): - """multi purpose move & init function - this is used - for the most common actions""" - - if what=='init': - response = requests.get(f'{my_url}{what}/',headers=self.header) - - elif what=='move': - response = requests.post(f'{my_url}move/',headers=self.header,json = {"direction":direction}) - - elif what=='backtrack': - response = requests.post(f'{my_url}move/',headers=self.header,json = {"direction":direction,"next_room_id": backtrack}) - - if response.status_code==200: - self.info = json.loads(response.content) - if self.player is not None: - self.player.currentRoom = self.info['room_id'] - - if 'cooldown' in self.info.keys(): #there are a lot of TRAPS which require extra cooldown - time.sleep(self.info['cooldown']) - - self.room_check() - return self.info - else: - print('cooldown triggered - waiting 20 seconds') - time.sleep(20) - self.get_info(what=what,direction=direction,backtrack=backtrack) - - def action(self,what='take',treasure=None): - """another multi purpose request function - this one focuses on less common actions""" - - if what in ['take','drop','sell','examine']: - response = requests.post(f'{my_url}{what}/',headers=self.header,json = {"name":treasure}) - - if what in ['status','pray']: - response = requests.post(f'{my_url}{what}/',headers=self.header) - - if what == 'confirm_sell': - response = requests.post(f'{my_url}{what}/',headers=self.header,json = {"name":treasure, "confirm" : "yes"}) - - if response.status_code==200: - self.info = json.loads(response.content) - if 'cooldown' in self.info.keys(): - time.sleep(self.info['cooldown']) - return self.info - else: - print('error',what,treasure,response.status_code) - - def room_check(self): - """checks for items in teh room or special rooms""" - #print('room check triggered. info: ',self.info) - if self.info['items']!=[] and self.accumulate: - for item in self.info['items']: - - self.info = self.action('take',item) - print(self.info) - - if self.info['title'] == "Linh's Shrine" and self.pray: #there may be other shrines - self.info = self.action('pray') - - def create_starting_map(self): - """"initiates your starting map which is stored under the vertices of a graph class""" - info_dict = self.get_info() - print(info_dict) #this can be deactivated - just helpful at first - self.my_map = Graph() - self.player = Player("scooby_doo",info_dict['room_id']) - exits = info_dict['exits'] - exit_dict = {} - for e in exits: - exit_dict[e] = '?' - if self.import_text_map: - print("load map triggered") - with open('map.txt','r') as file: - string_dict = json.loads(file.read()) - for key in string_dict: - self.my_map.vertices[int(key)] = string_dict[key] - else: - print("fresh map triggered") - self.my_map.vertices[self.player.currentRoom] = exit_dict - - return self.my_map,self.player - - def pop_map_on_move(self,move): - """fills in the map while moving in the direction specified""" - reverse_dir ={'n':'s','s':'n','w':'e','e':'w'} - old_room = self.player.currentRoom - info = self.get_info('move',move) - self.player.currentRoom = info['room_id'] - print(info) # leave this line in to get movement updates - new_room = info['room_id'] - if new_room not in self.my_map.vertices: - exit_dict = {} - for exits in info['exits']: - for e in exits: + def __init__(self, auth=auth_key, save=True, load_map=True): + self.auth = auth # the auth token + # the header for post and get + self.header = {'Authorization': f'Token {self.auth}'} + self.wait = 18 # the current sleep length - this is no longer required as wait always points to cooldown + self.info = {} # the last status json from post or get + # whether player picks up items or not - it is very easy to get overencumbered + self.accumulate = False + self.pray = False # can't pray without a name unfortunately + self.save_map_to_text = save # save latest map to a text file + # import map so far - setting to false starts from scratch + self.import_text_map = load_map + self.player = None + + def get_info(self, what='init', direction=None, backtrack=None): + """multi purpose move & init function - this is used + for the most common actions""" + + if what == 'init': + response = requests.get(f'{my_url}{what}/', headers=self.header) + + elif what == 'move': + response = requests.post( + f'{my_url}move/', headers=self.header, json={"direction": direction}) + + elif what == 'backtrack': + response = requests.post(f'{my_url}move/', headers=self.header, + json={"direction": direction, "next_room_id": backtrack}) + + if response.status_code == 200: + self.info = json.loads(response.content) + if self.player is not None: + self.player.currentRoom = self.info['room_id'] + + if 'cooldown' in self.info.keys(): # there are a lot of TRAPS which require extra cooldown + time.sleep(self.info['cooldown']) + + self.room_check() + return self.info + else: + print('cooldown triggered - waiting 20 seconds') + time.sleep(20) + self.get_info(what=what, direction=direction, backtrack=backtrack) + + def action(self, what='take', treasure=None): + """another multi purpose request function + this one focuses on less common actions""" + + if what in ['take', 'drop', 'sell', 'examine']: + response = requests.post( + f'{my_url}{what}/', headers=self.header, json={"name": treasure}) + print(f"Action: {what}") + + if what in ['status', 'pray']: + response = requests.post(f'{my_url}{what}/', headers=self.header) + + if what == 'confirm_sell': + response = requests.post( + f'{my_url}{what}/', headers=self.header, json={"name": treasure, "confirm": "yes"}) + + # Change Name +++++++ + # if what == 'change_name': + # response = request.post( + # f'{my_url}{what}/', headers=self.headers, json={"name": new}) + # # Confirm Name +++++++ + # if what == 'confirm_name': + # response = request.post( + # f'{my_url}{what}/', headers=self.headers, json={"confirm": "aye"}) + + if response.status_code == 200: + self.info = json.loads(response.content) + if 'cooldown' in self.info.keys(): + time.sleep(self.info['cooldown']) + return self.info + else: + print('error', what, treasure, response.status_code) + + def room_check(self): + """checks for items in teh room or special rooms""" + # print('room check triggered. info: ',self.info) + if self.info['items'] != [] and self.accumulate: + for item in self.info['items']: + # Examines the item + self.info = self.action('examine', item) + print(self.info) + self.info = self.action('take', item) + print(self.info) + + if self.info['title'] == "Linh's Shrine" and self.pray: # there may be other shrines + self.info = self.action('pray') + + # Would this sell? ++++++ + if self.info['title'] == "shop": + self.info = self.action('sell', item) + self.info = self.action('confirm_sell') + + # Could this do the name change? +++++++ + # if self.info['title'] == "Pirate Ry's": + # self.info = self.action('change_name', name) + # self.info = self.action('confirm_name') + + def create_starting_map(self): + """"initiates your starting map which is stored under the vertices of a graph class""" + info_dict = self.get_info() + print(info_dict) # this can be deactivated - just helpful at first + self.my_map = Graph() + self.player = Player("MicahJones", info_dict['room_id']) + exits = info_dict['exits'] + exit_dict = {} + for e in exits: exit_dict[e] = '?' - self.my_map.vertices[new_room] = exit_dict - self.my_map.vertices[old_room][move] = new_room - reverse_move = reverse_dir[move] - self.my_map.vertices[new_room][reverse_move] = old_room - if self.save_map_to_text: - with open('map.txt','w') as file: - file.write(json.dumps(self.my_map.vertices)) - - def count_unmapped(self): - """counts all the unmapped rooms""" - counter = 0 - for val1 in self.my_map.vertices.values(): - for val2 in val1.values(): - if val2=='?': - counter += 1 - return counter - - def get_dirs(self,traversal): - """gets the direction of travel given a room traversal list""" - point = traversal[0] - dir_list = [] - for t in traversal[1:]: - for key in self.my_map.vertices[point]: - if self.my_map.vertices[point][key]==t: - dir_list.append(key) - point = t - return dir_list - - def bfs_for_q(self): - """breadth first search for last ?""" - room = self.player.currentRoom - q = Queue() - q.enqueue([room]) - - while '?' not in self.my_map.vertices[room].values(): - - joins = self.my_map.vertices[room] - for j in joins.values(): - if j in q.queue[0]: - pass + if self.import_text_map: + print("load map triggered") + with open('map.txt', 'r') as file: + string_dict = json.loads(file.read()) + for key in string_dict: + self.my_map.vertices[int(key)] = string_dict[key] else: - _ = [x for x in q.queue[0]] - _.append(j) - q.enqueue(_) - q.dequeue() - room = q.queue[0][-1] - - return q.queue[0] - - def explore_random(self,counter=5): - """explores the map choosing random ? and backtracks using bfs - counter is the number of times you want it to explore unkown rooms""" - unmapped_number = self.count_unmapped() - moves = [] - c=0 - while unmapped_number > 0 and c <= counter: - print(self.my_map.vertices) - - room = self.player.currentRoom - unvisited_exits = [x for x in self.my_map.vertices[room] if self.my_map.vertices[room][x]=='?'] - if unvisited_exits !=[]: - print('exit checker',unvisited_exits) - move = random.choice(unvisited_exits) - moves.append(move) - self.pop_map_on_move(move) + print("fresh map triggered") + self.my_map.vertices[self.player.currentRoom] = exit_dict + + return self.my_map, self.player + + def pop_map_on_move(self, move): + """fills in the map while moving in the direction specified""" + reverse_dir = {'n': 's', 's': 'n', 'w': 'e', 'e': 'w'} + old_room = self.player.currentRoom + info = self.get_info('move', move) + self.player.currentRoom = info['room_id'] + print(info) # leave this line in to get movement updates + new_room = info['room_id'] + if new_room not in self.my_map.vertices: + exit_dict = {} + for exits in info['exits']: + for e in exits: + exit_dict[e] = '?' + self.my_map.vertices[new_room] = exit_dict + self.my_map.vertices[old_room][move] = new_room + reverse_move = reverse_dir[move] + self.my_map.vertices[new_room][reverse_move] = old_room + if self.save_map_to_text: + with open('map.txt', 'w') as file: + file.write(json.dumps(self.my_map.vertices)) + + def count_unmapped(self): + """counts all the unmapped rooms""" + counter = 0 + for val1 in self.my_map.vertices.values(): + for val2 in val1.values(): + if val2 == '?': + counter += 1 + return counter + + def get_dirs(self, traversal): + """gets the direction of travel given a room traversal list""" + point = traversal[0] + dir_list = [] + for t in traversal[1:]: + for key in self.my_map.vertices[point]: + if self.my_map.vertices[point][key] == t: + dir_list.append(key) + point = t + return dir_list + + def bfs_for_q(self): + """breadth first search for last ?""" + room = self.player.currentRoom + q = Queue() + q.enqueue([room]) + + while '?' not in self.my_map.vertices[room].values(): + + joins = self.my_map.vertices[room] + for j in joins.values(): + if j in q.queue[0]: + pass + else: + _ = [x for x in q.queue[0]] + _.append(j) + q.enqueue(_) + q.dequeue() + room = q.queue[0][-1] + + return q.queue[0] + + def explore_random(self, counter=5): + """explores the map choosing random ? and backtracks using bfs + counter is the number of times you want it to explore unkown rooms""" unmapped_number = self.count_unmapped() + moves = [] + c = 0 + while unmapped_number > 0 and c <= counter: + print(self.my_map.vertices) + + room = self.player.currentRoom + unvisited_exits = [x for x in self.my_map.vertices[room] + if self.my_map.vertices[room][x] == '?'] + if unvisited_exits != []: + print('exit checker', unvisited_exits) + move = random.choice(unvisited_exits) + moves.append(move) + self.pop_map_on_move(move) + unmapped_number = self.count_unmapped() + time.sleep(self.wait) + else: + # leave this line in to show you when you are backtracking + print('back track on') + backtrack = self.bfs_for_q() + backtrack_dirs = self.get_dirs(backtrack) + # this line shows details of backtrack + print('backtrack details', backtrack, backtrack_dirs) + for i in range(len(backtrack_dirs)): + b_info = self.get_info( + 'backtrack', backtrack_dirs[i], str(backtrack[i+1])) + self.player.currentRoom = b_info['room_id'] + c += 1 + + def go_to_room(self, destination): + """depth first traversal to particular room in shortest route + NOT OPERATIONAL YET""" + # self.accumulate = False + print('moving') + path = self.my_map.bfs(self.player.currentRoom, destination) + # print(f'Path: {path}') + for m in path: + room = self.player.currentRoom + # print(f'Room: {type(room)}') + exits = self.my_map.vertices[room] + # print(f'Room: {room}') + for direction in exits: + if self.my_map.vertices[room][direction] == m: + # print(f'direction: {direction}') + self.get_info(what='move', direction=direction) + print( + f"Current Room -> Title: {self.info['title']} ID: {self.info['room_id']} Items: {self.info['items']}") + else: + # print(f'go_to_room FAILED: Invalid Direction[{direction}], Room[{room}]') + continue + # self.accumulate = True + + def pirate(self): + # Goes directly to pirate ry + # self.go_to_room(self.important_rooms['pirate ry']) + # time.sleep(self.wait) + pass + + def wishing_well(self): + # Goes directly to pirate ry + # self.go_to_room(self.important_rooms['wishing well']) + # time.sleep(self.wait) + pass + + def vendor(self): + # Goes directly to the shop + self.go_to_room(1) time.sleep(self.wait) - else: - print('back track on') #leave this line in to show you when you are backtracking - backtrack = self.bfs_for_q() - backtrack_dirs = self.get_dirs(backtrack) - print('backtrack details',backtrack,backtrack_dirs) #this line shows details of backtrack - for i in range(len(backtrack_dirs)): - b_info = self.get_info('backtrack',backtrack_dirs[i],str(backtrack[i+1])) - self.player.currentRoom = b_info['room_id'] - c+=1 - - def go_to_room(self,destination): - """depth first traversal to particular room in shortest route - NOT OPERATIONAL YET""" - s = Stack() - s.push([self.player.currentRoom]) - - while destination not in s.stack[-1]: - current_point = s.stack[-1][-1] - - joins = self.my_map.vertices[current_point] - if joins is None: - s.pop() - else: - temp_list = [] - for j in joins: - _ = [x for x in s.stack[-1]] - _.append(j) - temp_list.append(_) - for tl in temp_list: - s.push(tl) - return s.stack[-1] + # Method to get treasure + # BFS Randomly to travel the maze, looting + # Once you get enough treasure, go sell + # Once you reach 1000 gold, buy a name + # Change name to something unique, that doesnt contain player + # Keep looting and selling until stopped. + def get_treasure(self): + while True: + # get a name + + #! This request is being used to get information about our player from the lambda server. + #! This would probably be better off initializing our local player attributes at the top, but + #! I tried this first. + url = 'https://lambda-treasure-hunt.herokuapp.com/api/adv/status/' + token = config('AUTH_KEY') + headers = {'Authorization': f'Token {token}'} + r = requests.post(url, headers=headers) + # get return data + # print(r) + ret_data = r.json() + # print(ret_data, 'retdata') + print(f"Current Inventory: {ret_data['inventory']}") + #!------------------------This name is specific to each person, be sure to change this to yours. + if ret_data['name'] == 'player446' and ret_data['gold'] >= 1000: + # Go to name changer (pirate ry) + print('Time to Buy a Name') + self.go_to_room(467) # pirate ry's room + time.sleep(self.wait) + # Buy name + self.action('change_name') + time.sleep(self.wait) + # confirm_name + self.action('confirm_name') + print('Got a name! Time to get a COIN.', {ret_data['name']}) + time.sleep(self.wait) + # self.action('status') #Check new name + elif ret_data['encumbrance'] <= ret_data['strength'] - 2: + # If encumbered is str-2 (at base = 8) + # Travel the room bfs style at random + # Loot as you go with room_check + print('Looting..') + + # self.explore_random(500) + self.go_to_room(random.randint(0, 499)) + print('Current Inventory: ', ret_data['inventory']) + time.sleep(self.wait) + # Could potentially add a section to manage miner + else: + # else go directly to the shop + # loop through inventory and sell + # Go back to looting + print('Need to offload my loot.') + self.vendor() + print('At the shop, time to sell.') + for item in ret_data['inventory']: + print(f"Selling {item}...") + self.action('sell', item) + time.sleep(self.wait) + self.action('confirm_sell', item) + time.sleep(self.wait) + # This doesn't actually update after each sell for some reason. + print(f"You're current gold: {ret_data['gold']}") + print('Back to Looting', {ret_data['inventory']}) + + +""" +need a function to go to name changer and buy a name. +funciton to go to wishing well and interact +function to go to where wishing well says and mine a coin + +SHOP is in room id 1 +Name changer is in 467 +Well is 55 +""" From 753512d6a7fc8593e652c6d2bc28517c4f3d0a5b Mon Sep 17 00:00:00 2001 From: micahjones13 Date: Tue, 19 Nov 2019 19:51:44 -0700 Subject: [PATCH 18/32] Can now change name. Added comments and toggled accumulate on/off accordingly --- utils.py | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/utils.py b/utils.py index 9baf2281..5cbc0207 100644 --- a/utils.py +++ b/utils.py @@ -77,7 +77,7 @@ def get_info(self, what='init', direction=None, backtrack=None): time.sleep(20) self.get_info(what=what, direction=direction, backtrack=backtrack) - def action(self, what='take', treasure=None): + def action(self, what='take', treasure=None, name=None): """another multi purpose request function this one focuses on less common actions""" @@ -94,13 +94,9 @@ def action(self, what='take', treasure=None): f'{my_url}{what}/', headers=self.header, json={"name": treasure, "confirm": "yes"}) # Change Name +++++++ - # if what == 'change_name': - # response = request.post( - # f'{my_url}{what}/', headers=self.headers, json={"name": new}) - # # Confirm Name +++++++ - # if what == 'confirm_name': - # response = request.post( - # f'{my_url}{what}/', headers=self.headers, json={"confirm": "aye"}) + if what == 'change_name': + response = requests.post( + f'{my_url}{what}/', headers=self.header, json={"name": name, "confirm": "aye"}) if response.status_code == 200: self.info = json.loads(response.content) @@ -307,23 +303,30 @@ def get_treasure(self): token = config('AUTH_KEY') headers = {'Authorization': f'Token {token}'} r = requests.post(url, headers=headers) - # get return data - # print(r) ret_data = r.json() - # print(ret_data, 'retdata') - print(f"Current Inventory: {ret_data['inventory']}") + + print(f"***********Current Character Attributes***************") + print(ret_data) + print("*******************************************************") + #!------------------------This name is specific to each person, be sure to change this to yours. if ret_data['name'] == 'player446' and ret_data['gold'] >= 1000: # Go to name changer (pirate ry) print('Time to Buy a Name') + # * Made this false here so that we don't somehow pick up a ton of treasure on the way, and + # * get over-encumbered. + self.accumulate = False self.go_to_room(467) # pirate ry's room time.sleep(self.wait) # Buy name - self.action('change_name') + #! -------------------------- Change the name here to be what you want!! + self.action('change_name', name='Micah') time.sleep(self.wait) - # confirm_name - self.action('confirm_name') - print('Got a name! Time to get a COIN.', {ret_data['name']}) + + #! This print isn't accurate. It doesn't update when you actually change your name. + #! Next time you see it, it should have changed though. + print( + f"Got a name! Time to get a COIN. New Name: {ret_data['name']}") time.sleep(self.wait) # self.action('status') #Check new name elif ret_data['encumbrance'] <= ret_data['strength'] - 2: @@ -331,6 +334,8 @@ def get_treasure(self): # Travel the room bfs style at random # Loot as you go with room_check print('Looting..') + # * accumlate is true here since that's the whole point of this block + self.accumulate = True # self.explore_random(500) self.go_to_room(random.randint(0, 499)) @@ -342,6 +347,8 @@ def get_treasure(self): # loop through inventory and sell # Go back to looting print('Need to offload my loot.') + # * Setting accumulate to false so we don't get overburdening on the way to shop. + self.accumulate = False self.vendor() print('At the shop, time to sell.') for item in ret_data['inventory']: @@ -352,16 +359,16 @@ def get_treasure(self): time.sleep(self.wait) # This doesn't actually update after each sell for some reason. print(f"You're current gold: {ret_data['gold']}") - print('Back to Looting', {ret_data['inventory']}) + print('Back to Looting') """ -need a function to go to name changer and buy a name. -funciton to go to wishing well and interact +need a function to go to name changer and buy a name. +funciton to go to wishing well and interact function to go to where wishing well says and mine a coin SHOP is in room id 1 -Name changer is in 467 -Well is 55 +Name changer is in 467 +Well is 55 """ From 25a1f3fc60de7072b0d3c24d8c036066a8314f00 Mon Sep 17 00:00:00 2001 From: mrsteveson Date: Tue, 19 Nov 2019 22:59:08 -0600 Subject: [PATCH 19/32] unique rooms mapped --- explore.py | 7 +++---- map.txt | 2 +- rooms.txt | 12 +++++++++++- utils.py | 46 +++++++++++++++++++++++++++------------------- 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/explore.py b/explore.py index a7a5c0c6..80f1b031 100644 --- a/explore.py +++ b/explore.py @@ -1,10 +1,9 @@ from utils import * mappy = mapper() -mappy.accumulate=False #set pick up items to false -# mappy.import_text_map=False - +mappy.accumulate=True #set pick up items to false +mappy.import_text_map=True mappy.create_starting_map() # mappy.go_to_room('10') # print(mappy.player.currentRoom) -mappy.explore_random(50) \ No newline at end of file +mappy.explore_random(150) \ No newline at end of file diff --git a/map.txt b/map.txt index 3258aff9..f6751b30 100644 --- a/map.txt +++ b/map.txt @@ -1 +1 @@ -{"483": {"w": 477}, "477": {"e": 483, "w": 388}, "388": {"e": 477, "w": 382}, "382": {"s": 322, "e": 388}, "322": {"n": 382, "e": "?", "w": 261}, "261": {"s": "?", "e": 322, "w": 247}, "247": {"e": 261, "w": 217}, "217": {"s": 164, "e": 247}, "164": {"n": 217, "e": "?", "w": 156}, "156": {"s": "?", "e": 164, "w": 141}, "141": {"n": 112, "e": 156}, "112": {"s": 141, "e": "?", "w": 100}, "100": {"s": "?", "e": 112, "w": 68}, "68": {"n": 52, "e": 100}, "52": {"n": 35, "s": 68, "e": 75}, "75": {"e": 85, "w": 52}, "85": {"e": 154, "w": 75}, "154": {"e": 193, "w": 85}, "193": {"e": 251, "w": 154}, "251": {"e": 315, "w": 193}, "315": {"w": 251}, "35": {"s": 52, "w": 34}, "34": {"n": "?", "s": 50, "e": 35}, "50": {"n": 34, "s": 89}, "89": {"n": 50, "s": 93}, "93": {"n": 89, "w": 108}, "108": {"n": "?", "s": 117, "e": 93}, "117": {"n": 108, "s": 131, "e": "?", "w": "?"}, "131": {"n": 117, "s": "?", "w": 138}, "138": {"s": "?", "e": 131, "w": 195}, "195": {"s": 228, "e": 138, "w": 225}, "225": {"s": 278, "e": 195}, "278": {"n": 225}, "228": {"n": 195, "s": 281}, "281": {"n": 228, "s": "?", "e": 309, "w": 317}, "309": {"s": 333, "e": 326, "w": 281}, "333": {"n": 309, "s": 378}, "378": {"n": 333}, "326": {"s": 342, "w": 309}, "342": {"n": 326, "s": 432}, "432": {"n": 342}, "317": {"s": 387, "e": 281, "w": 409}, "409": {"e": 317}, "387": {"n": 317, "s": 417, "w": 431}, "431": {"e": 387, "w": 492}, "492": {"e": 431}, "417": {"n": 387}} \ No newline at end of file +{"483": {"w": 477}, "477": {"e": 483, "w": 388}, "388": {"e": 477, "w": 382}, "382": {"s": 322, "e": 388}, "322": {"n": 382, "e": 435, "w": 261}, "261": {"s": 277, "e": 322, "w": 247}, "247": {"e": 261, "w": 217}, "217": {"s": 164, "e": 247}, "164": {"n": 217, "e": 298, "w": 156}, "156": {"s": 168, "e": 164, "w": 141}, "141": {"n": 112, "e": 156}, "112": {"s": 141, "e": 140, "w": 100}, "100": {"s": 106, "e": 112, "w": 68}, "68": {"n": 52, "e": 100}, "52": {"n": 35, "s": 68, "e": 75}, "75": {"e": 85, "w": 52}, "85": {"e": 154, "w": 75}, "154": {"e": 193, "w": 85}, "193": {"e": 251, "w": 154}, "251": {"e": 315, "w": 193}, "315": {"w": 251}, "35": {"s": 52, "w": 34}, "34": {"n": 14, "s": 50, "e": 35}, "50": {"n": 34, "s": 89}, "89": {"n": 50, "s": 93}, "93": {"n": 89, "w": 108}, "108": {"n": 78, "s": 117, "e": 93}, "117": {"n": 108, "s": 131, "e": 166, "w": 133}, "131": {"n": 117, "s": 244, "w": 138}, "138": {"s": 211, "e": 131, "w": 195}, "195": {"s": 228, "e": 138, "w": 225}, "225": {"s": 278, "e": 195}, "278": {"n": 225}, "228": {"n": 195, "s": 281}, "281": {"n": 228, "s": 318, "e": 309, "w": 317}, "309": {"s": 333, "e": 326, "w": 281}, "333": {"n": 309, "s": 378}, "378": {"n": 333}, "326": {"s": 342, "w": 309}, "342": {"n": 326, "s": 432}, "432": {"n": 342}, "317": {"s": 387, "e": 281, "w": 409}, "409": {"e": 317}, "387": {"n": 317, "s": 417, "w": 431}, "431": {"e": 387, "w": 492}, "492": {"e": 431}, "417": {"n": 387}, "318": {"n": 281, "s": 487}, "487": {"n": 318, "s": 489}, "489": {"n": 487}, "211": {"n": 138}, "244": {"n": 131, "e": 239}, "239": {"n": 198, "w": 244}, "198": {"n": 166, "s": 239, "e": 199}, "166": {"s": 198, "e": 150, "w": 117}, "133": {"e": 117, "w": 173}, "173": {"e": 133, "w": 214}, "214": {"n": 194, "e": 173, "w": 226}, "226": {"s": 300, "e": 214}, "300": {"n": 226, "s": 377, "w": 389}, "377": {"n": 300}, "389": {"e": 300}, "194": {"s": 214, "w": 129}, "129": {"n": 126, "e": 194, "w": 170}, "170": {"e": 129}, "126": {"n": 98, "s": 129}, "98": {"n": 102, "s": 126, "e": 70, "w": 109}, "102": {"s": 98, "w": 142}, "142": {"e": 102, "w": 159}, "159": {"e": 142, "w": 196}, "196": {"n": 222, "e": 159, "w": 197}, "197": {"n": 232, "e": 196, "w": 276}, "276": {"e": 197, "w": 419}, "419": {"e": 276}, "232": {"n": 272, "s": 197, "w": 235}, "235": {"n": 330, "e": 232, "w": 355}, "355": {"e": 235}, "330": {"n": 369, "s": 235, "w": 383}, "383": {"e": 330, "w": 495}, "495": {"e": 383}, "369": {"n": 400, "s": 330, "w": 376}, "376": {"e": 369}, "400": {"s": 369}, "272": {"n": 295, "s": 232}, "295": {"s": 272}, "222": {"n": 305, "s": 196}, "305": {"n": 365, "s": 222}, "365": {"s": 305}, "109": {"s": 185, "e": 98, "w": 175}, "185": {"n": 109}, "175": {"s": 183, "e": 109, "w": 179}, "179": {"s": 233, "e": 175, "w": 213}, "233": {"n": 179, "w": 238}, "238": {"e": 233}, "213": {"e": 179, "w": 420}, "420": {"s": 444, "e": 213, "w": 437}, "444": {"n": 420, "w": 490}, "490": {"e": 444, "w": 493}, "493": {"e": 490}, "437": {"e": 420, "w": 497}, "497": {"e": 437}, "183": {"n": 175, "s": 229}, "229": {"n": 183, "s": 250, "w": 236}, "250": {"n": 229, "s": 294, "e": 289}, "289": {"w": 250}, "294": {"n": 250, "s": 334}, "334": {"n": 294, "s": 393, "e": 341, "w": 391}, "393": {"n": 334, "s": 482}, "482": {"n": 393}, "391": {"s": 396, "e": 334, "w": 428}, "396": {"n": 391}, "428": {"e": 391}, "341": {"s": 449, "w": 334}, "449": {"n": 341}, "236": {"s": 264, "e": 229}, "264": {"n": 236, "s": 274, "w": 273}, "274": {"n": 264, "w": 308}, "308": {"e": 274}, "273": {"n": 343, "e": 264}, "343": {"s": 273, "w": 351}, "351": {"s": 491, "e": 343, "w": 478}, "491": {"n": 351}, "478": {"e": 351}, "70": {"s": 163, "e": 60, "w": 98}, "163": {"n": 70}, "60": {"n": 45, "e": 36, "w": 70}, "45": {"n": 29, "s": 60}, "29": {"s": 45, "e": 21, "w": 49}, "49": {"s": 79, "e": 29, "w": 136}, "136": {"e": 49, "w": 148}, "148": {"e": 136, "w": 292}, "292": {"n": 301, "e": 148}, "301": {"n": 304, "s": 292}, "304": {"s": 301}, "79": {"n": 49}, "21": {"e": 12, "w": 29}, "12": {"n": 9, "s": 18, "e": 14, "w": 21}, "14": {"s": 34, "e": 37, "w": 12}, "37": {"w": 14}, "18": {"n": 12, "s": 22, "w": 25}, "25": {"e": 18}, "22": {"n": 18, "s": 78, "w": 36}, "36": {"s": 48, "e": 22, "w": 60}, "48": {"n": 36, "s": 105, "w": 149}, "149": {"e": 48}, "105": {"n": 48, "w": 202}, "202": {"e": 105}, "78": {"n": 22, "s": 108}, "150": {"n": 135, "w": 166}, "135": {"s": 150, "e": 106}, "106": {"n": 100, "s": 111, "w": 135}, "111": {"n": 106, "s": 367, "e": 158}, "367": {"n": 111}, "158": {"s": 167, "w": 111}, "167": {"n": 158, "s": 262, "e": 260}, "260": {"w": 167}, "262": {"n": 167, "s": 370, "e": 358}, "370": {"n": 262, "s": 434, "e": 407}, "434": {"n": 370}, "407": {"s": 496, "w": 370}, "496": {"n": 407}, "358": {"e": 401, "w": 262}, "401": {"w": 358}, "140": {"w": 112}, "168": {"n": 156, "e": 340}, "340": {"w": 168}, "298": {"s": 324, "w": 164}, "324": {"n": 298, "s": 349, "e": 354}, "354": {"w": 324}, "349": {"n": 324, "s": 352, "e": 384, "w": 356}, "384": {"w": 349}, "352": {"n": 349, "s": 362, "e": 485}, "362": {"n": 352, "s": 399, "w": 463}, "463": {"s": 468, "e": 362}, "468": {"n": 463}, "399": {"n": 362, "s": 467}, "467": {"n": 399}, "485": {"w": 352}, "356": {"e": 349}, "277": {"n": 261, "e": 323}, "323": {"e": 433, "w": 277}, "433": {"s": 455, "e": 460, "w": 323}, "455": {"n": 433}, "460": {"w": 433}, "435": {"w": 322}, "199": {"s": 230, "w": 198}, "230": {"n": 199, "s": 307, "e": 297}, "307": {"n": 230, "s": 373, "e": 371, "w": 321}, "373": {"n": 307, "s": 480}, "480": {"n": 373}, "371": {"s": 475, "w": 307}, "475": {"n": 371, "s": 484}, "484": {"n": 475}, "321": {"s": 413, "e": 307}, "413": {"n": 321}, "297": {"w": 230}, "9": {"n": 3, "s": 12, "e": 11}, "3": {"s": 9, "e": 5, "w": 2}, "2": {"n": 0, "s": 6, "e": 3}, "6": {"n": 2, "w": 7}, "7": {"n": 8, "e": 6, "w": 56}, "56": {"e": 7, "w": 61}, "61": {"e": 56, "w": 171}, "171": {"e": 61}, "8": {"s": 7, "w": 16}, "16": {"n": 58, "e": 8, "w": 67}, "58": {"s": 16, "w": 65}, "65": {"n": 74, "e": 58, "w": 139}, "139": {"e": 65, "w": 188}, "188": {"e": 139, "w": 335}, "335": {"e": 188, "w": 366}, "366": {"e": 335}, "74": {"n": 87, "s": 65, "w": 161}, "87": {"s": 74}, "161": {"e": 74}, "67": {"e": 16, "w": 162}, "162": {"e": 67}, "0": {"n": 10, "s": 2, "e": 4, "w": 1}, "10": {"n": 19, "s": 0, "w": 43}, "43": {"e": 10, "w": 47}, "47": {"n": 71, "e": 43}, "71": {"s": 47}, "19": {"n": 20, "s": 10, "w": 77}, "77": {"e": 19}, "20": {"n": 63, "s": 19, "e": 27, "w": 46}, "63": {"n": 72, "s": 20, "w": 73}, "72": {"s": 63, "w": 76}, "76": {"n": 83, "e": 72, "w": 110}, "83": {"s": 76, "e": 130, "w": 125}, "125": {"n": 165, "e": 83, "w": 237}, "165": {"n": 203, "s": 125, "w": 204}, "204": {"n": 219, "e": 165, "w": 216}, "216": {"n": 234, "e": 204, "w": 218}, "234": {"n": 368, "s": 216, "w": 252}, "368": {"s": 234}, "252": {"n": 284, "e": 234}, "284": {"n": 302, "s": 252, "w": 303}, "302": {"n": 422, "s": 284}, "422": {"n": 426, "s": 302}, "426": {"n": 457, "s": 422}, "457": {"n": 461, "s": 426}, "461": {"s": 457}, "303": {"n": 361, "e": 284, "w": 405}, "361": {"n": 408, "s": 303}, "408": {"n": 458, "s": 361, "w": 423}, "458": {"s": 408, "w": 459}, "459": {"e": 458}, "423": {"e": 408, "w": 454}, "454": {"n": 470, "e": 423}, "470": {"s": 454}, "405": {"n": 406, "e": 303}, "406": {"s": 405, "w": 415}, "415": {"e": 406, "w": 418}, "418": {"n": 425, "s": 474, "e": 415}, "474": {"n": 418}, "425": {"s": 418, "w": 469}, "469": {"e": 425}, "218": {"s": 263, "e": 216, "w": 242}, "263": {"n": 218}, "242": {"n": 287, "s": 259, "e": 218, "w": 275}, "287": {"s": 242, "w": 339}, "339": {"e": 287, "w": 445}, "445": {"n": 447, "e": 339, "w": 450}, "447": {"s": 445}, "450": {"e": 445}, "259": {"n": 242, "w": 310}, "310": {"e": 259, "w": 412}, "412": {"s": 488, "e": 310}, "488": {"n": 412}, "275": {"e": 242, "w": 456}, "456": {"e": 275, "w": 499}, "499": {"e": 456}, "219": {"s": 204}, "203": {"n": 268, "s": 165, "e": 299}, "299": {"e": 311, "w": 203}, "311": {"w": 299}, "268": {"s": 203, "e": 411, "w": 312}, "411": {"w": 268}, "312": {"n": 328, "e": 268}, "328": {"n": 332, "s": 312, "e": 357, "w": 363}, "332": {"n": 350, "s": 328}, "350": {"n": 436, "s": 332, "e": 404}, "404": {"n": 481, "w": 350}, "481": {"s": 404}, "436": {"s": 350}, "363": {"n": 372, "e": 328}, "372": {"n": 441, "s": 363}, "441": {"s": 372}, "357": {"w": 328}, "237": {"e": 125, "w": 245}, "245": {"s": 254, "e": 237}, "254": {"n": 245, "w": 314}, "314": {"e": 254}, "130": {"w": 83}, "110": {"e": 76}, "73": {"e": 63}, "27": {"n": 40, "s": 28, "e": 30, "w": 20}, "30": {"s": 31, "e": 32, "w": 27}, "31": {"n": 30, "e": 33}, "33": {"e": 38, "w": 31}, "38": {"s": 59, "e": 66, "w": 33}, "59": {"n": 38, "s": 104, "e": 92}, "92": {"w": 59}, "104": {"n": 59, "e": 107}, "107": {"s": 120, "e": 121, "w": 104}, "121": {"n": 128, "e": 143, "w": 107}, "128": {"s": 121, "e": 189}, "189": {"e": 255, "w": 128}, "255": {"w": 189}, "143": {"e": 212, "w": 121}, "212": {"w": 143}, "120": {"n": 107, "e": 127}, "127": {"e": 184, "w": 120}, "184": {"e": 221, "w": 127}, "221": {"s": 253, "e": 240, "w": 184}, "253": {"n": 221, "e": 258}, "258": {"e": 306, "w": 253}, "306": {"e": 397, "w": 258}, "397": {"w": 306}, "240": {"n": 249, "e": 386, "w": 221}, "386": {"e": 414, "w": 240}, "414": {"w": 386}, "249": {"n": 265, "s": 240, "e": 282}, "265": {"n": 279, "s": 249, "e": 270}, "270": {"n": 416, "e": 338, "w": 265}, "416": {"s": 270}, "338": {"s": 379, "w": 270}, "379": {"n": 338, "e": 395}, "395": {"s": 403, "e": 421, "w": 379}, "421": {"n": 440, "w": 395}, "440": {"s": 421, "w": 476}, "476": {"e": 440}, "403": {"n": 395}, "279": {"s": 265}, "282": {"w": 249}, "66": {"n": 169, "e": 123, "w": 38}, "169": {"s": 66, "e": 186}, "186": {"e": 205, "w": 169}, "205": {"s": 241, "e": 479, "w": 186}, "241": {"n": 205, "e": 266}, "266": {"w": 241}, "479": {"w": 205}, "123": {"w": 66}, "32": {"n": 39, "e": 54, "w": 30}, "54": {"w": 32}, "39": {"n": 53, "s": 32, "e": 51, "w": 41}, "53": {"n": 95, "s": 39, "w": 88}, "88": {"e": 53, "w": 122}, "122": {"n": 124, "e": 88}, "124": {"n": 157, "s": 122}, "157": {"n": 210, "s": 124, "w": 182}, "182": {"e": 157, "w": 208}, "208": {"e": 182}, "210": {"s": 157}, "95": {"n": 119, "s": 53, "w": 115}, "115": {"n": 116, "e": 95}, "116": {"n": 132, "s": 115}, "132": {"s": 116}, "119": {"n": 134, "s": 95}, "134": {"n": 147, "s": 119, "e": 144}, "144": {"e": 155, "w": 134}, "155": {"s": 187, "e": 316, "w": 144}, "316": {"n": 344, "w": 155}, "344": {"n": 392, "s": 316, "e": 390}, "390": {"w": 344}, "392": {"s": 344, "e": 462}, "462": {"w": 392}, "187": {"n": 155}, "147": {"n": 200, "s": 134, "e": 153, "w": 151}, "151": {"n": 172, "e": 147, "w": 207}, "207": {"n": 231, "e": 151, "w": 290}, "231": {"s": 207, "w": 248}, "248": {"n": 296, "e": 231, "w": 280}, "296": {"s": 248}, "280": {"n": 325, "e": 248}, "325": {"n": 353, "s": 280, "w": 374}, "353": {"s": 325}, "374": {"e": 325}, "290": {"e": 207}, "172": {"n": 267, "s": 151}, "267": {"n": 285, "s": 172, "w": 271}, "285": {"n": 286, "s": 267}, "286": {"n": 336, "s": 285, "w": 291}, "336": {"s": 286}, "291": {"n": 410, "e": 286, "w": 347}, "347": {"n": 452, "s": 442, "e": 291}, "452": {"s": 347}, "442": {"n": 347}, "410": {"s": 291}, "271": {"n": 337, "e": 267}, "337": {"s": 271}, "200": {"n": 227, "s": 147, "e": 206}, "206": {"n": 288, "e": 380, "w": 200}, "380": {"n": 424, "w": 206}, "424": {"s": 380, "e": 473}, "473": {"e": 494, "w": 424}, "494": {"w": 473}, "288": {"s": 206}, "227": {"n": 269, "s": 200}, "269": {"n": 319, "s": 227}, "319": {"n": 359, "s": 269, "e": 345}, "359": {"s": 319}, "345": {"s": 375, "w": 319}, "375": {"n": 345, "e": 385}, "385": {"w": 375}, "153": {"e": 329, "w": 147}, "329": {"w": 153}, "51": {"n": 69, "e": 57, "w": 39}, "57": {"e": 145, "w": 51}, "145": {"n": 174, "e": 220, "w": 57}, "220": {"w": 145}, "174": {"n": 192, "s": 145, "e": 224}, "224": {"w": 174}, "192": {"n": 201, "s": 174, "e": 223}, "201": {"s": 192}, "223": {"n": 283, "w": 192}, "283": {"n": 331, "s": 223, "e": 313}, "331": {"s": 283, "e": 446}, "446": {"e": 466, "w": 331}, "466": {"s": 486, "e": 472, "w": 446}, "472": {"w": 466}, "486": {"n": 466}, "313": {"w": 283}, "69": {"n": 94, "s": 51, "e": 103}, "103": {"n": 160, "w": 69}, "160": {"s": 103}, "94": {"n": 152, "s": 69}, "152": {"s": 94}, "41": {"e": 39}, "28": {"n": 27}, "40": {"s": 27}, "46": {"e": 20, "w": 62}, "62": {"n": 64, "e": 46, "w": 84}, "84": {"e": 62, "w": 91}, "91": {"n": 180, "s": 101, "e": 84, "w": 99}, "101": {"n": 91, "w": 113}, "113": {"s": 114, "e": 101}, "114": {"n": 113, "w": 176}, "176": {"e": 114, "w": 402}, "402": {"e": 176, "w": 451}, "451": {"e": 402, "w": 453}, "453": {"s": 464, "e": 451}, "464": {"n": 453}, "99": {"n": 190, "e": 91, "w": 146}, "146": {"n": 215, "s": 177, "e": 99, "w": 257}, "215": {"n": 246, "s": 146}, "246": {"s": 215}, "177": {"n": 146, "w": 346}, "346": {"e": 177}, "257": {"n": 320, "e": 146, "w": 364}, "320": {"n": 348, "s": 257}, "348": {"s": 320}, "364": {"n": 429, "s": 381, "e": 257, "w": 448}, "429": {"s": 364}, "448": {"e": 364}, "381": {"n": 364, "w": 394}, "394": {"e": 381}, "190": {"s": 99}, "180": {"s": 91}, "64": {"s": 62, "w": 82}, "82": {"n": 191, "e": 64}, "191": {"s": 82}, "4": {"n": 23, "e": 13, "w": 0}, "13": {"e": 15, "w": 4}, "15": {"w": 13}, "23": {"s": 4, "e": 26}, "26": {"e": 55, "w": 23}, "55": {"w": 26}, "1": {"e": 0}, "5": {"w": 3}, "11": {"e": 17, "w": 9}, "17": {"n": 24, "e": 42, "w": 11}, "24": {"s": 17}, "42": {"n": 44, "s": 80, "e": 118, "w": 17}, "118": {"e": 137, "w": 42}, "137": {"w": 118}, "80": {"n": 42, "s": 81, "e": 86}, "86": {"s": 96, "e": 90, "w": 80}, "90": {"e": 178, "w": 86}, "178": {"n": 209, "e": 243, "w": 90}, "243": {"s": 293, "e": 256, "w": 178}, "293": {"n": 243}, "256": {"s": 360, "e": 327, "w": 243}, "327": {"e": 427, "w": 256}, "427": {"e": 430, "w": 327}, "430": {"n": 443, "e": 439, "w": 427}, "439": {"w": 430}, "443": {"s": 430, "e": 471}, "471": {"w": 443}, "360": {"n": 256, "e": 398}, "398": {"e": 438, "w": 360}, "438": {"e": 465, "w": 398}, "465": {"e": 498, "w": 438}, "498": {"w": 465}, "209": {"s": 178}, "96": {"n": 86, "e": 97}, "97": {"e": 181, "w": 96}, "181": {"w": 97}, "81": {"n": 80}, "44": {"s": 42}} \ No newline at end of file diff --git a/rooms.txt b/rooms.txt index 99d295be..ed58f247 100644 --- a/rooms.txt +++ b/rooms.txt @@ -1 +1,11 @@ -{"A misty room": 417, "Mt. Holloway": 131} \ No newline at end of file +{"A misty room": 44, +"Mt. Holloway": 11, +"The Transmogriphier": 495, +"The Peak of Mt. Holloway": 22, +"Pirate Ry's": 467, +"A brightly lit room": 0, +"A Dark Cave": 488, +"Linh's Shrine": 461, +"Glasowyn's Grave": 499, +"Wishing Well": 55, +"Shop": 1} \ No newline at end of file diff --git a/utils.py b/utils.py index 7f3d03f3..3bdf8e48 100644 --- a/utils.py +++ b/utils.py @@ -116,6 +116,7 @@ def room_check(self): self.info = self.action('confirm_sell') # Could this do the name change? +++++++ + # Need to send the new name here or above in request if self.info['title'] == "pirate ry": self.info = self.action('change_name', name) self.info = self.action('confirm_name') @@ -242,41 +243,33 @@ def explore_random(self, counter=5): c+=1 def go_to_room(self, destination): - """depth first traversal to particular room in shortest route""" - self.accumulate = False + """breath first traversal to particular room in shortest route""" print('moving') path = self.my_map.bfs(self.player.currentRoom, destination) - # print(f'Path: {path}') for m in path: room = self.player.currentRoom - # print(f'Room: {type(room)}') exits = self.my_map.vertices[room] - # print(f'Room: {room}') for direction in exits: if self.my_map.vertices[room][direction] == m: - # print(f'direction: {direction}') self.get_info(what='move', direction=direction) else: - # print(f'go_to_room FAILED: Invalid Direction[{direction}], Room[{room}]') continue - self.accumulate = True def pirate(self): - # Goes directly to pirate ry - # self.go_to_room(self.important_rooms['pirate ry']) - # time.sleep(self.wait) - pass + # Goes directly to pirate ry, room 467 + self.go_to_room(self.important_rooms['pirate ry']) def wishing_well(self): - # Goes directly to pirate ry - # self.go_to_room(self.important_rooms['wishing well']) - # time.sleep(self.wait) - pass + # Goes directly to wishing well, room 55 + self.go_to_room(self.important_rooms['wishing well']) + + def transmogriphier(self): + # Transmogriphier, room 495 + self.go_to_room(self.important_rooms['Transmogriphier']) def vendor(self): - # Goes directly to the shop + # Goes directly to the shop, room 1 self.go_to_room(1) - time.sleep(self.wait) # Method to get treasure # BFS Randomly to travel the maze, looting @@ -322,4 +315,19 @@ def get_treasure(self): self.action('confirm_sell') print({self.player.gold}) time.sleep(self.wait) - print('Back to Looting', {self.player.inventory}) \ No newline at end of file + print('Back to Looting', {self.player.inventory}) + + def get_coins(self): + coins = 0 + + while coins < 1000: + # Go to the wishing well & examine + self.wishing_well() + self.action('examine') + # Go to hinted block + # Call the miner + response = request.post(f'https://lambda-treasure-hunt.herokuapp.com/api/bc/last_proof/', headers=self.headers) + response = request.post(f'https://lambda-treasure-hunt.herokuapp.com/api/bc/mine/', headers=self.headers, json={"proof":''}) + # Submit last_proof request + # Submit Mine request + pass \ No newline at end of file From 247a8e9f6a14c398b2bec6c449aa80ba07e8bf03 Mon Sep 17 00:00:00 2001 From: mrsteveson Date: Wed, 20 Nov 2019 00:28:07 -0600 Subject: [PATCH 20/32] cleanup and updates to get_coins --- basic_utils.py | 5 --- explore.py | 15 ++------ utils.py | 95 +++++++++++++++++++++++++++----------------------- 3 files changed, 53 insertions(+), 62 deletions(-) diff --git a/basic_utils.py b/basic_utils.py index 8b2c4c6f..5276f57f 100644 --- a/basic_utils.py +++ b/basic_utils.py @@ -1,15 +1,10 @@ class Queue(): def __init__(self): self.queue = [] -<<<<<<< HEAD - def __repr__(self): - return f'{self.queue}' -======= def __repr__(self): return f'{self.queue}' ->>>>>>> 753512d6a7fc8593e652c6d2bc28517c4f3d0a5b def enqueue(self, value): self.queue.append(value) diff --git a/explore.py b/explore.py index 9e64a5b7..a861b541 100644 --- a/explore.py +++ b/explore.py @@ -1,20 +1,9 @@ from utils import * mappy = mapper() -<<<<<<< HEAD mappy.accumulate=True #set pick up items to false mappy.import_text_map=True mappy.create_starting_map() -# mappy.go_to_room('10') -# print(mappy.player.currentRoom) -mappy.explore_random(150) -======= -mappy.accumulate = True # set pick up items to false -mappy.import_text_map = True -mappy.create_starting_map() -# mappy.go_to_room(1) -mappy.get_treasure() -# mappy.get_info() -# mappy.explore_random(118) ->>>>>>> 753512d6a7fc8593e652c6d2bc28517c4f3d0a5b +# mappy.explore_random(150) +mappy.get_treasure() diff --git a/utils.py b/utils.py index 5cbc0207..8dbd82da 100644 --- a/utils.py +++ b/utils.py @@ -1,5 +1,6 @@ from decouple import config from basic_utils import * +from miner import * import requests import json import time @@ -46,6 +47,7 @@ def __init__(self, auth=auth_key, save=True, load_map=True): # import map so far - setting to false starts from scratch self.import_text_map = load_map self.player = None + self.important_rooms = {} # May not need this anymore (all special rooms are id'd) def get_info(self, what='init', direction=None, backtrack=None): """multi purpose move & init function - this is used @@ -93,7 +95,6 @@ def action(self, what='take', treasure=None, name=None): response = requests.post( f'{my_url}{what}/', headers=self.header, json={"name": treasure, "confirm": "yes"}) - # Change Name +++++++ if what == 'change_name': response = requests.post( f'{my_url}{what}/', headers=self.header, json={"name": name, "confirm": "aye"}) @@ -120,20 +121,14 @@ def room_check(self): if self.info['title'] == "Linh's Shrine" and self.pray: # there may be other shrines self.info = self.action('pray') - # Would this sell? ++++++ if self.info['title'] == "shop": self.info = self.action('sell', item) self.info = self.action('confirm_sell') - # Could this do the name change? +++++++ - # if self.info['title'] == "Pirate Ry's": - # self.info = self.action('change_name', name) - # self.info = self.action('confirm_name') - def create_starting_map(self): """"initiates your starting map which is stored under the vertices of a graph class""" info_dict = self.get_info() - print(info_dict) # this can be deactivated - just helpful at first + # print(info_dict) # this can be deactivated - just helpful at first self.my_map = Graph() self.player = Player("MicahJones", info_dict['room_id']) exits = info_dict['exits'] @@ -146,6 +141,11 @@ def create_starting_map(self): string_dict = json.loads(file.read()) for key in string_dict: self.my_map.vertices[int(key)] = string_dict[key] + # May not need this anymore since all important rooms are id'd + with open('rooms.txt', 'r') as file: + string_dict = json.loads(file.read()) + for key in string_dict: + self.important_rooms[key] = string_dict[key] else: print("fresh map triggered") self.my_map.vertices[self.player.currentRoom] = exit_dict @@ -172,6 +172,11 @@ def pop_map_on_move(self, move): if self.save_map_to_text: with open('map.txt', 'w') as file: file.write(json.dumps(self.my_map.vertices)) + # May not need this anymore since all important rooms are id'd + self.important_rooms.update({info['title']: info['room_id']}) + if self.save_map_to_text: + with open('rooms.txt', 'w') as file: + file.write(json.dumps(self.important_rooms)) def count_unmapped(self): """counts all the unmapped rooms""" @@ -200,7 +205,6 @@ def bfs_for_q(self): q.enqueue([room]) while '?' not in self.my_map.vertices[room].values(): - joins = self.my_map.vertices[room] for j in joins.values(): if j in q.queue[0]: @@ -216,7 +220,7 @@ def bfs_for_q(self): def explore_random(self, counter=5): """explores the map choosing random ? and backtracks using bfs - counter is the number of times you want it to explore unkown rooms""" + counter is the number of times you want it to explore unknown rooms""" unmapped_number = self.count_unmapped() moves = [] c = 0 @@ -247,39 +251,29 @@ def explore_random(self, counter=5): c += 1 def go_to_room(self, destination): - """depth first traversal to particular room in shortest route - NOT OPERATIONAL YET""" - # self.accumulate = False + """Breath First Traversal to particular room in shortest route""" print('moving') path = self.my_map.bfs(self.player.currentRoom, destination) - # print(f'Path: {path}') for m in path: room = self.player.currentRoom - # print(f'Room: {type(room)}') exits = self.my_map.vertices[room] - # print(f'Room: {room}') for direction in exits: if self.my_map.vertices[room][direction] == m: - # print(f'direction: {direction}') self.get_info(what='move', direction=direction) print( f"Current Room -> Title: {self.info['title']} ID: {self.info['room_id']} Items: {self.info['items']}") else: - # print(f'go_to_room FAILED: Invalid Direction[{direction}], Room[{room}]') continue - # self.accumulate = True def pirate(self): # Goes directly to pirate ry - # self.go_to_room(self.important_rooms['pirate ry']) - # time.sleep(self.wait) - pass + self.go_to_room(467) + time.sleep(self.wait) def wishing_well(self): # Goes directly to pirate ry - # self.go_to_room(self.important_rooms['wishing well']) - # time.sleep(self.wait) - pass + self.go_to_room(55) + time.sleep(self.wait) def vendor(self): # Goes directly to the shop @@ -294,8 +288,6 @@ def vendor(self): # Keep looting and selling until stopped. def get_treasure(self): while True: - # get a name - #! This request is being used to get information about our player from the lambda server. #! This would probably be better off initializing our local player attributes at the top, but #! I tried this first. @@ -304,29 +296,28 @@ def get_treasure(self): headers = {'Authorization': f'Token {token}'} r = requests.post(url, headers=headers) ret_data = r.json() - + print('\n') print(f"***********Current Character Attributes***************") print(ret_data) print("*******************************************************") #!------------------------This name is specific to each person, be sure to change this to yours. - if ret_data['name'] == 'player446' and ret_data['gold'] >= 1000: + if ret_data['name'] == 'player420' and ret_data['gold'] >= 1000: # Go to name changer (pirate ry) print('Time to Buy a Name') # * Made this false here so that we don't somehow pick up a ton of treasure on the way, and # * get over-encumbered. self.accumulate = False - self.go_to_room(467) # pirate ry's room + self.pirate() # pirate ry's room time.sleep(self.wait) # Buy name #! -------------------------- Change the name here to be what you want!! - self.action('change_name', name='Micah') + self.action('change_name', name='SirSnuffaluffagus') time.sleep(self.wait) #! This print isn't accurate. It doesn't update when you actually change your name. #! Next time you see it, it should have changed though. - print( - f"Got a name! Time to get a COIN. New Name: {ret_data['name']}") + print(f"Got a name! Time to get a COIN. New Name: {ret_data['name']}") time.sleep(self.wait) # self.action('status') #Check new name elif ret_data['encumbrance'] <= ret_data['strength'] - 2: @@ -361,14 +352,30 @@ def get_treasure(self): print(f"You're current gold: {ret_data['gold']}") print('Back to Looting') - -""" -need a function to go to name changer and buy a name. -funciton to go to wishing well and interact -function to go to where wishing well says and mine a coin - -SHOP is in room id 1 -Name changer is in 467 -Well is 55 - -""" + def get_coins(self): + # Want this to do 3 things: + # Function to go to the wishing well and examine + # Function to go to where the wishing well says + # Function to mine coin at specified location + # Could include if clause to go transmog coins + + coins = 0 + # variable for proof? + while coins < 1000: + # Go to wishing well + print('Going to the Wishing Well.') + self.wishing_well() + # Examine well + self.action('examine') + # Go to where it says + self.go_to_room('hinted location/room') + # Mine Coin + print('Getting proof...') + response = request.post(f'https://lambda-treasure-hunt.herokuapp.com/api/bc/last_proof/', headers=self.headers) + new_proof = proof_of_work(data.get('proof'), data.get('difficulty')) + time.sleep(self.wait) + # Need to send new_proof in the mine request json + response = request.post(f'https://lambda-treasure-hunt.herokuapp.com/api/bc/mine/', headers=self.headers, json={"proof":''}) + print('You got a coin!') + coins += 1 + time.sleep(self.wait) \ No newline at end of file From e39caba7a7fad3ce8ba3b91c400e290d5b0209b5 Mon Sep 17 00:00:00 2001 From: Daniel Horsley Date: Wed, 20 Nov 2019 12:17:42 +0000 Subject: [PATCH 21/32] added bunch of mining functionality --- cpu.py | 300 +++++++++++++++++++++++++++++++++++++++++++++++++++++ hinter.ls8 | 131 +++++++++++++++++++++++ ls8.py | 11 ++ miner.py | 15 ++- utils.py | 58 ++++++++++- 5 files changed, 509 insertions(+), 6 deletions(-) create mode 100644 cpu.py create mode 100644 hinter.ls8 create mode 100644 ls8.py diff --git a/cpu.py b/cpu.py new file mode 100644 index 00000000..14dee81e --- /dev/null +++ b/cpu.py @@ -0,0 +1,300 @@ +"""CPU functionality.""" + +import sys +import os +import datetime +import msvcrt, time +script_dir = os.path.dirname(__file__) + +class CPU: + """Main CPU class.""" + + def __init__(self): + """Construct a new CPU.""" + self.ram = [0] * 256 + self.reg = [0] * 8 + self.reg[7] = 0xF4 + self.pc = 0 + self.MAR = 0 + self.MDR = 0 + self.FL = 0b00000000 + self.errorcode = 0 + self.stopmoreinterrupts = False + self.then = datetime.datetime.today() + + + def load(self,to_load='examples\mult.ls8'): + """Load a program into memory.""" + + address = 0 + with open(os.path.join(script_dir, to_load)) as f: + program = f.readlines() + program = [x for x in program if x[0]!='#'] + program = [int(x[:8],2) for x in program] + + # For now, we've just hardcoded a program: + # program = [ + # # From print8.ls8 + # 0b10000010, # LDI R0,8 + # 0b00000000, + # 0b00001000, + # 0b01000111, # PRN R0 + # 0b00000000, + # 0b00000001, # HLT + # ] + + for instruction in program: + self.ram[address] = instruction + address += 1 + + + def alu(self, op, reg_a, reg_b): + """ALU operations.""" + + if op == "ADD": + self.reg[reg_a] += self.reg[reg_b] + elif op == "INC": + self.reg[reg_a] += 1 + elif op == "DEC": + self.reg[reg_a] -= 1 + elif op == "SUB": + self.reg[reg_a] = self.reg[reg_a]-self.reg[reg_b] + elif op == "MUL": + self.reg[reg_a] = self.reg[reg_a]*self.reg[reg_b] + elif op == "MOD": + if self.reg[reg_b]!=0: + self.reg[reg_a] = self.reg[reg_a]%self.reg[reg_b] + else: + raise Exception("can't divide by zero") + # elif op == "ST": + # self.reg[reg_a] = self.reg[reg_b] + elif op == "SHL": + self.reg[reg_a] = self.reg[reg_a] << self.reg[reg_b] + elif op == "CMP": + if self.reg[reg_a] == self.reg[reg_b]: + self.FL = 0b00000001 + elif self.reg[reg_a] > self.reg[reg_b]: + self.FL = 0b00000010 + elif self.reg[reg_a] < self.reg[reg_b]: + self.FL = 0b00000100 + + elif op == "SHR": + self.reg[reg_a] = self.reg[reg_a] >> self.reg[reg_b] + elif op == "XOR": + self.reg[reg_a] = self.reg[reg_a] ^ self.reg[reg_a] + elif op == "OR": + self.reg[reg_a] = self.reg[reg_a] | self.reg[reg_a] + elif op == "NOT": + self.reg[reg_a] = ~self.reg[reg_a] + elif op == "AND": + self.reg[reg_a] = self.reg[reg_a] & self.reg[reg_a] + elif op == "DIV": + if self.reg[reg_b]!=0: + self.reg[reg_a] = self.reg[reg_a]/self.reg[reg_b] + else: + raise Exception("can't divide by zero") + else: + raise Exception("Unsupported ALU operation") + + def ram_read(self,address): + return self.ram[address] + + def ram_write(self,address,value): + self.ram[address] = value + + def trace(self): + """ + Handy function to print out the CPU state. You might want to call this + from run() if you need help debugging. + """ + + print(f"TRACE: %02X | %02X %02X %02X |" % ( + self.pc, + #self.fl, + #self.ie, + self.ram_read(self.pc), + self.ram_read(self.pc + 1), + self.ram_read(self.pc + 2) + ), end='') + + for i in range(8): + print(" %02X" % self.reg[i], end='') + + print() + + def run(self): + """Run the CPU.""" + while self.pc!='HALT': + time.sleep(0.01) + if not self.stopmoreinterrupts: + if msvcrt.kbhit(): + self.ram[0xF4] = ord(msvcrt.getch()) + #print('key trigger',chr(self.ram[0xF4]),'**********',self.ram[0xF4]) + self.reg[6] = 0b00000010 + self.stopmoreinterrupts = True + #check timer + if not self.stopmoreinterrupts: + now = datetime.datetime.today() + if (now - self.then).seconds > 0: + #print('timer interrupt trigger') + self.then = now + #print('A') + self.reg[6] = 0b00000001 + else: + self.reg[6] = 0b00000000 + #interrupt handle + #first IM & IS register bitwise &ed and stored in masked interrupts + maskedInterrupts = (self.reg[5] & self.reg[6]) + #print(bin(maskedInterrupts),'mi',bin(self.reg[5]),'5',bin(self.reg[6]),'6') + i=0 + while (maskedInterrupts >> i) % 0b10 != 0b1 and i<8: + i=i+1 + if i<8: #if triggered + #print('*************************interrupt triggered') + self.stopmoreinterrupts = True + self.reg[6]=0b00000000 + self.reg[7] -=1 + self.ram[self.reg[7]] = self.pc + self.reg[7] -=1 + self.ram[self.reg[7]] = self.FL + for j in range(7): + self.reg[7]-=1 + self.ram[self.reg[7]] = self.reg[j] + # address (vector) of the appr handler looked up from the interrupt vector table. + self.pc = self.ram[0xF8+i] + #mainloop + opp_dict = {0b0000:'ADD', 0b0001:'SUB', 0b0101:'INC', 0b0110:'DEC', 0b0010:'MUL', 0b0011:'DIV', + 0b1011:'XOR', 0b0100:'ST', 0b1010:'OR',0b1100:'SHL', 0b1101:'SHR',0b1000 : 'AND', + 0b0111 :'CMP',0b1001:'NOT'} + ir = self.ram_read(self.pc) + operand_a = self.ram_read(self.pc+1) + operand_b = self.ram_read(self.pc+2) + if self.errorcode == f'pc{self.pc}ir{ir}a{operand_a}b{operand_b}': + self.pc='HALT' + #print('flag',bin(self.FL),'pc',self.pc,'ir',bin(ir),'a',operand_a,'b',operand_b)#,bin(self.reg[operand_a])) + self.errorcode = f'pc{self.pc}ir{ir}a{operand_a}b{operand_b}' + if ((ir >>5 ) % 0b10) == 0b1 : + #print('ALU trigger') ## USE ALU + self.alu(opp_dict[ir % (ir>>4)],operand_a,operand_b) + self.pc += (ir >> 6) + 1 + elif ir == 0b00000000: + #NOP + pass + elif ir == 0b01010000: + # CALL - first push address of next instruction onto stack + self.reg[7] -=1 + self.ram[self.reg[7]] = self.pc+2 + #then set pc equal to call address + self.pc = self.reg[operand_a] + pass + elif ir == 0b00000001: + # HLT + self.pc = 'HALT' + elif ir == 0b01010010: + + # INT + #set nth bit of IS register to given register + self.reg[6]= 0b1 << self.reg[operand_a] + + elif ir == 0b000010011: + # IRET + #R6 to #R0 popped off stack + for i in range(7): + self.reg[6-i] = self.ram[self.reg[7]] + self.reg[7]+=1 + #FL register popped off stack + self.FL = self.ram[self.reg[7]] + self.reg[7] +=1 + self.pc = self.ram[self.reg[7]] + self.reg[7] +=1 + self.stopmoreinterrupts = False + + + elif ir ==0b01010101: + # JEQ + if self.FL % 0b10 == 0b1: + self.pc = self.reg[operand_a] + else: + self.pc+=2 + elif ir ==0b01011010: + # JGE + if self.FL % 0b10==0b1 or (sl.FL >> 1)%0b10==0b1: + self.pc = self.reg[operand_a] + else: + self.pc+=2 + elif ir == 0b01011001: + #JLE + if self.FL % 0b10==0b1 or (sl.FL >> 2)%0b10==0b1: + self.pc = self.reg[operand_a] + else: + self.pc+=2 + elif ir == 0b01011000: + #JLT + if (sl.FL >> 2)%0b10==0b1: + self.pc = self.reg[operand_a] + else: + self.pc+=2 + elif ir == 0b01010100: + #JMP + self.pc = self.reg[operand_a] + elif ir == 0b01010110: + #JNE + if self.FL % 0b10==0b0: + self.pc = self.reg[operand_a] + else: + self.pc+=2 + elif ir == 0b10000011: + # LD + #print(operand_b,self.ram[operand_b],'****LD check') + self.reg[operand_a] = self.ram[self.reg[operand_b]] + self.pc+=3 + elif ir == 0b10000010: + #print('LDI trigger') + #LDI - sets value of register qual to integer + self.reg[operand_a] = operand_b + self.pc+=3 + elif ir == 0b01001000: + #PRA + #print('PRA check',self.reg[operand_a]) + #print(chr(self.reg[operand_a])) + print(chr(self.reg[operand_a]),end='') + self.pc +=2 + elif ir == 0b01000111: + #PRN + print(self.reg[operand_a]) + self.pc +=2 + elif ir == 0b01000101: + #PUSH + self.reg[7] -=1 + self.ram[self.reg[7]] = self.reg[operand_a] + self.pc +=2 + + elif ir == 0b01000110: + #POP + self.reg[operand_a] = self.ram[self.reg[7]] + self.reg[7] +=1 + self.pc +=2 + + elif ir == 0b00010001: + #RET + self.pc = self.ram[self.reg[7]] + self.reg[7] += 1 + + elif ir == 0b10000100: + #ST + self.ram[self.reg[operand_a]] = self.reg[operand_b] + self.pc +=3 + + elif ir == 0b10001111: + #ADDI - add an immediate value to a register + self.reg[operand_a] = self.reg[operand_a] + operand_b + self.pc +=3 + + + + exit() + + + + + diff --git a/hinter.ls8 b/hinter.ls8 new file mode 100644 index 00000000..b7a1e556 --- /dev/null +++ b/hinter.ls8 @@ -0,0 +1,131 @@ +10000010 +00000001 +01001101 +01001000 +00000001 +10000010 +00000001 +01101001 +01001000 +00000001 +10000010 +00000001 +01101110 +01001000 +00000001 +10000010 +00000001 +01100101 +01001000 +00000001 +10000010 +00000001 +00100000 +01001000 +00000001 +10000010 +00000001 +01111001 +01001000 +00000001 +10000010 +00000001 +01101111 +01001000 +00000001 +10000010 +00000001 +01110101 +01001000 +00000001 +10000010 +00000001 +01110010 +01001000 +00000001 +10000010 +00000001 +00100000 +01001000 +00000001 +10000010 +00000001 +01100011 +01001000 +00000001 +10000010 +00000001 +01101111 +01001000 +00000001 +10000010 +00000001 +01101001 +01001000 +00000001 +10000010 +00000001 +01101110 +01001000 +00000001 +10000010 +00000001 +00100000 +01001000 +00000001 +10000010 +00000001 +01101001 +01001000 +00000001 +10000010 +00000001 +01101110 +01001000 +00000001 +10000010 +00000001 +00100000 +01001000 +00000001 +10000010 +00000001 +01110010 +01001000 +00000001 +10000010 +00000001 +01101111 +01001000 +00000001 +10000010 +00000001 +01101111 +01001000 +00000001 +10000010 +00000001 +01101101 +01001000 +00000001 +10000010 +00000001 +00100000 +01001000 +00000001 +10000010 +00000001 +00110100 +01001000 +00000001 +10000010 +00000001 +00110111 +01001000 +00000001 +10000010 +00000001 +00110110 +01001000 +00000001 +00000001 diff --git a/ls8.py b/ls8.py new file mode 100644 index 00000000..c0f0724d --- /dev/null +++ b/ls8.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 + +"""Main.""" + +import sys +from cpu import * + +cpu = CPU() + +cpu.load('hinter.ls8') +cpu.run() \ No newline at end of file diff --git a/miner.py b/miner.py index ff52b3ba..3a7acbcf 100644 --- a/miner.py +++ b/miner.py @@ -14,21 +14,28 @@ def proof_of_work(last_proof, difficulty): total_tries = 0 prev_proof = f'{last_proof}'.encode() last_hash = hashlib.sha256(prev_proof).hexdigest() - while valid_proof(last_hash, proof, difficulty) is False: - proof = random.randint(0, 10000) + #while valid_proof(last_hash, proof, difficulty) is False: + while valid_proof(last_proof, proof, difficulty) is False: + #proof = random.randint(0, 10000) + proof+=1 total_tries += 1 + if total_tries % 1000000 == 0: + print(total_tries/1000000,'million tries') print("Proof found: " + str(proof) + " in " + str(timer() - start)) return proof def valid_proof(last_hash, proof, difficulty): - guess = f'{proof}'.encode() + guess = f'{proof}' + guess = (str(last_hash)+str(guess)).encode() #there was no ref to last hash in valid proof guess_hash = hashlib.sha256(guess).hexdigest() + #guess_hash = hash(guess)#.hexdigest() if difficulty is not None: leading_zeros = "0" * difficulty else: leading_zeros = "0" * 6 - return guess_hash[0:difficulty] == leading_zeros \ No newline at end of file + #return guess_hash[0:difficulty] == leading_zeros + return str(guess_hash)[0:difficulty] == leading_zeros \ No newline at end of file diff --git a/utils.py b/utils.py index 8dbd82da..41c5c7df 100644 --- a/utils.py +++ b/utils.py @@ -88,7 +88,7 @@ def action(self, what='take', treasure=None, name=None): f'{my_url}{what}/', headers=self.header, json={"name": treasure}) print(f"Action: {what}") - if what in ['status', 'pray']: + if what in ['status', 'pray','balance']: response = requests.post(f'{my_url}{what}/', headers=self.header) if what == 'confirm_sell': @@ -378,4 +378,58 @@ def get_coins(self): response = request.post(f'https://lambda-treasure-hunt.herokuapp.com/api/bc/mine/', headers=self.headers, json={"proof":''}) print('You got a coin!') coins += 1 - time.sleep(self.wait) \ No newline at end of file + time.sleep(self.wait) + + def hint_to_ld8(self): + "converts hint in well to room number" + self.action('examine','well') + z = self.info['description'] #read the last info to get the hint + z = assemb.split('\n')[2:] + # with open('hinter.ls8','w') as f: + # for ass in assemb: + # f.write("%s\n" % ass) + #quicker way to parse message + z = [int(zz,2) for zz in z] + chars = [chr(zz) for zz in z] + print(chars) + char_index = list(range(2,131,5)) + message = [] + for c in char_index: + message.append(chars[c]) + mine_room = message[-3:] + try: + self.mine_room = int(''.join(mine_room)) + except: + try: + self.mine_room = int(''.join(mine_room[-2:])) + except: + self.mine_room = int(''.join(mine_room[-1:])) + + return self.mine_room + + def get_proof(self): + """gets last proof then obtains proof of work + then posts new proof to server""" + print('Getting proof...') + response = requests.get(f'https://lambda-treasure-hunt.herokuapp.com/api/bc/last_proof/', headers=self.header) + self.last_proof = json.loads(response.content) + print(self.last_proof) + my_proof = proof_of_work(self.last_proof['proof'],self.last_proof['difficulty']) + self.get_mine(my_proof) + time.sleep(self.mine_response['cooldown']) + + def get_mine(self,new_proof): + """posts your new proof to the server - note high cooldown penalties for posting wrong proof""" + params = {"proof" : new_proof} + response = requests.post(f'https://lambda-treasure-hunt.herokuapp.com/api/bc/mine/', headers=self.header,json = params) + self.mine_response = json.loads(response.content) + print(self.mine_response) + + def sell_all_items(self): + """sells all items if you are in the shop""" + self.action('status') + inv = self.info['inventory'] + for i in inv: + self.action('sell',i) + self.action('confirm_sell',i) + From 39e0060e8537291dffe0d40ac0aa68af74cd6498 Mon Sep 17 00:00:00 2001 From: Daniel Horsley Date: Wed, 20 Nov 2019 16:07:57 +0000 Subject: [PATCH 22/32] dash functions almost working --- utils.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 3 deletions(-) diff --git a/utils.py b/utils.py index 41c5c7df..476ae14b 100644 --- a/utils.py +++ b/utils.py @@ -47,6 +47,8 @@ def __init__(self, auth=auth_key, save=True, load_map=True): # import map so far - setting to false starts from scratch self.import_text_map = load_map self.player = None + self.fly = False + self.dash = False self.important_rooms = {} # May not need this anymore (all special rooms are id'd) def get_info(self, what='init', direction=None, backtrack=None): @@ -57,8 +59,16 @@ def get_info(self, what='init', direction=None, backtrack=None): response = requests.get(f'{my_url}{what}/', headers=self.header) elif what == 'move': + if self.fly and self.info['elevation']>0: + response = requests.post( + f'{my_url}fly/', headers=self.header, json={"direction": direction}) + else: + response = requests.post( + f'{my_url}move/', headers=self.header, json={"direction": direction}) + + elif what == 'fly': response = requests.post( - f'{my_url}move/', headers=self.header, json={"direction": direction}) + f'{my_url}fly/', headers=self.header, json={"direction": direction}) elif what == 'backtrack': response = requests.post(f'{my_url}move/', headers=self.header, @@ -88,7 +98,7 @@ def action(self, what='take', treasure=None, name=None): f'{my_url}{what}/', headers=self.header, json={"name": treasure}) print(f"Action: {what}") - if what in ['status', 'pray','balance']: + if what in ['status', 'pray']: response = requests.post(f'{my_url}{what}/', headers=self.header) if what == 'confirm_sell': @@ -98,6 +108,10 @@ def action(self, what='take', treasure=None, name=None): if what == 'change_name': response = requests.post( f'{my_url}{what}/', headers=self.header, json={"name": name, "confirm": "aye"}) + + if what == 'balance': + response = requests.get( + 'https://lambda-treasure-hunt.herokuapp.com/api/bc/get_balance/', headers=self.header) if response.status_code == 200: self.info = json.loads(response.content) @@ -251,7 +265,7 @@ def explore_random(self, counter=5): c += 1 def go_to_room(self, destination): - """Breath First Traversal to particular room in shortest route""" + """Breadth First Traversal to particular room in shortest route""" print('moving') path = self.my_map.bfs(self.player.currentRoom, destination) for m in path: @@ -265,6 +279,69 @@ def go_to_room(self, destination): else: continue + def dash_to_room(self, destination): + "same as go to room but with dash" + print('dashing') + path = self.my_map.bfs(self.player.currentRoom, destination) + my_dirs = self.get_dirs(path) + i = 0 + while i < len(path): + if my_dirs[i]==my_dirs[i+1]: + dash_path = self.get_dash_path(path[i:],my_dirs[i:]) + self.make_dash(my_dirs[i],dash_path[1:]) + print(f"Current Room -> Title: {self.info['title']} ID: {self.info['room_id']} Items: {self.info['items']}") + i += len(dash_path[1:]) + else: + room = self.player.currentRoom + exits = self.my_map.vertices[room] + for direction in exits: + if self.my_map.vertices[room][direction] == path[i]: + self.get_info(what='move', direction=direction) + print( + f"Current Room -> Title: {self.info['title']} ID: {self.info['room_id']} Items: {self.info['items']}") + + else: + pass + i+=1 + + + def get_dash_path(self,traversal,dirs): + "check if the path in go to room contains a dashable stretch" + print('dash path check',traversal,dirs) + dash_list = [] + j = 0 + while dirs[j]==dirs[j+1]: + dash_list.append(traversal[j]) + j += 1 + dash_list.append(traversal[j]) + dash_list.append(traversal[j+1]) + print('dash_list',dash_list) + return dash_list + + def make_dash(self,direction,traversal): + "make a dash given direction and traversal" + string_rooms = ','.join([str(x) for x in traversal]) + params = {"direction":direction, "num_rooms":str(len(traversal)), + "next_room_ids": string_rooms} + print('dash_list_json',params,f'{my_url}dash/') + response = requests.post( + f'{my_url}dash/', headers=self.header, json=params) + + if response.status_code == 200: + self.info = json.loads(response.content) + if self.player is not None: + self.player.currentRoom = self.info['room_id'] + + if 'cooldown' in self.info.keys(): + time.sleep(self.info['cooldown']) + + self.room_check() + return self.info + else: + print('cooldown triggered - waiting 20 seconds. code =',response.status_code,response.content) + time.sleep(20) + self.get_info(what=what, direction=direction, backtrack=backtrack) + def pirate(self): # Goes directly to pirate ry self.go_to_room(467) @@ -433,3 +510,6 @@ def sell_all_items(self): self.action('sell',i) self.action('confirm_sell',i) + + + From c46aac26a91ccffa80dc37130e3f88ee9df7bb73 Mon Sep 17 00:00:00 2001 From: Daniel Horsley Date: Wed, 20 Nov 2019 16:37:41 +0000 Subject: [PATCH 23/32] ls8 sort of workgin --- hinter.ls8 | 42 +++++++++++++++++++++++++++++++++++--- utils.py | 59 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 70 insertions(+), 31 deletions(-) diff --git a/hinter.ls8 b/hinter.ls8 index b7a1e556..bcd471f5 100644 --- a/hinter.ls8 +++ b/hinter.ls8 @@ -115,17 +115,53 @@ 00000001 10000010 00000001 -00110100 +00010000 +10000010 +00000010 +10111010 +10101000 +00000001 +00000010 +10000010 +00000010 +00100100 +10101011 +00000001 +00000010 01001000 00000001 10000010 00000001 -00110111 +11010110 +10000010 +00000010 +11010000 +10101000 +00000001 +00000010 +10000010 +00000010 +11100110 +10101011 +00000001 +00000010 01001000 00000001 10000010 00000001 -00110110 +11111110 +10000010 +00000010 +00111001 +10101000 +00000001 +00000010 +10000010 +00000010 +00000000 +10101011 +00000001 +00000010 01001000 00000001 00000001 diff --git a/utils.py b/utils.py index 476ae14b..5407cb88 100644 --- a/utils.py +++ b/utils.py @@ -283,10 +283,12 @@ def dash_to_room(self, destination): "same as go to room but with dash" print('dashing') path = self.my_map.bfs(self.player.currentRoom, destination) + print('bfs',path) my_dirs = self.get_dirs(path) i = 0 - while i < len(path): + while i < len(path)-1: if my_dirs[i]==my_dirs[i+1]: + print('dashing') dash_path = self.get_dash_path(path[i:],my_dirs[i:]) self.make_dash(my_dirs[i],dash_path[1:]) print(f"Current Room -> Title: {self.info['title']} ID: {self.info['room_id']} Items: {self.info['items']}") @@ -295,27 +297,27 @@ def dash_to_room(self, destination): room = self.player.currentRoom exits = self.my_map.vertices[room] for direction in exits: - if self.my_map.vertices[room][direction] == path[i]: - self.get_info(what='move', direction=direction) + if self.my_map.vertices[room][direction] == path[i+1]: + self.get_info(what='backtrack', direction=direction,backtrack=path[i+1]) print( f"Current Room -> Title: {self.info['title']} ID: {self.info['room_id']} Items: {self.info['items']}") else: - pass + continue i+=1 def get_dash_path(self,traversal,dirs): "check if the path in go to room contains a dashable stretch" - print('dash path check',traversal,dirs) + #print('dash path check',traversal,dirs) dash_list = [] j = 0 - while dirs[j]==dirs[j+1]: + while (j Date: Wed, 20 Nov 2019 20:48:02 +0000 Subject: [PATCH 24/32] corrected ls8 --- cpu.py | 300 ------------------------------------------------------- cpu2.py | 267 +++++++++++++++++++++++++++++++++++++++++++++++++ ls8.py | 2 +- utils.py | 35 ++----- 4 files changed, 279 insertions(+), 325 deletions(-) delete mode 100644 cpu.py create mode 100644 cpu2.py diff --git a/cpu.py b/cpu.py deleted file mode 100644 index 14dee81e..00000000 --- a/cpu.py +++ /dev/null @@ -1,300 +0,0 @@ -"""CPU functionality.""" - -import sys -import os -import datetime -import msvcrt, time -script_dir = os.path.dirname(__file__) - -class CPU: - """Main CPU class.""" - - def __init__(self): - """Construct a new CPU.""" - self.ram = [0] * 256 - self.reg = [0] * 8 - self.reg[7] = 0xF4 - self.pc = 0 - self.MAR = 0 - self.MDR = 0 - self.FL = 0b00000000 - self.errorcode = 0 - self.stopmoreinterrupts = False - self.then = datetime.datetime.today() - - - def load(self,to_load='examples\mult.ls8'): - """Load a program into memory.""" - - address = 0 - with open(os.path.join(script_dir, to_load)) as f: - program = f.readlines() - program = [x for x in program if x[0]!='#'] - program = [int(x[:8],2) for x in program] - - # For now, we've just hardcoded a program: - # program = [ - # # From print8.ls8 - # 0b10000010, # LDI R0,8 - # 0b00000000, - # 0b00001000, - # 0b01000111, # PRN R0 - # 0b00000000, - # 0b00000001, # HLT - # ] - - for instruction in program: - self.ram[address] = instruction - address += 1 - - - def alu(self, op, reg_a, reg_b): - """ALU operations.""" - - if op == "ADD": - self.reg[reg_a] += self.reg[reg_b] - elif op == "INC": - self.reg[reg_a] += 1 - elif op == "DEC": - self.reg[reg_a] -= 1 - elif op == "SUB": - self.reg[reg_a] = self.reg[reg_a]-self.reg[reg_b] - elif op == "MUL": - self.reg[reg_a] = self.reg[reg_a]*self.reg[reg_b] - elif op == "MOD": - if self.reg[reg_b]!=0: - self.reg[reg_a] = self.reg[reg_a]%self.reg[reg_b] - else: - raise Exception("can't divide by zero") - # elif op == "ST": - # self.reg[reg_a] = self.reg[reg_b] - elif op == "SHL": - self.reg[reg_a] = self.reg[reg_a] << self.reg[reg_b] - elif op == "CMP": - if self.reg[reg_a] == self.reg[reg_b]: - self.FL = 0b00000001 - elif self.reg[reg_a] > self.reg[reg_b]: - self.FL = 0b00000010 - elif self.reg[reg_a] < self.reg[reg_b]: - self.FL = 0b00000100 - - elif op == "SHR": - self.reg[reg_a] = self.reg[reg_a] >> self.reg[reg_b] - elif op == "XOR": - self.reg[reg_a] = self.reg[reg_a] ^ self.reg[reg_a] - elif op == "OR": - self.reg[reg_a] = self.reg[reg_a] | self.reg[reg_a] - elif op == "NOT": - self.reg[reg_a] = ~self.reg[reg_a] - elif op == "AND": - self.reg[reg_a] = self.reg[reg_a] & self.reg[reg_a] - elif op == "DIV": - if self.reg[reg_b]!=0: - self.reg[reg_a] = self.reg[reg_a]/self.reg[reg_b] - else: - raise Exception("can't divide by zero") - else: - raise Exception("Unsupported ALU operation") - - def ram_read(self,address): - return self.ram[address] - - def ram_write(self,address,value): - self.ram[address] = value - - def trace(self): - """ - Handy function to print out the CPU state. You might want to call this - from run() if you need help debugging. - """ - - print(f"TRACE: %02X | %02X %02X %02X |" % ( - self.pc, - #self.fl, - #self.ie, - self.ram_read(self.pc), - self.ram_read(self.pc + 1), - self.ram_read(self.pc + 2) - ), end='') - - for i in range(8): - print(" %02X" % self.reg[i], end='') - - print() - - def run(self): - """Run the CPU.""" - while self.pc!='HALT': - time.sleep(0.01) - if not self.stopmoreinterrupts: - if msvcrt.kbhit(): - self.ram[0xF4] = ord(msvcrt.getch()) - #print('key trigger',chr(self.ram[0xF4]),'**********',self.ram[0xF4]) - self.reg[6] = 0b00000010 - self.stopmoreinterrupts = True - #check timer - if not self.stopmoreinterrupts: - now = datetime.datetime.today() - if (now - self.then).seconds > 0: - #print('timer interrupt trigger') - self.then = now - #print('A') - self.reg[6] = 0b00000001 - else: - self.reg[6] = 0b00000000 - #interrupt handle - #first IM & IS register bitwise &ed and stored in masked interrupts - maskedInterrupts = (self.reg[5] & self.reg[6]) - #print(bin(maskedInterrupts),'mi',bin(self.reg[5]),'5',bin(self.reg[6]),'6') - i=0 - while (maskedInterrupts >> i) % 0b10 != 0b1 and i<8: - i=i+1 - if i<8: #if triggered - #print('*************************interrupt triggered') - self.stopmoreinterrupts = True - self.reg[6]=0b00000000 - self.reg[7] -=1 - self.ram[self.reg[7]] = self.pc - self.reg[7] -=1 - self.ram[self.reg[7]] = self.FL - for j in range(7): - self.reg[7]-=1 - self.ram[self.reg[7]] = self.reg[j] - # address (vector) of the appr handler looked up from the interrupt vector table. - self.pc = self.ram[0xF8+i] - #mainloop - opp_dict = {0b0000:'ADD', 0b0001:'SUB', 0b0101:'INC', 0b0110:'DEC', 0b0010:'MUL', 0b0011:'DIV', - 0b1011:'XOR', 0b0100:'ST', 0b1010:'OR',0b1100:'SHL', 0b1101:'SHR',0b1000 : 'AND', - 0b0111 :'CMP',0b1001:'NOT'} - ir = self.ram_read(self.pc) - operand_a = self.ram_read(self.pc+1) - operand_b = self.ram_read(self.pc+2) - if self.errorcode == f'pc{self.pc}ir{ir}a{operand_a}b{operand_b}': - self.pc='HALT' - #print('flag',bin(self.FL),'pc',self.pc,'ir',bin(ir),'a',operand_a,'b',operand_b)#,bin(self.reg[operand_a])) - self.errorcode = f'pc{self.pc}ir{ir}a{operand_a}b{operand_b}' - if ((ir >>5 ) % 0b10) == 0b1 : - #print('ALU trigger') ## USE ALU - self.alu(opp_dict[ir % (ir>>4)],operand_a,operand_b) - self.pc += (ir >> 6) + 1 - elif ir == 0b00000000: - #NOP - pass - elif ir == 0b01010000: - # CALL - first push address of next instruction onto stack - self.reg[7] -=1 - self.ram[self.reg[7]] = self.pc+2 - #then set pc equal to call address - self.pc = self.reg[operand_a] - pass - elif ir == 0b00000001: - # HLT - self.pc = 'HALT' - elif ir == 0b01010010: - - # INT - #set nth bit of IS register to given register - self.reg[6]= 0b1 << self.reg[operand_a] - - elif ir == 0b000010011: - # IRET - #R6 to #R0 popped off stack - for i in range(7): - self.reg[6-i] = self.ram[self.reg[7]] - self.reg[7]+=1 - #FL register popped off stack - self.FL = self.ram[self.reg[7]] - self.reg[7] +=1 - self.pc = self.ram[self.reg[7]] - self.reg[7] +=1 - self.stopmoreinterrupts = False - - - elif ir ==0b01010101: - # JEQ - if self.FL % 0b10 == 0b1: - self.pc = self.reg[operand_a] - else: - self.pc+=2 - elif ir ==0b01011010: - # JGE - if self.FL % 0b10==0b1 or (sl.FL >> 1)%0b10==0b1: - self.pc = self.reg[operand_a] - else: - self.pc+=2 - elif ir == 0b01011001: - #JLE - if self.FL % 0b10==0b1 or (sl.FL >> 2)%0b10==0b1: - self.pc = self.reg[operand_a] - else: - self.pc+=2 - elif ir == 0b01011000: - #JLT - if (sl.FL >> 2)%0b10==0b1: - self.pc = self.reg[operand_a] - else: - self.pc+=2 - elif ir == 0b01010100: - #JMP - self.pc = self.reg[operand_a] - elif ir == 0b01010110: - #JNE - if self.FL % 0b10==0b0: - self.pc = self.reg[operand_a] - else: - self.pc+=2 - elif ir == 0b10000011: - # LD - #print(operand_b,self.ram[operand_b],'****LD check') - self.reg[operand_a] = self.ram[self.reg[operand_b]] - self.pc+=3 - elif ir == 0b10000010: - #print('LDI trigger') - #LDI - sets value of register qual to integer - self.reg[operand_a] = operand_b - self.pc+=3 - elif ir == 0b01001000: - #PRA - #print('PRA check',self.reg[operand_a]) - #print(chr(self.reg[operand_a])) - print(chr(self.reg[operand_a]),end='') - self.pc +=2 - elif ir == 0b01000111: - #PRN - print(self.reg[operand_a]) - self.pc +=2 - elif ir == 0b01000101: - #PUSH - self.reg[7] -=1 - self.ram[self.reg[7]] = self.reg[operand_a] - self.pc +=2 - - elif ir == 0b01000110: - #POP - self.reg[operand_a] = self.ram[self.reg[7]] - self.reg[7] +=1 - self.pc +=2 - - elif ir == 0b00010001: - #RET - self.pc = self.ram[self.reg[7]] - self.reg[7] += 1 - - elif ir == 0b10000100: - #ST - self.ram[self.reg[operand_a]] = self.reg[operand_b] - self.pc +=3 - - elif ir == 0b10001111: - #ADDI - add an immediate value to a register - self.reg[operand_a] = self.reg[operand_a] + operand_b - self.pc +=3 - - - - exit() - - - - - diff --git a/cpu2.py b/cpu2.py new file mode 100644 index 00000000..93dfece4 --- /dev/null +++ b/cpu2.py @@ -0,0 +1,267 @@ +import sys +from datetime import datetime # for timer interrupt +# Opcodes: +ADD = 0b10100000 +AND = 0b10101000 +CALL = 0b01010000 +CMP = 0b10100111 +DEC = 0b01100110 +DIV = 0b10100011 +HLT = 0b00000001 +INC = 0b01100101 +IRET = 0b00010011 +JEQ = 0b01010101 +JLE = 0b01011001 +JLT = 0b01011000 +JMP = 0b01010100 +JNE = 0b01010110 +LD = 0b10000011 +LDI = 0b10000010 +MUL = 0b10100010 +OR = 0b10101010 +POP = 0b01000110 +PRA = 0b01001000 +PRN = 0b01000111 +PUSH = 0b01000101 +RET = 0b00010001 +SHL = 0b10101100 +ST = 0b10000100 +SUB = 0b10100001 +XOR = 0b10101011 +# Reserved general-purpose register numbers: +IM = 5 +IS = 6 +SP = 7 +# CMP flags: +FL_LT = 0b100 +FL_GT = 0b010 +FL_EQ = 0b001 +# IS flags +IS_TIMER = 0b00000001 +IS_KEYBOARD = 0b00000010 +class CPU: + def __init__(self): + self.pc = 0 # program counter + self.fl = 0 # flags + self.ie = 1 + self.halted = False + self.last_timer_int = None + self.inst_set_pc = False # True if this instruction set the PC + self.ram = [0] * 256 + self.reg = [0] * 8 + self.reg[SP] = 0xf4 + self.bt = { # branch table + ADD: self.op_add, + AND: self.op_and, + CALL: self.op_call, + CMP: self.op_cmp, + DEC: self.op_dec, + DIV: self.op_div, + HLT: self.op_hlt, + INC: self.op_inc, + IRET: self.op_iret, + JEQ: self.op_jeq, + JLE: self.op_jle, + JLT: self.op_jlt, + JMP: self.op_jmp, + JNE: self.op_jne, + LD: self.op_ld, + LDI: self.op_ldi, + MUL: self.op_mul, + OR: self.op_or, + POP: self.op_pop, + PRA: self.op_pra, + PRN: self.op_prn, + PUSH: self.op_push, + RET: self.op_ret, + SHL: self.op_shl, + ST: self.op_st, + SUB: self.op_sub, + XOR: self.op_xor, + } + def load(self, filename): + address = 0 + with open(filename) as fp: + for line in fp: + comment_split = line.split("#") + num = comment_split[0].strip() + if num == '': # ignore blanks + continue + val = int(num, 2) + self.ram[address] = val + address += 1 + def ram_write(self, mdr, mar): + self.ram[mar] = mdr + def ram_read(self, mar): + return self.ram[mar] + def push_val(self, val): + self.reg[SP] -= 1 + self.ram_write(val, self.reg[7]) + def pop_val(self): + val = self.ram_read(self.reg[7]) + self.reg[SP] += 1 + return val + def alu(self, op, reg_a, reg_b): + if op == "ADD": + self.reg[reg_a] += self.reg[reg_b] + elif op == "AND": + self.reg[reg_a] &= self.reg[reg_b] + elif op == "SUB": + self.reg[reg_a] -= self.reg[reg_b] + elif op == "MUL": + self.reg[reg_a] *= self.reg[reg_b] + elif op == "DIV": + self.reg[reg_a] /= self.reg[reg_b] + elif op == "DEC": + self.reg[reg_a] -= 1 + elif op == "INC": + self.reg[reg_a] += 1 + elif op == "CMP": + self.fl &= 0x11111000 # clear all CMP flags + if self.reg[reg_a] < self.reg[reg_b]: + self.fl |= FL_LT + elif self.reg[reg_a] > self.reg[reg_b]: + self.fl |= FL_GT + else: + self.fl |= FL_EQ + elif op == "OR": + self.reg[reg_a] |= self.reg[reg_b] + elif op == "SHL": + self.reg[reg_a] <<= self.reg[reg_b] + elif op == "XOR": + self.reg[reg_a] ^= self.reg[reg_b] + else: + raise Exception("Unsupported ALU operation") + def check_for_timer_int(self): + """Check the time to see if a timer interrupt should fire.""" + if self.last_timer_int == None: + self.last_timer_int = datetime.now() + now = datetime.now() + diff = now - self.last_timer_int + if diff.seconds >= 1: # OK, fire! + self.last_timer_int = now + self.reg[IS] |= IS_TIMER + def handle_ints(self): + if not self.ie: # see if interrupts enabled + return + # Mask out interrupts + masked_ints = self.reg[IM] & self.reg[IS] + for i in range(8): + # See if the interrupt triggered + if masked_ints & (1 << i): + self.ie = 0 # disable interrupts + self.reg[IS] &= ~(1 << i) # clear bit for this interrupt + # Save all the work on the stack + self.push_val(self.pc) + self.push_val(self.fl) + for r in range(7): + self.push_val(self.reg[r]) + # Look up the address vector and jump to it + self.pc = self.ram_read(0xf8 + i) + break # no more processing + def trace(self): + print(f"TRACE: %02X | %02X | %d | %02X %02X %02X |" % ( + self.pc, + self.fl, + self.ie, + self.ram_read(self.pc), + self.ram_read(self.pc + 1), + self.ram_read(self.pc + 2) + ), end='') + for i in range(8): + print(" %02X" % self.reg[i], end='') + print() + def run(self): + while not self.halted: + # Interrupt cod + self.check_for_timer_int() # timer interrupt check + #self.check_for_keyboard_int() # keyboard interrupt check + self.handle_ints() # see if any interrupts occurre + # Normal instruction processing + #self.trace() + ir = self.ram[self.pc] + operand_a = self.ram_read(self.pc + 1) + operand_b = self.ram_read(self.pc + 2) + inst_size = ((ir >> 6) & 0b11) + 1 + self.inst_set_pc = ((ir >> 4) & 0b1) == 1 + if ir in self.bt: + self.bt[ir](operand_a, operand_b) + else: + raise Exception(f"Invalid instruction {hex(ir)} at address {hex(self.pc)}") + # If the instruction didn't set the PC, just move to the next instruction + if not self.inst_set_pc: + self.pc += inst_size + def op_ldi(self, operand_a, operand_b): + self.reg[operand_a] = operand_b + def op_prn(self, operand_a, operand_b): + print(self.reg[operand_a]) + def op_pra(self, operand_a, operand_b): + print(chr(self.reg[operand_a]), end='') + sys.stdout.flush() + def op_add(self, operand_a, operand_b): + self.alu("ADD", operand_a, operand_b) + def op_and(self, operand_a, operand_b): + self.alu("AND", operand_a, operand_b) + def op_sub(self, operand_a, operand_b): + self.alu("SUB", operand_a, operand_b) + def op_mul(self, operand_a, operand_b): + self.alu("MUL", operand_a, operand_b) + def op_div(self, operand_a, operand_b): + self.alu("DIV", operand_a, operand_b) + def op_dec(self, operand_a, operand_b): + self.alu("DEC", operand_a, None) + def op_inc(self, operand_a, operand_b): + self.alu("INC", operand_a, None) + def op_or(self, operand_a, operand_b): + self.alu("OR", operand_a, operand_b) + def op_xor(self, operand_a, operand_b): + self.alu("XOR", operand_a, operand_b) + def op_pop(self, operand_a, operand_b): + self.reg[operand_a] = self.pop_val() + def op_push(self, operand_a, operand_b): + self.push_val(self.reg[operand_a]) + def op_call(self, operand_a, operand_b): + self.push_val(self.pc + 2) + self.pc = self.reg[operand_a] + def op_ret(self, operand_a, operand_b): + self.pc = self.pop_val() + def op_ld(self, operand_a, operand_b): + self.reg[operand_a] = self.ram_read(self.reg[operand_b]) + def op_shl(self, operand_a, operand_b): + self.alu("SHL", operand_a, operand_b) + def op_st(self, operand_a, operand_b): + self.ram_write(self.reg[operand_b], self.reg[operand_a]) + def op_jmp(self, operand_a, operand_b): + self.pc = self.reg[operand_a] + def op_jeq(self, operand_a, operand_b): + if self.fl & FL_EQ: + self.pc = self.reg[operand_a] + else: + self.inst_set_pc = False + def op_jle(self, operand_a, operand_b): + if self.fl & FL_LT or self.fl & FL_EQ: + self.pc = self.reg[operand_a] + else: + self.inst_set_pc = False + def op_jlt(self, operand_a, operand_b): + if self.fl & FL_LT: + self.pc = self.reg[operand_a] + else: + self.inst_set_pc = False + def op_jne(self, operand_a, operand_b): + if not self.fl & FL_EQ: + self.pc = self.reg[operand_a] + else: + self.inst_set_pc = False + def op_cmp(self, operand_a, operand_b): + self.alu("CMP", operand_a, operand_b) + def op_iret(self, operand_a, operand_b): + # restore work from stack + for i in range(6, -1, -1): + self.reg[i] = self.pop_val() + self.fl = self.pop_val() + self.pc = self.pop_val() + # enable interrupts + self.ie = 1 + def op_hlt(self, operand_a, operand_b): + self.halted = True \ No newline at end of file diff --git a/ls8.py b/ls8.py index c0f0724d..06b94d77 100644 --- a/ls8.py +++ b/ls8.py @@ -3,7 +3,7 @@ """Main.""" import sys -from cpu import * +from cpu2 import * cpu = CPU() diff --git a/utils.py b/utils.py index 5407cb88..81900683 100644 --- a/utils.py +++ b/utils.py @@ -281,24 +281,26 @@ def go_to_room(self, destination): def dash_to_room(self, destination): "same as go to room but with dash" - print('dashing') + #print('dashing') path = self.my_map.bfs(self.player.currentRoom, destination) - print('bfs',path) + #print('bfs',path) my_dirs = self.get_dirs(path) i = 0 while i < len(path)-1: - if my_dirs[i]==my_dirs[i+1]: + if i < len(path)-2 and my_dirs[i]==my_dirs[i+1]: print('dashing') dash_path = self.get_dash_path(path[i:],my_dirs[i:]) self.make_dash(my_dirs[i],dash_path[1:]) print(f"Current Room -> Title: {self.info['title']} ID: {self.info['room_id']} Items: {self.info['items']}") i += len(dash_path[1:]) else: + print('normal walking') room = self.player.currentRoom exits = self.my_map.vertices[room] for direction in exits: if self.my_map.vertices[room][direction] == path[i+1]: - self.get_info(what='backtrack', direction=direction,backtrack=path[i+1]) + #print('walk triggered',path[i+1],direction) + self.get_info(what='backtrack', direction=direction,backtrack=str(path[i+1])) print( f"Current Room -> Title: {self.info['title']} ID: {self.info['room_id']} Items: {self.info['items']}") @@ -309,7 +311,7 @@ def dash_to_room(self, destination): def get_dash_path(self,traversal,dirs): "check if the path in go to room contains a dashable stretch" - #print('dash path check',traversal,dirs) + print('dash path check',traversal,dirs) dash_list = [] j = 0 while (j Date: Wed, 20 Nov 2019 21:11:52 +0000 Subject: [PATCH 25/32] auto-coins --- cpu2.py | 4 ++++ hinter.ls8 | 18 +++++++++--------- utils.py | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/cpu2.py b/cpu2.py index 93dfece4..3ca4f0bb 100644 --- a/cpu2.py +++ b/cpu2.py @@ -1,5 +1,6 @@ import sys from datetime import datetime # for timer interrupt +ret_list = [] # Opcodes: ADD = 0b10100000 AND = 0b10101000 @@ -172,6 +173,7 @@ def trace(self): print(" %02X" % self.reg[i], end='') print() def run(self): + while not self.halted: # Interrupt cod self.check_for_timer_int() # timer interrupt check @@ -191,12 +193,14 @@ def run(self): # If the instruction didn't set the PC, just move to the next instruction if not self.inst_set_pc: self.pc += inst_size + return ret_list def op_ldi(self, operand_a, operand_b): self.reg[operand_a] = operand_b def op_prn(self, operand_a, operand_b): print(self.reg[operand_a]) def op_pra(self, operand_a, operand_b): print(chr(self.reg[operand_a]), end='') + ret_list.append(chr(self.reg[operand_a])) sys.stdout.flush() def op_add(self, operand_a, operand_b): self.alu("ADD", operand_a, operand_b) diff --git a/hinter.ls8 b/hinter.ls8 index bcd471f5..727c2b08 100644 --- a/hinter.ls8 +++ b/hinter.ls8 @@ -115,16 +115,16 @@ 00000001 10000010 00000001 -00010000 +00000001 10000010 00000010 -10111010 +10010110 10101000 00000001 00000010 10000010 00000010 -00100100 +00110100 10101011 00000001 00000010 @@ -132,16 +132,16 @@ 00000001 10000010 00000001 -11010110 +01010101 10000010 00000010 -11010000 +10111100 10101000 00000001 00000010 10000010 00000010 -11100110 +00101100 10101011 00000001 00000010 @@ -149,16 +149,16 @@ 00000001 10000010 00000001 -11111110 +10100101 10000010 00000010 -00111001 +01011010 10101000 00000001 00000010 10000010 00000010 -00000000 +00110101 10101011 00000001 00000010 diff --git a/utils.py b/utils.py index 81900683..9662678f 100644 --- a/utils.py +++ b/utils.py @@ -6,6 +6,7 @@ import time import random import os +from cpu2 import * auth_key = config('AUTH_KEY') # MAKE SURE YPU HAVE .ENV SET UP my_url = config('LAMBDA_URL') # AND PYTHON DECOUPLE INSTALLED @@ -500,6 +501,26 @@ def sell_all_items(self): self.action('sell',i) self.action('confirm_sell',i) + def auto_coins(self): + while True: + self.dash_to_room(55) + self.hint_to_ld8() + cpu = CPU() + cpu.load('hinter.ls8') + room_inst = cpu.run() + room_inst = ''.join(room_inst) + print(room_inst) + try: + mine_room = int(room_inst[-3:]) + except: + try: + mine_room = int(room_inst[-2:]) + except: + mine_room = int(room_inst[-1:]) + print(mine_room) + self.dash_to_room(mine_room) + self.get_proof() + From 17bd535237ce14ca134616b3d6c4e846b7a2ed65 Mon Sep 17 00:00:00 2001 From: danielhorsley Date: Wed, 20 Nov 2019 21:30:40 +0000 Subject: [PATCH 26/32] add retry on mine loop --- hinter.ls8 | 18 +++++++++--------- utils.py | 3 +++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/hinter.ls8 b/hinter.ls8 index 727c2b08..743396be 100644 --- a/hinter.ls8 +++ b/hinter.ls8 @@ -115,16 +115,16 @@ 00000001 10000010 00000001 -00000001 +10000110 10000010 00000010 -10010110 +00110101 10101000 00000001 00000010 10000010 00000010 -00110100 +00110110 10101011 00000001 00000010 @@ -132,16 +132,16 @@ 00000001 10000010 00000001 -01010101 +01111010 10000010 00000010 -10111100 +10000111 10101000 00000001 00000010 10000010 00000010 -00101100 +00110011 10101011 00000001 00000010 @@ -149,16 +149,16 @@ 00000001 10000010 00000001 -10100101 +11111000 10000010 00000010 -01011010 +00111001 10101000 00000001 00000010 10000010 00000010 -00110101 +00001111 10101011 00000001 00000010 diff --git a/utils.py b/utils.py index 9662678f..4d4d5cb7 100644 --- a/utils.py +++ b/utils.py @@ -491,6 +491,9 @@ def get_mine(self,new_proof): params = {"proof" : new_proof} response = requests.post(f'https://lambda-treasure-hunt.herokuapp.com/api/bc/mine/', headers=self.header,json = params) self.mine_response = json.loads(response.content) + if 'errors' in self.mine_response.keys() and self.mine_response['errors']!=[]: + time.sleep(self.mine_response['cooldown']) + self.get_proof() print(self.mine_response) def sell_all_items(self): From b85935d717ba280342b28afb4884f09dc23cfd23 Mon Sep 17 00:00:00 2001 From: danielhorsley Date: Wed, 20 Nov 2019 22:10:52 +0000 Subject: [PATCH 27/32] auto auto auto --- hinter.ls8 | 18 +++++++++--------- utils.py | 5 +++++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/hinter.ls8 b/hinter.ls8 index 743396be..09b5faa9 100644 --- a/hinter.ls8 +++ b/hinter.ls8 @@ -115,16 +115,16 @@ 00000001 10000010 00000001 -10000110 +10110000 10000010 00000010 -00110101 +11101011 10101000 00000001 00000010 10000010 00000010 -00110110 +10010100 10101011 00000001 00000010 @@ -132,16 +132,16 @@ 00000001 10000010 00000001 -01111010 +01100011 10000010 00000010 -10000111 +11011110 10101000 00000001 00000010 10000010 00000010 -00110011 +01111010 10101011 00000001 00000010 @@ -149,16 +149,16 @@ 00000001 10000010 00000001 -11111000 +10001000 10000010 00000010 -00111001 +10111010 10101000 00000001 00000010 10000010 00000010 -00001111 +10111000 10101011 00000001 00000010 diff --git a/utils.py b/utils.py index 4d4d5cb7..7323e553 100644 --- a/utils.py +++ b/utils.py @@ -482,6 +482,7 @@ def get_proof(self): response = requests.get(f'https://lambda-treasure-hunt.herokuapp.com/api/bc/last_proof/', headers=self.header) self.last_proof = json.loads(response.content) print(self.last_proof) + time.sleep(self.last_proof['cooldown']) my_proof = proof_of_work(self.last_proof['proof'],self.last_proof['difficulty']) self.get_mine(my_proof) time.sleep(self.mine_response['cooldown']) @@ -505,7 +506,11 @@ def sell_all_items(self): self.action('confirm_sell',i) def auto_coins(self): + self.accumulate = True while True: + if 'encumbrance' in self.info and self.info['encumbrance']>8: + self.dash_to_room(1) + self.sell_all_items() self.dash_to_room(55) self.hint_to_ld8() cpu = CPU() From 060077727e347f16063ddbf55de7d87af3714551 Mon Sep 17 00:00:00 2001 From: Daniel Horsley Date: Thu, 21 Nov 2019 12:47:01 +0000 Subject: [PATCH 28/32] better mining logic --- hinter.ls8 | 18 +++++++++--------- miner.py | 12 ++++++++---- utils.py | 18 +++++++++++++----- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/hinter.ls8 b/hinter.ls8 index 09b5faa9..b3f35364 100644 --- a/hinter.ls8 +++ b/hinter.ls8 @@ -115,16 +115,16 @@ 00000001 10000010 00000001 -10110000 +10001110 10000010 00000010 -11101011 +00110101 10101000 00000001 00000010 10000010 00000010 -10010100 +00110111 10101011 00000001 00000010 @@ -132,16 +132,16 @@ 00000001 10000010 00000001 -01100011 +11000010 10000010 00000010 -11011110 +00101001 10101000 00000001 00000010 10000010 00000010 -01111010 +00111001 10101011 00000001 00000010 @@ -149,16 +149,16 @@ 00000001 10000010 00000001 -10001000 +00110101 10000010 00000010 -10111010 +01001010 10101000 00000001 00000010 10000010 00000010 -10111000 +00111001 10101011 00000001 00000010 diff --git a/miner.py b/miner.py index 3a7acbcf..9e7511c1 100644 --- a/miner.py +++ b/miner.py @@ -10,20 +10,24 @@ def proof_of_work(last_proof, difficulty): start = timer() print("Searching for next proof") - proof = 0 + proof = 80000000 total_tries = 0 prev_proof = f'{last_proof}'.encode() last_hash = hashlib.sha256(prev_proof).hexdigest() #while valid_proof(last_hash, proof, difficulty) is False: - while valid_proof(last_proof, proof, difficulty) is False: + while valid_proof(last_proof, proof, difficulty) is False and total_tries < 10000000: #proof = random.randint(0, 10000) - proof+=1 + #proof+=1 + proof = random.randint(0, 100000000) total_tries += 1 if total_tries % 1000000 == 0: print(total_tries/1000000,'million tries') print("Proof found: " + str(proof) + " in " + str(timer() - start)) - return proof + if total_tries < 10000000: + return proof + else: + return 'rerun' def valid_proof(last_hash, proof, difficulty): diff --git a/utils.py b/utils.py index 7323e553..9e1cefb7 100644 --- a/utils.py +++ b/utils.py @@ -484,8 +484,11 @@ def get_proof(self): print(self.last_proof) time.sleep(self.last_proof['cooldown']) my_proof = proof_of_work(self.last_proof['proof'],self.last_proof['difficulty']) - self.get_mine(my_proof) - time.sleep(self.mine_response['cooldown']) + if my_proof != 'rerun': + self.get_mine(my_proof) + time.sleep(self.mine_response['cooldown']) + else: + self.get_proof() def get_mine(self,new_proof): """posts your new proof to the server - note high cooldown penalties for posting wrong proof""" @@ -505,12 +508,15 @@ def sell_all_items(self): self.action('sell',i) self.action('confirm_sell',i) - def auto_coins(self): - self.accumulate = True + def auto_coins(self,acc=True,fly=True): + self.fly = fly + self.accumulate = acc while True: - if 'encumbrance' in self.info and self.info['encumbrance']>8: + self.action('status') + if 'encumbrance' in self.info.keys() and self.info['encumbrance']>16: self.dash_to_room(1) self.sell_all_items() + self.dash_to_room(55) self.hint_to_ld8() cpu = CPU() @@ -528,6 +534,8 @@ def auto_coins(self): print(mine_room) self.dash_to_room(mine_room) self.get_proof() + self.action('balance') #new lines + print(self.info) #new lines From 86bce483b1469fe6f081bfab14c0753d383456ae Mon Sep 17 00:00:00 2001 From: Daniel Horsley Date: Thu, 21 Nov 2019 13:06:45 +0000 Subject: [PATCH 29/32] shorter mine cycle and time stop for auto --- hinter.ls8 | 18 +++++++++--------- miner.py | 6 ++++-- utils.py | 7 ++++++- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/hinter.ls8 b/hinter.ls8 index b3f35364..2002f061 100644 --- a/hinter.ls8 +++ b/hinter.ls8 @@ -115,16 +115,16 @@ 00000001 10000010 00000001 -10001110 +11000011 10000010 00000010 -00110101 +01110011 10101000 00000001 00000010 10000010 00000010 -00110111 +01110010 10101011 00000001 00000010 @@ -132,16 +132,16 @@ 00000001 10000010 00000001 -11000010 +00011110 10000010 00000010 -00101001 +01011000 10101000 00000001 00000010 10000010 00000010 -00111001 +00101000 10101011 00000001 00000010 @@ -149,16 +149,16 @@ 00000001 10000010 00000001 -00110101 +11000001 10000010 00000010 -01001010 +10101100 10101000 00000001 00000010 10000010 00000010 -00111001 +10110110 10101011 00000001 00000010 diff --git a/miner.py b/miner.py index 9e7511c1..06d8a8a5 100644 --- a/miner.py +++ b/miner.py @@ -15,7 +15,7 @@ def proof_of_work(last_proof, difficulty): prev_proof = f'{last_proof}'.encode() last_hash = hashlib.sha256(prev_proof).hexdigest() #while valid_proof(last_hash, proof, difficulty) is False: - while valid_proof(last_proof, proof, difficulty) is False and total_tries < 10000000: + while valid_proof(last_proof, proof, difficulty) is False and total_tries < 6000000: #proof = random.randint(0, 10000) #proof+=1 proof = random.randint(0, 100000000) @@ -23,10 +23,12 @@ def proof_of_work(last_proof, difficulty): if total_tries % 1000000 == 0: print(total_tries/1000000,'million tries') - print("Proof found: " + str(proof) + " in " + str(timer() - start)) + if total_tries < 10000000: + print("Proof found: " + str(proof) + " in " + str(timer() - start)) return proof else: + print('re-run') return 'rerun' diff --git a/utils.py b/utils.py index 9e1cefb7..bcf6e1d4 100644 --- a/utils.py +++ b/utils.py @@ -7,6 +7,7 @@ import random import os from cpu2 import * +import datetime auth_key = config('AUTH_KEY') # MAKE SURE YPU HAVE .ENV SET UP my_url = config('LAMBDA_URL') # AND PYTHON DECOUPLE INSTALLED @@ -51,6 +52,8 @@ def __init__(self, auth=auth_key, save=True, load_map=True): self.fly = False self.dash = False self.important_rooms = {} # May not need this anymore (all special rooms are id'd) + self.hour = datetime.datetime.today().hour + self.auto_stop = 19 def get_info(self, what='init', direction=None, backtrack=None): """multi purpose move & init function - this is used @@ -509,9 +512,11 @@ def sell_all_items(self): self.action('confirm_sell',i) def auto_coins(self,acc=True,fly=True): + "now added timer stop" self.fly = fly self.accumulate = acc - while True: + while True and self.hour16: self.dash_to_room(1) From 17f9cbbfbc3f25a2e8eae1f64d8fb7ff961badea Mon Sep 17 00:00:00 2001 From: Daniel Horsley Date: Thu, 21 Nov 2019 13:10:57 +0000 Subject: [PATCH 30/32] auto.py --- auto.py | 4 ++++ hinter.ls8 | 18 +++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 auto.py diff --git a/auto.py b/auto.py new file mode 100644 index 00000000..858a98bf --- /dev/null +++ b/auto.py @@ -0,0 +1,4 @@ +from utils import * +mappy = mapper() +mappy.create_starting_map() +mappy.auto_coins(acc=True,fly=True) \ No newline at end of file diff --git a/hinter.ls8 b/hinter.ls8 index 2002f061..444f2cb3 100644 --- a/hinter.ls8 +++ b/hinter.ls8 @@ -115,16 +115,16 @@ 00000001 10000010 00000001 -11000011 +10010011 10000010 00000010 -01110011 +00111010 10101000 00000001 00000010 10000010 00000010 -01110010 +00100011 10101011 00000001 00000010 @@ -132,16 +132,16 @@ 00000001 10000010 00000001 -00011110 +00100010 10000010 00000010 -01011000 +10111000 10101000 00000001 00000010 10000010 00000010 -00101000 +00010101 10101011 00000001 00000010 @@ -149,16 +149,16 @@ 00000001 10000010 00000001 -11000001 +11110000 10000010 00000010 -10101100 +00011101 10101000 00000001 00000010 10000010 00000010 -10110110 +00100010 10101011 00000001 00000010 From c90e555d1b948c7f8d0bfa6d83c023f940808e08 Mon Sep 17 00:00:00 2001 From: Daniel Horsley Date: Thu, 21 Nov 2019 13:23:59 +0000 Subject: [PATCH 31/32] correction --- hinter.ls8 | 18 +++++++++--------- miner.py | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/hinter.ls8 b/hinter.ls8 index 444f2cb3..f577bbe8 100644 --- a/hinter.ls8 +++ b/hinter.ls8 @@ -115,16 +115,16 @@ 00000001 10000010 00000001 -10010011 +00100100 10000010 00000010 -00111010 +00100010 10101000 00000001 00000010 10000010 00000010 -00100011 +00010011 10101011 00000001 00000010 @@ -132,16 +132,16 @@ 00000001 10000010 00000001 -00100010 +00011011 10000010 00000010 -10111000 +10101111 10101000 00000001 00000010 10000010 00000010 -00010101 +00111110 10101011 00000001 00000010 @@ -149,16 +149,16 @@ 00000001 10000010 00000001 -11110000 +10101010 10000010 00000010 -00011101 +11100101 10101000 00000001 00000010 10000010 00000010 -00100010 +10011001 10101011 00000001 00000010 diff --git a/miner.py b/miner.py index 06d8a8a5..b4aef0a6 100644 --- a/miner.py +++ b/miner.py @@ -24,7 +24,7 @@ def proof_of_work(last_proof, difficulty): print(total_tries/1000000,'million tries') - if total_tries < 10000000: + if total_tries < 6000000: print("Proof found: " + str(proof) + " in " + str(timer() - start)) return proof else: From bb1c391538426e90b79c4581ce1075476b911884 Mon Sep 17 00:00:00 2001 From: Daniel Horsley Date: Thu, 21 Nov 2019 14:30:46 +0000 Subject: [PATCH 32/32] minor changes --- hinter.ls8 | 18 +++++++++--------- utils.py | 2 ++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/hinter.ls8 b/hinter.ls8 index f577bbe8..2ace2b17 100644 --- a/hinter.ls8 +++ b/hinter.ls8 @@ -115,16 +115,16 @@ 00000001 10000010 00000001 -00100100 +00000000 10000010 00000010 -00100010 +11110101 10101000 00000001 00000010 10000010 00000010 -00010011 +00110011 10101011 00000001 00000010 @@ -132,16 +132,16 @@ 00000001 10000010 00000001 -00011011 +00110101 10000010 00000010 -10101111 +00000000 10101000 00000001 00000010 10000010 00000010 -00111110 +00111000 10101011 00000001 00000010 @@ -149,16 +149,16 @@ 00000001 10000010 00000001 -10101010 +11001100 10000010 00000010 -11100101 +01110110 10101000 00000001 00000010 10000010 00000010 -10011001 +01110111 10101011 00000001 00000010 diff --git a/utils.py b/utils.py index bcf6e1d4..a82ebbf3 100644 --- a/utils.py +++ b/utils.py @@ -521,6 +521,8 @@ def auto_coins(self,acc=True,fly=True): if 'encumbrance' in self.info.keys() and self.info['encumbrance']>16: self.dash_to_room(1) self.sell_all_items() + self.action('status') + print(self.info) self.dash_to_room(55) self.hint_to_ld8()