可乐机器
要求:
变量,数据类型和数字运算符
基本输入/输出
逻辑(if语句,switch语句)
编写一个程序,向用户提供您最喜欢的5种饮料(可口可乐,水,雪碧,......)。
然后允许用户通过输入数字1-5来选择饮料。输出他们选择的饮料。
★如果您使用if语句而不是switch语句编程,请修改它以使用switch语句。
如果您的程序使用switch语句,请修改它以使用if / else-if语句。
★★修改程序,以便当用户输入1-5以外的选项时,它将输出“Error. choice was not valid, here is your money back.”。
调教恶作剧
要求:
变量,数据类型和数字运算符
基本输入/输出
逻辑(if语句,switch语句)
循环(for,while,do-while)
编写一个程序,持续要求用户输入5以外的任何数字,直到用户输入数字5。
然后告诉用户“Hey! you weren't supposed to enter 5!” 并退出该程序。
★修改程序,以便在10次迭代后,如果用户仍然没有输入5将告诉用户“Wow, you're more patient then I am, you win.” 并退出。
★★修改程序,以便它要求用户输入除了他们被要求输入数字的次数之外的任何数字。 (即在第一次迭代“请输入0以外的任何数字”和第二次迭代“请输入除1以外的任何数字”等等。当用户输入他们被要求的号码时,程序必须相应地退出。
吃煎饼的人
要求:
变量,数据类型和数字运算符
基本输入/输出
逻辑(if语句,switch语句)
循环(for,while,do-while)
数组
写一个程序,要求用户输入10个不同的人吃的早餐煎饼数量(Person 1, Person 2, ..., Person 10)。输入数据之后,程序必须分析数据并输出哪个人早餐吃了最多的煎饼。
★修改程序,以便它还输出哪个人吃早餐最少的煎饼数。
★★★★修改程序,使其按照所有10个人吃的煎饼数量顺序输出一个列表。
即
Person 4: ate 10 pancakes,Person 3: ate 7 pancakes,Person 8: ate 4 pancakes,...
,Person 5: ate 0 pancakes
生成随机数
#include <iostream>
#include <cstdlib> //利用cstdlib中的rand函数来生成随机数
// #include <cassert> //你不要管这个是啥
using namespace std;
//生成一定范围内的随机数
int RandomNumberInRange(int from, int to) {
//assert(from < to);
return from + rand() % (to - from);
}
int main() {
for (int i = 0; i < 10; i++)
cout << rand() << endl;
//限制这个随机数在0-99
for (int i = 0; i < 10; i++)
cout << rand() % 100 << endl;
//生成一定范围内的随机数
//注意from一定要小于to
cout << RandomNumberInRange(-32, 123) << endl;
}
搜索
要求:
变量,数据类型和数字运算符
基本输入/输出
逻辑(if语句,switch语句)
循环(for,while,do-while)
伪随机数 (cstdlib中的rand函数)
编写一个计算随机数1到100的程序。程序然后要求用户猜出该数字。
如果用户猜测太高或太低,则程序应相应地输出“too high”或“too low”。
该程序必须让用户继续猜测,直到用户正确猜出该号码。
★修改程序以输出用户正确猜测正确数字所花费的猜测次数。
★★修改程序,以便计算机猜测用户决定的数字。 用户必须告诉计算机它是否过高或过低。
★★★★修改程序,以便无论用户想到什么号码(1-100),计算机都可以在7次或更少次数的猜测中猜出它。
搜索

数字倒序
输入任意位数的正整数n,将n逆序(记为p)并输出p*2
比如 输入 1234 输出 8642(即4321*2)
来源 玄学意淫
地下城爬行
要求:
变量,数据类型和数字运算符
基本输入/输出
逻辑(if语句,switch语句)
循环(for,while,do-while)
二维数组
制作一个程序,使用数字或字符将简单的基于网格的游戏板输出到屏幕。
比如说
. . . . . . . . . .
. G . . . . . . . .
. . . . . . T . . .
. . . . . . . . . .
. . . . T . . . . .
. . . . . . T . . .
. . . . . . . . . X
或者
| 0 0 0 0 0 0 0 0 0 0 0 5 0 0 6 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 4 |
允许用户(在示例中标记为G)每回合向上,向下,向左或向右移动。如果玩家踩到陷阱,那么他们就输了。如果把移动到宝藏'X'那么他们就赢了。
★★添加每回合随机向任意方向移动的敌人。 (像陷阱一样的敌人会导致玩家在被触碰时失败)
提示:不要让玩家离开游戏板!如果玩家从板的顶部或底部移开,程序将崩溃!
(敌人也是如此)
参考答案:调教恶作剧
//
// Created by 31838 on 8/8/2019.
//
#include <iostream>
#include <chrono>
#include <random>
using std::cout, std::cin, std::endl;
using std::chrono::system_clock, std::uniform_int_distribution, std::default_random_engine;
auto main() -> int {
auto now = system_clock::now();
auto dist = uniform_int_distribution<int>();
auto engine = default_random_engine(now.time_since_epoch().count());
for (int i = 0; i < 10; i++) {
int unexpected = dist(engine) % 100;
cout << "Please enter a number other than " << unexpected << ": ";
int entered = 0;
cin >> entered;
if (entered == unexpected) {
cout << "Hey! you weren't supposed to enter " << unexpected << " !" << endl;
return EXIT_FAILURE;
}
}
cout << "Wow, you're more patient then I am, you win." << endl;
return EXIT_SUCCESS;
}参考答案:可乐机器
#include <iostream>
#include <array>
#include <string>
using std::array, std::cout, std::endl, std::cin, std::string, std::getline, std::stoi;
const size_t num_drinks = 5;
array<string, num_drinks> all_drinks;
template<size_t arr_len = num_drinks>
auto ShowDrinks(array<string, arr_len> drinks) -> void {
cout << "There are " << arr_len << " drinks, which one do you like?" << endl;
for (size_t i = 0; i < arr_len; i++)
cout << " " << i + 1 << ") " << drinks[i] << endl;
}
auto Init() -> void {
all_drinks = {"Cola", "Water", "Sprite", "Juice", "Tea"};
}
auto main() -> int {
Init();
ShowDrinks(all_drinks);
string choice_str;
int choice = 0;
getline(cin, choice_str);
if (choice_str.size() != 1)goto invalid_choice;
choice = stoi(choice_str);
if (choice >= 1 && choice <= num_drinks) {
cout << "OK, your choice is " << all_drinks[choice - 1] << "." << endl;
exit(EXIT_SUCCESS);
}
invalid_choice:
cout << "Error, your choice was invalid. Here is your money back." << endl;
exit(EXIT_FAILURE);
}参考答案:吃煎饼的人
//
// Created by 31838 on 8/8/2019.
//
#include <iostream>
#include <array>
using std::cin, std::cout, std::endl, std::array, std::swap;
struct person {
uint num;
uint id;
bool operator<(const person &p) {
return num < p.num;
}
bool operator>(const person &p) {
return num > p.num;
}
};
const uint num_person = 5;
auto main() -> int {
if (num_person < 2) {
cout << "number of people should bigger or equal to 2" << endl;
return EXIT_FAILURE;
}
array<person, num_person> people = {};
for (int i = 0; i < num_person; i++) {
cout << "Person" << i + 1 << " eat: ";
cin >> people[i].num;
people[i].id = i;
}
person max = people[0], min = people[0];
for (int i = 0; i < num_person; i++) {
max = people[i] > max ? people[i] : max;
min = people[i] < min ? people[i] : min;
}
cout << endl;
cout << "Person" << max.id + 1 << " ate the most pancakes, the number is " << max.num << endl;
cout << "Person" << min.id + 1 << " ate the least pancakes, the number is " << min.num << endl;
cout << endl;
for (uint i = 0; i < num_person; i++)
for (uint j = 0; j < num_person - 1; j++)
if (people[j] < people[j + 1])
swap(people[j], people[j + 1]);
for (uint i = 0; i < num_person; i++)
cout << "Person" << people[i].id + 1 << " ate " << people[i].num << " pancake(s)" << endl;
return EXIT_SUCCESS;
}
参考答案:地下城爬行
//
// Created by 31838 on 8/8/2019.
//
#include <iostream>
#include <array>
#include <random>
#include <chrono>
using std::cout, std::endl, std::cin, std::getchar;
using std::array, std::string;
const size_t row_num = 10, col_num = 20, trap_num = 50;
auto panic(const string &msg) -> void {
cout << "\033[2J\033[1;1H";
cout << "[PANIC] " + msg << endl;
exit(EXIT_FAILURE);
}
class Position {
protected:
uint row;
uint col;
public:
auto GetRow() -> uint {
return row;
}
auto GetCol() -> uint {
return col;
}
auto SetRow(uint r) -> void {
row = r;
}
auto SetCol(uint c) -> void {
col = c;
}
auto operator==(const Position &pos) -> bool {
return pos.row == row && pos.col == col;
}
auto operator!=(const Position &pos) -> bool {
return !operator==(pos);
}
};
template<size_t row_num, size_t col_num>
class Player : public Position {
auto MoveOutOfBoard() {
panic("You are moving out of board!");
}
public:
auto GoUp() -> void {
if (!row)
MoveOutOfBoard();
row--;
}
auto GoDown() -> void {
row++;
if (row >= row_num)
MoveOutOfBoard();
}
auto GoLeft() -> void {
if (!col)
MoveOutOfBoard();
col--;
}
auto GoRight() -> void {
col++;
if (col >= col_num)
MoveOutOfBoard();
}
};
enum class GameOverFlag {
kSuccess,
kFail,
kNotOver
};
enum class KeyboardCommand {
kUp,
kDown,
kLeft,
kRight
};
template<size_t row_num = 10, size_t col_num = 10, size_t trap_num = 10>
class Game {
private:
Player<row_num, col_num> player_{};
array<Position, trap_num> traps_{};
Position randomTrap_{};
Position treasure_{};
auto RandomPosition() -> Position {
using std::chrono::system_clock, std::uniform_int_distribution, std::default_random_engine;
auto now = system_clock::now();
auto dist = uniform_int_distribution<size_t>();
auto engine = default_random_engine(now.time_since_epoch().count());
Position pos = {};
pos.SetRow(dist(engine) % row_num);
pos.SetCol(dist(engine) % col_num);
return pos;
}
auto ValidateTrapPosition(Position pos) -> bool {
return pos != player_ && pos != treasure_;
}
auto ValidateTreasurePosition(Position pos) -> bool {
return pos != player_;
}
auto Init() -> void {
ClearScreen();
player_.SetCol(0);
player_.SetRow(0);
gen_treasure:
treasure_ = RandomPosition();
if (!ValidateTreasurePosition(treasure_))
goto gen_treasure;
for (size_t i = 0; i < trap_num; i++) {
gen_trap:
auto trap = RandomPosition();
if (!ValidateTrapPosition(trap))
goto gen_trap;
traps_[i] = trap;
}
gen_random_trap:
auto trap = RandomPosition();
if (!ValidateTrapPosition(trap))
goto gen_random_trap;
randomTrap_ = trap;
}
public:
Game() {
Init();
}
~Game() {
ClearScreen();
}
private:
auto ClearScreen() -> void {
cout << "\033[2J\033[1;1H";
}
auto ShowBoard() -> void {
ClearScreen();
array<array<char, col_num>, row_num> board = {};
for (array<char, col_num> &row:board) {
for (char &col:row)
col = '.';
}
for (auto trap:traps_)
board[trap.GetRow()][trap.GetCol()] = 'T';
board[randomTrap_.GetRow()][randomTrap_.GetCol()] = 'T';
board[treasure_.GetRow()][treasure_.GetCol()] = 'X';
board[player_.GetRow()][player_.GetCol()] = 'G';
for (array<char, col_num> row:board) {
for (char col:row)
cout << col;
cout << endl;
}
}
auto IsGameOver() -> GameOverFlag {
if (player_ == treasure_)
return GameOverFlag::kSuccess;
if (player_ == randomTrap_)
return GameOverFlag::kFail;
for (auto trap:traps_) {
if (trap == player_)
return GameOverFlag::kFail;
}
return GameOverFlag::kNotOver;
}
auto GenerateRandomTrap() -> void {
gen_random_trap:
auto trap = RandomPosition();
if (!ValidateTrapPosition(trap))
goto gen_random_trap;
randomTrap_ = trap;
}
auto DoCommand(KeyboardCommand cmd) {
switch (cmd) {
case KeyboardCommand::kUp:
player_.GoUp();
break;
case KeyboardCommand::kDown:
player_.GoDown();
break;
case KeyboardCommand::kLeft:
player_.GoLeft();
break;
case KeyboardCommand::kRight:
player_.GoRight();
break;
}
}
auto Reset() -> void {
Init();
}
public:
auto Play() -> void {
GameOverFlag flag;
do {
GenerateRandomTrap();
ShowBoard();
char ch;
cin >> ch;
switch (char(ch)) {
case 'w':
DoCommand(KeyboardCommand::kUp);
break;
case 's':
DoCommand(KeyboardCommand::kDown);
break;
case 'a':
DoCommand(KeyboardCommand::kLeft);
break;
case 'd':
DoCommand(KeyboardCommand::kRight);
break;
case 'r':
Reset();
break;
default:
panic("Unknown key from Keyboard");
}
flag = IsGameOver();
ShowBoard();
} while (flag == GameOverFlag::kNotOver);
if (flag == GameOverFlag::kFail)
cout << "Sorry, you failed" << endl;
else
cout << "Wow! You win!" << endl;
}
};
auto main() -> int {
cout << "\033[2J\033[1;1H";
cout << R"(
Welcome to dungeon crawl game.
You can use w,s,a,d to control player and r to reset the game.
Ready to go? Press ENTER to move on!
)";
getchar();
play:
auto g = new Game<row_num, col_num, trap_num>();
g->Play();
ask:
{
cout << "Would you like to play again? [y/n] ";
string str;
cin >> str;
if (str == "y") {
delete (g);
goto play;
} else if (str == "n") {
delete (g);
cout << "Bye Bye." << endl;
return EXIT_SUCCESS;
} else {
cout << "Please enter y or n." << endl;
goto ask;
}
}
}
2019 Chaoyue Summer Camp Presentation 4
By 方泓睿
2019 Chaoyue Summer Camp Presentation 4
- 57