crappy algorithm

This commit is contained in:
randomuser 2022-07-11 19:27:49 -05:00
parent b30c1a7ffe
commit 842291d6d6
2 changed files with 48 additions and 7 deletions

52
cli.py
View File

@ -1,8 +1,13 @@
#!/usr/bin/env python #!/usr/bin/env python
from board2 import Board from board2 import Board
from board2 import BoardTools
from pos import TwoDPos from pos import TwoDPos
from pos import TwoDUtils from pos import TwoDUtils
from node import Node
class StateError(BaseException):
pass
piecemaps = { piecemaps = {
0: '.', 0: '.',
@ -11,6 +16,36 @@ piecemaps = {
} }
mappings = TwoDUtils.reverse mappings = TwoDUtils.reverse
def tree_of_node(node):
parent = node
print("considering board with moveset " + str(parent.inner.moves))
possible = parent.inner.possible()
if len(possible) != 0:
print("permuting!")
for i in possible:
copy = parent.inner.copy()
copy.append(i)
childnode = Node()
childnode.inner = copy
parent.children.append(childnode)
for i in parent.children:
tree_of_node(i)
else:
print("calculated score")
rawscore = BoardTools.winning(None, parent.inner.metaboard())
if rawscore == Board.cross:
score = -1
elif rawscore == Board.nought:
score = 1
elif rawscore == Board.tie:
score = 0
else:
score = None
raise StateError("aaaa")
print("score" + str(score))
def render(board): def render(board):
print("123 456 789") print("123 456 789")
moves = board.possible() moves = board.possible()
@ -36,7 +71,7 @@ def render(board):
print("to play: " + str(board.turn)) print("to play: " + str(board.turn))
def help(): def help_text():
print("m - make a move") print("m - make a move")
print("p - see coordinates of legal moves") print("p - see coordinates of legal moves")
print("t - see whose turn it is") print("t - see whose turn it is")
@ -59,7 +94,7 @@ def main():
b = Board() b = Board()
print("type h for help") print("type h for help")
while True: while True:
try: # try:
cmd = input("> ") cmd = input("> ")
spl = cmd.split(' ') spl = cmd.split(' ')
if spl[0] == "m": if spl[0] == "m":
@ -71,11 +106,18 @@ def main():
elif spl[0] == "r": elif spl[0] == "r":
render(b) render(b)
elif spl[0] == "h": elif spl[0] == "h":
render(b) help_text()
elif spl[0] == "z":
# create node from current board
node = Node()
node.inner = b
tree_of_node(node)
elif spl[0] == "q": elif spl[0] == "q":
break; break;
except: # except:
print("error occured") # print("error occured")
return return
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -4,6 +4,7 @@ class Node:
def __init__(self): def __init__(self):
self.children = [] self.children = []
self.score = None self.score = None
self.inner = None
def calculate(self): def calculate(self):
if self.children == []: if self.children == []:
return self.score return self.score
@ -14,5 +15,3 @@ class Node:
if score != None: if score != None:
items += 1 items += 1
total += score total += score