![]() And eight wasn't really so bad.īut what if we had a 4x4 board? That's 10 solutions. Here, we basically just wrote them out by hand. Not with how hasWinner() works, but with how we get those winningCombos. But maybe we can do something a bit more fun that does the job. If we find one, that means there's three of the same value in a row on the board, and we have a winner.Īnd this works and performs fine.Then we use find() to look for a group which has all X's or all O's.We use map() over our combos to get the grid values for every cell.Under the hood, our "grid" is just an object whose fields are the numbers of the cells and who's values are "X"s, "O"s, and nulls (for empty cells).Įnter fullscreen mode Exit fullscreen modeįirst, we create a list of lists representing all the potential winning sequences of cells, all the rows and columns and the two diagonals. Each turn a user clicks a cell in the grid, and the game checks to see if they've won. This is our basic Tic-Tac-Toe implementation in React. ![]() Like Marvel's What-If, but with a smaller animation budget. But today we're gonna start at the end we're gonna take an existing solution and look at what if we just changed a small bit in an interesting way. Now, normally we'd start a post like this at the beginning we'd talk about the domain of the physical game, and what elements we'd need in our digital solution. Starting from the end with sensible solutions. If you just want to see the final solution, visit the sandbox here.ġ. The code is all done in React and Typescript, and as always, full links and examples will be provided. Specifically, this is a post about an alternative algorithm for finding winning combos in Tic-Tac-Toe using a concept called Magic Squares, but also about burnout, productivity, and finding joy in code. While you’re here, check out Math Experts Split the Check and the epic rhyming proof-poem A Fight with Euclid.My name is Kirk.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |