

So why not check out Scrimba's brand new, free tutorial on how to build the classic tic-tac-toe game in React? cards.forEach(card => card.classList = "card") Ĭards.forEach(card => card.If you have a good understanding of HTML, CSS, JavaScript, and React you might be wondering where to go next on your learning journey. Winnerpleyr("Congrats player two you win") Ĭonst w = document.querySelector(".winner") Winnerpleyr("Congrats player one you win") tAttribute("onclick","rep() ") įirstPlayer.push(Number()) Replay.appendChild(document.createTextNode("Replay")) Let res = item.every(val => array.indexOf(val) != -1) Ĭonst modal = document.createElement("div") Ĭonst player = document.createTextNode(p) Ĭonst replay = document.createElement("button") Let firstPlayer =, secondPlayer =, count = 0 We also make a function if no one wins the game then they can again play that game.Ĭonst cards = om(document.querySelectorAll(".card")) Ĭonst winner = ,] Then we make a function for each move player is playing, in the function we check that players wins or not by that move.
#Tic tac toe html css javascript tutorial code#
In the code firstly we write the winning case scenarios at which numbers he/she can win the game.

When any player presses on a blank space, that respective player places either an X or O on the playing board. Here we can write the logic this way, if ((count%2)=1), then it is player 1’s turn, otherwise it is player 2’s turn. So, Player 1 plays when move is an odd number. Player 2 plays when the move is equal to 2, 4, 6 and 8. Player 1 plays when the move is equal to 1, 3, 5, 7 and 9. So we load code on the loading of HTML document.īefore writing code, we also have to check what are the winning conditions. You can create this by using plain javascript.

So firstly we make a design or UI part then start work on its functionality. So basically we create 3 files here or we can do all code in one file which includes all html, css and javascript. I am sharing my simple code so that you can understand the game easily. This is quite complex when we start coding. In this js program, we track each move for the next players move. Its programming is not that simple as it looks. It also displays the replay button if a tie happens. When the game ends it displays three outcomes: By clicking on the box he can make a move as soon as the move is done, the game proceeded to give a chance to another player.Īt each move, it will show whose player moves it is either is A or it is B There are some rules that we are going to define here is that as we know the primary thing is move, here the player cannot undo that move. When the board fills up and no combination making then the game is in draw state. In this game there are two players, the player who makes three of their marks in row and column or one of the two diagonals wins the game. Those who don’t know about this game let me give you little overview. Most of the people know that when we were kids we used to play this game on paper. In this tutorial, I will tell you how to build a basic tic tac toe game using HTML, CSS and JavaScript.
