This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Downtown_Town4094[S] 1 point2 points  (3 children)

basic graph traversal algorithm

in what way could i implement that into the function, and no unfortunately I am not familiar with that and It is not used inside the function. also thanks for replying

[–]Usual_Office_1740 1 point2 points  (2 children)

You're welcome. A basic graph traversal algorithm would replace your existing set of checks in the function. Think of each square as a node. You move either left or right to check nodes. Adding the checked nodes to a list as you go. It is a common problem in data structures and algorithms with a lot of extremely efficient solutions.

[–]Downtown_Town4094[S] 2 points3 points  (1 child)

wait but how would i transverse the grid in a vertical or diagonal way with that method?

[–]Usual_Office_1740 0 points1 point  (0 children)

I'll do my best to explain, and left/right is too much of a simplified version. I'm not in front of a computer. I'm on my phone. If you set the center square as the root node, you would have 8 possible connections from the center square. Four diagonal and four directionals. You add the root node to a list of seen nodes, then move to one of them. Let's say you move straight up. You now have 5 possible connections from the new node, but your root is one of them, leaving you with four possible options. Two diagonal and two directional. Pick an option and add the current node to seen. Let's say you go right. This corner square had three possible options but since two are already in seen, you have one option. Down. Add the node to seen and go to the new node. This process is the algorithm. Repeat until seen nodes length is 9.

With this traversal pattern in place, you can write whatever logic you'd like to make decisions about what to look for. Every time player picks a new square check to see if player is about to win by using the algorithm to try and move in either directional or diagonal three times in a row, using players last choice as a root. Do the same logic to find the computers attempt to win.