Homework #11
Topic:Displaying card bitmaps
Course:CS 102 – Basic Programming
Date Assigned:December 4, 2009
Date Due:December 11, 2009
Reading:None assigned
1) For this homework you need to finish up the war project with a bit of finish and polish. The first thing you need to do is to display the bitmaps of the cards that are being turned over for each player, not just the name of the card. So, for example, you might show this:
(Please note how the program is showing the top card for each player, not the name of the card). If you want to show both the bitmap and the name of the card, that’s great too!)
It turns out that you really don’t need to do very much to make this happen. The first thing you will need (as we discussed in class), is an array of bitmaps, representing the cards in the deck. You can use a declaration something like this:
Dim bitmaps(52) As System.Drawing.Bitmap ' Bitmaps of card faces
Please note that this array was defined to 52, not 51, as we have an extra bitmap for the blank card back. When you start the program, you need to fill the array with the bitmaps. Remember (from the class assignment on Wednesday) that you nee to add the resources for the card pictures to your solution. Then, you can have some code something like this in your Form_Load method:
Dim tInt As Integer
Dim tBitmaps() As System.Drawing.Bitmap = {WindowsApplication1.My.Resources.b1fv, WindowsApplication1.My.Resources.c2, WindowsApplication1.My.Resources.c3, WindowsApplication1.My.Resources.c4, _
WindowsApplication1.My.Resources.c5, WindowsApplication1.My.Resources.c6, WindowsApplication1.My.Resources.c7, WindowsApplication1.My.Resources.c8, _
WindowsApplication1.My.Resources.c9, WindowsApplication1.My.Resources.c10, WindowsApplication1.My.Resources.cj, WindowsApplication1.My.Resources.cq, _
WindowsApplication1.My.Resources.ck, WindowsApplication1.My.Resources.c1, WindowsApplication1.My.Resources.d2, WindowsApplication1.My.Resources.d3, _
WindowsApplication1.My.Resources.d4, WindowsApplication1.My.Resources.d5, WindowsApplication1.My.Resources.d6, WindowsApplication1.My.Resources.d7, WindowsApplication1.My.Resources.d8, …
' Copy the temporary bitmaps to the global ones
For tInt = 0 To 52
bitmaps(tInt) = tBitmaps(tInt)
Next
You might ask: Why do we have the temporary array tBitmaps, and then iterate and copy those bitmaps to the global array bitmaps? The answer is that Visual Basic does not allow you to declare a variable as global (outside of a method), and then also assign values to it with the “=”. By doing this this way, we declare the array globally, and then copy from the local array tBitmaps to the global one Bitmaps. If you have questions about this, please feel free to ask during office hours.
2) Extra Credit. If you’re feeling really adventuresome and creative, there are a bunch of additions that you can add to this war game to make it more interesting and fun to play. Here are just a few:
- You can delay the display of the cards in a war to make it more dramatic. Not sure how to do this? Look at the system.threading.thread.sleep method. It allows you to sleep for a specified number of milliseconds.
- Create an animated celebration when one player or the other wins a war (or the entire game). You can use bitmaps, animation, whatever you’d like.
- Save the results of the game in an external file. Perhaps you could record the number of games played, and the number of games that player 1 won. Then, when you start the game you’d need to read the file and increase the total number of games by 1 (and write it back to the file). Ditto for when one player or the other wins the game.
- Anything else you can think of. Not sure about this? Just ask!!
3) Trivia question (Subject: US States): Can you name the most densely populated US State? How about the least densely populated state?
4) Bonus question: Can you name the only US state that has more head of livestock than it has people? Hint: It’s the US State with the smallest population overall.