C++지뢰 제거 프로그램 개발 실현
11572 단어 C++지뢰 를 제거 하 다
//
#pragma once
class Game{
public:
//
void play();
//
int quit();
//
void rule();
private:
// ,
int error = 0;
//
int score = 0;
//
int Rocord[5] = { 0,0,0,0,0 };
//
int map[40][40];
// Size*Size
int Size = 10;
//
int fault_tolerant = 10;
//
int _difficulty=1;
//
void reset();
//
void drawGrid();
//
void Cheak();
//
int isWin();
//
void get_Rocord();
//
void put_Rocord();
//
int Selection_difficulty();
//
void loading();
};
그리고 클래스 함수 에 대한 정의 입 니 다.
// Game
#include " .h"
#include<Windows.h>
#include<iostream>
#include<fstream>
#include<time.h>
#include <conio.h>
#pragma warning(disable:4996) // Visual Studio 2017 getch()
//
#define RocordPath "D:\\VS/ .txt"
using namespace std;
#define none ""
// 5 , ,
enum players { Boom, None, Boom1, None1, Show1 };
//
enum _Difficulty{Easy,General,Difficulty,Purgatory};
int D_size[4][2] = { {10,10} ,{15,8},{20,5},{30,3} };
//
void Game::rule() {
loading();
//
system("cls");
cout << "
";
cout << " :
";
cout << "1. , “*”, 10 " << endl;
cout << "2. “ ”( 8 )" << endl;
cout << "3. , , " << endl;
cout << "4. " << endl;
cout << "5. " << endl;
cout << "
\t\t\t\t30 !";
Sleep(30000);
}
//
int Game::quit() {
system("cls");
//
COORD c = { 40, 13 };
//
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << " !!!" << endl;
Sleep(1000);
loading();
return 0;
}
//
void Game::play() {
//
get_Rocord();
while (true) {
//
_difficulty=Selection_difficulty();
//
int res = 1;
//
reset();
//
drawGrid();
while (true) {
//
Cheak();
//
drawGrid();
if (!isWin()) {
if (score > Rocord[_difficulty])Rocord[_difficulty] = score;
put_Rocord();
char s;
cout << " ? (y|Y)/ (n|N)" << endl;
cin >> s;
if (s == 'y' || s == 'Y')res = 1;
else res = 0;
break;
}
}
if (!res)break;
}
}
// ( )
void Game::reset() {
//
score = 0;
error = 0;
//
srand(time(NULL));
for (int i = 0; i < Size; i++) {
for (int j = 0; j < Size; j++) {
int t = rand() % 2;
if (t==1)map[j][i] = Boom;
else map[j][i] = None;
//cout << t<< " ";
}
//cout << endl;
}
}
//
void Game::drawGrid() {
//
system("cls");
//
COORD c = { 0, 2 };
//
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
//
for (int i = 0; i <= Size; i++) {
if (i < 10)
cout << i << " ";
else cout << i;
for (int j = 0; j < Size; j++) {
if (i == 0) {
if (j < 9)
cout << j + 1 << " ";
else cout << j + 1;
}
else cout << none;
}
cout << endl;
}
for (int y = 0; y < Size; y++) {
for (int x = 0; x < Size; x++) {
if (map[x][y] == Boom1|| map[x][y] == None1) {
//
COORD c = { x * 2 + 2, 3 + y };
//
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);//GetStdHandle
string o;
if (map[x][y] == Boom1) o = "* ";
if (map[x][y] == None1) o = " ";
cout << o;
}
if (map[x][y] == Show1) {
int cnt = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i >= 0 && i < Size && j >= 0 && j < Size) {
if (map[i][j] == Boom || map[i][j] == Boom1)cnt++;
}
}
}
//
COORD c = { x*2+2, 3 + y };
//
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);//GetStdHandle
cout << cnt << " ";
}
}
}
c.Y = Size+3;
//
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << " :"<<score<<"
:"<<Rocord[_difficulty]<<"
" << endl;
}
//
void Game::Cheak() {
int x = 0, y = 0;
cin >> x >> y;
x -= 1, y -= 1;
while(map[x][y] == Boom1 || map[x][y] == None1 || map[x][y] == Show1 || x < 0 || x >= Size || y < 0 || y >= Size) {
//
COORD c = { 0, 2 };
c.Y = Size+6;
//
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << " , " << endl;
cin >> x >> y;
x -= 1, y -= 1;
}
if (map[x][y] == Boom) {
map[x][y] = Boom1;
score -= 10;
error++;
}
else {
score += 10;
int cnt = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i >= 0 && i < Size && j >= 0 && j < Size) {
if (map[i][j] == Boom || map[i][j] == Boom1)cnt++;
}
}
}
if (cnt == 0) {
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i >= 0 && i < Size && j >= 0 && j < Size) {
map[i][j] = None1;
}
}
}
}
else map[x][y] = Show1;
}
}
//
int Game::isWin() {
int cnt = 0;
for (int i = 0; i < Size; i++) {
for (int j = 0; j < Size; j++) {
if (map[i][j] == None)cnt++;
}
}
if (cnt == 0) {
system("cls");
//
COORD c = { 50, 15 };
//
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << "You Win!!!" << endl;
return 0;
}
else if (error >= fault_tolerant) {
system("cls");
//
COORD c = { 50, 15 };
//
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << "You Loss!!!" << endl;
return 0;
}
else return 1;
}
//
void Game::get_Rocord() {
ifstream fin(RocordPath, ios::in);
for (int i = 0; i < 5; i++) {
fin >> Rocord[i];
}
}
//
void Game::put_Rocord() {
ofstream fout(RocordPath, ios::out);
for(int i=0;i<5;i++)
fout << Rocord[i] << endl;
}
//
int Game::Selection_difficulty() {
//
system("cls");
//
COORD c = { 0, 6 };
//
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << "\t\t\t\t\t\t1. (10*10 10 )
" << endl;
cout << "\t\t\t\t\t\t2. (15*15 8 )
" << endl;
cout << "\t\t\t\t\t\t3. (20*20 5 )
" << endl;
cout << "\t\t\t\t\t\t4. (30*30 3 )
" << endl;
cout << "\t\t\t\t\t\t5.
" << endl;
cout << "\t\t\t\t\t\t :";
int t = 1;
cin >> t;
while (t < 1 || t>5) {
COORD c = { 0, 21 };
//
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << "\t\t\t\t\t\t :" << endl;;
cin >> t;
}
switch (t) {
case 1:Size = D_size[Easy][0], fault_tolerant = D_size[Easy][1]; break;
case 2:Size = D_size[General][0], fault_tolerant = D_size[General][1]; break;
case 3:Size = D_size[Difficulty][0], fault_tolerant = D_size[Difficulty][1]; break;
case 4:Size = D_size[Purgatory][0], fault_tolerant = D_size[Purgatory][1]; break;
case 5: {
//
system("cls");
cout << "
\t\t\t\t ( 10-30, 10 )";
cout << "\t\t\t\t\t\t\t\t\t :";
cin >> Size;
cout << "
\t\t\t\t\t :";
cin >> fault_tolerant;
}break;
}
loading();
return t;
}
void Game::loading() {
COORD c = { 50,15 };
//
int t = 6;
while (t--) {
system("cls");
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
if(t%3==0)
cout << "loading..." << endl;
if (t % 3 == 1)
cout << "loading.." << endl;
if (t % 3 == 2)
cout << "loading." << endl;
Sleep(500);
}
}
마지막 으로 주 함수 부분 입 니 다.
//
#include<iostream>
#include<Windows.h>
#include" .h"
using namespace std;
int main() {
Game game;
while (true) {
int t, g = 1;
system("cls");
//
COORD c = { 30, 10 };
//
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
cout << " !!!
";
cout << "\t\t\t\t\t1.
\t\t\t\t\t2.
\t\t\t\t\t3. " << endl;
cin >> t;
switch (t) {
case 1:game.play(); break;
case 2:game.rule(); break;
case 3:g=game.quit(); break;
}
if (g == 0)break;
}
return 0;
}
블 로 그 를 쓰 는 것 도 처음 이 고 독립 적 으로 프로젝트 를 완성 하 는 것 도 처음 입 니 다.부족 한 점 이 있 으 니 큰 소 들 의 지 도 를 바 랍 니 다.이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Visual Studio에서 파일 폴더 구분 (포함 경로 설정)Visual Studio에서 c, cpp, h, hpp 파일을 폴더로 나누고 싶었습니까? 어쩌면 대부분의 사람들이 있다고 생각합니다. 처음에 파일이 만들어지는 장소는 프로젝트 파일 등과 같은 장소에 있기 때문에 파일...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.