숫자 퍼즐 게임(클래스 + 1차원 배열 셔플)

//헤더파일
#pragma once
#include <iostream>
#include <conio.h>
#include <time.h>
using namespace std;
class puzzleGame
{
private:
//이곳에서 변수를 선언한다.
int _num[5][5];
int _numS[25];
public:
//이곳에서 함수를 만든다.
//출력 함수
void board();
void board1();
puzzleGame();
~puzzleGame();
};
//cpp 파일
void puzzleGame::board1()
{
// 0 1 2 3 4 ?
// 5 6 7 8 9 ?
//3.판 만들기
system("cls");
for (int j = 0; j < 25; j = j + 5)
{
for (int i = 0 + j; i < 5 + j; i++)
{
cout << _numS[i] << "\t";
}
cout << "\n\n\n" << endl;
}
}
puzzleGame::puzzleGame()
{
srand(time(NULL));
//이곳에서 게임을 만든다
cout << "1.1차원배열, 2.2차원배열" << endl;
int choice;
choice = _getch();
//1차원 배열
if (choice == 49)
{
cout << "1차원 배열로 만든 숫자퍼즐" << endl;
system("pause");
//1.숫자 배열 초기화 하기
for (int i = 0; i < 25; i++) _numS[i] = i;
//2.셔플
for (int i = 0; i < 1000; i++)
{
int index1 = rand() % 25;
int index2 = rand() % 25;
int temp = _numS[index1];
_numS[index1] = _numS[index2];
_numS[index2] = temp;
//4.숫자 0을 배열의 마지막에 위치하도록 만들어라
if (_numS[24] == 0)
{
cout << i << " 번 째 셔플에서 멈췄습니다." << endl;
system("pause");
break;
}
}
//for (int i = 0; i < 25; i++) cout << _numS[i] << endl;
int temp = 0;
int x = 24;
while (true)
{
board1();
//5.숫자 움직이게 처리
int input;
input = _getch();
//cin >> input;
if (input == 50 || input == 52 || input == 54 || input == 56)
{
switch (input)
{
case 50: //아래로
if (x > 19) break;
else
{
temp = _numS[x];
_numS[x] = _numS[x + 5];
_numS[x + 5] = temp;
x = x + 5;
break;
}
case 52: //좌로
if (x == 0 || x == 5 || x == 10 || x == 15 || x == 20) break;
else
{
temp = _numS[x];
_numS[x] = _numS[x - 1];
_numS[x - 1] = temp;
x = x - 1;
break;
}
case 54: //우로
if (x == 4 || x == 9 || x == 14 || x == 19 || x == 24) break;
else
{
temp = _numS[x];
_numS[x] = _numS[x + 1];
_numS[x + 1] = temp;
x = x + 1;
break;
}
case 56: //위로
if (x < 5) break;
else
{
temp = _numS[x];
_numS[x] = _numS[x - 5];
_numS[x - 5] = temp;
x = x - 5;
break;
}
}
}
else continue;
}
}//end of if(choice == 49)
}
댓글
댓글 쓰기