作业解析-c++ 21点游戏

目录

1. 作业题目

2. 作业目的

3. 运行效果

4. 实现过程

5. 知识点巩固

6 知识拓展

7 学习建议

作业辅导解析

Assignment #3: Conditionals and Loops (150 pts) 28th Jan,2019

1.作业题目:

We are going to create a game called TwentyOne, which is similar to the Black Jack and War card games. This game has a dealer/computer and 1-4 players, and the game will generate random numbers from 1 to 11 to simulate the values in cards. All players start with a specified amount of money, which they determine at the start of the game, and the goal is to see if you can get closer than the dealer to twenty-one points without going over. With each player’s turn, they first bet against the dealer, and then play against the dealer before moving on to the next player. An initial random number, 1-11, is generated, and after each random number is generated, the player decides whether they want to generate another to add to their total points trying to get as close to 21 as possible without going over. The dealer is also given random numbers, but the strategy you use for determining whether they get another random number is your choice. The rules for the game play are below.
  • If the player busts, the player automatically loses their bet, and the dealer doesn’t need to play against that player.
  • If the dealer busts, the player automatically wins their bet.
  • If the dealer ties with the player, then the player doesn’t win or lose their bet.
  • If the player’s bet is more than their bank, then you need to continue to re-prompt for a new bet until a good value is given.
  • If a player’s bank is 0, they get skipped.
  • If all players’ banks are 0, then the game is automatically over!
  • After all players take a turn against the dealer, then the players can collectively cash out or play again.

2.作业目的:

1、 掌握 C++的条件语句与循环语句的使用。 2、 掌握 while(true)用法和按条件 break3、 掌握函数调用方法及基本传参。 4、 掌握变量的输入及使用。

3.运行效果:

image image image

4.实现过程:

image

参考源码图 1.1

5.知识点巩固:[相关知识点 1-3 点总结]

条件语句

if (…) { …; } else if (…) { …; } else { …; }

循环语句

for (…; …; …) { …; } while (…) { } do { …; …; } while (…);

6.知识拓展:

死循环条件跳出,break 只能跳出一层循环

while (true) { if (…) { break; } } for (…; ; …) { if (…) { break; } do { } if (…) { break; } } while (…); 对于需要跳出多层循环的,使用标记。 while (true) { flag = true; while (true) { flag = false; break; } if (flag) { break; } }

函数的跳出

不管有多少层循环,return 直接退出当前函数。

7.学习建议:

1、 通过流程图理解条件和循环。 2、 变量命名要具备可读性。 3、 代码格式一定要良好可读。