Card Naming Program
Toward the
end of this class, we’re going to create a card game program. In this program, we’re going to create an
important part of that game; a program that will name a card based on the
number of the card.
We know that
cards have suits and values (suits = hearts, clubs, diamonds, and spades;
values = 2 – 10, Jack, Queen, King, and Ace.)
Your computer is best equipped to “think” of these cards as numbers…(instead of the Ace of Spades, the computer processes card
#40).
The table below
shows a possible configuration for how these cards could be viewed by your
computer:
|
Hearts |
Diamonds |
Clubs |
Spades
|
||||
|
Card# |
Name |
Card# |
Name |
Card# |
Name |
Card# |
Name |
|
1 |
Ace |
14 |
Ace |
27 |
Ace |
40 |
Ace |
|
2 |
Two |
15 |
Two |
28 |
Two |
41 |
Two |
|
3 |
Three |
16 |
Three |
29 |
Three |
42 |
Three |
|
4 |
Four |
17 |
Four |
30 |
Four |
43 |
Four |
|
5 |
Five |
18 |
Five |
31 |
Five |
44 |
Five |
|
6 |
Six |
19 |
Six |
32 |
Six |
45 |
Six |
|
7 |
Seven |
20 |
Seven |
33 |
Seven |
46 |
Seven |
|
8 |
Eight |
21 |
Eight |
34 |
Eight |
47 |
Eight |
|
9 |
Nine |
22 |
Nine |
35 |
Nine |
48 |
Nine |
|
10 |
Ten |
23 |
Ten |
36 |
Ten |
49 |
Ten |
|
11 |
Jack |
24 |
Jack |
37 |
Jack |
50 |
Jack |
|
12 |
Queen |
25 |
Queen |
38 |
Queen |
51 |
Queen |
|
13 |
King |
26 |
King |
39 |
King |
52 |
King |
Write a
program that allows the user to enter a number and the computer responds by
naming the card.
There are
several ways to do this:
Method 1 is very simple, but very inefficient. It uses a ton of code and very little problem
solving ability. If you use method 1,
you should be ashamed of yourself; it reflects an inability or lack of desire to think
critically.
Method 2 involves compound conditions and
concatenation. It’s involves less code
and little more work from the brain.
It’s more efficient than method 1 and implies that you may not be a
great mathematical thinker, but at least you’re somewhat creative and you have
a fighting chance at making something of yourself.
Method 3 involves the relationships between
numbers. It involves critical thinking
and a good mind for math. Method 3 is
the most efficient method and uses the least code. To use method 3, you need to figure out what
all of the hearts have in common and what all the eights have in common. If you can do this, you can tell that a card
is the eight of hearts. If you can
figure out method 3, you’ll be a fine programmer someday and make a lot of
money. Congratulations!