{{{ /*지뢰밭*/ /* 1. 지뢰밭의 크기를 입력 2. 지뢰는 '*' 로 표시하고, 지뢰가 아닌 것은 '.'으로 표시한다. 3. 입력을 하면 지뢰가 있는 곳을 출력한다. 4. 지뢰밭이 0, 0이면 종료한다. 5. C++초보인 야시가 프로그램 짠것이라 허접하니..보고 지적할 것이 있다면 지적해 주면 좋겠다.. */ /* 결과 지뢰밭 생성: X, Y축을 입력하시오. X축: 4 Y축: 4 지뢰를 입력하시오. (지뢰: *, 지뢰가 아닌것: .) *... .... .*.. .... Field #1 * 1 0 0 2 2 1 0 1 * 1 0 1 1 1 0 지뢰밭 생성: X, Y축을 입력하시오. X축: 3 Y축: 3 지뢰를 입력하시오. (지뢰: *, 지뢰가 아닌것: .) *** *.* *** Field #2 * * * * 8 * * * * 지뢰밭 생성: X, Y축을 입력하시오. X축: 0 Y축: 0 아무키나 누르면 종료합니다. */ #include "stdafx.h" #include #include #define Y_SIZE 100 using namespace std; //각 행에 지뢰 입력 class CCreateMine { private: char* pX; public: CCreateMine(int x); ~CCreateMine(); void InputMineX(int x); char OutputMineX(int x); }; //지뢰 생성 CCreateMine::CCreateMine(int x) { pX = new char[x]; } //소멸자 CCreateMine::~CCreateMine() { delete pX; } //지뢰입력 void CCreateMine::InputMineX(int x) { for(int i = 0; i < x; i++) { cin >> pX[i]; } } //지뢰출력 char CCreateMine::OutputMineX(int x) { return pX[x]; } //각 로우를 저장할 클래스 class CRowStorage { private: int x; int y; CCreateMine* pArray[Y_SIZE]; //Y_SIZE 100 public: CRowStorage(); void InputXY(); void InputMineY(); void OutputMineY(); int returnX(); int returnY(); }; CRowStorage::CRowStorage() { x = 0; y = 0; } //지뢰밭의 X, Y 입력 void CRowStorage::InputXY() { cout << "지뢰밭 생성: X, Y축을 입력하시오." << endl; cout << "X축: "; cin >> x; cout << "Y축: "; cin >> y; if(x > 0 && y > 0) cout << "지뢰를 입력하시오. (지뢰: *, 지뢰가 아닌것: .)" << endl; else return; } //각 로우별로 지뢰를 입력받는다. void CRowStorage::InputMineY() { if(x > 0 && y > 0 && y <= Y_SIZE) //Y_SIZE 100 { for(int i = 0; i < y; i++) { pArray[i] = new CCreateMine(x); pArray[i]->InputMineX(y); } cout << endl; } else { cout << "지뢰밭의 X, Y 부터 입력해야 합니다." << endl; CRowStorage::InputXY(); } } //지뢰출력 //지뢰이면 * 아니면 우, 하 -> [i+1], 좌, 상 -> [i-1] void CRowStorage::OutputMineY() { int num = 0; if(x > 1 && y > 1 && y <= Y_SIZE) //Y_SIZE 100 { for(int i = 0; i < y; i++) { for(int j = 0; j < x; j++) { char ch = pArray[i]->OutputMineX(j); if(ch != '*') //지뢰가 아니면 주변에 지뢰가 있는지 파악 { for(int i2 = i - 1; i2 <= i + 1; i2++) { for(int j2 = j - 1; j2 <= j + 1; j2++) { if(i2 >= 0 && i2 < y) { if(j2 >= 0 && j2 < x) { if(pArray[i2]->OutputMineX(j2) == '*') num++; } } } } cout << num << " "; } else //지뢰이면 { cout << "*" << " "; } num = 0; if(j == x - 1) cout << endl; } } } else { cout << "지뢰밭의 X, Y를 입력(x, y 모두 1 이상)해야 합니다." << endl; CRowStorage::InputXY(); } } int CRowStorage::returnX() { return x; } int CRowStorage::returnY() { return y; } int _tmain(int argc, _TCHAR* argv[]) { int i = 1; CRowStorage* rs = new CRowStorage(); do { rs->InputXY(); if(rs->returnX() > 0 && rs->returnY() > 0) { rs->InputMineY(); cout << "Field #" << i << endl; rs->OutputMineY(); cout << endl; } i++; }while(rs->returnX() > 0 || rs->returnY() > 0); delete rs; cout << "아무키나 누르면 종료합니다."; cin.ignore(i); cout << endl; return 0; } }}}