//Name: Nicholas Brinley //Date: 2/22/09 //class: csis 250 //Assign: #2 blocks //Description: Implementation file for blocks problem #include "blocks.h" //Definition: Retrieve the number of blocks to be simulated from user //Pre: none //Post: the number of simulated blocks is returned. int getNumBlocks() { int blocks = 0; //varible to hold the input from user cout << "Please enter the number of blocks you would like to create: "; cin >> blocks; if(blocks > 25) //Check to make sure a reasonable value is used { while(blocks > 25) { cout << "Please enter a number between 0 - 25: "; cin >> blocks; } } return blocks; } void commandHelp() { cout << "Enter your command in the following format: " << " onto/over " << endl << "You may also simply enter 'quit'" << endl; return; } //Definition: Reads in the command from the user and makes appropriate calls //Pre: none //Post: Call Made to a function to perform action for a block or quit bool readInCmd(int stack[][MAX_SIZE]) { string cmd; int blockMoved; int referenceBlock; string adverb; bool quitFlag = false; cout << ">"; cin >> cmd; if(cmd != "quit") //stop reading in if quit is entered cin >>blockMoved >> adverb >> referenceBlock; //read the users cmd /*---break down the usr's cmd and seperate it into parts to make func call--*/ if(cmd == "move") { if(adverb == "onto") moveOnto(stack, blockMoved, referenceBlock); else moveOver(stack, blockMoved, referenceBlock); } if(cmd == "pile") { if(adverb == "onto") pileOnto(stack, blockMoved, referenceBlock); else pileOver(stack, blockMoved, referenceBlock); } if(cmd == "quit") quitFlag = true; return quitFlag; } //Definition: Function to search the array for a block //Pre: Call made to the function with the block to be found //Post: the posistion of desired block is returned void findBlock(int stack[][MAX_SIZE], int referenceBlock, int horizontal, int vertical) { int x = 0; int y = 0; int current = 0; bool quitFlag = true; while(quitFlag == true && x <= MAX_SIZE) { current = stack[y][x]; if(current == referenceBlock) { horizontal = x; vertical = y; quitFlag = false; } if(current == 0) x++; y++; } return; } //Definition: return block to its initail posistion //Pre: Posistion of block known //Post: Block will be returned to its starting posistion void returnBlock(int stack[][MAX_SIZE], int horizontal, int vertical) { int workingBlock = 0; int yAdder = 1; bool columnCleared = false; do{ workingBlock = stack[vertical + yAdder][horizontal]; cout << "The current block is " << workingBlock << endl; if(workingBlock > 0) stack[0][workingBlock -1] = workingBlock; else columnCleared = true; yAdder++; } while(columnCleared == false); return; } void moveOnto(int stack[][MAX_SIZE], int blockMoved, int referenceBlock) { int horizontal; int vertical; int referenceXpos, referenceYpos; int movedXpos, movedYpos; findBlock(stack, referenceBlock, horizontal, vertical); cout << "working...." << endl; returnBlock(stack, horizontal, vertical); //clear blocks from top of block //that will be stacked on referenceXpos = horizontal; referenceYpos = vertical; cout << "still working...." << endl; findBlock(stack, blockMoved, horizontal, vertical); returnBlock(stack, horizontal, vertical); //clear blocks from top of block //that will be moved cout << "still working...." << endl; stack[referenceYpos + 1][referenceXpos] = blockMoved; //move block onto other stack[vertical][horizontal] = 0; //erase old copy of block return; } void moveOver(int stack[][MAX_SIZE], int blockMoved, int referenceBlock) { int horizontal; int vertical; int referenceXpos, referenceYpos; int movedXpos, movedYpos; findBlock(stack, referenceBlock, horizontal, vertical); referenceXpos = horizontal; referenceYpos = vertical; findBlock(stack, blockMoved, horizontal, vertical); returnBlock(stack, horizontal, vertical); //clear blocks from top of block //that will be stacked onto stack[referenceYpos + 1][referenceXpos] = blockMoved; //move block onto other stack[vertical][horizontal] = 0; //erase old copy of block return; } void pileOnto(int stack[][MAX_SIZE], int blockMoved, int referenceBlock) { return; } void pileOver(int stack[][MAX_SIZE], int blockMoved, int referenceBlock) { return; } //Definition: Fill the array with the specified number of blocks //Pre: user has entered a value for the number of blocks //Post: array is filled to specified amount. void fillArray(int stack[][MAX_SIZE], int numBlocks) { int YstartValue = 0; cout << numBlocks << endl; for(int i = 0; i < numBlocks; i++) { stack[YstartValue][i] = i + 1; cout << "block: " << stack[YstartValue][i] << " @ posistion " << i << endl; } YstartValue = 1; for(int j = 0; j < MAX_SIZE; j++) { stack[YstartValue][j] = 0; cout << "block: " << stack[YstartValue][j] << " @ posistion " << j << endl; if(j == (numBlocks - 1)) { cout << "Row : " << YstartValue << endl; j = 0; YstartValue++; } } return; } void printBlocks(int stack[][MAX_SIZE], int numBlocks) { int x = 0; int y = 0; while(x < numBlocks) { if(y == 0) cout << x << ": "; if(stack[y][x] > 0) { cout << stack[y][x]; y++; } else { x++; y = 0; cout << endl; } } return; }