코딩

블랙잭 ver2

찬이2 2022. 11. 15. 04:45

기존의 블랙잭에서 참가 인원 수 정할 수 있게 추가

A카드 뽑을 시, 1 또는 11 중 원하는 값으로 적용되게 추가(dealer는 무조건 1)

카드 덱 3뭉치로 셔플하는 것으로 변경

#include<iostream>
#include<string>
#include<time.h>
#include<vector>
using namespace std;
//매크로
#define SET 3

//숫자 열거형
enum Number
{
	A = 1,
	J = 11,
	Q,
	K
};

//문양 열거형
enum Shape
{
	DIA,
	CLOVER,
	HEART,
	SPADE
};

//카드 구조체
struct Card
{
	int number;
	string shape;

	void Print()
	{
		switch (number)
		{
		case A:
			cout << shape << "A" << "\t";
			break;
		case J:
			cout << shape << "J" << "\t";
			break;
		case Q:
			cout << shape << "Q" << "\t";
			break;
		case K:
			cout << shape << "K" << "\t";
			break;
		default:
			cout << shape << number << "\t";
			break;
		}
	}
};

//플레이어 구조체
struct Player
{
	string name;
	vector<Card> card;
	int jokbo = 0;
	int money = 10000;
	bool win;

	void MakeJokbo()
	{
		jokbo = 0;
		for (int i = 0; i < card.size(); i++)
		{
			if (card[i].number > 10)
			{
				jokbo += 10;
			}
			else
			{
				jokbo += card[i].number;
			}
		}
	}

	void Print(bool dealer = 0)
	{
		int input;
		bool SelectA = false;
		cout << "플레이어 이름 : " << name << endl;
		for (int i = 0; i < card.size(); i++)
		{
			card[i].Print();
		}

		for (int i = 0; i < card.size(); i++)
		{
			if (card[i].number == A && dealer == 0)
			{
				cout << "1.a = 1 OR 2.a = 11" << endl << "선택 : ";
				cin >> input;
				if (input == 2)
				{
					jokbo += 10;
				}
				SelectA = true;
			}
		}

		if (SelectA)
		{
			for (int i = 0; i < card.size(); i++)
			{
				card[i].Print();
			}
			SelectA = false;
		}
		
		cout << "/ 합 : " << jokbo << "\t";

		if (!dealer)
		{
			cout << " / 돈 : " << money << "\t";
		}
		cout << endl;
	}
};

//덱만들기 함수
Card MakeDeck(int number, string shape)
{
	Card temp;
	temp.number = number;
	temp.shape = shape;

	return temp;
}

//셔플 함수
void Shuffle(vector<Card>& deck)
{
	for (int i = 0; i < 1000; i++)
	{
		int sour = rand() % (52 * SET);
		int dest = rand() % (52 * SET);

		Card temp = deck[sour];
		deck[sour] = deck[dest];
		deck[dest] = temp;
	}
}

int main()
{
	srand(time(NULL));
	vector<Card> deck;
	vector<Player> player;
	Player dealer;
	int input;

	//덱 만들기
	for (int i = 0; i < 52 * SET; i++)
	{
		string temp;
		int cut = i % 52;

		switch (cut / 13)
		{
		case DIA:
			temp = "◇";
			break;
		case CLOVER:
			temp = "♧";
			break;
		case HEART:
			temp = "♡";
			break;
		case SPADE:
			temp = "♤";
			break;
		}
		deck.push_back(MakeDeck(i % 13 + 1, temp));
	}
		
	cout << "플레이할 인원을 입력해주세요 : ";
	cin >> input;
	player.resize(input);

	for (int i = 0; i < player.size(); i++)
	{
		cout << i + 1 << "번째 플레이어 이름 입력 : ";
		cin >> player[i].name;
	}
	dealer.name = "딜러";

	//반복 시작
	while (true)
	{
		int cardcount = 0;
		Shuffle(deck);
		
		for (int i = 0; i < player.size(); i++)
		{
			player[i].win = true;
			player[i].card.clear();
			player[i].money -= 1000;
			player[i].card.push_back(deck[cardcount++]);
			player[i].card.push_back(deck[cardcount++]);
			cout << player[i].name << " 차례=========================================" << endl;
			player[i].MakeJokbo();
			player[i].Print();

			while (true)
			{
				cout << "1.더받기 / 2.안받기" << endl << "입력 : ";
				cin >> input;
				if (input == 2)
				{
					break;
				}
				
				player[i].card.push_back(deck[cardcount++]);
				player[i].MakeJokbo();
				player[i].Print();

				if (player[i].jokbo > 21)
				{
					cout << player[i].name << " 버스트!!!" << endl;
					player[i].win = false;
					break;
				}
				else
				{
					player[i].win = true;
				}
			}
		}

		//딜러 출력
		dealer.card.clear();
		dealer.card.push_back(deck[cardcount++]);
		dealer.card.push_back(deck[cardcount++]);
		cout << dealer.name << " 차례=========================================" << endl;
		dealer.MakeJokbo();
		dealer.Print(true);
		
		while (dealer.jokbo < 17)
		{
			dealer.card.push_back(deck[cardcount++]);
			dealer.MakeJokbo();
			dealer.Print(true);

			if (dealer.jokbo > 21)
			{
				cout << "딜러 버스트!!!!!" << endl;
				dealer.win = false;
				break;
			}
			else
			{
				dealer.win = true;
			}
		}


		for (int i = 0; i < player.size(); i++)
		{
			if (player[i].win == true)
			{
				if (dealer.win == false)
				{
					player[i].money += 2000;
					cout << player[i].name << " 승리!!" << endl;
				}
				else if (player[i].jokbo > dealer.jokbo)
				{
					player[i].money += 2000;
					cout << player[i].name << " 승리!!" << endl;
				}
				else if (player[i].jokbo <= dealer.jokbo)
				{
					cout << player[i].name << " 패배..." << endl;
				}
			}
			
		}
	}

	return 0;
}

3.0Ver Patch Note

A카드 1 또는 11 결정 코드를 Player.Print()에서 Player.MakeJokbo()로 변경

Player.MakeJokbo()의 매개변수 bool dealer = 0 추가

vector<Player> player를 Player* player로 변경(동적 할당 추가)

 

#include<iostream>
#include<string>
#include<time.h>
#include<vector>
using namespace std;
//매크로
#define SET 3

//숫자 열거형
enum Number
{
	A = 1,
	J = 11,
	Q,
	K
};

//문양 열거형
enum Shape
{
	DIA,
	CLOVER,
	HEART,
	SPADE
};

//카드 구조체
struct Card
{
	int number;
	string shape;

	void Print()
	{
		switch (number)
		{
		case A:
			cout << shape << "A" << "\t";
			break;
		case J:
			cout << shape << "J" << "\t";
			break;
		case Q:
			cout << shape << "Q" << "\t";
			break;
		case K:
			cout << shape << "K" << "\t";
			break;
		default:
			cout << shape << number << "\t";
			break;
		}
	}
};

//플레이어 구조체
struct Player
{
	string name;
	vector<Card> card;
	int jokbo = 0;
	int money = 10000;
	bool win;

	void MakeJokbo(bool dealer = 0)
	{
		int input;
		jokbo = 0;
		for (int i = 0; i < card.size(); i++)
		{
			if (card[i].number > 10)
			{
				jokbo += 10;
			}
			else if (card[i].number == 1)
			{
				if (!dealer)
				{
					for (int i = 0; i < card.size(); i++)
					{
						card[i].Print();
					}
					cout << "1. a = 1 / 2. a = 11" << endl << "입력 : ";
					cin >> input;
					if (input == 2)
					{
						jokbo += 11;
					}
					else
					{
						jokbo += card[i].number;
					}
				}
				else
				{
					jokbo += card[i].number;
				}
			}
			else
			{
				jokbo += card[i].number;
			}
		}
	}

	void Print(bool dealer = 0)
	{
		int input;
		bool SelectA = false;
		cout << "플레이어 이름 : " << name << endl;
		for (int i = 0; i < card.size(); i++)
		{
			card[i].Print();
		}

		cout << "/ 합 : " << jokbo << "\t";

		if (!dealer)
		{
			cout << " / 돈 : " << money << "\t";
		}
		cout << endl;
	}
};

//덱만들기 함수
Card MakeDeck(int number, string shape)
{
	Card temp;
	temp.number = number;
	temp.shape = shape;

	return temp;
}

//셔플 함수
void Shuffle(vector<Card>& deck)
{
	for (int i = 0; i < 1000; i++)
	{
		int sour = rand() % (52 * SET);
		int dest = rand() % (52 * SET);

		Card temp = deck[sour];
		deck[sour] = deck[dest];
		deck[dest] = temp;
	}
}

int main()
{
	srand(time(NULL));
	vector<Card> deck;
	Player* player;
	Player dealer;
	int input, size;

	//덱 만들기
	for (int i = 0; i < 52 * SET; i++)
	{
		string temp;
		int cut = i % 52;

		switch (cut / 13)
		{
		case DIA:
			temp = "◇";
			break;
		case CLOVER:
			temp = "♧";
			break;
		case HEART:
			temp = "♡";
			break;
		case SPADE:
			temp = "♤";
			break;
		}
		deck.push_back(MakeDeck(i % 13 + 1, temp));
	}
		
	cout << "플레이할 인원을 입력해주세요 : ";
	cin >> size;
	
	player = new Player[size];

	for (int i = 0; i < size; i++)
	{
		cout << i + 1 << "번째 플레이어 이름 입력 : ";
		cin >> player[i].name;
	}
	dealer.name = "딜러";

	//반복 시작
	while (true)
	{
		int cardcount = 0;
		Shuffle(deck);
		
		for (int i = 0; i < size; i++)
		{
			player[i].win = true;
			player[i].card.clear();
			player[i].money -= 1000;
			player[i].card.push_back(deck[cardcount++]);
			player[i].card.push_back(deck[cardcount++]);
			cout << player[i].name << " 차례=========================================" << endl;
			player[i].MakeJokbo();
			player[i].Print();

			while (true)
			{
				cout << "1.더받기 / 2.안받기" << endl << "입력 : ";
				cin >> input;
				if (input == 2)
				{
					break;
				}
				
				player[i].card.push_back(deck[cardcount++]);
				player[i].MakeJokbo();
				player[i].Print();

				if (player[i].jokbo > 21)
				{
					cout << player[i].name << " 버스트!!!" << endl;
					player[i].win = false;
					break;
				}
				else
				{
					player[i].win = true;
				}
			}
		}

		//딜러 출력
		dealer.card.clear();
		dealer.card.push_back(deck[cardcount++]);
		dealer.card.push_back(deck[cardcount++]);
		cout << dealer.name << " 차례=========================================" << endl;
		dealer.MakeJokbo(true);
		dealer.Print(true);
		
		while (dealer.jokbo < 17)
		{
			dealer.card.push_back(deck[cardcount++]);
			dealer.MakeJokbo(true);
			dealer.Print(true);

			if (dealer.jokbo > 21)
			{
				cout << "딜러 버스트!!!!!" << endl;
				dealer.win = false;
				break;
			}
			else
			{
				dealer.win = true;
			}
		}


		for (int i = 0; i < size; i++)
		{
			if (player[i].win == true)
			{
				if (dealer.win == false)
				{
					player[i].money += 2000;
					cout << player[i].name << " 승리!!" << endl;
				}
				else if (player[i].jokbo > dealer.jokbo)
				{
					player[i].money += 2000;
					cout << player[i].name << " 승리!!" << endl;
				}
				else if (player[i].jokbo <= dealer.jokbo)
				{
					cout << player[i].name << " 패배..." << endl;
				}
			}
			
		}
	}

	return 0;
}