📄 bomb.cpp
字号:
#include <iostream>
#include <time.h> //for rand()
#include <cstdlib>
#include <conio.h> //for getch()
#include <process.h> //for exit()
#include "msoftcon.h" //for graphics
using namespace std;
enum colorWire {red, green, blue};
class bomb
{
colorWire wire;
colorWire guess;
colorWire deadWire;
public:
bomb() //constructor to initialize bombs
{
int j=rand() % 3; //random number in range 0 to 2
int k=rand() % 3;
while(j==k) // make sure deadwire does not equal correct wire
k=rand() % 3;
switch(j) //set color of correct wire
{
case 0:
wire = red;
break;
case 1:
wire = green;
break;
case 2:
wire = blue;
}
switch(k) //set color of deadwire
{
case 0:
deadWire = red;
break;
case 1:
deadWire = green;
break;
case 2:
deadWire = blue;
}
}
int compareWire(char g);
};
int bomb::compareWire(char g)
{
switch(g)
{
case 'r':
guess = red;
break;
case 'g':
guess = green;
break;
case 'b':
guess = blue;
break;
default:
cout << "Invalid choice.\n";
}
if(guess==wire)
return 0; //correct
else
if(guess==deadWire)
return 1; //try again
else
return 2; //bomb explodes
}
void getChoice(bomb); //input choice and output results
int main()
{
init_graphics(); //initialize graphics
srand( time( NULL ) ); //random number seed
bomb bomb1, bomb2, bomb3;
set_color(cLIGHT_GRAY); //set cursor color
cout << "Three bombs have been set to go off and it is up to you to stop it.\n";
cout << "Each bomb has three wires and you will have to decide which wire to\n";
cout << "disconnect. The wires are colored ";
set_color(cRED);cout << "red";
set_color(cLIGHT_GRAY);cout << ", ";
set_color(cGREEN);cout << "green";
set_color(cLIGHT_GRAY);cout << ", and ";
set_color(cBLUE);cout << "blue";
set_color(cLIGHT_GRAY);cout << ". Cutting the\n";
cout << "correct wire will disarm the bomb and you can proceed to the next\n";
cout << "bomb. One of the wires will have no effect; it will niether disarm\n";
cout << "nor set the bomb off. However, cutting the wrong wire will set the\n";
cout << "bomb off and you will fail.";
set_cursor_pos(1,25); //set cursor position
cout << "Press any key to proceed...";
getch();
clear_screen();
cout << "Please carefully cut the wire for the first bomb: ";
getChoice(bomb1);
cout << "Please carefully cut the wire for the second bomb: ";
getChoice(bomb2);
cout << "Please carefully cut the wire for the last bomb: ";
getChoice(bomb3);
cout << "Great Job! You disarmed all three bombs.";
set_cursor_pos(1,25);
cout << "Press any key to exit...";
getch();
return 0;
}
void getChoice(bomb currentBomb)
{
char ch;
int guessResult;
set_color(cRED);cout << "red";
set_color(cLIGHT_GRAY);cout << ", ";
set_color(cGREEN);cout << "green";
set_color(cLIGHT_GRAY);cout << ", or ";
set_color(cBLUE);cout << "blue";
set_color(cLIGHT_GRAY);cout << ".\n (r, g, or b?)";
cin >> ch;
guessResult = currentBomb.compareWire(ch); //0=correct wire, 1=dead wire, 2=bomb explodes
while(guessResult==1)
{
cout << "You found the dead wire. Try again. (r, g, or b?)";
cin >> ch;
guessResult = currentBomb.compareWire(ch);
}
if(guessResult==0)
cout << "You have successfully disarmed the bomb!\n";
else
{
cout << "\nBOOOOOOOOOM!!!\n";
wait(3000); //pause 3 seconds
exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -