今天TA给大家带来一款小游戏(所需软件Dev-c++)。
希望大家喜欢(不喜勿喷)
#include<bits/stdc++.h>
#define random(x) (rand()%x)
using namespace std;
string player[2]={"玩家1","玩家2"};
int a[100];
int playerPos[2]={0,0};
int gamePlayer = 0;
bool isGameOver = false;
int winer = -1;
int pausePlayer = -1;
string getLogo(int pos){
string res = "-";
if((playerPos[0] == pos) && (playerPos[1] == pos)){
res = "!";
}else if(playerPos[0]==pos){
res = "+";
}else if(playerPos[1]==pos){
res = "=";
}else{
switch(a[pos]){
case 1:
res = "☆";
break;
case 2:
res = "◎";
break;
case 3:
res = "△";
break;
case 4:
res = "卍";
break;
}
}
return res;
}
void drowMap(){
for(int i = 0;i<=29;++i){
cout<<getLogo(i);
}
cout<<endl;
for(int i = 30;i<=34;++i){
for(int j = 0;j<=28;j++){
cout<<" ";
}
cout<<getLogo(i)<<endl;;
}
for(int i =64;i>=35;i--){
cout<<getLogo(i);
}
cout<<endl;
for(int i = 65;i<=69;++i){
cout<<getLogo(i)<<endl;
}
for(int i = 70;i<=99;i++){
cout<<getLogo(i);
}
cout<<endl;
}
void gameOver(){
isGameOver = true;
winer = gamePlayer;
playerPos[gamePlayer]=99;
system("cls");
drowMap();
cout<<"***游戏结束!恭喜["<<player[gamePlayer]<<"]取得胜利!"<<endl;
system("PAUSE");
}
void initMap(){
int luckyTurn[] = {6,23,40,55,69,83};
int landMine[] = {5,13,17,33,38,50,64,80,94};
int pause[] = {9,27,60,93};
int timeTunnel[] = {20,25,45,63,72,88,90};
for(int i =0;i<6;++i){
int pos = luckyTurn[i];
a[pos] = 1;
}
for(int i =0;i<9;++i){
int pos = landMine[i];
a[pos] = 2;
}
for(int i =0;i<4;++i){
int pos = pause[i];
a[pos] = 3;
}
for(int i =0;i<7;++i){
int pos = timeTunnel[i];
a[pos] = 4;
}
}
void initUI(){
cout<<"*******************小游戏*****************"<<endl;
cout<<"* *"<<endl;
cout<<"* 骑士飞行棋 *"<<endl;
cout<<"* *"<<endl;
cout<<"*****************xr20110206***************"<<endl;
}
void joinPlayer(){
string tmpStr = "";
cout<<"玩家1的名字___" <<endl;
cin>>tmpStr;
while(tmpStr==""){
cout<<"玩家名字不能为空请重新输入___" <<endl;
cin>>tmpStr;
}
player[0] = tmpStr;
cout<<"请输入玩家2的名字___" <<endl;
cin>>tmpStr;
while(tmpStr==""){
cout<<"玩家名字不能为空请重新输入___" <<endl;
cin>>tmpStr;
}
while(tmpStr == player[0]){
cout<<"玩家名字不能重复,请重新输入___" <<endl;
cin>>tmpStr;
}
player[1] =tmpStr;
system("cls");
cout<<"***玩家加入成功..."<<endl;
cout<<"***地图中+表示玩家["<<player[0]<<"]的位置..."<<endl;
cout<<"***地图中=表示玩家["<<player[1]<<"]的位置..."<<endl;
}
void yaoYiYao(){
short number = 0;
while(!isGameOver){
char b;
cout<<"***请["<<player[gamePlayer]<<"]输入0投掷骰子..."<<endl;
cin>>b;
if(b=='0'){
system("cls");
number = random(6)+1;
cout<<"***玩家["<<player[gamePlayer]<<"]投掷的骰子数为:"<<number<<endl;
playerPos[gamePlayer] += number;
int pos = playerPos[gamePlayer];
if(pos >=99){
gameOver();
}else{
switch(a[pos]){
case 0:
if(pausePlayer = -1){
gamePlayer = !gamePlayer;
}else if(pausePlayer = 0){
pausePlayer++;
}else if(pausePlayer = 1){
pausePlayer = -1;
}
break;
case 1:
int cnumber;
cout<<"***玩家["<<player[gamePlayer]<<"]获得幸运转盘"<<endl;
cout<<"***请输入数字选择要进行的操作...."<<endl;
cout<<"1--与对方交换位置"<<endl<<"2--轰炸对方"<<endl;
cin>>cnumber;
if(cnumber == 1){
int t = 0;
t = playerPos[gamePlayer];
playerPos[gamePlayer] = playerPos[!gamePlayer];
playerPos[!gamePlayer] = t;
}else if(cnumber == 2){
playerPos[!gamePlayer] -=6 ;
}else{
cout<<"输入非规定数字!机会丢失。"<<endl;
}
gamePlayer = !gamePlayer;
break;
case 2:
cout<<"***玩家["<<player[gamePlayer]<<"]踩到地雷啦,后退6步..."<<endl;
playerPos[gamePlayer] -= 6;
gamePlayer = !gamePlayer;
break;
case 3:
cout<<"***玩家["<<player[gamePlayer]<<"]暂停投掷一次..."<<endl;
pausePlayer = 0;
gamePlayer = !gamePlayer;
break;
case 4:
cout<<"***玩家["<<player[gamePlayer]<<"]穿越时空隧道..."<<endl;
playerPos[gamePlayer] += 10;
if(playerPos[gamePlayer]>=99){
gameOver();
}
gamePlayer = !gamePlayer;
break;
}
}
}else if(b == 'a'){
winer = 0;
gameOver();
}else if(b == 'b'){
winer = 1;
gameOver();
}
for(int i=0;i<=1;i++){
if(playerPos[i]<0)
playerPos[i] = 0;
}
if(!isGameOver){
drowMap();
}
}
}
int main(){
srand((unsigned)time(NULL));
initUI();
cout<<"***开始初始化玩家设置..."<<endl;
joinPlayer();
initMap();
drowMap();
cout<<"***本场游戏开始:["<<player[0]<<"] VS ["<<player[1]<<"]"<<endl;
gamePlayer = random(2);
yaoYiYao();
return 0;
}
如有错误希望大家可以多多指出。
上期因为魂斗罗出错频频所以以失败告终
请多多包含,对不起