VioletaBabel
6일 : 가위바위보, 클래스, 생성자, 소멸자, 함수 오버로딩 본문
수업 전 몸풀기문제 : 가위바위보 하나 빼기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | #include<iostream> #include<cstdlib> #include<ctime> using namespace std; void out(int i); void check(int c, int p); int main() {//0 = 가위, 1 = 바위, 2 = 보, 가위바위보 하나 빼기 게임 srand(time(NULL)); int iP[2], iC[2], com = -1, think[3];//iP는 플레이어, iC는 컴퓨터가 내는 값. com은 커맨드, think는 컴퓨터 계산 용 while (1) { for (int i = 0; i < 2; ++i) think[i] = 0;//think 초기화 cout << "가위바위보 하나 빼기." << endl; while (1) {//플레이어 왼손 입력 cout << "왼손에는 어떤 것을 내시겠습니까?\n0 : 가위, 1 : 바위, 2 : 보\n -> "; cin >> iP[0]; if (iP[0] < 3 && iP[0] > -1) break; cout << "\n잘못 누르셨습니다. \n"; } while (1) {//플레이어 오른손 입력 cout << "오른손에는 어떤 것을 내시겠습니까?\n0 : 가위, 1 : 바위, 2 : 보\n -> "; cin >> iP[1]; if (iP[1] < 3 && iP[1] > -1) break; cout << "\n잘못 누르셨습니다. \n"; } iC[0] = rand() % 3; //컴퓨터 왼손 입력 do {//컴퓨터 오른손 입력, 양 손이 안겹치게 함 iC[1] = rand() % 3; } while (iC[1] == iC[0]); cout << "컴퓨터의 선택은 "; out(iC[0]); out(iC[1]); cout << "\n당신의 선택은 "; out(iP[0]); out(iP[1]); cout << endl; while (1) {//플레이어가 뭐를 낼지 정함. cout << "어떤 것을 내시겠습니까?\n왼손 : 0, 오른손 : 1\n ->"; cin >> com; if (com == 0 || com == 1) break; cout << "\n잘못 누르셨습니다. \n"; } for(int i = 0; i < 2; ++i) for (int j = 0, k; j < 2; ++j) {//컴퓨터가 뭐를 낼지 정함. 상대편이 낸 두 값을 비교해 이기면 2점, 무승부는 1점씩 가중치를 주어 결정. k = iC[i] - iP[j]; if (k == 1 || k == -2) think[i] += 2; else if (k == 0) ++think[i]; } if (think[0] == think[1])//양 손이 동일한 가중치 값을 가지면 랜덤으로 낸다. think[2] = rand() % 2; else//그렇지 않을 경우 가중치가 높은 값을 낸다. think[2] = (think[0] > think[1]) ? 0 : 1; check(iC[think[2]], iP[com]);//승패결과 출력 } } void out(int i) { switch (i) { case 0: cout << "가위 "; break; case 1: cout << "바위 "; break; case 2: cout << "보 "; } } void check(int c, int p) { int w = c - p; cout << "컴퓨터는 "; out(c); cout << ", 플레이어는 "; out(p); cout << endl; if (w == 0) cout << "무승부!!!\n"; else if (w == 1 || w == -2) cout << "컴퓨터 승!\n"; else cout << "플레이어 승!\n"; cout << endl; } | cs |
--
클래스, 생성자, 소멸자, 함수 오버로딩
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //Main.cpp #include<iostream> #include"CAnimal.h" using namespace std; int main() { CAnimal dog; dog.sound(); CAnimal *cat = new CAnimal(5,6); cat->sound(); CAnimal cat2 = *cat; delete cat; cat2.sound(); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //CAnimal.h #pragma once class CAnimal { public: CAnimal(); CAnimal(int a, int b); CAnimal(const CAnimal& an); // 복사생성자. 클래스를 복사하는 생성자. ~CAnimal(); void sound(); private: int age; int *weight; }; | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | //CAnimal.cpp #include "CAnimal.h" #include<iostream> using namespace std; CAnimal::CAnimal() { age = 10; weight = new int; *weight = 15; } CAnimal::CAnimal(int a, int b) { age = a; weight = new int; *weight = b; } CAnimal::CAnimal(const CAnimal & an) {//따로 메모리를 잡아서 복사로 넣음 age = an.age; weight = new int; *weight = *an.weight; } CAnimal::~CAnimal() { delete weight; } void CAnimal::sound() { cout << age << "/" << *weight << endl; } | cs |
사각형 10개를 만들어 점을 찍었을 때 사각형 중 몇 개나 충돌하는지 확인하는 프로그램.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | //Main.cpp #include<iostream> #include<cstdlib> #include<ctime> #include "CRec.h" using namespace std; int main() { srand(time(NULL)); CRec r[10]; int x, y; for (int i = 5; i > 0; i--) { int z = 0; cin >> x >> y; for (int i = 0; i < 10; ++i) z += r[i].Collision(x, y); cout << "충돌 " << z << "회" << endl; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //CRec.h #pragma once #include<cstdlib> #include<iostream> using namespace std; class CRec { private: int x, y, w, h; public: CRec(); ~CRec(); int Collision(int _x, int _y); }; | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //CRec.cpp #include "CRec.h" CRec::CRec() { x = rand() % 10; y = rand() % 10; w = rand() % 6 + 5; h = rand() % 6 + 5; cout << x << "/" << y << "/" << w << "/" << h << "/" << endl; } CRec::~CRec() { } int CRec::Collision(int _x, int _y) { if (_x > x && _x < x + w) if (_y > y && _y < y + h) return 1; return 0; } | cs |
--
'BCA > 1. C,C++,C#' 카테고리의 다른 글
8일 : 상속과 다형성 (0) | 2018.02.19 |
---|---|
7일 : 연산자 오버로딩 (0) | 2018.02.13 |
5일 : 재귀 함수와 이진 트리 (0) | 2018.02.09 |
4일 : 단방향 리스트 (0) | 2018.02.08 |
3일 : 포인터 입문, 아스키코드, 문자열 기초 (0) | 2018.02.07 |
Comments