ECE 1021

HOMEWORK #6

(Last Modified: 27 Nov 2010 )

PROGRAM A - Assignment Statement Parser

Programming Problem #8.6 (p434).

PROGRAM B - Tic-Tac-Toe

Programming Problem #8.10 (p436).

MODIFICATION: Have two panels on the screen. The left panel displays the present board and the right panel displays the board as it was on move N. The user can hit the '+' key to advance the right panel or the '-' key to step back one move. The move number should be displayed above each panel. To make a move, the user moves the screen cursor to the desired square using the {2,4,6,8} keys and "Enter" to make the move. Your program needs to be sure that it is a legal move (square not already taken).

PROGRAM C - The Mysterious Disapearing String (EXTRA CREDIT)

A source code file has been added to the References Section of the main page. Look the code over and then compile and run the program. The instructions on what you are being asked to do are contained in the file. As before, this program counts for up to an additional 25% of the homework assignment. Note that you are not being asked to write any code - just explain why the short loop in the code given behaves the way it does.

Hints on Program B

Consider the behavior that you want for the right panel of the tic-tac-toe game. You basically want the most recent move shown in that panel to be erased (if there is one) if the user hits the '-' key and for the next move made (if there is one) to be displayed if they hit the '+' key. This behavior is very similar to the stack behavior we have already seen. The difference is that you want to push the actual moves onto the stack (in response to the user making a legal move) but you don't want to really pop them off. Instead, you want a second pointer - a stack read pointer - that moves up and down the stack just like the real pointer does (but is constrained between the bottom of the stack and the top of the contents as indicated by the true stack pointer). You can do a ReadPush and ReadPop in order to add or remove moves from the History Display but doing so does not actually change the contents of the stack.

Now consider the type of stack that you need - what is it that you need to push onto the stack? You need to push the move - meaning which column and which row the player chose. You can do this with two stacks - one for the row and one for the col of each move. You could also create a stack using a 2D array (which is really the same thing except in some of the implementation details). Do you need to save the which player made the move? Not really - one player always makes the odd numbered moves and the other player always makes the even numbered moves. Take advantage of that.

Another thing you might consider is having the players be able to continue on to play a new game once the present game is over. To do that, you need to reinitialize the game the same way you did prior to the first game. One way to do that is to have place the code that gets a command from the user at the bottom of the loop - which means that you have to force the command to be something meaningful on the first pass. Simply set the command string to be whatever it needs to be in order to mimic the players choosing to start a new game.