Objective: Build a tic tac toe game in HTML and pure JavaScript.
This week we have been learning about writing functions, working with loops, and writing conditionals. We also learned about how to use JavaScript to interact with the DOM.
Now we're going to make a Tic Tac Toe game using all of this.
- an HTML/CSS structure that resembles a tic tac toe grid
- two users sitting behind the same computer take turns clicking on the grid, alternating clicks display X/O in the clicked cell
- make it clear to the user when the game is over (putting a message on the page would be great, but a console.log() message will do in a pinch)
Hint: Consider storing the contents of the board in an array. Can you represent the winning combinations in another array? You could then check if any of the winning combinations are on the board after every move.
- Touch an
index.htmlfile. This is your starting point for the project. Add the necessary HTML tags, includingscriptandlinktags to link to your JavaScript and CSS files respectively. - Before you start working with JavaScript, construct the
gameboard. The gameboard page should include the 3x3 grid. Using
idandclasson clickable elements will help you wire your board up to your JavaScript. - JavaScript portion will be next:
- Locate the elements you want to use within your app. Think about
using
document.getElementById,document.getElementsByClassNameor something similar to locate your target elements. Try this in your console to make sure your selection works. - After finding the elements, start writing logic to listen for
clickevents on those elements. - You will also need a variable to keep track of moves. As this
will be used to indicate whether or not to draw an
Xor anO.
- Locate the elements you want to use within your app. Think about
using
- make it sexy - style it up with CSS, be creative!
- A cell should not be able to be replayed once marked.
- you can reset the game without refreshing the browser
- keep track of and display to user how many times X and O have won since the last time the browser was refreshed
- have fun! ;-)
- build functionality that allows a user to play against the computer choosing squares at random
- then make your computer smart