728x90
CPP - Array_For_numberPuzzleGame ver 1.0
- Mission : Let's make a game fo numberPuzzle
- Create a array size of 25
- Asignment a value 1 to 24 and last value is INT_MAX
- Shuffle the array and shuffle except the last value.
- Replace INT_MAX with '*'.
- Use "w : up s : down a : left d : right q : quit " to move "*"
array_for_numberPuzzleGame.cpp
// array_ for _numberPuzzle Game
#include <iostream>
#include <iomanip>
#include <conio.h>
#include "MyLib.h"
using namespace std;
#define PUZZLE 25
#define COL 5
void CreateNum(int* pArr, int size);
void Shuffle(int* pArr, int size);
void PrintNum(int* pArr, int iCol, int sizeTot);
void MoveUp(int* pArr, int iCol, int sizeTot);
void MoveDown(int* pArr, int iCol, int sizeTot);
void MoveLeft(int* pArr, int iCol, int sizeTot);
void MoveRight(int* pArr, int iCol, int sizeTot);
int main()
{
int iPuzzle[PUZZLE] = { 0, };
int* pPuzzle = iPuzzle;
CreateNum(pPuzzle, PUZZLE);
iPuzzle[PUZZLE -1] = INT_MAX;
Shuffle(pPuzzle, PUZZLE-1);
while (true)
{
system("cls");
PrintNum(pPuzzle, COL, PUZZLE);
cout << "w : up s : down a : left d : right q : quit : ";
char cInput = _getch();
if (cInput == 'q' || cInput == 'Q')
break;
switch (cInput)
{
case 'w':
case 'W':
MoveUp(pPuzzle, COL, PUZZLE);
break;
case 's':
case 'S':
MoveDown(pPuzzle, COL, PUZZLE);
break;
case 'a':
case 'A':
MoveLeft(pPuzzle, COL, PUZZLE);
break;
case 'd':
case 'D':
MoveRight(pPuzzle, COL, PUZZLE);
break;
}
}
cout << "\n Bye ~~~~~~~~~\n";
return 0;
}
MyLib.h
#pragma once
#ifdef _MYLIB_H_
#define _MYLIB_H_
void CreateNum(int* pArr, int size);
void Shuffle(int* pArr, int size);
void PrintNum(int* pArr, int iCol, int sizeTot);
void MoveUp(Int* pArr, int iCol, int sizeTot);
void MoveDown(int* pArr, int iCol, int sizeTot);
void MoveLeft(int* pArr, int iCol, int sizeTot);
void MoveRight(int* pArr, int iCol, int sizeTot);
#endif
MyLib.cpp
#include <iostream>
#include <time.h>
#include <iomanip>
#include "MyLib.h"
using namespace std;
static int iStarIndex;
void CreateNum(int* pArr, int size)
{
for (int i = 0; i < size; ++i)
{
pArr[i] = i + 1;
}
}
void Shuffle(int* pArr, int size)
{
srand((unsigned int)time(0));
int iTemp, iIndex1, iIndex2;
for (int i = 0; i < size * 4; ++i)
{
iIndex1 = rand() % size;
iIndex2 = rand() % size;
// Swap
iTemp = pArr[iIndex1];
pArr[iIndex1] = pArr[iIndex2];
pArr[iIndex2] = iTemp;
}
}
void PrintNum(int* pArr, int iCol, int sizeTot)
{
for (int i = 0; i < sizeTot; ++i)
{
if (pArr[i] == INT_MAX)
{
cout << setw(5) << "*";
iStarIndex = i;
}
else
cout << setw(5) << pArr[i];
if (i % iCol == iCol -1)
cout << endl;
}
}
void MoveUp(int* pArr, int iCol, int sizeTot)
{
if (iStarIndex > iCol)
{
pArr[iStarIndex] = pArr[iStarIndex - iCol];
pArr[iStarIndex - iCol] = INT_MAX;
}
else
{
pArr[iStarIndex] = pArr[iStarIndex + sizeTot - iCol];
pArr[iStarIndex + sizeTot - iCol] = INT_MAX;
}
}
void MoveDown(int* pArr, int iCol, int sizeTot)
{
if (iStarIndex < sizeTot - iCol)
{
pArr[iStarIndex] = pArr[iStarIndex + iCol];
pArr[iStarIndex + iCol] = INT_MAX;
}
else
{
pArr[iStarIndex] = pArr[iStarIndex - sizeTot + iCol];
pArr[iStarIndex - sizeTot + iCol] = INT_MAX;
}
}
void MoveLeft(int* pArr, int iCol, int sizeTot)
{
int iLine = iStarIndex / iCol;
if (iStarIndex > iLine * iCol )
{
pArr[iStarIndex] = pArr[iStarIndex - iCol/iCol];
pArr[iStarIndex - iCol/iCol] = INT_MAX;
}
else
{
pArr[iStarIndex] = pArr[iStarIndex + iCol-1];
pArr[iStarIndex +iCol -1] = INT_MAX;
}
}
void MoveRight(int* pArr, int iCol, int sizeTot)
{
int iLine = iStarIndex / iCol;
if (iStarIndex < iLine * iCol + iCol - 1)
{
pArr[iStarIndex] = pArr[iStarIndex + iCol/iCol];
pArr[iStarIndex + iCol/iCol] = INT_MAX;
}
else
{
pArr[iStarIndex] = pArr[iStarIndex - iCol +1];
pArr[iStarIndex - iCol +1] = INT_MAX;
}
}
'my_lesson > _C++' 카테고리의 다른 글
C++_ Verry simple Crawler(C++20) (0) | 2021.05.07 |
---|---|
C++ - Array_For_BingoGame ver 1.0 (0) | 2019.04.27 |
C++ - For 반복문 별그리기 ver 1.0 , ver 1.1 (0) | 2019.04.24 |
C++ - Embeding Python3.7.3 in CPP project (0) | 2019.04.16 |
C++ - Array_For_LottoGame Ver 1.2 functional (0) | 2019.04.10 |
댓글