C language allows knights to fly chess
- 2020-07-21 09:18:57
- OfStack
This article shares the specific code of C language to realize knight flying chess for your reference. The specific content is as follows
/* Author Mr.Long
* Date 2015 years 12 month 2 day 17:33:17
*/
#include<iostream>
#include<string>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#define random(x) (rand()%x)
using namespace std;
string player[2]={" The player A"," The player B"};
int map[100];
int playerPos[2]={0,0};
int gamePlayer = 0;
bool isGameOver = false;
int winer = -1;
int pausePlayer = -1;
//0 - normal 1 Do things Lucky wheel 2 It was not mine 3 Delta suspended 4 Space-time tunnel
string getLogo(int pos){
string res = " - ";
if((playerPos[0] == pos) && (playerPos[1] == pos)){
res = "<>";
}else if(playerPos[0]==pos){
res = " a. ";
}else if(playerPos[1]==pos){
res = " B ";
}else{
switch(map[pos]){
case 1:
res = " Do things "; // Wheel of fortune
break;
case 2:
res = " Thecountry. "; // mines
break;
case 3:
res = " delta "; // suspended
break;
case 4:
res = " � "; // Time and space tunnel
break;
}
}
return res;
}
void drowMap(){ // map
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;
cout<<" Map description: 【 Do things Lucky wheel Space-time tunnel It was not mine Delta suspended <> Players in 1 Place 】 "<<endl;
}
void gameOver(){ // Game over
isGameOver = true;
winer = gamePlayer;
playerPos[gamePlayer]=99;
system("cls");
drowMap();
cout<<"*** Game over! congratulations ["<<player[gamePlayer]<<"] Win! "<<endl;
system("PAUSE");
}
void initMap(){ // Initialization map
int luckyTurn[] = {6,23,40,55,69,83};// Wheel of fortune 1
int landMine[] = {5,13,17,33,38,50,64,80,94};// mines 2
int pause[] = {9,27,60,93};// suspended 3
int timeTunnel[] = {20,25,45,63,72,88,90};// Time and space tunnel 4
int i;
for(i =0;i<6;++i){
int pos = luckyTurn[i];
map[pos] = 1;
}
for(i =0;i<9;++i){
int pos = landMine[i];
map[pos] = 2;
}
for(i =0;i<4;++i){
int pos = pause[i];
map[pos] = 3;
}
for(i =0;i<7;++i){
int pos = timeTunnel[i];
map[pos] = 4;
}
}
void initUI(){ // Initialization interface
cout<<"******************* Little game *****************"<<endl;
cout<<"* *"<<endl;
cout<<"* Knight flight *"<<endl;
cout<<"* *"<<endl;
cout<<"****************@ Poetic rebellion ***************"<<endl;
}
void joinPlayer(){ // Join the player
string tmpStr = "";
cout<<" Please enter player A The name of the ___" <<endl;
cin>>tmpStr;
while(tmpStr==""){
cout<<" Player name cannot be empty, please re-enter ___" <<endl;
cin>>tmpStr;
}
player[0] = "A" + tmpStr;
cout<<" Please enter player B The name of the ___" <<endl;
cin>>tmpStr;
while(tmpStr==""){
cout<<" Player name cannot be empty, please re-enter ___" <<endl;
cin>>tmpStr;
}
while(tmpStr == player[0]){
cout<<" Player name cannot be repeated, please re-enter ___" <<endl;
cin>>tmpStr;
}
player[1] = "B" + tmpStr;
system("cls");
cout<<"*** Player joins successfully ..."<<endl;
cout<<"*** In the map [ a. ] Said the player ["<<player[0]<<"] The location of the ..."<<endl;
cout<<"*** In the map [ B ] Said the player ["<<player[1]<<"] The location of the ..."<<endl;
}
void yaoYiYao(){ // Throw the dice
short number = 0;
while(!isGameOver){
char a;
cout<<"*** please ["<<player[gamePlayer]<<"] The input g Throw the dice ..."<<endl;
cin>>a;
if(a=='g'){
system("cls");
number = random(6)+1;
cout<<"*** The player ["<<player[gamePlayer]<<"] The number of dice rolled is: "<<number<<endl;
playerPos[gamePlayer] += number;
int pos = playerPos[gamePlayer];
if(pos >=99){
gameOver();
}else{
switch(map[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<<"*** Wow! The player ["<<player[gamePlayer]<<"] Get lucky turn 1 The opportunity to turn ..."<<endl;
cout<<"*** Please enter a number to select the operation to be performed ...."<<endl;
cout<<"1-- Switch places with each other "<<endl<<"2-- Bombing of the other party "<<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<<" Enter a non-specified number! Lost opportunity. "<<endl;
}
gamePlayer = !gamePlayer;
break;
case 2:
cout<<"*** Oh oh! The player ["<<player[gamePlayer]<<"] Step on a landmine. Back up 6 step ..."<<endl;
playerPos[gamePlayer] -= 6;
gamePlayer = !gamePlayer;
break;
case 3:
cout<<"*** Tragedy! The player ["<<player[gamePlayer]<<"] Stop throwing 1 time ..."<<endl;
pausePlayer = 0;
gamePlayer = !gamePlayer;
break;
case 4:
cout<<"*** That's great! The player ["<<player[gamePlayer]<<"] Tunneling through time ..."<<endl;
playerPos[gamePlayer] += 10;
if(playerPos[gamePlayer]>=99){
gameOver();
}
gamePlayer = !gamePlayer;
break;
}
}
}else if(a == 'a'){
winer = 0;
gameOver();
}else if(a == '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<<"*** Start initializing player Settings ..."<<endl;
joinPlayer();
initMap();
drowMap();
cout<<"*** This game begins: ["<<player[0]<<"] VS ["<<player[1]<<"]"<<endl;
gamePlayer = random(2);
yaoYiYao();
return 0;
}