代写C++代码 代做C++程序 C++辅导 C++教学

C++网络家教 远程写代码 在线Debug 讲解答疑 不是中介,本人直接写

微信: ittutor QQ: 14061936 Email: ittutor@qq.com

导航

Your task is to write a program that plays a simplified version of the card game called 

‘Blackjack’, or ‘Pontoon’ (or ’21’ and probably lots of other things as well).

Rules It turns out that there are lots of minor variations on the rules of this game – most of which 

CS-110/CS-111

Neal Harman

Task

Your task is to write a program that plays a simplified version of the card game called 

‘Blackjack’, or ‘Pontoon’ (or ’21’ and probably lots of other things as well).

Rules

It turns out that there are lots of minor variations on the rules of this game – most of which 

are far too complicated for a first programming course coursework. (Please don’t tell me 

that the game does not match your understanding of the rules: (a) I know, I’m trying to 

keep it simple; and (b) there are variations on the rules anyway :-)

If you are not familiar with the game then the simplified version of the rules you need to 

use is:

1. Each card is allocated a value according to the table below.

2. The aim of the game is to get as close as possible to the value 21 without going over.

3. To start with, the computer ‘deals’ itself a card at random (see below for how to generate 

random numbers in Java) and checks its value. For example, suppose it gets a seven.

4. It then has the option of twisting – dealing itself more cards and adding the value to the 

one(s) it already has. For example, suppose the computer next deals itself an Eight - it will 

then have a score of 7+8 = 15.

5. It can choose to carry on doing this as long as it wants to try to get as close to a score of 

21 as possible. However, the more cards it deals itself the more risk it has of getting a 

score of more than 21. For example, in the case above any extra card with a value of 7 or 

more will cause this to happen. At which point it will be ‘bust’ and cannot win - the best it 

can do is draw if the human player also ends up ‘bust’.

Value Card

1 Ace

2 Two

3 Three

4 Four

5 Five

6 Six

7 Seven

8 Eight

9 Nine

10 Ten, Jack, Queen, King

Page !

 of !

1 66.The computer needs a strategy – it needs to decide that when its score reaches a 

certain point it should stick and hope the human player either ends up ‘bust’ or gets a 

lower score. Note this strategy is nowhere near as hard as it sounds (see advice below).

7.At this point the computer has finished and keeps its score secret – it is now the human 

player’s turn.

8.The human player does exactly the same as the computer – they get one card to start 

with and can then choose to twist or stick until they have a score which is as close to 21 as 

they can get (or until they go ‘bust’).

9.Then, the computer decides the result according to the following rules

a. If both computer and player have scores > 21 then they are ‘bust’ and it’s a draw. 

Note it doesn’t matter if one is higher than the other (e.g. player = 23 and computer 

= 24) – it’s still a draw.

b. If the computer is ‘bust’ and the player isn’t the player wins regardless of their 

score; similarly if the player is ‘bust’ and the computer isn’t, the computer wins.

c.If both scores are the same (and neither are ‘bust’) it’s a draw.

d.Otherwise, whoever has the highest score less than or equal to 21 wins.

10. Finally the the game is played again.

Here are some example games.

Game 1

The computer first deals itself a two. Since this is a lot less than 21, it takes another card 

(twists) and gets a king (value =10), so it now has 12. It ‘decides’ that this is still too low, so 

it takes another card and gets an 8, so it now has 20. It ‘decides’ this is close enough to 20 

and so it sticks. 

The (human) player is dealt a nine, asks for another card and gets an Ace (total of 10 so 

far), and then another card and gets a seven. The player decides to stick - remember the 

player does not know up to this point what the computer has scored.  At this point, the 

computer shows its score, which is 20 - less than 21 so it isn’t bust, but more than 17, so 

the computer wins.

Game 2

The computer starts with a Queen (10) and asks for another card (twists) and gets a six. It 

decides to twist again and gets a King (10) so it has 26 which means it’s bust. 

The player gets a Jack, twists and gets a 10 (score 20) and decides to stick. Because the 

computer is bust (> 21) the player wins.

More Games

Here are some simplified sequences to show other outcomes.

Computer: 10, 3, King = 23 - Computer is bust

Player:       9, 7, Queen = 26 - Player is bust

Even though the scores are different the outcome is a draw because both are bust.

Computer: Queen, King = 20

Player:       10, Queen = 20

This game is a draw.

Computer: 9, 8, 4 = 19.

Player: 10, Ace, Jack = 21

The player wins this game - neither player nor computer is bust and the player’s score is 

higher.

Page !

 of !

2 6If you missed the lecture when the sample solution to this was demonstrated and you are 

not clear on the rules of the game even after looking at the example, then please come 

and see me and I’ll explain it to you and show you a running program so you can see how 

it works.

WARNING 1

You may have worked out that if the player (and computer) get only one card to start with, 

it’s impossible for them to be ‘bust’ if they ask for one more card. Therefore you might be 

tempted to write a program that starts off giving them two cards. And you might point out 

that most versions of the real game do this. However, please don’t! One of the things you 

generally need to do when writing software is to write something that meets the 

specification you were given – not some different one even if you’re sure it’s better! Doing 

this will mean you have not met the specification and you will lose credit. Please also avoid 

other variations you may be familiar with (for example, the Ace often has two possible 

values: one or 10 - in this game it should be one.)

WARNING 2

In the past some people have ‘short cut’ the computer’s part by just generating a single 

random number between 1 and 21. This is wrong - first it means the computer cannot go 

bust; second it makes it much less likely that the computer can win. Follow the algorithm 

described above.

WARNING 3

Another common mistake - if the computer goes bust some solutions don’t bother to let the 

player have a turn but just say “computer’s bust - you win”. This is wrong - it means there 

is no possibility of a draw when both computer and player go bust.

Versions

In order to make this coursework accessible to those of you with no programming 

experience, there will be a very basic version than can do done with about 40 lines of code 

excluding blank ones and comments. There will also be a series of more advanced and 

challenging versions for those with more experience. You are encouraged to try to do the 

more advanced versions if you can, but Version 1 is sufficient as only 15% of the marks 

are reserved for more advanced solutions - so it’s possible to get 85% (and the standard 

mark boundary for a first class result (i.e. an ‘A’) in UK universities is 70%. Marks for the 

different versions will only be differentiated under the Output Format category, which will 

be worth 15% of the total. In this category, Version 1 will be worth 0% towards the total 

mark, Version 2 5%, Version 3 10%, and Version 4 15%.

(If you’re wondering what the incentive for doing the more advanced versions is – more 

experience at programming will be very valuable in lots of future modules in computer 

science, so the more practice you get now the better.)

Page !

 of !

3 6Version 1 (Maximum mark 85%)

In this version you do not have to worry about the actual cards – just about their values. 

That is, you only need to generate numbers randomly-generated (see below for how) in 

the range 1 to 10, and then keep track of the sum of the values for the computer and the 

player.

You will only need to print out the scores for the player and computer. A game result might 

look like this:

Computer Value: 18

Player Value: 20

Player wins!

Note the exact form of the output is up to you – but please don’t go mad! This version will 

require you to use assignments, if statements, while loops, a Scanner, and a random 

number generator (see below).

Version 2 (Maximum mark 90%)

In this version, you should also print out what cards the computer and player have, but not 

the suites. A game result might look like this:

Computer has: Seven, Jack, Two

Value: 19

Player has: Ace, King, Ten

Value: 21

Player wins!

Note that you should not worry about the number of each card in this case – so something 

like this:

Computer has: Ace, Ace, Ace, Ace, Ace, Ten

(spot the number of aces) is fine.

This version will be a lot shorter and simpler if you know how to use arrays. In that case, this 

version is about 50 lines of code.

Version 3 (Maximum mark 95%)

In this version, you should also worry about what suite the cards are from – but you shouldn’t worry 

about duplicates. A game result might look like this:

Computer has: Seven of Hearts, Ten of Clubs

Value: 17

Player has: Ten of Clubs, King of Diamonds

Value: 20

Player wins!

Note the duplicate card Ten of Clubs – don’t worry about that in this version.

Version 4 (Maximum mark 100%)

In this version, you should make sure that there are no duplicate cards used.

Page !

 of !

4 6Strategy

This may look daunting if you haven’t done any programming before. So the trick is to break the 

problem into parts. There are three main problems to solve.

1. Generating the computer’s score. You will need to use a loop for this and you will need a 

strategy to decide when the loop should end (i.e when the computer is ‘happy’ with its score). 

The simplest way to do this is to choose a maximum value – when the computer reaches or 

exceeds it, it sticks. Hint: this is basically what human players do - if they reach or go over some 

threshold, they stick.

2. Generating the player’s score. You will also need a loop for this but when it ends is up to the 

player. You will have to get the player’s input each time around the loop and use that to decide 

when to stop.

3. Deciding who has won. This is most easily done with a set of if else statements. This bit is 

harder than it looks at first sight!

Advice

1. To generate random integers in Java:

a. first put import java.util.Random; at the top of your program.

b. then in your main method put Random rnd = new Random(); Note you only need to put 

this line in once and you can call it something else other than rnd.

c. then you can use value = rnd.nextInt(n); to generate a random number in the range 

0 to n - 1, where value is an integer variable (which obviously doesn’t have to be called 

value).

d. in general, if you want to generate a random number in the range 1 to n, you can do it with 

value = rnd.nextInt(n+1) + 1;

2. I suggest the first part of the program you write is the bit that decides who has won. That is,write 

something like:

public class BlackJack {

public static void main(String[] args) {

int computerValue = ???; //put different test values

int playerValue = ???;   //here to check the logic

if (blah blah blah) {...

//where all this is your logic to tell who has

// won or not

}

}

3. The reason for this is that you can test  this piece of code first by putting in different values for 

the player and computer’s value.

4. Once you have done that and are sure your code to decide who wins is correct, you can replace 

the line int computerValue =  with the code to generate the computer’s value; and then you 

can do the same for the line int playerValue =

5. This will let you build and test your program in parts – which is always a lot easier than trying to 

it all in one go. (For some reason lots of people ignore this and do try to write the whole thing in 

one go - big mistake…)

6.While I don’t want to encourage anyone to leave coursework to the last minute, I will be going 

over material relevant to this coursework in the next two weeks.

7. Come and ask me questions (hardly anyone asks enough questions which is a big mistake) and 

discuss it in tutorials if you have them.

相关推荐