GAME poker /* Information for a new game of cards */
#CARD FIVE PRINTING WRONG CODE#
Hint – this code does the right thing for just ONE player: Your program should produce an error if it runs out of cards. Print_hand(, _cards) Įxercise 6(quite difficult – only do this if you like a challenge): Adapt your code along the above lines to request a number of players and then deal a seven card poker hand to each player.
Similarly we could print the first player's hand using the following line: * Assuming that in our typedef for CARD we had set the value of the card to be stored in the variable "int value " */ For example, to look at the value of the first card in the first players hand we would do: Of course this make things quite complex if we want to get at individual bits of information. (Note, in real code we'd have an const int instead of a plain 100 in those). Int no_players /* No of players in the game */Ĭhar name_of_game /* Name of game we are playing */ HAND *hands /*Hands for each of the players */ We could even have a further type definition for all the players: This type definition must, naturally, come after the one for the card itself.
Int no_cards /* No of cards in the hand */ We could add routines to play cards from the hand – perhaps set up another structure which contains a hand of cards:ĬARD *cards /*Cards which are in the hand */ The final exercise (and a little more about card play) If poker is more your game then deal 5 or 7 cards to each player instead of 13. * Deal east's hand – remembering there are now 13 less cards Print_hand(north, 13) /* Print north's hand */ North= deal_hand (deck, 52, 13) /* Deal a hand to north */ * Your code to set up and shuffle here */ If we've done it right then we can put the following in main() assuming we are a bridge player:ĬARD *north /*Store hand for the north player */ĬARD *east /*Store hand for the east player */ĬARD *south /*Store hand for the south player */ĬARD *west /*Store hand for the west player */ Hint 2: It would be good to check that there are enough cards left in the deck to deal the hand. you need to figure out the appropriate for loop to do this. Hint 1: Deal the cards from the back of the pack – if there are 52 cards left then we can deal by making hand= deck, hand= deck, hand= deck etc etc etc. Return hand /* Return the pointer to the hand of cards we * Copy the right number of cards from the deck to hand */ Hand= (CARD *)malloc (to_deal*sizeof(CARD)) The function might look something like this:ĬARD *deal_hand (CARD deck, int cards_left, int to_deal) The function will be passed your shuffled deck of cards, the number of cards remaining in the deck and the number of cards to deal and will return a pointer to allocated memory containing a hand of cards. If you didn't finish then there's one in cards.cpp in the usual place.Įxercise 5: Write a function which deals a hand of cards. Get your cards example from last week's worksheet. You don't have to understand how this program works but you should try to understand what goes in the source files and what goes in header files. Notice what is in the header files and what is in the source files. The use the FileView tab to look at the various Source and Header files in the program. txt part (if it cannot find the file you have typed the name wrong or have the file in the wrong directory).ĩ) If the build doesn't work then try again with a different project name FOLLOWING THE INSTRUCTIONS CLOSELY. txt files from the week5 directory in the same folder as the project you just created.Ĩ) Run the program and type one of the names of the. Find life.cpp that you downloaded and add it.ħ) Save all the. * Set up an array for the board and fill it with spaces */įor (i= 0 i New->Console Application->Win32 Console Application (call your project life or something similar).Ĥ) Select Project->Add To Project->Files. * Function to print the board to screen */ Consider the code below which is the start of a program to play chess (you can load it from chess.cpp): Recall from last lecture that we can define an array with two (or even more) dimensions. The final exercise (and a little more about card play) 4