BingoClass -> 플레이어
BingoGameClass -> 게임 진행
빙고
#pragma once
#include<iostream>
#include<time.h>
#include<string>
#include<Windows.h>
using std::cout;
using std::endl;
using std::string;
using std::cin;
#define BINGO_SIZE 4
struct STCard
{
int num;
string img;
};
class Bingo
{
private:
int _size;
int _totalSize;
//STCard **arr;
STCard arr[4][4];
int _lineCount = 0;
bool _isPlayer;
int _inputNum;
public:
Bingo();
Bingo(bool ispalyer);
//Bingo(bool ispalyer, int size);
~Bingo();
int GetLineCount() const { return _lineCount; }
bool GetIsPlayer() const { return _isPlayer; }
void Shuffle();
void Init();
void Display();
int CallNumber();
void CheckNumber(int inputNum);
void CheckLine();
//bool IsInputNum(int num);
};
#include "Bingo.h"
Bingo::Bingo()
{
}
//
//Bingo::Bingo(bool ispalyer , int size)
//{
// srand(time(NULL));
// _isPlayer = ispalyer;
// _size = size;
// _totalSize = size * size;
// Init();
// Shuffle();
//
//}
Bingo::Bingo(bool ispalyer)
{
_isPlayer = ispalyer;
_size = BINGO_SIZE;
_totalSize = BINGO_SIZE * BINGO_SIZE;
Init();
Shuffle();
}
Bingo::~Bingo()
{
}
void Bingo::Init()
{
//2차 배열 동적 생성
/*arr = new STCard*[_size];
for (int i = 0; i < _size; i++)
{
arr[i] = new STCard[_size];
}*/
//배열 초기화
for (int i = 0; i < _totalSize; i++)
{
arr[0][i].img = "■";
arr[0][i].num = i + 1;
}
}
void Bingo::Shuffle()
{
int dest, sour, temp;
for (int i = 0; i < 20; i++)
{
dest = rand() % _totalSize;
sour = rand() % _totalSize;
temp = arr[0][dest].num;
arr[0][dest].num = arr[0][sour].num;
arr[0][sour].num = temp;
}
}
void Bingo::Display()
{
if (_isPlayer)
{
cout << "player" << endl;
cout << "bingo : "<<_lineCount << endl;
}
else
{
cout << "computer" << endl;
cout << "bingo : " << _lineCount << endl;
}
cout << "ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ" << endl;
for (int i = 0; i < _size; i++)
{
for (int j = 0; j < _size; j++)
{
if (arr[i][j].num == 0)
{
cout << arr[i][j].img << " ";
}
else
{
cout << arr[i][j].num << " ";
}
}
cout << endl;
}
cout << "========================" << endl;
}
int Bingo::CallNumber()
{
if (_isPlayer)
{
cout<< "빙고 숫자 입력" << endl;
cin >> _inputNum;
}
else
{
_inputNum = (rand() % _totalSize) + 1;
}
if (_inputNum > _totalSize || _inputNum == 0)
{
cout << "잘못된 입력" << endl;
CallNumber();
}
return _inputNum;
}
void Bingo::CheckNumber(int inputNum)
{
for (int i = 0; i < _totalSize; i++)
{
if (arr[0][i].num == inputNum)
{
arr[0][i].num = 0;
arr[0][i].img = "X";
}
}
}
void Bingo::CheckLine()
{
_lineCount = 0;
bool xBool , yBool,cross1,cross2;
//cout << "Bingo::CheckLine" << endl;
for (int i = 0; i < _size; i++)
{
xBool = true;
yBool = true;
//열 체크
for (int x = 0; x < _size; x++)
{
if (arr[i][x].num != 0)
{
xBool = false;
}
}
//행 체크
for (int y = 0; y < _size; y++)
{
if (arr[y][i].num != 0)
{
yBool = false;
}
}
if (xBool ) _lineCount++;
if (yBool ) _lineCount++;
}
cross1 = true;
for (int i = 0; i < _size; i++) {
if (arr[i][i].num != 0)
{
cross1 = false;
}
}
if (cross1) _lineCount++;
cross2 = true;
for (int i = 0, temp=0; i < _size; i++)
{
temp = (_size - 1) - i;
if (arr[i][temp].num != 0)
{
cross2 = false;
}
}
if (cross2) _lineCount++;
}
#pragma once
#include<vector>
#include"Bingo.h"
using std::vector;
class BingoGame
{
private:
vector<int> _checkVec;
vector<int>::iterator _iter;
int _bingoSize;
int _totalBingoSize;
int _numOfplayers;
int _playerTurn;
vector<Bingo*> _vecPlayer;
vector<Bingo*>::iterator _iterPlayer;
public:
BingoGame();
BingoGame(int players,int bingoSize);
~BingoGame();
void Play();
bool IsEnd();
void PlayersCheckLine();
void PlayersDisPlay();
void InputNum(int inputNum);
void AllInputNumber(int inputNum);
};
#include "BingoGame.h"
BingoGame::BingoGame()
{
srand(time(NULL));
int players ;
//, bingoSize
cout << "컴퓨터 몇개 생성? : " ;
cin >> players;
//cout << "빙고 사이즈? : ";
//cin >> bingoSize;
for (size_t i = 0; i < players; i++)
{
_vecPlayer.push_back(new Bingo(false));
}
_vecPlayer.push_back(new Bingo(true));
_numOfplayers = players + 1;
_bingoSize = BINGO_SIZE;
_totalBingoSize = BINGO_SIZE * BINGO_SIZE;
_checkVec.reserve(_totalBingoSize);
Play();
}
BingoGame::BingoGame(int players, int bingoSize)
{
for (size_t i = 0; i < players; i++)
{
_vecPlayer.push_back(new Bingo(false));
}
_vecPlayer.push_back(new Bingo(true));
_numOfplayers = players+1;
_bingoSize = bingoSize;
_totalBingoSize = bingoSize * bingoSize;
_checkVec.reserve(_totalBingoSize);
Play();
}
BingoGame::~BingoGame()
{
}
void BingoGame::Play()
{
int callNumberTemp=0;
_playerTurn = 0;
while (true)
{
_playerTurn %= _numOfplayers;
callNumberTemp =
_vecPlayer[_playerTurn]->CallNumber();
InputNum(callNumberTemp);
AllInputNumber(callNumberTemp);
PlayersCheckLine();
PlayersDisPlay();
if (IsEnd())
{
break;
}
system("pause");
system("cls");
_playerTurn++;
}
}
bool BingoGame::IsEnd()
{
for (_iterPlayer = _vecPlayer.begin();
_iterPlayer != _vecPlayer.end();
++_iterPlayer)
{
if (((*_iterPlayer)->GetLineCount()) >= 3)
{
if ((*_iterPlayer)->GetIsPlayer())
{
cout << "====Player Win====" << endl;
}
else
{
cout << "====Player Lose====" << endl;
}
return true;
}
}
return false;
}
void BingoGame::PlayersCheckLine()
{
for (_iterPlayer = _vecPlayer.begin();
_iterPlayer != _vecPlayer.end();
++_iterPlayer)
{
(*_iterPlayer)->CheckLine();
}
}
void BingoGame::PlayersDisPlay()
{
for (_iterPlayer = _vecPlayer.begin();
_iterPlayer != _vecPlayer.end();
++_iterPlayer)
{
(*_iterPlayer)->Display();
}
}
void BingoGame::InputNum(int inputNum)
{
for (_iterPlayer = _vecPlayer.begin();
_iterPlayer != _vecPlayer.end();
++_iterPlayer)
{
(*_iterPlayer)->CheckNumber(inputNum);
}
}
void BingoGame::AllInputNumber(int inputNum)
{
_checkVec.push_back(inputNum);
for (_iter = _checkVec.begin(); _iter != _checkVec.end(); ++_iter)
{
cout << *_iter << " ";
}
cout << endl;
}
슬라이드 퍼즐
#pragma once
#include <iostream>
#include <conio.h>
using std::cout;
using std::endl;
using std::cin;
#define SLIDE_SIZE 4
class SlidePuzzle
{
private:
int arr[SLIDE_SIZE][SLIDE_SIZE];
int tempNum;
int arrSize = SLIDE_SIZE * SLIDE_SIZE;
int x, y;
public:
SlidePuzzle();
~SlidePuzzle();
void Init();
void Play();
void Display();
void Shuffle();
void InputArrow();
bool CheckGameClear();
bool CheckNum(int num);
void StartChit();
};
#include "SlidePuzzle.h"
SlidePuzzle::SlidePuzzle()
{
Init();
Play();
}
SlidePuzzle::~SlidePuzzle()
{
}
void SlidePuzzle::Init()
{
tempNum = 0;
x = SLIDE_SIZE - 1, y = SLIDE_SIZE - 1;
for (size_t i = 0; i < arrSize-1; i++)
{
arr[0][i] = i+1;
}
Shuffle();
}
void SlidePuzzle::Play()
{
while (true)
{
Display();
InputArrow();
system("cls");
if (CheckGameClear())
{
Display();
cout << "-----------------\n----Game Clear----\n-----------------" << endl;
break;
}
}
}
void SlidePuzzle::Display()
{
cout << "ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ" << endl;
for (int i = 0; i < SLIDE_SIZE; i++)
{
for (int j = 0; j < SLIDE_SIZE; j++)
{
cout << arr[i][j]<< " ";
}
cout << endl;
}
cout << "ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ" << endl;
}
void SlidePuzzle::Shuffle()
{
int dest, sour, temp;
for (int i = 1; i < 100; i++)
{
dest = rand() % (arrSize-1);
sour = rand() % (arrSize-1);
temp = arr[0][dest];
arr[0][dest] = arr[0][sour];
arr[0][sour] = temp;
}
arr[0][arrSize-1] = 0;
}
void SlidePuzzle::InputArrow()
{
cout << "화살표로 움직이기 \n 치트 -> 0" << endl;
int i;
int cNum;
i = _getch();
if (i == 224)
{
i = _getch();
switch (i)
{
case 72:
//cout << "위쪽";
if (CheckNum(y - 1))
{
tempNum = arr[y][x];
arr[y][x] = arr[y - 1][x];
arr[y - 1][x] = tempNum;
y--;
}
break;
case 80:
//cout << "아래쪽";
if (CheckNum(y + 1))
{
tempNum = arr[y][x];
arr[y][x] = arr[y + 1][x];
arr[y + 1][x] = tempNum;
y++;
}
break;
case 75:
//cout << "왼쪽";
if (CheckNum(x - 1))
{
tempNum = arr[y][x];
arr[y][x] = arr[y][x - 1];
arr[y][x - 1] = tempNum;
x--;
}
break;
case 77:
//cout << "오른쪽";
if (CheckNum(x + 1))
{
tempNum = arr[y][x];
arr[y][x] = arr[y][x + 1];
arr[y][x + 1] = tempNum;
x++;
}
break;
}
}
else if (i == 48)
{
StartChit();
}
}
bool SlidePuzzle::CheckGameClear()
{
for (size_t i = 0; i < arrSize-1; i++)
{
if (arr[0][i] != i + 1)
{
return false;
}
}
return true;
}
bool SlidePuzzle::CheckNum(int num)
{
if (num < 0) return false;
else if (num > SLIDE_SIZE - 1) return false;
return true;
}
void SlidePuzzle::StartChit()
{
for (size_t i = 0; i < arrSize - 1; i++)
{
arr[0][i] = i + 1;
}
arr[0][arrSize - 1] = arrSize-1;
arr[0][arrSize - 2] = 0;
x = SLIDE_SIZE - 2;
y = SLIDE_SIZE - 1;
}
#include"BingoGame.h"
#include"SlidePuzzle.h"
int main()
{
BingoGame bingo;
SlidePuzzle sp;
}
'C++ > 과제' 카테고리의 다른 글
피보나치 수열 c++ (0) | 2021.12.24 |
---|---|
shop 장비 해제 추가 (0) | 2021.12.24 |
2021_1220_상속사각형 (0) | 2021.12.20 |