본문 바로가기
my_lesson/_C++

C++ - Array_For_BingoGame ver 1.0

by boolean 2019. 4. 27.
728x90

C++ - Array_For_BingoGame ver 1.0

 

Create numbers 1 to 25  at iBingo

Shuffle numbers

Input 5 numbers 1 to 25 at iInput

Compare btween iBingo to iInput

Bingo line check

If it is more 4 lines, output BINGO !!

 

array_for_bingoGame.cpp

// Let's play bingo Game
#include <iostream>
#include <iomanip>
#include <typeinfo>
#include "MyLib.h"

using namespace std;
#define MATRIX 25
#define BINGO 5

void CreateNum(int* pArr, int sizeTot);
void Shuffle(int* pArr, int sizeTot);
void PrintNum(int* pArr, int iCol, int sizeTot);
int UserInput(int* pArr, int iCol, int& iEnd);
void Compare(int* pArr, int* iInput, int iCol, int sizeTot);
void Count(int* pArr, int iCol, int sizeTot, int& iCnt);
int main()
{
	int iMatrix[MATRIX] = { 0, };
	int iBingo[BINGO] = { 0, };
	int iInput[BINGO] = { 0, };

	int* pMatrix = iMatrix;
	int* pBingo = iBingo;
	int* pInput = iInput;
	int iCnt = 0, iEnd = 1;

	CreateNum(pMatrix, MATRIX);
	Shuffle(pMatrix, MATRIX);
	PrintNum(pMatrix, BINGO, MATRIX);
	while (true)
	{		
		cout << "Input number 1 to 25, Please [include 0 = Exit] : ";
		UserInput(pInput, BINGO, iEnd);
		if (iEnd == 0)
			break;
		system("cls");
		Compare(pMatrix, pInput, BINGO, MATRIX);
		PrintNum(pMatrix, BINGO, MATRIX);
		Count(pMatrix, BINGO, MATRIX, iCnt);
		cout << "BINGO = " << iCnt << endl;
		if (iCnt >= 4)
		{
			cout << "\n B.I.N.G.O ! !  Congratulation ~ ~ ! !" << endl;
			break;
		}
	}
	cout << "\n Bye ~ Bye ~ ~ ~" << endl;
	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 PrintNum(int* pArr, int size);
int UserInput(int* pArr, int size, int& iEnd);
void Compare(int* pArr1, int* pArr2,int* pStr, int size);
void Compare(int* pArr1, int* pArr2, 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);
void Count(int* pArr, int iCol, int sizeTet, int& iCnt);

#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 << endl;
	}
}

void PrintNum(int* pArr, int size)
{
	cout << "AI's  number is ";
	for (int i = 0; i < size; ++i)
	{
		cout << setw(5) << "*";
	}
	cout << endl;
}

int UserInput(int* pArr, int size, int& iEnd)
{
	for (int i = 0; i < size; ++i)
	{
		cin >> pArr[i];
		if (pArr[i] == 0)
			return iEnd = 0;
	}
	cout << " Your number is ";
	for (int i = 0; i < size; ++i)
	{
		cout << setw(5) << pArr[i];
	}
	cout << endl;
}

void Compare(int* pArr1, int* pArr2,int* pStr, int size)
{
	for (int i = 0; i < size; ++i)
	{
		for (int j = 0; j < size; ++j)
		{
			if (i == j && pArr1[i] == pArr2[j])
			{
				++pStr[0];
			}
			else if (i != j && pArr1[i] == pArr2[j])
			{
				++pStr[1];
			}
		}
	}
}

void Compare(int* pArr1, int* pArr2, int iCol, int sizeTot)
{
	for (int i = 0; i < sizeTot; ++i)
	{
		for (int j = 0; j < iCol; ++j)
		{
			if (pArr1[i] == pArr2[j])
			{
				pArr1[i] = INT_MAX;
			}
		}
	}
}

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)
{
	if (iStarIndex % iCol != 0 )
	{
		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)
{
	if (iStarIndex % iCol != 4)
	{
		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 Count(int* pArr, int iCol, int sizeTot,int& iCnt)
{
	iCnt = 0;
	for (int i = 0; i < iCol; ++i)
	{


		if (pArr[iCol * i + 0] == pArr[iCol * i + 1] && 
			pArr[iCol * i + 0] == pArr[iCol * i + 2] && 
			pArr[iCol * i + 0] == pArr[iCol * i + 3] &&
			pArr[iCol * i + 0] == pArr[iCol * i + 4])
		{
			++iCnt;
		}
		if(pArr[iCol * 0 + i] == pArr[iCol * 1 + i] &&
		   pArr[iCol * 0 + i] == pArr[iCol * 2 + i] &&
		   pArr[iCol * 0 + i] == pArr[iCol * 3 + i] &&
		   pArr[iCol * 0 + i] == pArr[iCol * 4 + i])
		{
			++iCnt;
		}

	}
	if (pArr[iCol * 0 + 0] == pArr[iCol * 1 + 1] &&
		pArr[iCol * 0 + 0] == pArr[iCol * 2 + 2] &&
		pArr[iCol * 0 + 0] == pArr[iCol * 3 + 3] &&
		pArr[iCol * 0 + 0] == pArr[iCol * 4 + 4])
	{
		++iCnt;
	}

	if (pArr[iCol * 0 + 4] == pArr[iCol * 1 + 3] &&
		pArr[iCol * 0 + 4] == pArr[iCol * 2 + 2] &&
		pArr[iCol * 0 + 4] == pArr[iCol * 3 + 1] &&
		pArr[iCol * 0 + 4] == pArr[iCol * 4 + 0])
	{
		++iCnt;
	}
	//return pCnt;
}

 

댓글