원격 수업의 과제로 아주 간단하게 가위바위보 게임을 만들어서 제출해야 한다.
필요한 언어는 C++ 코딩 테스트 준비를 C++로 해서 거부감은 없지만 헤더 파일과 클래스를 사용해야 한다.
난 헤더 파일이 뭔지도 모르는 응애 인데… 큰일이다.
그래도 인터넷을 뒤지며 만들기는 만들어 보았다.
만들다 보니 이 기능을 추가하면 어떨까? 더 이쁘게 구조를 잡으며 만들까? 하다가
간단한 원격 수업 과제라는 걸 인지하고 마무리를 지었다.
C++ 초보자가 만든 프로그램이니 참고할 부분만 참고하세요.
다운로드 주소
https://github.com/lycos7560/storehouse/tree/main/MiniProject/Mini_RockPaperScissors
Main.cpp
#include <iostream>
#include <windows.h>
#include "game.h"
using namespace std;
int main()
{
Game game;
game.InitGame();
cout << "------------------------------";
cout << "\n!!!!!!!!가위바위보 게임!!!!!!!!\n";
cout << "------------------------------\n";
Sleep(2000);
system("cls");
// 사용자의 기본 세팅
string temp;
cout << "\n------------------------------";
cout << "\n당신의 닉네임을 설정하세요\n";
cout << "------------------------------\n";
cout << "닉네임 : ";
cin >> temp;
game.CreatePlayerAndNickName(temp);
system("cls");
cout << "\n------------------------------";
cout << "\n안녕하세요! ";
cout << game.GetPlayerInfo().GetnickName() << " 님\n";
cout << "------------------------------\n";
game.GameStart();
return 0;
}
Game.cpp
#include <string>
#include <random>
#include <iostream>
#include "game.h"
#include <windows.h>
using namespace std;
// 상대가 랜덤으로 타입을 결정합니다.
int Game::RandomPickHand()
{
int rangeMin = 0;
int rangeMax = 2;
random_device rd; // Only used once to initialise (seed) engine
mt19937 rng(rd()); // Random-number engine used (Mersenne-Twister in this case)
uniform_int_distribution<int> uni(rangeMin, rangeMax); // Guaranteed unbiased
int random_integer = uni(rng);
return random_integer;
}
// 게임의 룰을 설정합니다.
void Game::InitGame()
{
arrJudge[(int)Game::RockPaperScissorsType::Scissors][(int)Game::RockPaperScissorsType::Scissors] = "무승부";
Game::arrJudge[(int)Game::RockPaperScissorsType::Scissors][(int)Game::RockPaperScissorsType::Paper] = "승리";
Game::arrJudge[(int)Game::RockPaperScissorsType::Scissors][(int)Game::RockPaperScissorsType::Rock] = "패배";
Game::arrJudge[(int)Game::RockPaperScissorsType::Rock][(int)Game::RockPaperScissorsType::Rock] = "무승부";
Game::arrJudge[(int)Game::RockPaperScissorsType::Rock][(int)Game::RockPaperScissorsType::Paper] = "패배";
Game::arrJudge[(int)Game::RockPaperScissorsType::Rock][(int)Game::RockPaperScissorsType::Scissors] = "승리";
Game::arrJudge[(int)Game::RockPaperScissorsType::Paper][(int)Game::RockPaperScissorsType::Paper] = "무승부";
Game::arrJudge[(int)Game::RockPaperScissorsType::Paper][(int)Game::RockPaperScissorsType::Scissors] = "패배";
Game::arrJudge[(int)Game::RockPaperScissorsType::Paper][(int)Game::RockPaperScissorsType::Rock] = "승리";
}
// 플레이어의 닉네임을 설정합니다.
void Game::CreatePlayerAndNickName(string nickName)
{
player.SetnickName(nickName);
}
// 플레이어를 가져옵니다.
Player Game::GetPlayerInfo()
{
return player;
}
string Game::PlayerPrintHand()
{
if (player.GetType() == (int)Game::RockPaperScissorsType::Rock)
{
return "주먹";
}
else if (player.GetType() == (int)Game::RockPaperScissorsType::Scissors)
{
return "가위";
}
else if (player.GetType() == (int)Game::RockPaperScissorsType::Paper)
{
return "보";
}
}
string Game::ComputerPrintHand()
{
if ( computer.GetType() == (int)Game::RockPaperScissorsType::Rock)
{
return "주먹";
}
else if (computer.GetType() == (int)Game::RockPaperScissorsType::Scissors)
{
return "가위";
}
else if (computer.GetType() == (int)Game::RockPaperScissorsType::Paper)
{
return "보";
}
}
void Game::checkInput(int temp)
{
switch (temp)
{
case 0:
break;
case 1:
break;
case 2:
break;
default:
cout << "\n잘못된 입력입니다. 프로그램을 종료합니다.\n";
system("pause");
exit(0);
break;
}
}
void Game::GameStart()
{
Sleep(1500);
system("cls");
cout << "\n------------------------------";
cout << "\n게임을 시작합니다.\n";
cout << "------------------------------\n";
Sleep(1500);
system("cls");
cout << "\n------------------------------";
cout << "\n 타입을 결정해주세요!! \n";
cout << "Rock = 0, Paper = 1, Scissors = 2\n";
cout << "숫자 : ";
int temp;
cin >> temp;
checkInput(temp);
player.SetType(temp);
computer.SetType(RandomPickHand());
computer.SetnickName("토리");
system("cls");
cout << "\n------------------------------\n";
cout << player.GetnickName() << "님은 " << PlayerPrintHand() << " 을(를) 선택하셨습니다.\n";
cout << "------------------------------\n";
cout << "상대인 : " << computer.GetnickName() << "님은 " << ComputerPrintHand() << "을(를) 선택하셨습니다.\n";
cout << "\n------------------------------\n";
cout << "결과는 당신의 " << arrJudge[player.GetType()][computer.GetType()] << "입니다.\n";
system("pause");
}
Player.cpp
#include <string>
#include "player.h"
using namespace std;
// 플레이어의 닉네임을 설정하는 함수
void Player::SetnickName(string Name) {
this->nickName = Name;
};
// 플레이어의 닉네임을 가져오는 함수
string Player::GetnickName() {
return nickName;
};
void Player::SetType(int tempType)
{
type = tempType;
};
int Player::GetType()
{
return type;
}
Game.h
#pragma once
#include <string>
#include "Player.h"
using namespace std;
class Game
{
public :
Game() {}; // 생성자
// 여기서 가위바위보를 정의해 줍니다.
enum class RockPaperScissorsType {
Rock = 0, Paper = 1, Scissors = 2
};
// 랜덤으로 가위바위보 선택 하기
int RandomPickHand();
// 게임의 룰을 설정합니다.
void InitGame();
// 플레이어의 닉네임을 설정합니다.
void CreatePlayerAndNickName(string nickName);
// 플레이어의 정보를 가져옵니다.
Player GetPlayerInfo();
void GameStart();
string PlayerPrintHand();
string ComputerPrintHand();
void checkInput(int temp);
private :
// 가위바위보 룰 셋팅
string arrJudge[3][3];
Player player;
RockPaperScissorsType playerType;
Player computer;
RockPaperScissorsType computerType;
};
Player.h
#pragma once
#include <string>
using namespace std;
class Player
{
public:
Player() {
type = 0;
nickName = "";
};
// 플레이어의 닉네임을 설정하는 함수
void SetnickName(string Name);
// 플레이어의 닉네임을 가져오는 함수
string GetnickName();
void SetType(int type);
int GetType();
private:
int type;
string nickName;
};

![백준 11650번 (좌표 정렬하기, C++, multimap) [BAEKJOON]](https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-1024x535.png)
![백준 2583번 (영역 구하기, C++) [BAEKJOON]](https://lycos7560.com/wp-content/uploads/boj-og.png)
![Programmers 72412 순위 검색 [2021 KAKAO BLIND RECRUITMENT]](https://lycos7560.com/wp-content/uploads/2023/03/programmers.jpg)