컴퓨터와 겨루는 숫자빙고게임을 만들어보았습니다.
동영상 서비스가 종료되어 해당 콘텐츠를 재생할 수 없습니다.
어소트락 게임아카데이미 채널의 영상을 보고 공부하였으며, 영상대로 코딩은 짜지 않고, 설명을 듣고, 내 나름대로 한번 짜보고 나서, 영상을 시청하며, 부족한점을 보완하였습니다.
시작하기 전, Easy mode 와 Hard mode 중 선택
1부터25까지의 숫자를 겹치지 않고, 섞은 후 출력.
상대방(컴퓨터)의 숫자는 보이지 않고, *로 출력.
먼저, 5줄 이상 빙고를 한 플레이어가 승리.
잘 짠 코드는 아니지만, 올려봅니다.
코드:
#include <iostream>
#include <random>
#include <time.h>
#include <conio.h>
#include <algorithm>
#include "MyBingoGame.h"
using namespace std;
int main()
{
const int size = 25;
int plnums[size]{};
int aiNums[size]{};
// set nums
for (int i = 0; i < size; ++i)
{
plnums[i] = i + 1;
aiNums[i] = i + 1;
}
srand(unsigned int(time(0)));
// shuffle nums
int ix1, ix2;
for (int i = 0; i < 100; ++i)
{
ix1 = rand() % 25;
ix2 = rand() % 25;
swap(plnums[ix1], plnums[ix2]);
//AI
ix1 = rand() % 25;
ix2 = rand() % 25;
swap(aiNums[ix1], aiNums[ix2]);
}
int ai_mode;
while (true)
{
system("cls");
// Choose mode
cout << "Choose mode." << endl;
cout << "1. Easy" << endl;
cout << "2. Hard" << endl;
cout << "Input: a number: ";
cin >> ai_mode;
if (ai_mode == AI_EASY || ai_mode == AI_HARD)
break;
else
{
cout << "Wrong input. Input again" << endl;
cout << "Press any key.";
char ch;
ch = _getch();
continue;
}
}
// game loop
int pl_Bingo_count = 0;
int AI_Bingo_count = 0;
int input;
bool pl_star_ix[size]{0};
bool AI_star_ix[size]{0};
bool play_turn = true; // true is play turn.
while (true)
{
system("cls");
pl_Bingo_count = Check_Bingo_line(pl_star_ix, 5);
AI_Bingo_count = Check_Bingo_line(AI_star_ix, 5);
switch (ai_mode)
{
case AI_EASY:
cout << "Easy mode" << endl;
break;
case AI_HARD:
cout << "Hard mode" << endl;
break;
}
//Player
cout << "============ Player ==============" << endl;
Print(plnums,pl_star_ix, 5, 5);
cout << "Bingo Line: " << pl_Bingo_count << endl<<endl;
//AI
cout << "============== AI ================" << endl;
PrintStar();
//Print(aiNums, AI_star_ix, 5, 5);
cout << "Bingo Line: " << AI_Bingo_count << endl;
//Check Bingo Line
if (pl_Bingo_count >= 5 && AI_Bingo_count >= 5)
{
if (pl_Bingo_count > AI_Bingo_count)
{
PrintPlWin();
break;
}
else if (pl_Bingo_count < AI_Bingo_count)
{
PrintAIWIn();
break;
}
else
{
cout << "Draw!!" << endl;
break;
}
}
else if (pl_Bingo_count >= 5)
{
PrintPlWin();
break;
}
else if (AI_Bingo_count >= 5)
{
PrintAIWIn();
break;
}
// play
if (play_turn)
{
cout << "Your turn. Input a number(1~25, 0: end game): ";
cin >> input;
if (input > size || input < 0)
{
cout << "Wrong input. Press any key to input again.";
char ch = _getch();
continue;
}
else if (input == 0)
break;
//중복 검사
bool try_again = false;
for (int i = 0; i < size; ++i)
{
if (plnums[i] == input && pl_star_ix[i] == true)
{
cout << "This number was already chosen. try again.";
char ch = _getch();
try_again = true;
}
}
if (try_again) continue;
}
else
{
switch(ai_mode)
{
case AI_EASY:
/*
이지모드는 별이 아닌 숫자중 하나중
아무거나 하나 랜덤으로 뽑음
*/
bool flag;
flag=true;
while (flag)
{
input = rand() % 25 + 1;
// 이미 언급한 숫자인지 검사
for (int i = 0; i < 25; ++i)
{
if (aiNums[i] == input)
{
if (AI_star_ix[i] != true)
{
flag = false;
break;
}
else break;
}
}
}
break;
case AI_HARD:
// 하드모드는 현재 숫자중 빙고줄 완성
// 가능성이 가장 높은 줄을 찾아서
// 그 줄에 있는 숫자중 하나를 *로 만듦
int star_count[LN_RT+1]={};
CountAiStar(AI_star_ix, star_count);
// 개수 비교
int max_ix=0;
for (int i = 1; i < LN_RT; ++i)
{
if (star_count[i] == LN_V1)
continue;
if (star_count[i] == LN_H5)
{
max_ix = i;
break;
}
if (star_count[max_ix] < star_count[i])
max_ix = i;
}
while (true)
{
bool flag = false;
int ix = rand() % 5;
switch (max_ix)
{
case LN_H1:
case LN_H2:
case LN_H3:
case LN_H4:
case LN_H5:
int row_ix;
row_ix = 5 * max_ix + ix;
if (AI_star_ix[row_ix] != true)
{
AI_star_ix[row_ix] = true;
input = aiNums[row_ix];
flag = true;
}
break;
case LN_V1:
case LN_V2:
case LN_V3:
case LN_V4:
case LN_V5:
int col_ix;
col_ix = 5 * ix + (max_ix - 5);
if (AI_star_ix[col_ix] != true)
{
AI_star_ix[col_ix] = true;
input = aiNums[col_ix];
flag = true;
}
break;
case LN_LT:
int left_diag;
left_diag = 6 * ix;
if (AI_star_ix[left_diag] != true)
{
AI_star_ix[left_diag] = true;
input = aiNums[left_diag];
flag = true;
}
break;
case LN_RT:
int right_diag;
right_diag = 4 * (ix + 1);
if (AI_star_ix[right_diag] != true)
{
AI_star_ix[right_diag] = true;
input = aiNums[right_diag];
flag = true;
}
break;
}
if (flag)
break;
}
}
}
// find pl number
FindNum(plnums, pl_star_ix, size, input);
// find AI number
FindNum(aiNums, AI_star_ix, size, input);
// turn change
play_turn = !play_turn;
}
cout << "End the game" << endl;
return 0;
}
헤더:
#pragma once
#include <iostream>
enum AI_MODE
{
AI_EASY = 1,
AI_HARD,
};
enum LINE_NUMBER
{
LN_H1,
LN_H2,
LN_H3,
LN_H4,
LN_H5,
LN_V1,
LN_V2,
LN_V3,
LN_V4,
LN_V5,
LN_LT,
LN_RT,
};
void Print(int* nums, bool* star_ix, int row, int col)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
int ix_num = 5 * i + j;
if (star_ix[ix_num] == true)
std::cout << "*\t";
else
std::cout << nums[ix_num] << "\t";
}
std::cout << std::endl;
}
}
int Check_Bingo_line(bool* star_ix, int n)
{
int Bingo_line = 0;
// Check row,col Bingo line
for (int i = 0; i < n; i++)
{
bool row_bingo = true;
bool col_bingo = true;
for (int j = 0; j < n; ++j)
{
if (star_ix[n * i + j] != true)
row_bingo = false;
if (star_ix[n * j + i] != true)
col_bingo = false;
}
if (row_bingo && col_bingo)
Bingo_line += 2;
else if (row_bingo || col_bingo)
Bingo_line++;
}
// Check cross Bingo line
bool cross_bingo1 = true; // left
bool cross_bingo2 = true; // right
for (int i = 0; i < n; ++i)
{
if (star_ix[6 * i] != true)
cross_bingo1 = false;
if (star_ix[4 * (i + 1)] != true)
cross_bingo2 = false;
if (cross_bingo1 != true && cross_bingo2 != true)
break;
}
if (cross_bingo1) Bingo_line++;
if (cross_bingo2) Bingo_line++;
return Bingo_line;
}
void FindNum(int* nums, bool* star_ix, int size, int input)
{
for (int i = 0; i < size; ++i)
{
if (nums[i] == input)
{
star_ix[i] = true;
break;
}
}
}
void PrintPlWin()
{
std::cout << "Player win!!!" << std::endl;
}
void PrintAIWIn()
{
std::cout << "AI win!!!" << std::endl;
}
void PrintStar()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; ++j)
{
std::cout << "*\t";
}
std::cout << std::endl;
}
}
void CountAiStar(bool* ai_star, int* star_count)
{
const int size = 5;
// 모든 라인별 별 개수 조사
for (int i = 0; i < LN_RT + 1; ++i)
{
int count = 0;
if (i <= LN_H5)
for (int j = 0; j < size; ++j)
(ai_star[size * i + j] == true) ? count++ : count;
else if (i <= LN_V5)
for (int j = 0; j < size; ++j)
(ai_star[size * j + (i - size)] == true) ? count++ : count;
else if (i <= LN_LT)
for (int j = 0; j < size; ++j)
(ai_star[6 * j] == true) ? count++ : count;
else
for (int j = 0; j < size; ++j)
(ai_star[4 * (j + 1)] == true) ? count++ : count;
star_count[i] = count;
}
}
'C++' 카테고리의 다른 글
| 간단한 마리오 게임 (0) | 2024.10.25 |
|---|---|
| 간단한 미로탈출 게임 (2) | 2024.07.15 |
| TEXT RPG 완료 (0) | 2024.07.02 |
| TEXT RPG 중간단계 (0) | 2024.07.01 |
| 간단한 책 대여 프로그램 (0) | 2024.06.25 |