3강 전체 코드

#include <iostream> // 콘솔 입출력
#include <stdlib.h> // 랜덤 모듈
#include <time.h> // 타임 추출
#include <string>
#include <CoreWindow.h>
using namespace std;

//가위 바위 보
void KKB(string name, int num){
cout << name << " 가";
if (num == 1){
cout << " [ 가위 ]를 냈습니다." << endl;
}
else if (num == 2){
cout << " [ 바위 ]를 냈습니다." << endl;
}
else{
cout << " [ 보 ] 를 냈습니다." << endl;
}
}

int KBBGame(int playerNum, int comNum){
int gamerWin = 1;
int comWin = 2;
int d = 0;

if (playerNum == comNum){
cout << "비겼습니다." << endl;
}
else if ((playerNum == 1 && comNum == 2) || (playerNum == 2 && comNum == 1) || (playerNum == 3 && comNum == 2)){
cout << "플레이가 이겼습니다." << endl;
return gamerWin;
}
else{
cout << "플레이가 졌습니다." << endl;
return comWin;
}
return d;
}

void main()
{
srand((unsigned int)time(NULL));

int playerTurn;
int comTURN;

int playerWin = 0;
int comWin = 0;
int draw = 0;

for (int i = 0; i < 100; i++){
cout << i + 1 << "번째 가위바위보 게임을 진행 하세요." << endl;
cout << "플레이어 (1:가위, 2:바위, 3:보, 4:게임종료 ) >>> ";

cin >> playerTurn; // cin >> 입력받을 변수

if (playerTurn == 4) break; // 브레이크 추가.  반복을 중단함

if (playerTurn > 3 || playerTurn < 1){ //
cout << "1,2,3,4 중의 번호를 골라주세요" << endl;
i--; // 게임회수 그대로.
continue; // 컨티뉴 추가. 사용자가 1~4번 외에 번호를 넣었을때.
}
comTURN = rand() % 3 + 1;

KKB("플레이어", playerTurn);
KKB("컴퓨터", comTURN);

int score = KBBGame(playerTurn, comTURN);
if (score == 1){
playerWin++;
}
else if (score == 2){
comWin++;
}
else{
draw++;
}
cout << "플레이어 " << playerWin << " 승 컴퓨터" << comWin << " 승 무승부 : " << draw << endl;
cout << "" << endl;
}

cout << "게임이 종료 되었습니다." << endl;

}



while 문으로.

#include <iostream> // 콘솔 입출력
#include <stdlib.h> // 랜덤 모듈
#include <time.h> // 타임 추출
#include <string>
#include <CoreWindow.h>
using namespace std;

//가위 바위 보
void KKB(string name, int num){
cout << name << " 가";
if (num == 1){
cout << " [ 가위 ]를 냈습니다." << endl;
}
else if (num == 2){
cout << " [ 바위 ]를 냈습니다." << endl;
}
else{
cout << " [ 보 ] 를 냈습니다." << endl;
}
}


int KBBGame(int playerNum, int comNum){
int gamerWin = 1;
int comWin = 2;
int d = 0;

if (playerNum == comNum){
cout << "비겼습니다." << endl;
}
else if ((playerNum == 1 && comNum == 2) || (playerNum == 2 && comNum == 1) || (playerNum == 3 && comNum == 2)){
cout << "플레이가 이겼습니다." << endl;
return gamerWin;
}
else{
cout << "플레이가 졌습니다." << endl;
return comWin;
}
return d;
}

void main()
{
srand((unsigned int)time(NULL));

int playerTurn;
int comTURN;

int playerWin = 0;
int comWin = 0;
int draw = 0;


int i = 0;
while (true){ // 조건식이 참이면 무한반복. 

cout << i + 1 << "번째 가위바위보 게임을 진행 하세요." << endl;
cout << "플레이어 (1:가위, 2:바위, 3:보, 4:게임종료 ) >>> ";
cin >> playerTurn; // cin >> 입력받을 변수

if (playerTurn == 4) break; // 브레이크 추가. 
if (playerTurn > 3){
cout << "1,2,3,4 중의 번호를 골라주세요" << endl;
continue; // 컨티뉴 추가. 사용자가 임이의 번호를 넣었을때.
}
comTURN = rand() % 3 + 1;

KKB("플레이어", playerTurn);
KKB("컴퓨터", comTURN);

int score = KBBGame(playerTurn, comTURN);
if (score == 1){
playerWin++;
}
else if (score == 2){
comWin++;
}
else{
draw++;
}
cout << "플레이어 " << playerWin << " 승 컴퓨터" << comWin << " 승 무승부 : " << draw << endl;
cout << "" << endl;

i++;
}

cout << "게임이 종료 되었습니다." << endl;

}



==============================================================


#include <iostream> // 콘솔 입출력
#include <stdlib.h> // 랜덤 모듈
#include <time.h> // 타임 추출
#include <string>
#include <CoreWindow.h>
using namespace std;



// 클래스 생성후 외부에서 접근 가능하도록 public으로 변수 생성
class Monster{
public : 
string name;
int hp;
int level;


void PrintInfo(){ // 메소드 작성
cout << "몬스터 이름 : " << name << endl;
cout << "몬스터 체력 : " << hp << endl;
cout << "몬스터 레벨 : " << level << endl;
}
};

void main()
{
//srand((unsigned int)time(NULL));

/*  기본 배열.
int array[5] = { 10, 20, 30, 40, 50 };

for (int i = 0; i < 5; i++){

cout << array[i] << endl;
}
*/


Monster monsters[3]; // monsters 객체 생성

monsters[0].name = "오우거"; // 각 객체에 값 대입.
monsters[0].hp = 100;
monsters[0].level = 1;

monsters[1].name = "고블린";
monsters[1].hp = 80;
monsters[1].level = 2;

monsters[2].name = "멀록";
monsters[2].hp = 50;
monsters[2].level = 10;


for (int i = 0; i < 1; i++){
monsters[i].PrintInfo(); // 메소드를 불러온다.
}


}

댓글

이 블로그의 인기 게시물

날짜 시간 시간차 시간 계산 하기

코루틴에서 CallBack 함수 적용하기

C++ 언어 퍼센트 구하는 방법 / 기본 언어 퍼센트 구하는 방법