This is a very simple project. The key to this is realizing that you need pages for both the computer and for the player. The computer pages represent the decision that the computer is making with a link to the next player page while the player pages must lead to three computer pages. Thinking about this, not all pages are necessary as only pages that can be reached are necessary. Figuring this out could be done on the fly, but a little bit of planning makes this process much easier.
A state diagram is ideal for this type of problem. Here you have pages representing the different states of the game (example, C16 would be computer’s turn with 16 items remaining) from here the computer would simply choose how many items to take, and as the game is static we have the computer take 2 items. Two is an arbitrary number, with your design being however many you wanted. That number was determined by rolling a six-sided die, with 1-2 being computer take 1 item, 3-4 being take two items, and 5-6 being take three items.
Nodes are connected to other nodes based on triggers, which are essentially what link the player clicks on. For player pages, there are three choices each leading to one of the three computer pages. For computer choices there is only one choice so that leads to the appropriate player page. As you can see by my state diagram, there are situations where multiple computer pages go to the same player page which allows for a reduced number of pages. Instead of 41 pages, I only need 32. The best part is that as the pages are all very similar, you only need to create one single good page and then alter it for the other pages.
I started at the Player21 page and worked my way to the win pages, but the opposite path would also work. My page is very simple, here is a sample of the Player page with five items remaining.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Treasure NIM (no CSS/JS)</title>
</head>
<body>
<h1>Treasure NIM</h1>
<h2>Player's Turn</h2>
<img src="chest.png" alt="Treasure Chest">
<img src="coin.png" alt="coin">
<a href="NIM_02c.html"><img src="coin.png" alt="coin"></a>
<a href="NIM_03c.html"><img src="coin.png" alt="coin"></a>
<a href="NIM_04c.html"><img src="coin.png" alt="coin"></a>
<h2>There are 5 objects left, take how many?</h2>
<a href="NIM_04c.html">Take 1 item</a><br />
<a href="NIM_03c.html">Take 2 items</a><br />
<a href="NIM_02c.html">Take 3 items</a><br />
</body>
</html>
As you can see, I simply have images for each item remaining, with the last three images being links so the player can click on the coins instead of the text links. Next is some instructions with the proper links to each page that the player can go to.
This is repeated for each of the other pages, and is all there is to creating the game. It is, however, important that you do test each of the pages and make sure all the links work the way they should. The map above is handy for doing this testing as you can simply play through the game marking which triggers you tried and once you have checked off all of the triggers on the diagram then you have fully tested the paths through the game.
The cheat sheet for this chapter.
A brief history of HTML
What makes up a HTML file
How tags are used to create HTML elements
How to display things such as <, >, &, 😀
Linking to other pages, other sites, and within the page
Adding images and image maps to your page.
Ordered and unordered lists and nesting lists.
Tables with spanning rows and columns.
The game of NIM is the project for this chapter.
How my solution to the NIM game was put together
Solving Nim.