react-tictactoe | Realtime Tic Tac Toe in React | Frontend Framework library
kandi X-RAY | react-tictactoe Summary
kandi X-RAY | react-tictactoe Summary
Realtime Tic Tac Toe in React
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of react-tictactoe
react-tictactoe Key Features
react-tictactoe Examples and Code Snippets
Community Discussions
Trending Discussions on react-tictactoe
QUESTION
I completed the React tic-tac-toe tutorial. I then wanted to add some simple AI, so that when a player makes a move, the AI will answer.
However, when I click on a square to place an X, my move seems to be discarded and computer makes its move instead. So the state isn't updated immediately after my move and when AI makes it's move, it hasn't "seen" my move yet. I suspect it might be something to do with how onClick or setState function works in React but can't really get my finger on it. Maybe someone could point out the problem in my code?
Here are the relevant code bits:
...ANSWER
Answered 2021-Apr-09 at 06:54This turned out to be a problem of setState being async and not updating the state immediately. More info here: setState doesn't update the state immediately
I put the AI move as a callback function to the first setstate (player move) and that fixed the issue.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install react-tictactoe
You need to enable presence to detect the number of players in the channel. This prevents having more than two players in a given channel. Go to your PubNub Admin Dashboard, click on the Demo Project App, or create a new app for this project, and click on Keyset. Scroll down to Application add-ons and toggle the Presence switch to on. Keep the default values the same.
Clone the repo.
Open the project in your favorite text editor.
Go to App.js and replace 'ENTER_YOUR_PUBLISH_KEY_HERE' and 'ENTER_YOUR_SUBSCRIBE_KEY_HERE' with the keys you got from Step 1.
Install the dependencies by running the script that's in the root directory. Make sure to make the script executable first.
If you ran the above script, the app will open in http://localhost:3000 as soon as the dependencies are installed. You can also run the app (make sure to install dependencies first) by running the command:
When the app opens, copy and paste http://localhost:3000 to another window. In one window, create a channel by pressing the 'Create' button and copy the room id. In the other window, press the 'Join' button and paste the room id in the input field. Press enter and the game will start. Place the 'X' piece in the window that you created the room and watch as the other window is updated in realtime. Play around with both windows until there is a winner or a tie game!
To learn more about this project or if you want to build this project from scratch, check out the tutorial.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page