Thursday, 5 March 2015

Week 8 - Assignment 2

Assignment 2 started out as a nightmare. At a certain point in attempting to write minimax I seriously considered why I was specializing in computer science. Until I made a breakthrough. When I first started to try and write minimax I tried to do it using the current state and running for loops, if statements, accumulators, and list sum opperators. But the way I was trying to do it was nearly impossible, because I hadn't ordered the data in any way. After considering my code for a long time I decided to scrap it and look for other ways of approaching the problem. I looked up explanations of minimax online and eventually came to the conclusion that I would have to somehow implement a Tree structure with nodes.
Learning about Abstract Data Structures in class, at the time I thought it was just a convenient way of storing data that I likely would never need or use. Until I was attempting to write the minimax function. Suddenly it became clear that in order to solve the minimax problem I would have to set up the state data into a tree of the current state, with the children being the next states of every possible move given the root state. The way I wrote the node structure was recursive, because every move called a new node with the move applied to the new state. This would recur for every move made in the root, then every possible move of the next leaf, then the next, etc. Until a tree of every possible move was created.
Once the tree was created I could run a minimax function that runs through the built tree to find the final outcomes of each leaf. If the player won it would return a 1, a draw or still playing was a 0, and a loss was a -1. The recursive function would sum together each leaf for each move root until a value was returned for each possible next move to make. The computer could then pick the outcome with the greatest number, guaranteeing it made the best possible move.

No comments:

Post a Comment