Professional Documents
Culture Documents
C# Programming:
Hands-On
419/MA/N.4/308/N.3
by Gregory Adams
Technical Editor:
Sean Smith
© LEARNING TREE INTERNATIONAL, INC.
All rights reserved.
All trademarked product and company names are the property of their respective trademark holders.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic,
mechanical, photocopying, recording or otherwise, or translated into any language, without the prior written permission of the publisher.
Copying software used in this course is prohibited without the express permission of Learning Tree International, Inc. Making unauthorized
copies of such software violates federal copyright law, which includes both civil and criminal penalties.
Exercise Manual Contents
1. Action Hint
Checkpoint Stop
Question Congratulations
Information Bonus
Objective
Become familiar with Visual Studio (VS) and test a sample C# program that
does currency conversion. Do all three parts—A, B, and C.
The installation of Visual Studio 2012 you are about to use has
been minimally preconfigured for course use. The following changes
have been made:
• Update 1 has been added.
• Help has been installed locally so that it is not necessary to be
online during class.
• C# developer layout settings were chosen on first launch.
• C# editor has been set to keep tabs rather than substitute spaces.
None of the course exercises are dependent on this configuration,
and you should be able to directly use a virgin installation of Visual
Studio 2012 to do them—update 1 is recommended.
1. Click the Start Orb and from its pop-up menu, select Visual Studio.
This will start the integrated development environment (IDE) that we
will use during the course.
Although you could select the link “Open Project” from the start
page, the start page might not be available depending on how Visual
Studio is configured.
5. Set local help as the default. From the menu bar select Help | Set
Help Preference | Launch in Help Viewer.
Open the Solution Explorer window so that you are able to see
the files included in the project.
7. If the Solution Explorer window is not already there, select from the
menu bar View | Solution Explorer, or click the Solution Explorer
icon in the top toolbar on the right side. Use “tool tips” if necessary to
locate it.
8. If the files are not already visible in the Solution Explorer, click the
triangle icon next to the CurrencyConverter project. This will open
to reveal all of the files and folders in the project.
10. Briefly review it and see if you can determine what it does.
11. Select from the VS menu bar Debug | Start Without Debugging.
This will both compile and run the program.
12. The program will now prompt you for input with:
14. Try moving around Program.cs using the mouse and cursor keys.
Change some text by adding, modifying, or deleting.
15. Now use undo to completely undo all changes you have made. You’ll
know when all changes have been undone because the toolbar and
menu items will gray out.
Notice that the edit window has [+] and [-] icons; these can be
used to fold away text that you are not currently working with and
then to unfold it again when you need it.
16. Try folding and unfolding a few of the code sections, leaving only
Main exposed.
18. Attempt to compile and execute the program using Debug | Start
Without Debugging.
20. In the message window, double-click the text of the first error
message. This will have the effect of jumping to the error line in the
edit window.
If you hover the mouse pointer over the highlighted error in the
edit window, a tool tip will appear possibly explaining the error.
21. Enter a few other errors and compiling to see what happens. Try:
• Removing a semicolon at the end of a statement
• Removing a curly bracket: { or }
22. Select File | Close Solution and select No if asked to save changes.
Ensure that the project window and the edit windows disappear.
23. Select File | New | Project from the VS menu bar and wait for the
New Project dialog box to appear.
26. Modify the Main method—insert the logic to output the text message
"Hello World!".
29. Dismiss the help window by clicking the [x] icon in the upper right
corner.
30. Using Windows Explorer, examine the files and directory layout of
the solution you created in C:\Course\419\Exercises\Ex11C.
Objective
Become familiar with using namespace directives, methods, and overloading.
2. From the VS menu bar, select File | Open Solution and, when the
File dialog appears, go to C:\Course\419\Exercises\Ex21 and
double-click the solution control file CurrencyConverter.sln.
6. Find the four occurrences in the program and make the substitution.
7. Compile.
What happens?
10. If it is not already open, select the file Program.cs for editing.
13. In the Solution Explorer window, click the triangle icon next to
References.
The libraries currently being used for this solution will be listed.
14. Right-click References and select Add Reference from the pop-up
menu.
23. Edit the main program to use different input currencies. You should
have to change only one line of code to achieve this; e.g., something
like string inCur = "JPY";
Compile and test a few times, trying different currency codes like:
• SEK for Swedish Kronen
• GBP for Great Britain pounds
24. Modify the specification of the Convert method to take both the
input and output currencies as arguments. It should look like this:
… Convert(string ic, string oc, double amt)
26. Compile and test with different input and output currencies. Change
the inCur and outCur values in the Main(…) method.
Objective
To make our program more convenient to use, we want to modify it so that
we can pass in the amount to be converted and the currency type to convert;
i.e., the command line should allow something like:
$ CurrencyConverter 34101.25 USD JPY
This should convert 34,101.25 U.S. dollars to Japanese yen.
4. Compile.
6. Compile and test to ensure that the program is still working as before.
7. Before the first line of code in the Main method, enter something like
the following (substitute your code for the … sections):
if (…number of args… != 3)
{
Console.WriteLine(…report the number of args…);
return …error code value…
}
8. Get a clean compile of the program (but do not run it). From the VS
menu bar, select Build | Build or press <F6>.
You can use the <Up arrow> and <Down arrow> keys to
recall previous commands.
14. Find the line of code that contains string inCur = ... and
change it to initialize inCur using the second element of the
argument array converted to all uppercase.
16. Find the line of code that assigns a value to inAmt, as in:
inAmt = GetAmount("Enter…");
Objective
Become familiar with writing Windows Forms programs. During the course,
we will be developing a card game known as “The Eyes Have It,” or “Tehi.”
To start, we will begin with the user-interface tier.
For this exercise, we will use a ListBox as an area to display results. When
done, the game table should look something like the following.
Close the previous solution if you have not already done so.
After a few seconds, the project will have been created and the
Forms design window should appear.
3. Click the form and then move the mouse pointer to the grip at the
right edge of the form. A cursor that looks like <-> will appear so that
you can visualize the resizing.
4. Drag the right edge so that the form is about twice as wide as it is
high.
5. Right-click within the body of the form. From the pop-up menu that
appears, select Properties.
6. Select the property called Text and modify its entry to read The
Eyes Have It!
7. Click the property called BackColor and click the color selection
combo box next to it. Since most card tables have a green or a blue
felt surface, click the Web tab and select a green or dark blue color
that suits your fancy.
9. Click the Toolbox icon at the far left side of the screen (in the left
bar) and click the “thumbtack” icon at the top of the window—that will
hold it in place. If the Toolbox icon is not visible, select the menu
choice View | Toolbox.
10. If it is not already selected, click to open the All Windows Forms tab
in the Toolbox window.
11. Click the ListBox control. Move the cursor to the form and drag the
outline of where you want the list box to appear.
See the screenshot at the start of this exercise for the look of
the list box control.
12. Click the ListBox control so that its property sheet appears. Set its
font property to Lucida Sans Unicode with a size of 10 point.
14. Drag and drop a StatusStrip control from the toolbox to the form. It
should snap to the bottom docked position on the form.
15. Click the StatusStrip control (which you just added at the bottom of
the form), and a drop-down menu will appear. From the drop-down
menu, select StatusLabel. A label should appear on the status strip
on the left side.
16. Click the label StatusLabel (not StatusStrip) and, in its Properties
window, set the following:
• (Name) property to be StatusLabel (the (Name) property is
located near the top of the property list)
• BackColor to System | Control
• Font property a little bigger—10 is good
• Text property to be blank
17. Click the Button control in the toolbox. Move the cursor to the form
and drag the outline of where you want the Deal button (see
screenshot at start of this exercise).
As you resize, the Deal button should stay centered near the
bottom. The list box, however, should stretch to fill the middle.
20. Exit the program by clicking the [x] in the upper right corner.
In the future you should always exit from the program before
returning to Visual Studio, even if the instructions do not explicitly
say to do so.
22. Position within the event method and clear the list box using:
LogListBox.Items.Clear();
24. Set some information in the status label using something like:
StatusLabel.Text = "Deal button pressed";
When you click Deal, you should see the spade symbol
indicating that the proper Unicode font is selected. Also, the deal
message should be in the status bar.
If you have more time, add the remaining controls and menus
that we will require in the future for various actions of the
game.
Drag a menu strip to the form and populate its menu and
menu-item choices.
26. In the toolbox, drag a MenuStrip to the form. It should snap to the
top of the form. Move your list box down if necessary to make room
for the menu strip.
28. Compile and test. The menus should select but won’t do anything
after selection just yet. Check that you can select them using
something like <Alt><G><X> to exit the game. Note that this is
done in three distinct key strokes—you do not need to hold the
<Alt> key down while you press the <G> or the <X>.
30. Compile and test. Ensure the menus do the expected functions.
31. In Solution Explorer, right-click the Tehi project (not the solution) and
select Add | Existing Item.
32. In the dialog Filename field, enter *.ico and press <Enter>. This
is so that only icon files will show.
35. In Solution Explorer, right-click the Tehi project (not the solution).
Select Properties.
36. In the properties page, select the Application tab and then, from the
drop-down list under Icon, select App.ico.
This is so that the file Tehi.exe will show in Windows with the
icon you selected.
37. Get a clean compile and close the properties page (click the X).
38. In the designer, click the form and then, from its property sheet,
select the Icon property. Click the […] button so that an icon
selection dialog box appears.
The icon you selected should now be showing on the left side
of the title bar.
41. Unpin the toolbox (click the thumbtack) so that it returns to the far left
side of Visual Studio.
Add a user name text box and a login button to the form.
42. Open Form1.cs in design mode and click the TextBox control in
the toolbox. Move the cursor to the form and place the text box at the
position shown (login button will not be there yet).
44. Right-click the Deal button. Select Copy from the pop-up menu.
45. Right-click the form (not any control) and select Paste from the pop-
up menu. Drag it to the position for the Login button (see screenshot
above).
48. Compile and test. Go back and forth between the Deal button and
the Login button.
50. Return to design view and right-click the form again. Select Paste.
Drag this button to the appropriate place for the first Swap button
(see screenshot).
53. Right-click the form and select Paste. Drag this button to the position
for the next Swap button and set its (Name) to the next button
number: SwapButton1, SwapButton2, and so on.
54. Repeat the previous step for the remaining three Swap buttons.
55. In design view, double-click the first Swap button. In its event
method, call SwapCard(0)
56. Repeat the previous step for the remaining Swap buttons. Increment
the reported button number by one each time.
57. Compile and test. Click all buttons to ensure they are working.
58. Drag and drop a ColorDialog control to your form and ensure it
appears in the non-attached list at the bottom of the design window.
60. In the event method, add the code to display colorDialog1 and
capture its DialogResult. This is much like displaying a
MessageBox, but the ShowDialog() method is used, as in:
DialogResult result = colorDialog1.ShowDialog(this);
62. If the user did not cancel the color dialog, then set the background
color of the game table using something like this:
BackColor = colorDialog1.Color;
Objective
Become familiar with writing ASP.NET thin-client Web pages. During the
course, we will be developing a card game known as “The Eyes Have It,” or
“Tehi.” To start, we will begin with the user-interface tier.
For this exercise, we will use a ListBox as an area to display results. When
done, the game table should look something like the following.
2. In the dialog box, select or confirm the following, then click OK.
• Installed | Templates | Visual C# | Web
• ASP.NET Empty Web Application
• .NET Framework 4 (in a drop-down )
• Name: Tehi
• Location: C:\Course\419\Exercises\
• Solution Name: Tehi
• Create Directory For Solution: Selected
After a short wait, the Web site will be created. You should see
only the Web.config file in the project.
3. Right-click the Tehi Web project and select Add | New Item. In the
dialog, select a Web Form and name it Default.aspx
5. Click the Toolbox icon at the far left side of the screen (in the left
bar), and click the “thumbtack” icon at the top of the window that
appears to hold it in place. If the Toolbox icon is not visible, select
the menu choice View | Toolbox.
8. Modify the body tag to set the background color to silver. The tag
should look like <body bgcolor="Silver">. Note that you will
have to type it in; the autocomplete will not recognize it until it is fully
entered.
9. Click the Design tab, and then click inside the rectangle beneath the
body tag. The tag will change to a div tag. From the menu bar (not
the tool box), select Table | Insert Table. In the dialog, select the
following:
• 7 rows by 5 columns
• Center aligned
• 8 for the Cell Padding
• 0 for the Cell Spacing
• 0 for the Border Size
• 550 pixels for the Width
• Green Background color (type it in)
Leave the remainder as is and click OK. A green table should have
appeared.
10. Merge the columns of the first row. To do this, click in the first cell, then
hold down the <Shift> key and click in each of the remaining cells in
that row. Don’t worry if the first cell looks deselected—it is still selected.
Right-click and select Modify | Merge Cells.
11. Repeat the merge for the second, third, and bottom rows.
13. Exit the browser by clicking the [x] in the upper right corner.
Add the Deal button, a list box, and labels to the Web page.
See the screenshot at the start of this exercise for how the Web
page should look.
14. Drag and drop a ListBox control to the third row in the table. If
necessary, press the <Esc> key to dismiss the ListBox Tasks dialog.
In its property sheet, change the following:
• (ID) property to LogListBox
• Enabled to false
• Font | Name to Lucida Sans Unicode
• Font Size to 12 (type it in)
• Height to 120px
• Width to 100%
15. Drag a Button to the form in the middle cell of the sixth row. In its
property sheet, set:
• (ID) to DealButton
• Font size to 12 (click the triangle icon next to the font property to
display the subproperties)
• Text to Deal
• Width to 96
16. Run the program to see what you have so far. Don’t worry if the Deal
button is offset. This will adjust automatically as more controls are
added to the page.
17. Drag and drop a Label to the bottom row of the table. In its property
sheet, set:
• (ID) to StatusLabel
• Font Size to 14 (type it in)
• ForeColor to white (type it in)
• Text to blank
18. Right-click the status label and select Copy. Right-click the top row
and select Paste. In its property sheet, set:
• Font to Bold and Italic
• Text to THE EYES HAVE IT!
In the following, you will open the source code to add the
event-handling method for the button.
20. Exit the browser. When you are back in the designer, double-click
the Deal button to open the source code for its event method.
23. Set some information in the status label using something like:
StatusLabel.Text = "Deal button pressed";
If you see the spade symbol, the list box has the correct font.
Ensure the status message is appearing, too.
If you have more time, add additional controls to the page that
we will need to implement the game.
27. Click to the right of the New link button and enter two spaces, then
right-click and select Paste. Repeat this step so that you have three
New link buttons.
28. Change the Text property of the second link button to Top10 and
the last one to About
29. Double-click the New link button to go to its event method. Clear the
list box by using LogListBox.Items.Clear(). Also clear the
status label text by assigning string.Empty to it.
30. Return to design view and double-click the Top10 link button. In its
event method, display “Top 10 Selected” in the status label.
31. Compile and test. Ensure New clears the list box and status label and
Top 10 shows the message.
32. In Solution Explorer, right-click the Tehi Web project and select
Add | New Folder. Name this folder Images
33. Right-click the new Images folder and select Add | Add Existing
Item. Select All Files (*.*) from the drop-down. Navigate to the
directory C:\Course\419\Graphics\Icons. Pick an icon you like
and select Add to copy it into our assembly. Rename it App.ico
34. From under the Standard tab in the toolbox, drag and drop an Image
control onto the first row of the table, placing it before the label.
Place a couple of spaces after the image.
The icon you selected should now be showing on the left side
of the title bar.
You have a choice. You can design the About page manually or
you can include one that has already been laid out. In either case, it
will be necessary for you to implement the page navigation logic.
39. Do this step only if manually designing the page: Using the
designer, lay out the About form so it looks similar to the screenshot
on the previous page. Here are some suggestions that might help:
• In design view, add the labels, icons, and buttons as shown and
set their properties as appropriate
• Substitute your name in the appropriate text position
• In the source view, find the Style1 tag and set the width to 565
• Also in source view, set the page title to About TEHI
41. Compile and test. When you click OK on the About page, the user
should be redirected to the game page.
43. Compile and test. When you click the About link, the user should be
redirected to the About page.
44. Return to the event method for the About link button modify it to do a
response redirect call to go to About.aspx.
After the following eight steps, your screen will look something
like this:
47. Drag a TextBox into the first cell of the same row as the Deal button
(see partial screen shot on previous page).Set its properties to:
• (ID) property to NameTextBox
• Font Size to 12
• Text to blank
• Width to 96
48. Drag a Button to the form into the cell to the right of the name text
(see earlier screenshot). In its property sheet, set:
• (ID) to LoginButton
• Font size to 12
• Text to Login
• Width to 96
50. Compile and test. Go back and forth between the Deal button and
the Login button.
52. In design view, drag a Button to the form into the first cell of the fifth
row as in the previous screenshot. In its property sheet, set:
• (ID) to SwapButton0
• Font size to 12
• Text to Swap
• Width to 96
54. Right-click the next cell to the right and select Paste. Repeat for the
remaining cells on this row so that you have five Swap buttons.
55. In design view, double-click the first Swap button. In its event
method, call SwapCard(0)
56. Repeat the previous step for the remaining Swap buttons. Increment
the button number by one each time.
57. Compile and test. Click all buttons to ensure they are working.
Objective
Become familiar with creating classes by producing abstractions of standard
playing cards.
A standard deck of 52 playing cards is a common basis for many popular and
entertaining games, including Poker, Bridge, Hearts, Euchre, Blackjack,
Cribbage, Crazy Eights, Fish—or even your favorite! If you were to program
any of these games in C#, creating a PlayingCard class would be a good
place to start.
This abstraction of playing cards will form the basis of the business logic for
our game, “The Eyes Have It.”
Close the previous solution if you have not already done so.
2. Right-click the solution Tehi (not the project) and select Add | New
Project. In the dialog box that pops up, select a Project Type of
Visual C# | Windows | C# | Class Library and then enter
Name: CardLib
Location: C:\Course\419\Exercises\Ex41-web
or C:\419\Exercises\Ex41-desktop
as appropriate. Click OK.
4. Move to before the class declaration and add a public enum called
CardSuit. The elements are Clubs, Diamonds, Hearts, and
Spades.
5. Move to the first line inside of the class and insert an integer field
called rank. It should be declared private and readonly (cannot
be changed except in a constructor).
6. Repeat for the suit field, but its type should be CardSuit.
In the real world, once the rank and suit of a playing card are
established, they cannot be changed. This is why we made these
fields readonly.
7. Add a private Boolean field called faceUp to track if the front or the
back of the card is showing. If the card is face-up, then the front will
be showing. It should not be declared readonly!
10. In the body of the constructor, add the code to assign the rank
parameter to the rank field. You will need to use the this reference
to discriminate between them.
Add the logic for flipping cards to change them from face-up to
face-down and vice versa.
14. Add the method public void Flip(). It should flip the state of
the faceUp field. This can be done in one line of code without the
use of any if/else statements.
16. Move to the line after the Flip() method. Enter the word override
and press the <Spacebar>. Select ToString() from the drop-down
list and the method’s framework will be implemented for you.
18. Add the logic to detect whether the card is face-up or face-down. If it
is face-up, return rank + " " + suit; If it is not face-up,
return "XX"; indicating the card is hidden.
23. Move to the DealButton_Click method and delete the line that
displays the spade symbol.
24. After the line that clears the list box, create an instance of a playing
card. Call its reference card. Use a rank value of 5 and a suit of
clubs (CardSuit.Clubs).
26. Compile (use <F5>) and test by clicking the Deal button.
27. Before the card is displayed in the list box, add card.Flip();
30. Replace the return statement with the Indexing logic. Note that the
CardSuit enum must be cast to int to be used as an index.
return "" + ranks[rank] + suits[(int)suit];
31. Get a clean compile and test. Ensure that you got the correct two-
digit code, 5♣.
Add constant values in the playing card class for the named
ranks.
32. Move to the top of the PlayingCard class and, before the fields,
insert constants for ace, jack, queen, and king. Jack is 11; king is 13.
Use something like: public const int Ace = 1;
40. Compile and test. The program should throw an exception. Select
from the VS menu bar Debug | Stop Debugging.
41. Restore the program back to the queen so that it works again.
Objective
Become familiar with using properties, both auto-implemented and manual.
Currently we have no way of obtaining the rank or suit values, or setting or
getting the face-up status. We will implement properties for these now.
Close the previous solution if you have not already done so.
5. After the card is displayed in the list box, add the logic to display its
rank and suit individually. Rank, for example:
LogListBox.Items.Add("Rank: " + card.Rank);
6. Compile and test (use <F5>). The queen should have a rank of 12.
10. Try to get a clean compile. You should get a number of errors
indicating that faceUp (lower case f) is undefined.
16. Insert the framework for a property called Code. It should look
something like:
public string Code
{
get
{
}
}
17. Copy the entire body of the ToString() method and paste it into
the body of the get of Code.
20. Insert before the Code an Index property. If the card is face-up,
return (int)suit * 13 + rank, else return 0
23. Compile and test. The unique index should be 12 for the QC.
Objective
Use inheritance to create a derived class, called FaceCard, to track the
number of eyes that a “royal” card has. Royal cards are also called “court” or
“face” cards. The number of eyes is an example of the kinds of extensions a
royal card might have.
All face cards have two eyes, except the jack of hearts, the jack
of spades, and the king of diamonds—they have only one. Ask your
instructor to explain this if it is not clear.
Close the previous solution if you have not already done so.
3. Make the class declaration public and add the code to indicate that
FaceCard inherits from PlayingCard.
12. Edit FaceCard.cs and move to the constructor. After the line that
sets the number of eyes to 2, add the code to change the number of
eyes to 1 if the card is any of jack of hearts, jack of spades, or king
of diamonds. Use something like the following and repeat for each of
the one-eyed cards, then get a clean compile.
13. Following the constructor, type the word override followed by one
press of the <Spacebar> and select ToString from the list. This
will produce the framework for the ToString method.
17. Try setting the card to be face-down. It should just show XX.
19. Add the code to display the number of eyes. Use something like:
21. Return to FaceCard.cs and before the line Eyes = 2; add a call
to check that the card’s rank is appropriate for a face card. Type the
following (IntelliSense will not work here):
What happens?
24. Compile and run. The program should behave as before. Try testing
with a bad rank value. Restore it to a good value when done.
Objective
Become familiar with using collection classes from the .NET library
specifically to produce a deck of playing cards. A stack collection will most
closely match the behavior of a card deck.
Close the previous solution if you have not already done so.
3. Open C:\Course\419\CopyAndPaste\CardDeckCtor.txt. It
contains a framework for the constructor—but it will not compile
without changes! Copy and paste it into the inside of the CardDeck
class and indent as appropriate. Review for a few minutes and make
sure you know what it is doing.
4. At the place indicated, insert the code to add the card to the deck.
You are “pushing” something onto the deck stack.
13. Web only: Move inside the Page_Load(…) method and add the
logic to retrieve the user’s card deck from the session. You can use
the code from the copy-and-paste file DeckSessionLogic.txt if
you like. It should look something like:
deck = (CardDeck) Session["deck"];
if (deck == null)
{ deck = new CardDeck();
Session["deck"] = deck;
}
15. After the line that clears the list box, add the code:
LogListBox.Items.Add(deck.ToString());
You should see the deck displayed. The list box is not wide
enough to show the whole deck and there is no horizontal scroll bar
in the Web version—so please estimate that all 52 cards are there.
17. Select CardDeck.cs for editing and position the cursor on a new
blank line before the ToString() method.
18. Insert the layout for a method called Deal using the specification
public PlayingCard Deal()
19. In the body of Deal, check if there are fewer than two cards left in
the deck. If so, throw a new InvalidOperationException with
the message Too few cards in the deck.
20. Return the top card to the caller face down. Use something like:
a. Pop the card off the stack first (into a temporary variable).
b. Set popped card to be face down (FaceUp = false)
c. Return the card to the caller.
23. Remove the comment characters from the six lines of code that set
the card to face up and display information about it. Do not
uncomment the line that creates the face card or the line that shows
the eyes.
24. Compile and test. Repeat many times (50+) to confirm that cards are
being removed permanently from the deck and that finally the
exception will be thrown. Use Debug | Stop Debugging to recover
from the exception.
25. Select CardDeck.cs for editing and position the cursor on a new
blank line after the end of the Deal method.
26. Insert the overall layout for an additional method called Add using
public void Add(PlayingCard card)
27. In the body of Add, set the card back to facing up, and then push it
back on the stack.
30. Compile and test. The same card should be repeatedly dealt, since
we are always putting it back. Click Deal more than 52 times to
ensure the exception is never thrown.
32. Position the cursor to after the Add method, and insert the overall
layout for an additional method using the specification:
33. Convert the deck to an array and then clear the cards from the deck:
34. Now loop through the array using a for loop to select every card in
the deck: for (int i = 0; i < cards.Length; i++)
35. In the body of the for loop, add the logic to shuffle the cards. Pick
the random card using something like:
int swapIndex = rnd.Next(0,cards.Length);
This will return a random number within the array index range.
37. After the for loop, use a foreach loop to push every card in the
now-shuffled cards array back into the deck stack.
40. Compile and test. Note that every click of the Deal button will change
the order of the cards in the deck and a new card will be dealt.
Objective
Become familiar with casting up and down inheritance hierarchies.
Specifically, we will create a control class to encapsulate the game logic for
Tehi. If you are not familiar with how Tehi is played, your instructor will
explain. There are two parts to implementing the TehiGame class—
manipulating the cards and then evaluating the hand.
Close the previous solution if you have not already done so.
Add the fields to hold the card deck and the game hand.
1. Within the TehiGame class, declare a private field for the card deck
using CardDeck deck = new CardDeck();
6. Add a ToString() method that lists all the cards in the hand.
Again, please use the copy-and-paste code found in
C:\Course\419\CopyAndPaste\TehiGameToString.txt.
9. Web only: Move to the Page_Load method and replace the deck-
saving logic so it becomes the game-saving logic; i.e., change all six
occurrences of deck to game and both occurrences of CardDeck to
TehiGame.
10. Move to the DealButton_Click method and delete all of the code
except the line that clears the list box. After it, add:
game.Deal();
ShowOutput();
11. Right-click on the word ShowOutput() and when the drop-down list
appears, select Generate | Method Stub. Delete the exception code
that is generated.
12. Cut the line of code to clear the list box from DealButton_Click
and paste it into ShowOutput.
13. After the line that clears the list box, display the game. Use
LogListBox.Items.Add(game.ToString());
14. Compile and test. You should see different hands being dealt.
15. Select TehiGame.cs for editing and move the cursor to after the
fields.
16. Add the framework for a Score property. It should look like:
public int Score
{
get
{
}
}
17. Insert into the get of the Score property the logic to evaluate the
hand. It should look something like the following (finish the logic for
the … portions):
int totalEyes = 0;
int totalRank = 0;
foreach (PlayingCard card in hand)
{
… Determine if it is a FaceCard by checking its type, not its rank…
… If so, cast it as a FaceCard and add the number of
eyes to totalEyes …
… If not, add the rank to the totalRank …
}
return totalRank * totalEyes;
20. Add the code to show the game score in the LogListBox with some
appropriate message.
Add the logic to track the total hands dealt and the best hand
score.
22. Open TehiGame.cs for editing and as the first line of code within
the class, add two auto-implemented properties—one called
HandsDealt and one called BestHandScore. They should both be
declared public, return an integer, and have a private set.
23. Move to the Deal(…) method and after the loop add the logic to:
a. Count the HandsDealt.
b. Determine if this score is the best score and, if so, record it.
What happens when you start a new game via the menu?
29. Web only: Store the game back into the session.
Session["game"] = game;
30. Compile and test. Ensure that your best score is restarted for each
new game (it will be the same as the first score you achieve).
Objective
Although the ToString() method allows us to display a game hand, the
application will need to be able to loop through and obtain individual playing
cards in order to display them graphically (future exercise). We can support
looping through the cards by realizing IEnumerable<PlayingCard> in the
TehiGame class.
Close the previous solution if you have not already done so.
This project contains the solution files from the bonus of the
previous exercise.
4. Scroll to the end of the class and find the newly added methods.
9. Compile and test. In the desktop version, you can resize the form as
necessary to see all cards. Note that the individual cards are shown
in the same order as in the game.
10. Change the foreach loop to output the cards in the reverse order;
i.e., call the Reverse() extension method on the game.
13. Move to the Deal() method and at the end set CanSwapCards to
true. Cards can only be swapped after dealing.
14. After the end of the Deal() method, add the framework for a new
method: public void SwapCard(int cardIndex)
15. Add the logic to swap the card specified below or use
C:\Course\419\CopyAndPaste\SwapCardLogic.txt.
• Add hand[cardIndex] back into the deck
• Shuffle the deck
• Deal a card back into hand[cardIndex]
• Set hand[cardIndex].FaceUp = true;
• Record if this new hand produces the best hand score (copy
the line of code from the Deal() method)
• Set CanSwapCards to false (no more swapping until the
next deal operation)
16. Get a clean compile.
18. Insert the logic to enable/disable the Swap buttons based on whether
the game can swap cards. Use something like:
SwapButton0.Enabled = game.CanSwapCards;
21. Compile and test. The buttons should be disabled until the Deal
button is clicked.
game.SwapCard(ix);
ShowOutput();
24. Compile and test. Confirm that the corresponding card in the output
is being changed each time you click a Swap button.
25. Move to the event method for the new game menu item and replace
its logic with:
Objective
So far, our game is not very graphical. We really should be showing card
images, not just text. Because many components are available to render the
images of playing cards, it is probably quickest to reuse one of these rather
than write our own.
Close the previous solution if you have not already done so.
7. Right-click within the body of the All Windows Forms tab of the
ToolBox and select Choose Items from the pop-up menu.
14. Move to the game table and drag an outline where you want a card
image to be (above the first Swap button). Adjust the position so that
it looks correct. Adjust the height of the form and the list box if
necessary.
Note that the size is fixed, so the card image will snap to the
predefined size.
16. Copy the first card image and paste it so that there are a total of five
card images on the game table.
17. Compile and execute the program. The cards should not yet appear.
18. Open Form1.cs for editing and move before the button1_Click
method. Add the framework for a method with the specification:
private void PaintCard(AxTehi.AxCard image, PlayingCard card)
20. Set the image’s Card property to the card’s rank using something
like image.Card = card.Rank
21. In the next line, set the image’s suit to the card’s suit. Use something
like image.suit = (int)card.Suit.
22. Assign to the image’s FaceUp property the card’s FaceUp property.
24. Find the ShowOutput() function and move to just before the end of
the method.
25. Insert a conditional to check if any cards have been dealt. Use:
if (game.Count() > 4)
{
}
26. Inside the body of this conditional, insert five calls to a card-painting
method. Use PaintCard(axCard1, game.ElementAt(0));
and so on.
The card images should now show when you click deal. Your
screen should look something like the screenshot below:
28. Move to the event method for the new game menu item and at the
end, set the Visible property of each axCard to false.
Objective
In this exercise, you will use a .NET image list component to add playing card
images to your Tehi game.
This exercise starts with the bonus solution from the previous
Exercise 6.2.
3. Drag a PictureBox control to the position where you want the first
card image. In its property sheet, set:
• Anchor to the bottom only
• Size: Height to 96
• Size: Width to 69
• Visible to false\
Adjust its position so that it looks correct. Resize the list box if
necessary to make room for the card image.
4. Copy this picture box and paste it in four more times. Position the
picture boxes so that they look right.
5. Compile and execute the program. The cards should not yet appear.
Modify our game-table form to use the picture boxes and the
image list.
10. Open Form1.cs for editing and move to after the end of the
ShowOutput() method. Add a method to paint the cards on the
display as shown below or use the copy-and-paste code in
C:\Course\419\CopyAndPaste\PictureBoxPaintCard.txt.
12. Find the ShowOutput() method, and insert at the end a conditional
to check if any cards have been dealt. Use:
if (game.Count() > 4)
{
}
13. Inside the body of this conditional, insert five calls to a card-painting
method. Use:
PaintCard(pictureBox1, game.ElementAt(0)); and so on.
14. Compile and test. The card images should now show. After dealing a
few hands, click the New menu item. Note the card images were not
cleared.
15. Move to the event method for the new game menu item and, before
the call to ShowOutput(), create an array of image components
using something like:
16. In the next line, loop through all the images and set their Visible
property to false.
17. Compile and test. Try starting a new game. The card images should
disappear.
So far, our game is not very graphical. We really should be showing card
images, not just text. Delivering images to a browser in a Web application is
straightforward.
4. Drag and drop an ImageButton onto the table in the first cell of the
fourth row. Set its properties as follows:
• Enabled to false
• Height to 96px
• ImageUrl to ~/Images/green.bmp
• Width to 69px
5. Right-click the first Image button and copy it. Paste it four times into
each of the adjacent cells in the same row.
The filenames for the playing card images we have are in the
form 5D.gif. We can use the Code to compose the filename.
if (game.Count() > 4)
{
}
10. Inside the body of this conditional, insert five calls to a card-painting
method. They should look like:
PaintCard(ImageButton1, game.ElementAt(0));.
11. Compile and test. The card images should now show. After dealing a
few hands, click the New link button. Note the card images were not
cleared.
You might notice that the screen flashes a bit when it updates
now that the card images are showing. Don’t worry—we’ll fix this in
the bonus.
Modify the New menu to clear the cards (set their images back
to green).
12. Move to the event method for the new game menu item and at the
end, create an array of image buttons using something like:
13. In the next line, loop through all the images and set their ImageUrl
property to "~/Images/green.bmp".
14. Compile and test. Try starting a new game after dealing a few hands.
If you have more time, add event handling for the image
buttons (card swapping).
16. Repeat for the remaining buttons, adjusting the card index each time.
21. Highlight and cut the entire table element—from start tag to end tag.
22. From the AJAX Extensions tab in the toolbox, drag an UpdatePanel
into the div area.
25. Compile and test. The annoying flashing of the display should have
disappeared.
Objective
Implement the logic to display the top 10 players listed by their best game
scores in the fewest number of hands played.
Close the previous solution if you have not already done so.
This project contains the solution files from the bonus of the
previous exercise.
2. From the main menu bar, select View | Server Explorer. This
window should appear on the left side.
4. If the Add Connection dialog appears, click the Change button to the
right of the data source text box.
6. In the Add Connection dialog that will reappear, click the Browse
button and select the file C:\Course\419\Data\Tehi.mdf. Click
Open.
9. In the next dialog, select Generate from database and click Next.
The next page of the wizard should appear.
Click Next. If you get a message asking to move the local data file,
click No.
11. In the next dialog, expand the tree menu to expose the Player table
and select it. After ensuring that TehiModel has been entered for the
model namespace, click Finish. If you get a dialog indicating that the
template should be from a trusted source, select the Do not show
again checkbox and click OK. Because we created the template, we
know it is trusted.
12. In the Server Explorer window, expand the Data Connections tree
menu to find the Player table, right-click it, and select Show Table
Data. Briefly review the players in the table and notice the column
names. Close the table view when done by clicking the [x] in its tab.
Add a data accessor class to load the player data from the
database.
We will first obtain all the player records unsorted. Later we will
return and modify the query to select the top 10.
17. In its get block, create a query to simply obtain all the players. Use
something like the following, but replace the ??? as appropriate.
IEnumerable<???> result = from p in db.? select p;
18. Finish with a statement that returns the result converted to a list:
return result.ToList();
22. In Solution Explorer for the form or Web project, add references to
the .NET assembly System.Data.Entity and the project
assembly Tehi.Data. Don’t forget to click the checkboxes.
25. In design mode, double-click the Top 10 menu in its event method
loop through all players and display them. Use something like:
LogListBox.Items.Clear();
DataAccessor da = new DataAccessor();
foreach (Player p in da.Top10Players)
{
LogListBox.Items.Add(p.Name + " " +
p.BestHandScore + " " +
p.HandsDealt);
}
26. Compile and test. Select Top 10 to display the players. Do not click
the Deal button by mistake. That doesn’t test what we want.
27. Open DataAccessor.cs for editing and return to the top-10 query.
Add a where clause to remove any players whose best hand score
is 0; i.e., we want all players whose score is not 0.
29. Add an orderby clause to the query to first sort by best hand score
in descending order, then by the number of hands dealt.
31. Now use the Take(…) query method to limit the number of players
to 10. Insert it into the return statement before the call to
ToList().
Now just the top 10 players should be shown. Your data should
look something like this (probably with different values):
LogListBox.Items.Add(p.ToString())
As each game is played, the best hand score and the number
of hands dealt are tracked. If this score is better than the one
previously recorded in the database, the database should be
updated. This is very easy with LINQ/EF!
Refactor the Tehi game controller and prepare it for adding the
login functionality.
42. Open TehiGame.cs for editing and find the HandsDealt property.
Rename it HandsDealtInThisGame. This is to be clear that this
value is only for this game, not the all-time one stored in the
database. Hover over the red underscore at the end of
HandsDealtInThisGame and select Rename to change all
occurrences in the program.
46. In TehiGame, add using directives at the top for both of the
components added in the previous step.
49. Add a constructor. Type ctor<tab><tab> and in the body set the
CanLogin property to true and the message to "Enter your
game name and click login";
54. Move to the new game event method and delete the line that writes
to the status label.
55. Compile and test. The messages will not yet be correct.
56. Continue editing ShowOutput() and, after the line that clears the
list box, add DealButton.Enabled = game.CanDeal;
60. Move to after the SwapCard() method. Add the Login(…) method
using the copy-and-paste file LoginMethod.txt. It should be:
63. Double-click the Login button to go to its event method. Delete all
existing code and add:
game.Login(NameTextBox.Text);
ShowOutput();
LoginButton.Visible = game.CanLogin;
66. Compile and test. You should be able to log in and play the game
again. Start a new game to log in again if necessary.
67. Open TehiGame.cs for editing and before the ToString method,
add a new function called ScoreUpdate(…) or use the copy-and-
paste file ScoreUpdateMethod.txt. It should be:
69. Move to the Deal() method and replace the line of code that
checks if the score has improved with a call to ScoreUpdate().
71. Compile and test. Login with a new game name (make one up). After
one or more hands, a message that a new all-time score has been
achieved will appear.
72. Play for a bit—see if you can get on the high-scores list.