📄 9_2.cpp
字号:
/*第2题 闯城堡游戏
2.2.1 课程设计的程序功能简介:
闯城堡游戏。城堡中设有一些房间和一些陷阱,玩的时候不小心,就落入陷阱。游戏要求玩家能够安全离开城堡。
2.2.2 课程设计的任务要求:
(1)一进入程序,显示汉化命令提示,以便用户选择;
(2)扩充游戏规则,让玩者按城堡的复杂程度选择游戏的难易程度;
(3)记录玩者获胜次数,失败次数到文件中,并打印输出;
(4)将所有的函数与数据封装到类中,并改写主程序,使程序简化。
(5)合做时,扩充游戏规则必须不同。
2.2.3 课程设计说明:
(1)可分三种难易程度:简单;一般;较难。可以为程序增加了一个选择难易程度的函数。
(2)由于游戏的难易程度不同,城堡的地图也不同。
简单:玩家只能进入roomone,roomtwoeast,roomthreeeast,roomnorth,parlor。相对而言获胜的机会就比较大,因此最简单。是为初级者设计的
一般:玩家能够进入的地方要比"简单"级的多一些。除了"简单"级能进入的屋子外,还能进入roomwest,backdoor,fireplacre。
较难:在这一级中城堡的地形会变的很复杂。
为了实现以上的功能,需在原来的程序的基础上增加了一些新的控制语句。
(3)游戏结束时,需自动显示玩家的胜败情况:一共玩了几次,其中胜利几次,失败几次。为实现该功能,可设了三个变量sum,win,lost..两个函数printwin(), printlost()。并且在函数doyouquit()和bravesoul()中分别调用了这两个函数。
(4)评定难易级别:B级,可合做。
2.2.4 课程设计的源代码注解:*/
// This program was created by M. Carter Brown. Created September 19, 2000.
// system("PAUSE"); in the program in a few places. This allowed the program
// to pause until you hit a key before going on to the next step.
#include <iostream>
#include <stdlib.h>
using namespace std;
char outside(); //This is where the game starts
char roomone(); //This is when we step in the castle
char roomtwonorth(); //just what the name says is where we are going in the game
char roomtwoeast();
char roomtwowest();
char bravesoul(); // If you die first a consolation then char doyouquit();
char roomthreewest();
char dungeon();
char trapped(); // two choices here 1 == live, 2 == die
char roomthreeeast(); //I let you try and kill yourself here
char roomthreeeast2(); //Here's where I tell you that you can't
char backdoor();
char fireplace();
char fireplace2(); // I swear I'll have two for every room by the time I'm done this
char parlor();
char outside2(); //This is where you win the game.
char easttower();
char roomone2(); // this is roomone() but without the dramatics of the front door!
char outside3(); // bump on the head, beginning repeat!
char dungeon2(); // Gotta put this one in so we're not walking down the stairs again!!
char roomtwoeast2(); // another double room because of what I say.
char doyouquit(); //I figured instead of return 0; if you die I'll give you an option to keep playing
// This will also work for invalid characters
int main()
{
cout << " *---* *---* *---* *---* \n";
cout << " | |___| | | |___| | \n";
cout << " | | | | \n";
cout << " | __ |__________| __ | \n";
cout << " | / \\ / \\ | \n";
cout << " | | | ______ | | | \n";
cout << " | |__| / \\ |__| | \n";
cout << " | | | | \n";
cout << " | | | | \n";
cout << " | | | | \n";
cout << " |_____________|______|_____________| \n";
cout << "\nWelcome to The Castle";
cout << "\n\n\n";
system("PAUSE");
cout << outside();
}
char outside()
{
int x;
cout << "You are standing in front of the castle.\n";
cout << "Next to you is a mailbox. The castle is to the north.\n";
cout << "What do you want to do\?\n";
cout << "(1) North: \n";
cout << "(2) East: \n";
cout << "(3) West: \n";
cout << "(4) South: \n";
cout << "(5) Look inside Mailbox: \n";
cin >> x;
if (x == 1){ cout << roomone(); }
else if (x == 2)
{
cout << "\nOUCH! You run into a tree!\nTry again.\n\n";
system("PAUSE");
return outside3();
}
else if (x == 3)
{
cout << "You can not go that way. \n\n";
system("PAUSE");
return outside();
}
else if (x == 4)
{
cout << "You sink into the swamp!\n\nNext time you should be more careful.\n\nEnd Game\n\n";
system("PAUSE");
return bravesoul();
}
else if (x == 5)
{
cout << "*------------------------------------------------*\n";
cout << "| Thank you for trying and playing The Castle |\n";
cout << "| By M. Carter Brown, written in C++ |\n";
cout << "| |\n";
cout << "| The object of this game is simply to get |\n";
cout << "| through the castle alive. |\n";
cout << "| Good Luck! |\n";
cout << "| - M. Carter Brown |\n";
cout << "*------------------------------------------------*\n";
cout << "\n\n\n\n";
system("PAUSE");
return outside();
}
else if ((x != 1) || (x != 2) || (x != 3) || (x != 4) || (x != 5))
{
return doyouquit();
}
}
char roomone()
{
int x;
cout << "\nYou open the creaky wooden door. You step into the castle\n";
cout << "and notice that it is very dark except for the scattered light coming\n";
cout << "through the windows. All of a sudden, you feel a large gust of wind.\n";
cout << "The heavy front door slams shut. The door is locked. You look around.\n";
cout << "There are 3 doors, one to the North, East, and West.\n\n";
cout << "Where do you want to go?\n";
cout << "(1) North: \n";
cout << "(2) East: \n";
cout << "(3) West: \n";
cout << "(4) South: \n";
cout << "(5) Look around: \n";
cin >> x;
if (x == 1)
{
cout << roomtwonorth();
}
else if (x == 2)
{
cout << roomtwoeast();
}
else if (x == 3)
{
cout << roomtwowest();
}
else if (x == 4)
{
cout << "You just tried the door, you know it is locked.\n";
cout << "Why are you trying again?\n\n";
system("pause");
return roomone2();
}
else if (x == 5)
{
return roomone2();
}
else if ((x != 1) || (x != 2) || (x != 3) || (x != 4) || (x != 5))
{
cout << "\n\nInvalid Character\n\n";
system("pause");
return doyouquit();
}
}
char roomtwonorth()
{
int x;
cout << "\nYou walk North and into a room so bright that is hurts your eyes.\n";
cout << "You still can't believe that a drab Castle such as this could be this\n";
cout << "bright. You can see high up the winding stairs. You must be able to \n";
cout << "get to the stairs from another room. To the North is a closed door. \n";
cout << "To the east is another door with a sign that reads \"Backdoor\". \n";
cout << "You think to yourself \"They wouldn't make it that easy, would they?\"\n";
cout << "Where do you want to go?\n";
cout << "(1) North: \n";
cout << "(2) South: \n";
cout << "(3) East: \n";
cin >> x;
if (x == 1)
{
cout << "\n\n";
return parlor();
}
else if (x == 2)
{
cout << "\n\n";
return roomone2();
}
else if (x == 3)
{
cout << "\n\n";
return backdoor();
}
else if ((x != 1) || (x != 2) || (x != 3))
{
cout << "\n\n";
return doyouquit();
}
}
char roomtwoeast()
{
int x;
cout << "You walk into a nicely funished room. There is a large table in\n";
cout << "the middle of the room with ten chairs around it. There is a large\n";
cout << "painting on the wall with an inscription. There are three doors.\n";
cout << "North, West, and East.\n";
cout << "Where do you want to go?\n";
cout << "(1) West: \n";
cout << "(2) East: \n";
cout << "(3) North: \n";
cout << "(4) South: \n";
cout << "(5) Look at the painting: \n";
cin >> x;
if (x == 1)
{
cout << "\n\n" << roomone2();
}
else if (x == 2)
{
cout << "\n\n" << roomthreeeast();
}
else if (x == 3)
{
cout << "You look but the door has no handles. You can not open it.\n\n";
system("pause");
return roomtwoeast();
}
else if (x == 4)
{
cout << "There is no window to look out of in this room.\n\n";
system("pause");
return roomtwoeast();
}
else if (x == 5)
{
cout << "Looking at the painting closely you can read the writing at the bottom.\n\n";
cout << "\"Three lefts make a right, do none of these\n";
cout << "and you\'ll be alright!\"\n\n\n";
system("pause");
return roomtwoeast2();
}
else if ((x != 1) || (x != 2) || (x != 3) || (x != 4) || (x != 5))
{
cout << "\n\nInvalid Character\n\n";
system("pause");
return doyouquit();
}
}
char roomtwoeast2()
{
int x;
cout << "You are back in the nicely funished room. There is a large table in\n";
cout << "the middle of the room with ten chairs is still here. And so is the large\n";
cout << "painting on the wall. There are three doors.\n";
cout << "North, West, and East.\n";
cout << "Where do you want to go?\n";
cout << "(1) West: \n";
cout << "(2) East: \n";
cout << "(3) North: \n";
cout << "(4) South: \n";
cout << "(5) Look at the painting: \n";
cin >> x;
if (x == 1)
{
cout << "\n\n" << roomone2();
}
else if (x == 2)
{
cout << "\n\n" << roomthreeeast();
}
else if (x == 3)
{
cout << "You look but the door has no handles. You can not open it.\n\n";
system("pause");
return roomtwoeast();
}
else if (x == 4)
{
cout << "There is no window to look out of in this room.\n\n";
system("pause");
return roomtwoeast();
}
else if (x == 5)
{
cout << "Looking at the painting closely you can read the writing at the bottom.\n\n";
cout << "\"Three lefts make a right, do none of these\n";
cout << "and you\'ll be alright!\"\n\n\n";
system("pause");
return roomtwoeast2();
}
else if ((x != 1) || (x != 2) || (x != 3) || (x != 4) || (x != 5))
{
return doyouquit();
}
}
char roomtwowest()
{
int x;
cout << "\nYou are now in a large room with a vaulted ceiling.\n";
cout << "There are these old swords on the wall. They look like they might be useful.\n";
cout << "You look around the rest of the room.\n";
cout << "There are 3 doors, one to the West, North, and East (Back where you came).\n\n";
cout << "What do you want to do?\n";
cout << "(1) West: \n";
cout << "(2) North: \n";
cout << "(3) South: \n";
cout << "(4) East: \n";
cin >> x;
if (x == 1)
{
cout << roomthreewest();
}
else if (x == 2)
{
cout << "The door won't open. You assume that it must be locked.\n\n";
system("pause");
return roomtwowest();
}
else if (x == 3)
{
cout << "\nYou look outside the front window. Not a spectacular view.\n\n";
system("pause");
return roomtwowest();
}
else if (x == 4)
{
cout << "\n\n";
return roomone2();
}
else if ((x != 1) || (x != 2) || (x != 3) || (x != 4))
{
return doyouquit();
}
}
char roomthreewest()
{
int x;
cout << "\nAs you enter the room you feel shiver as a chill runs\n";
cout << "ove your body. The room is bright with two rather large windows\n ";
cout << "You hear thumping coming from the door to your right.\n";
cout << "There are 2 doors, one to the North and one to the East (Back where you came).\n\n";
cout << "What do you want to do?\n";
cout << "(1) East: \n";
cout << "(2) North: \n";
cout << "(3) South: \n";
cout << "(4) West: \n";
cin >> x;
if (x == 1)
{
cout << "You look out of the rather large window expecting to see something.\n";
cout << "Nope.\n\n";
system("pause");
return roomthreewest();
}
else if (x == 2)
{
cout << dungeon();
}
else if (x == 3)
{
cout << "\nYou look outside the front window. Not a spectacular view.\n\n";
system("pause");
return roomthreewest();
}
else if (x == 4)
{
cout << "\n\n";
return roomtwowest();
}
else if ((x != 1) || (x != 2) || (x != 3) || (x != 4))
{
return doyouquit();
}
}
char dungeon()
{
int x;
cout << "You open the door and head down a flight of concrete stairs.\n";
cout << "Your eyes start to focus on the room ahead. They are adjusting\n";
cout << "to the dark. As you enter the room, you notice chains on the walls.\n";
cout << "You look around and finally realize that you're in a dungeon!\n";
cout << "You can make out a door to the east, and the one behind you.\n";
cout << "Where do you want to go?\n";
cout << "(1) North: \n";
cout << "(2) East: \n";
cout << "(3) West: \n";
cout << "(4) South: \n";
cin >> x;
if (x == 1)
{
cout << "You step forward and loose your footing.\nYou fall down a hole in the floor.\n";
cout << "You are so disorientated that you don\'t even realize you were impailed.\n\n";
system("pause");
return doyouquit();
}
else if (x == 2)
{
cout << trapped();
}
else if (x == 3)
{
cout << "\nYou feel something soft against the wall. Not being sure what";
cout << "it is you decide to leave it alone.\n\n";
system("pause");
return dungeon2();
}
else if (x == 4)
{
cout << "THE DOOR IS LOCKED!!!\n\n";
system("pause");
return dungeon2();
}
else if ((x != 1) || (x != 2) || (x != 3) || (x != 4))
{
return doyouquit();
}
}
char dungeon2()
{
int x;
cout << "You look around and realize that you're still in the dungeon!\n";
cout << "Wish there was a way to get out of here? Did you try the door\?\n";
cout << "You can make out a door to the east, and the one behind you.\n";
cout << "Where do you want to go?\n";
cout << "(1) North: \n";
cout << "(2) East: \n";
cout << "(3) West: \n";
cout << "(4) South: \n";
cin >> x;
if (x == 1)
{
cout << "You step forward and loose your footing.\nYou fall down a hole in the floor.\n";
cout << "You are so disorientated that you don\'t even realize you were impailed.\n\n";
system("pause");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -