본문 바로가기
my_lesson/_C++

C++ - Array_For_BaseballGame Ver.1.0

by boolean 2019. 4. 5.
728x90

Mission : Let's make a Baseball Game

  • Random input : 3 numbers  between 1 to 9 without duplication.
  • It is a game to find the hidden numbers in the form * * *.
  • Enter three numbers until user find them.
  • If the numbers provided at random are 7, 3, 8 and user enterd numbers are 1, 2, and 4.
  • Outputs 'OUT' because there is no matching number.
  • If the user enters 7,5,or 9, '1 STRIKE 0 BALL' is ouput because there is a 7 that matches the position and the value. 
  • If the user enters 7,8,or 6, '1 STRIKE 1 BALL' is ouput because there is a 7 that matches the position and the value, and there is a 8 that matches the value but have different positions. 
  • Finally, matching the position and the value of the three digits will end the game.
  • If there is 0 one of them, game finish.

안녕하세요,

다음 링크를 클릭해 회의에 참가하십시오:
https://go.teamviewer.com/v14/m68206362

회의 ID: m68-206-362

 

array_for_baseBall.cpp

// Create a BaseBall Game
#include <iostream>
#include <iomanip>

#include "MyLib.h"

using namespace std;
#define NINE 9
#define THREE 3


void CreateNum(int* pArr, int size);
void Shuffle(int* pArr, int size);
void PrintNum(int* pArr, int size);
void UserInput(int* pArr, int size);
void Compare(int* pArr1, int* PArr2,int* pStr, int size);
int main()
{
	int aiArray[NINE] = { 0, };
	int uiArray[THREE] = { 0, };
	int baseBall[THREE] = { 0, };
	int* apArray = aiArray;
	int* upArray = uiArray;
	int* pStr = baseBall;

	CreateNum(apArray, NINE);
	Shuffle(apArray, NINE);
	while (true)
	{
		//system("cls");
		baseBall[0] = baseBall[1] = 0;
		cout << "Input number 0 to 9, Please (0 : Exit) : ";
		UserInput(upArray, THREE);
		PrintNum(apArray, THREE);
		Compare(apArray, upArray, pStr, THREE);
		if (uiArray[0] * uiArray[1] * uiArray[2] == 0)
		{
		cout << "Bye Bye~ ";
		break;
		}
		else if (uiArray[0] < 0 || uiArray[0] > 9 || uiArray[1] < 0 || uiArray[1] > 9 || uiArray[2] < 0 || uiArray[2] > 9)
		{
			cout << "Input wrong number! \n";
			continue;
		}
		else if (uiArray[0] == uiArray[1] || uiArray[1] == uiArray[2] || uiArray[2] == uiArray[0])
		{
			cout << "Don't have to input same number! \n";
			continue;
		}
		else if (baseBall[0] < THREE)
		{
			cout << setw(5) << baseBall[0] << " STRIKE " << setw(7) << baseBall[1] << " BALL !!" << endl;
			continue;
		}
		else if (baseBall[0] == THREE)
		{
			cout << setw(5) << baseBall[0] << " STRIKE " << setw(7) << baseBall[1] << " BALL !!" << endl;
			cout << "AI's  number is ";
			for (int i = 0; i < THREE; ++i)
			{
				cout << setw(5) << aiArray[i];
			}
			cout << endl << "Congratulation !!";
			break;
		}
		
	}
	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 sizeLine, int sizeTot);
void PrintNum(int* pArr, int size);
void UserInput(int* pArr, int size);
void Compare(int* pArr1, int* pArr2,int* pStr, int size);

#endif

MyLib.cpp

#include <iostream>
#include <time.h>
#include <iomanip>

#include "MyLib.h"

using namespace std;

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 sizeLine, int sizeTot)
{
	for (int i = 0; i < sizeTot; ++i)
	{
		cout << setw(5) << pArr[i];
		if (i % 6 == sizeLine -1)
			cout << endl;
	}
}

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

void UserInput(int* pArr, int size)
{
	for (int i = 0; i < size; ++i)
	{
		cin >> pArr[i];
	}
	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];
			}
		}
	}
}



감사합니다.
boolean 

댓글