//========================================================================= #define PROGRAMMER "SOLUTIONS, NoFrills" #define PROG_CODE "soln" #define COURSE "ECE-1021" #define YEAR (2003) #define TERM "Fall" #define SECTION (0) #define ASSIGNMENT "HW #6BA" #define REVISION (0) #define TITLE "Tic-Tac-Toe" #define SUBTITLE "Problem 8.10" #define EMAIL "wbahn@eas.uccs.edu" #define FILENAME "06b0soln.txt" //========================================================================= // PROBLEM: // // Write a Tic Tac Toe game that has 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). // // SOLUTION: // // We need some type of cursor, so we will create one that we can erase // and display without affecting anything else (such as the mark from // a prior move in that square. // // Our x's and o's will be made using astrisks. The cursor will be made // using # signs in the spots used by neither the x nor the o. // // * *| *** | // * * |* *| // * |* *| Shows an x and an o with no cursor // * * |* *| // * *| *** | // ------------------- Horizontal line extends 1 space left // | | and right of pane boundary // | | // | | // | | // | | // ------------------- // * *| *** | // *#* |* # *| // #*# |*# #*| Shows cursor on an x and on an o // *#* |* # *| // * *| *** | // // Relative to the top left corner of the pane, the cell centers are // at offsets of 2, 8, and 14. // // row or col offset // 0 2 // 1 8 (2+6) // 2 14 (2+12) // // Therefore the offset is simply 2 + 6rc where rc is the row or col // 1) TASK: Display Empty Pannels and Instructions // 2) TASK: Initialize the game // 2.1) SET: games = 0 // 2.2) SET: xwon = 0 // 2.3) SET: exflag = FALSE // 2.4) SET: command = 'n' // 3) LOOP: // =================================================================== // 3.1) TASK: Execute Command // =================================================================== // ================================================================= // 3.1.2) TASK: Check for Global Command // ================================================================= // --------------------------------------------------------------- // 3.1.2.1) IF: Command is ('x') or ('X') // 3.1.2.1.1) TASK: Process Exit command // --------------------------------------------------------------- // 3.1.2.1.1.1) OUT: exit message // 3.1.2.1.1.2) SET: exflag = TRUE // --------------------------------------------------------------- // 3.1.2.2) IF: Command is ('n') or ('N') // 3.1.2.2.1) TASK: Process New Game command // --------------------------------------------------------------- // 3.1.2.2.1.1) SET: winner = 0 // 3.1.2.2.1.2) TASK: Clear the game board // 3.1.2.2.1.3) TASK: Flush the moves stack // 3.1.2.2.1.4) TASK: Flush the history stack // 3.1.2.2.1.5) TASK: Erase Winner's name // 3.1.2.2.1.6) TASK: Place Left Panel Cursor (LPC) in center // 3.1.2.2.1.6.1) SET: LPCx = LPCy = 1 // 3.1.2.2.1.6.2) TASK: Show LPC cusor // ================================================================= // 3.1.3) TASK: Check for Left Panel Command // ================================================================= // --------------------------------------------------------------- // 3.1.3.1) IF: Command is ('2') // 3.1.3.1.1) TASK: Process Move Down command // --------------------------------------------------------------- // 3.1.3.1.1.1) IF: LPCy < 2 // 3.1.3.1.1.1.1) TASK: Erase LPC cusor // 3.1.3.1.1.1.2) SET: LPCy++ // 3.1.3.1.1.1.3) TASK: Show LPC cusor // --------------------------------------------------------------- // 3.1.3.2) IF: Command is ('4') // 3.1.3.2.1) TASK: Process Move Left command // --------------------------------------------------------------- // 3.1.3.2.1.1) IF: LPCx > 0 // 3.1.3.2.1.1.1) TASK: Erase LPC cusor // 3.1.3.2.1.1.2) SET: LPCx-- // 3.1.3.2.1.1.3) TASK: Show LPC cusor // --------------------------------------------------------------- // 3.1.3.3) IF: Command is ('6') // 3.1.3.3.1) TASK: Process Move Right command // --------------------------------------------------------------- // 3.1.3.3.1.1) IF: LPCx < 2 // 3.1.3.3.1.1.1) TASK: Erase LPC cusor // 3.1.3.3.1.1.2) SET: LPCx++ // 3.1.3.3.1.1.3) TASK: Show LPC cusor // --------------------------------------------------------------- // 3.1.3.4) IF: Command is ('8') // 3.1.3.4.1) TASK: Process Move Up command // --------------------------------------------------------------- // 3.1.3.4.1.1) IF: LPCy > 0 // 3.1.3.4.1.1.1) TASK: Erase LPC cusor // 3.1.3.4.1.1.2) SET: LPCy-- // 3.1.3.4.1.1.3) TASK: Show LPC cusor // --------------------------------------------------------------- // 3.1.3.5) IF: Command is ('\n') // 3.1.3.5.1) TASK: Process Make Move command // --------------------------------------------------------------- // 3.1.3.5.1.1) TASK: Push LPC onto moves stack (Push updates left panel) // ================================================================= // 3.1.4) TASK: Check for Right Panel Command // ================================================================= // --------------------------------------------------------------- // 3.1.4.1) IF: Command is ('+') // 3.1.4.1.1) TASK: Process Advance Game History command // --------------------------------------------------------------- // 3.1.4.1.1.1) IF: (read stack isn't at end) // 3.1.4.1.1.2) TASK: Perform a Read Push (ReadPush updates right panel) // --------------------------------------------------------------- // 3.1.4.2) IF: Command is ('-') // 3.1.4.2.1) TASK: Process Retard Game History command // --------------------------------------------------------------- // 3.1.4.2.1.1) IF: (read stack isn't at beginning) // 3.1.4.2.1.2) TASK: Perform a Read Pop (ReadPop updates right panel) // --------------------------------------------------------------- // =================================================================== // 3.2) TASK: Determine if there is a winner // =================================================================== // ================================================================= // 3.2.1) TASK: See if a winner across // ================================================================= // 3.2.1.1) SET: row = 0 // 3.2.1.2) WHILE: row < 3 // 3.2.1.2.1) IF: (col0 = col1) AND (col1 = col2) // 3.2.1.2.1.1) SET: winner = piece in col1 // 3.2.1.2.1.2) TASK: Draw horizontal line over row // ================================================================= // 3.2.2) TASK: See if a winner down // ================================================================= // 3.2.2.1) SET: col = 0 // 3.2.2.2) WHILE: col < 3 // 3.2.2.2.1) IF: (row0 = row1) AND (row1 = row2) // 3.2.2.2.1.1) SET: winner = piece in row1 // 3.2.2.2.1.2) TASK: Draw vertical line over col // ================================================================= // 3.2.3) TASK: See if a winner diagonally // ================================================================= // 3.2.3.1) IF: (upper left = center) AND (center = lower right) // 3.2.3.1.1) SET: winner = piece in center // 3.2.3.1.2) TASK: Draw diagonal line upper left to lower right // 3.2.3.2) IF: (lower left = center) AND (center = upper right) // 3.2.3.2.1) SET: winner = piece in center // 3.2.3.2.2) TASK: Draw diagonal line lower left to upper right // ================================================================= // 3.2.4) TASK: Perform Post Game Tasks // ================================================================= // 3.2.4.1) TASK: Display winner on screen // 3.2.4.2) TASK: Update Cumulative Statistics // =================================================================== // 3.3) TASK: Get Command from User // =================================================================== // 3.3.1) TASK: Get command from User // 3.3.1.1) LOOP: // 3.3.1.1.1) GET: Keystroke from User // 3.3.1.1.2) WHILE: (not a valid command - {2,4,6,8,+,-,x,n,\n) // 3.4) WHILE: Game not Over (moves stack not full)&&(no winner)&&(!exflag)