//========================================================================= #define PROGRAMMER "SOLUTIONS, Nofrills" #define PROG_CODE "soln" #define COURSE "ECE-1021" #define YEAR (2004) #define TERM "Spring" #define SECTION (0) #define ASSIGNMENT "HW6B" #define REVISION (1) #define TITLE "Mancala Engine" #define SUBTITLE "Last Modified on 16 MAR 04" #define EMAIL "wbahn@eac.ucce.edu" #define FILENAME "06b0soln.txt" //========================================================================= //========================================================================= // PROBLEM //========================================================================= // // Write a Mancala Engine to allow two-player human play. // // ================================================================= // | | 1 | 2 | 3 | 4 | 5 | 6 | | // ================================================================= // | | | | | | | | | // | | 4 | 4 | 4 | 4 | 4 | 4 | | // | | | | | | | | | // | 0 |=======|=======|=======|=======|=======|=======| 0 | // | | | | | | | | | // | | 4 | 4 | 4 | 4 | 4 | 4 | | // | | | | | | | | | // ================================================================= // | | 6 | 5 | 4 | 3 | 2 | 1 | | // ================================================================= // // Player[2][7] // Player[n][0] => Player n's Kalaha // Player[n][b] => Plyaer n's bin b ( 1 <= b <= 6 ) // //========================================================================= // TOP LEVEL PSEUDOCODE //========================================================================= // // 1) TASK: Initial Setup Actions // 1.1) TASK: Initialize the Board. // 1.2) TASK: Select which player will start. // 1.3) TASK: Display the Game Board // 2) TASK: Play a Game of Mancala // 2.1) WHILE: (Game is not over) // 2.1.1) TASK: Get move from present player. // 2.1.2) TASK: Check if move is legal and forfeit if not // 2.1.2.1) IF: (move is not legal) // 2.1.2.1.1) TASK: Make present player forfeit game. // 2.1.3) TASK: Execute the move // 2.1.4) TASK: Update present player // 2.1.5) TASK: Display the Game Board // 3) TASK: Display the results //========================================================================= // PSEUDOCODE for lower level tasks //========================================================================= //------------------------------------------------------------------------- // TASK: Initialize the Board. //------------------------------------------------------------------------- // // PASSED IN: board // 1) TASK: Emply kalahas // 1.1) SET: board[n][0] = 0 for n = {0->1} // 2) TASK: Load bins // 2.1) SET: board[n][b] = 0 for n = {0->1} and b = {1->6} // 3) RETURN: //------------------------------------------------------------------------- // TASK: Select which player will start. //------------------------------------------------------------------------- // // PASSED IN: // 1) TASK: Select starting player // 1.1) SET: player = random integer in the range (0->1) // 2) RETURN: player //------------------------------------------------------------------------- // TASK: Display the Game Board //------------------------------------------------------------------------- // // PASSED IN: board // 1) TASK: Display Player B Bin Labels // 2) TASK: Display Player B Bin Contents // 3) TASK: Display Dividing Line and Kalaha Contents // 4) TASK: Display Player A Bin Contents // 5) TASK: Display Player A Bin Labels //------------------------------------------------------------------------- // TASK: Check if Game is over //------------------------------------------------------------------------- // // PASSED IN: board // 1) TASK: Check for any seeds in any bins other than kalahas // 1.1) IF( sum of seeds in both kalahas < 48 ) // 1.1.1) SET: gameover = TRUE // 1.2) ELSE // 1.2.1) SET: gameover = FALSE // 2) RETURN: gameover //------------------------------------------------------------------------- // TASK: Get move from present player. //------------------------------------------------------------------------- // // PASSED IN: board, player // 1) TASK: Prompt player which bin they want to sow. // 2) TASK: Get number of bin to sow from player. // 3) RETURN: bin //------------------------------------------------------------------------- // TASK: Check if move is legal //------------------------------------------------------------------------- // // PASSED IN: board, player, bin // 1) TASK: Check that the player's bin has at least one seed // 1.1) IF( board[player][bin] > 0) // 1.1.2) SET: legal = TRUE // 1.2) ELSE // 1.2.1) SET: legal = FALSE // RETURN: legal //------------------------------------------------------------------------- // TASK: Make present player to forfiet game. //------------------------------------------------------------------------- // // 1) OUT: "Illegal Move" message to user. // 2) SET: bin = 0 (to inform the Execute Move task of the forfeit. //------------------------------------------------------------------------- // TASK: Execute the move //------------------------------------------------------------------------- // // PASSED IN: board, player, bin // 1) IF: (game is forfeit) // 1.1) TASK: Empty all bins // 1.2) TASK: Empty player's kalaha // 1.3) TASK: Place all seeds in other players kalaha // 2) ELSE: // 2.1) TASK: Empty the bin to be sown // 2.1.1) SET: board[player][bin] = 0 // 2.2) TASK: Sow the seeds around the board // 3) IF: (last seed placed in empty bin on player's side) // 3.1) TASK: Place last seed sown in player's kalaha // 3.1.1) SET: board[player][kalaha] += 1 // 3.1.2) SET: board[player][lastbin] = 0 // 3.2) TASK: Place seeds in bin opposite in player's kalaha // 3.2.1) SET: board[player][kalaha] += board[other player][7-lastbin] = 0 // 3.2.2) SET: board[other player][7 - lastbin] = 0 // 3) IF: (last seed placed in player's kalaha) // 3.1) SET: nextplayer = player // 4) ELSE: // 4.1) SET: nextplayer = other player // 5) RETURN: nextplayer //------------------------------------------------------------------------- // TASK: Display the results //------------------------------------------------------------------------- // // PASSED IN: board // 1) TASK: Display final score // 1.1) OUT: Player A: board[0][kalaha] // 1.2) OUT: Player B: board[1][kalaha] // 2) TASK: Announce winner // 2.1) IF: (board[0][kalaha] == board[1][kalaha]) // 2.1.1) OUT: Tie Game // 2.2) ELSE: // 2.2.1) IF: (board[0][kalaha] > board[1][kalaha]) // 2.2.1.1) OUT: Player A wins // 2.2.2) ELSE: // 2.2.2.1) OUT: Player B wins