Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. ask for the number of heaps (simple printf then scanf to an integer)
- 2. check whether num of heaps is between 1 and 32(define constants for 1 and 32 and use them), if not, print an error and terminate
- 3. get heap sizes from user (a loop of scanfs should work), if a non positive (<=0) heap size was entered, print an error and terminate
- - useful link: https://stackoverflow.com/questions/2539796/c-reading-multiple-numbers-from-single-input-line-scanf
- - the heap sizes can be entered in many ways such as: size1ENTERsize2ENTER... or size1SPACEsize2SPACE...ENTER, make sure our program is ok with these different inputs
- 4. function that simulates a game:
- - contains a while loop, while(not all heaps are empty), if all the heaps are empty, we exit while and announce the winner
- - we can maintain a variable userPlayedLastTurn (is 0/1). If we left the while with userPlayedLastTurn = 1, we print that user
- won, otherwise we print that computer won
- - now let's look at what happens inside the while loop
- - print status of the heaps (a variable numOfTurns should be maintained) and proceed with user/computer 's turn
- - if this is a user turn:
- print a proper message and get heapIndex and numItemsToRemove from user
- check validity of input:
- while(input is not valid) ask the user to insert a valid input, once a valid input was inserted print a proper message
- and move on to computer's turn (we also need to update the heapSize of the heap we chose)
- - if this is a computer turn:
- same as user turn, but in this case, we get heapIndex and numItemsToRemove from function(computation) and not from user
- 5. in section 4 we use several functions we need to implement:
- - int allHeapsAreEmpty: receives an int array, returns 0/1
- - various print functions
- - int isValidUserTurnInput: receives the two integers (heapIndex, numItemsToRemove) the user entered, returns 0/1
- - COMPUTATION OF COMPUTER'S CHOICE OF heapIndex AND numItemsToRemove (this is the computer's strategy function)
- Assumptions:
- * the user enters only int values from -2^31 to 2^31-1 (I think we can use int variable, no need for long or anything like that)
- * user starts the game
- Where do we implement what:
- - main:
- * receive numOfHeaps from user
- * initialize heap array (including getting heapSizes)
- * handling user's move [simply two scanfs]
- * run the game itself
- - sp_nim:
- * handling computer's move [two functions - one for heapIndex, one for numOfItemsToRemove]
- * determine whether there is a winner (are all the heaps empty?)
- - main_aux:
- * check validity of numOfHeaps
- * check validity of heapSize
- * all print methods
- * check validity of heapIndex and numOfItemsToRemove
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement