EIW Fall 2003     HW3: Perl Tic-Tac-Toe

HW3 FAQ



Question:

Help, I have no idea how to get started!

Answer:

Start small, and build the program a little bit at a time:

  1. Write a Perl CGI program that simply draws a tic-tac-toe board. Make it so that whenever the user clicks on a cell in the game board, the program is run again (each click can be a hyperlink, or a button).

  2. Now make it so that each time the user clicks on a cell, information is sent to the Perl CGI that indicates which cell was clicked on. There are 2 simple ways to do this:

    1. You can make each cell hold a hyperlink (could be around an image) that includes a query string that indicates a form field name and value. For example, clicking on the upper left cell might be sent to the URL hw3.pl?row=0&col=0 and clicking on the middle cell might be sent to the URL hw3.pl?row=1&col=1.

    2. You can make each cell hold a form with nothing visible except a submit button. The form could hold hidden fields set to send the same kind of query string as the approach mentioned about (hw3.pl?row=0&col=1).

  3. Now add code that modifies the page sent back so that an "X" is displayed in the cell the user clicked on.

  4. Add code that makes the move for the computer - pick any cell that is different than the cell the user clicked on (works for the first move only).

  5. Now you need to worry about the session, as after the first move your program needs to be able to keep track of previous moves (the state of the game). You could encode the current game in a single string which could be placed in a hidden field (or hard-coded into all URLs placed in your game board), or you could put the state of the game in a bunch of cookies (or encoded as a single string in one cookie).

  6. Write code that extracts the state of the game from each request and can use this to determine legal moves, when there is a winner, etc. I'd suggest you create an array that holds the value of each cell, and set the values from the hidden-fields/cookie (however the state of the game arrives).

  7. Write code that checks for legal moves, wins, ties, etc. Add a "reset game" button or link that wipes out the state-of-the-game (set to all blanks).