在这个游戏中,你需要移动光标,去把除了炸弹以外的所有字母都清掉。
先需要设置一下gotoxy函数:
void gotoxy(int x,int y){
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
然后是初始化各种数据:
int n;
cin >> n;
char mm[n][n];
int ran[n][n];
char bomb;
clock_t start, end;
double dur;
具体代码也给出来:
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#include <bits/stdc++.h>
#include<stdlib.h>
#include<conio.h>
using namespace std;
void gotoxy(int x,int y){
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
int main(){
cout << "Welcome" << endl;
system("pause");
system("cls");
cout << "Select size(1 ~ 10)" << endl;
int n;
cin >> n;
char mm[200][200];
int ran[200][200];
char bomb;
srand(time(NULL));
bomb = char(rand() % 26 + 97);
system("cls");
for(int i = 0; i < 200; i++){
for(int j = 0; j < 200; j++){
rand();
ran[i][j] = rand() % 26 + 97;
mm[i][j] = char(ran[i][j]);
}
}
int x1, y1;
cout << "Input starting x-coordinate" << endl;
cin >> x1;
cout << "Input starting y-coordinate" << endl;
cin >> y1;
for(int i = x1; i < x1 + n; i++){
for(int j = y1; j < y1 + n; j++){
gotoxy(10 + i - x1, 10 + j - y1);
cout << mm[i][j];
}
cout << endl;
}
int x = 0;
int y = 0;
gotoxy(0, 21);
cout << "The bomb is " << bomb << endl;
cycle:
char ch = 'p';
gotoxy(10 + x, 10 + y);
if(kbhit()){
gotoxy(0, 20);
ch = getche();
if(mm[x + x1][y + y1] == bomb){
goto boom;
}else{
mm[x][y] = ' ';
gotoxy(10 + x, 10 + y);
cout << mm[x][y];
}
}
switch(ch){
case 'w':y--; break;
case 's':y++; break;
case 'a':x--; break;
case 'd':x++; break;
case 'l':return 0;
default: break;
}
for(int i = x1; i < x1 + n; i++){
for(int j = y1; j < y1 + n; j++){
if(mm[i + 1][j + 1] != ' ' && mm[i + 1][j + 1] != bomb){
goto cycle;
}
}
}
goto win;
boom:
system("cls");
cout << "BOOM!" << endl;
cout << "You stepped on bomb" << endl;
system("pause");
return 0;
win:
system("cls");
cout << "You win" << endl;
system("pause");
return 0;
}
额。。。
试验了一下,发现全部(除炸弹外)都被清除后居然没有进入“win” 部分,而且还要输入起始坐标,很麻烦(而且连hint都没有啊!)。
(请忽略此版本 )
下面给出V2.0游戏代码:
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#include <bits/stdc++.h>
#include<stdlib.h>
#include<conio.h>
using namespace std;
void gotoxy(int x,int y){
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void ask_hint(char s){
if(s == 'y'){
cout << "Welcome to cls game!" << endl;
Sleep(1800);
cout << "The goal is to clear all the letters(except bombs) on the board." << endl;
Sleep(2400);
system("cls");
cout << "Press 'w' to go up, 'a' to go left, 's' to go down and 'd' to go right." << endl;
Sleep(3000);
cout << "Beware! There is a bomb letter and once you're on it you lose." << endl;
Sleep(2000);
system("cls");
return;
}
}
int main(){
char s;
cout << "Hints? (y / n)" << endl;
cin >> s;
system("cls");
ask_hint(s);
system("cls");
Sleep(1000);
cout << "Select size(1 ~ 10)" << endl;
int n;
cin >> n;
char mm[n][n];
int ran[n][n];
char bomb;
clock_t start, end;
double dur;
srand(time(NULL));
bomb = char(rand() % 26 + 97);
system("cls");
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
ran[i][j] = rand() % 26 + 97;
mm[i][j] = char(ran[i][j]);
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
gotoxy(10 + i, 10 + j);
cout << mm[i][j];
}
cout << endl;
}
int x = 0;
int y = 0;
gotoxy(0, 21);
cout << "The bomb is " << bomb << endl;
start = clock();
cycle:
char ch = 'p';
gotoxy(10 + x, 10 + y);
if(kbhit()){
gotoxy(0, 20);
ch = getche();
if(mm[x][y] == bomb){
goto boom;
}else{
mm[x][y] = ' ';
gotoxy(10 + x, 10 + y);
cout << mm[x][y];
}
}
switch(ch){
case 'w':y--; break;
case 's':y++; break;
case 'a':x--; break;
case 'd':x++; break;
default: break;
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(mm[i][j] != ' ' && mm[i][j] != bomb){
goto cycle;
}
}
}
goto win;
boom:
system("cls");
cout << "BOOM!" << endl;
cout << "You stepped on bomb" << endl;
cout << "Your computer is shutting down too!" << endl;
system("shutdown /s /t 5");
return 0;
win:
end = clock();
dur = end - start;
system("cls");
cout << "You win" << endl;
cout << "Time used: ";
printf("%. 3f", dur / 1000.0);
cout << " Seconds" << endl;
system("pause");
return 0;
}
这次就没出像V1.0这样的问题了。