Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Looking for help with my tic tac toe game

Hi all, After taking the OOPJS Challenge course I decided to build out a Tic Tac Toe game because it would be similar to the Four In a Row game, but different enough to challenge me. I have successfully built a first iteration, but the problem is that the user plays against the computer, and the computer is very bad at the game. Right now I just have the computer selecting a random empty space on the game board, but I know the computer should use more strategy to actually try and beat the user. I have read that games like this often implement the "minimax" algorithm, but I have never studied algorithms and I would have no idea where to start in making my game implement that, and I don't even know if I would have to re-write significant parts of my app to do so. Anyway, I am just looking for some suggestions and a bit of direction in building in some strategy to make the game more challenging. Here is the github repo:

https://github.com/michaelacook/tic-tac-toe

The computer calculates its move in the file Computer.js in the move(emptySpaces) method. But as I said, it just randomly selects an empty space. I need to figure out a way to make it actually calculate a strategic move.

Live demo: https://michaelacook.github.io/tic-tac-toe/

Any and all input would be super appreciated! I am hoping the final iteration of this project can be displayed on my future portfolio.

2 Answers

Ben Slivinn
Ben Slivinn
10,156 Points

When you think algorithms think mathematics, in tic tac toe, you have 3x3 board, i would iterate over all the 9 cells and rate each cell for the best probability to complete a full row, column or diagonal. The ratings depend on the possibilities to complete rows columns or diagonal, blocking the opponent and probability of the moves the opponent can do.

For example if the opponent have in the first row two figures, the rating of the third cell is higher then the other cells on the board because you want to block it to prevent win, and then probability that the opponent's next move will be to complete to full row is very high.

Hope it gives you some ideas...

Hey Ben, thanks that does give me a starting point for thinking about this. I'm just not sure how to calculate the probability for each space on the board, but I will think hard about that.

Here is a semi-final product. It works, the game is moderately difficult to beat. I implemented something that works similarly to what you described. Now I think I might tinker more with the algorithm that rates empty spaces to see if I can make versions that are easier and harder. https://michaelacook.github.io/tic-tac-toe/

Ben Slivinn
Ben Slivinn
10,156 Points

Just think how to rate each cell, give each one a score based on some parameters you will think of, of course, more parameters "smarter" your logic will be. At the end the cell with the highest score will be the computer's next move. You will fine tune the algorithm on the fly after playing few games,its even better give other people to play and watch them doing so to find the vulnerabilities of your algorithm.

Cheers!

Thanks again. I've never done much game programming so I wasn't sure how to frame the problem but now that you've given me a basic way to see it I think I can work out the details and implement something that will at least work decently. Cheers!