CPP 윷놀이 코드 질문

CPP 윷놀이 코드 질문

작성일 2024.03.19댓글 0건
    게시물 수정 , 삭제는 로그인 필요

윷놀이 간단하게 이동만 하는 코드를 짜는데 첫 번째 이동은 되는데, 두 번째 이동이 전혀 안 되네요...
뭐가 문제인지 알려주시면 좋겠습니다!

#include <iostream>
#include <random>
#include <string>
#include <algorithm>
#include <limits>

using namespace std;

random_device rd;
default_random_engine dre(rd());

class Jut {
public:
Jut() {
initializeBoard();
printBoard();
}

void printMenu();
void run();
char getInput();


private:
static const int max = 7;
char jut[max][max]; // 판, 세로 가로
char koma1 = 'A'; // 플레이어 1
char koma2 = 'B'; // 플레이어 2
int posX1 = 0, posY1 = 6; // 1의 좌표
int posX2 = 0, posY2 = 6; // 2의 좌표
int idou = 0; // 이동할 수 있는 거리
int aStarted = 0;
int bStarted = 0; // A, B 시작 여부
void initializeBoard(); // 보드 초기화

void printBoard() const{ // 보드 출력
for (int i = 0; i < max; ++i) {
for (int j = 0; j < max; ++j) {
cout << jut[i][j] << ' ';
}
cout << '\n';
}
cout << '\n';
}

void rollJut();

void move(char koma, int xpos, int ypos);
};

void Jut::initializeBoard()
{
for (int i = 0; i < max; ++i) {
for (int j = 0; j < max; ++j) {
jut[i][j] = ' ';

if (i == 0 || i == max - 1) { // 외곽
if (j == 3) continue;
jut[i][j] = 'O';
}
else if (i == 1 || i == max - 2) {
if (j == 3) continue;
if (j == 0 || j == 1 || j == 5 || j == 6)
jut[i][j] = 'O';
}
else if (i == 2 || i == max - 3) {
if (j == 3) continue;
if (j == 0 || j == 2 || j == 4 || j == 6)
jut[i][j] = 'O';
}
else if (i == 3) {
if (j == 3)
jut[i][j] = 'O';
}
}
}
}

void Jut::printMenu()
{
cout << "#########################################################" << '\n';
cout << "- p: 게임 시작하기" << '\n';
cout << "- 1: 첫 번째 플레이어가 윷 던지기" << '\n';
cout << "- 2: 두 번째 플레이어가 윷 던지기" << '\n';
cout << "- wasd: 현재 위치에서 위 / 좌 / 아래 / 우로 말 움직이기" << '\n';
cout << "- ikl: 현재 위치에서 대각선방향으로 말 움직이기" << '\n';
cout << "#########################################################" << '\n';
}

void Jut::run()
{
while (true) {
switch (getInput())
{
case '0':
exit(0);

case '1':
cout << "A의 턴" << '\n';
rollJut();
move(koma1, posX1, posY1);
break;

case '2':
cout << "B의 턴" << '\n';
rollJut();
move(koma2, posX2, posY2);
break;

default:
break;
}
//printBoard();
}
}

char Jut::getInput()
{
char input;
cin >> input;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return input;
}

void Jut::rollJut()
{
uniform_int_distribution uid(0, 4);

int dice = uid(dre);

switch (dice) {
case 0:
cout << "도: ";
idou = 1;
break;

case 1:
cout << "개: ";
idou = 2;
break;

case 2:
cout << "걸: ";
idou = 3;
break;

case 3:
cout << "윷: ";
idou = 4;
break;

case 4:
cout << "모: ";
idou = 5;
break;
}
cout << idou << "칸 이동" << '\n';
}

void Jut::move(char koma, int xpos, int ypos)
{
if (koma == koma1 && aStarted == 0) {
aStarted++; // A 시작
jut[6][0] = koma1; // 배치
}
else if (koma == koma2 && bStarted == 0) {
bStarted++; // B 시작
jut[6][0] = koma2; // 배치
}

while (idou > 0) { // 이동 가능한 만큼
switch (getInput())
{
case 'w':
if (xpos == 6 && ypos < 0) {
int newpos = ypos + 1;

if (idou == 1 && jut[newpos][xpos] != 'O' && jut[newpos][xpos] != ' ') {
char target = (koma == koma1) ? koma2 : koma1;
cout << target << "잡음" << '\n';
swap(jut[6][0], jut[newpos][xpos]);
}

if (ypos == 4) {
newpos--;
swap(jut[ypos][xpos], jut[newpos][xpos]);
idou--;
ypos -= 2;
}
else {
swap(jut[ypos][xpos], jut[newpos][xpos]);
idou--;
ypos--;
}
}
break;

case 'a':
if (ypos == 0 && xpos > 0) {
int newpos = xpos - 1;

if (idou == 1 && jut[ypos][newpos] != 'O') {
char target = (koma == koma1) ? koma2 : koma1;
cout << target << "잡음" << '\n';
swap(jut[6][0], jut[ypos][newpos]);
}

if (xpos == 2) {
newpos--;
swap(jut[ypos][xpos], jut[ypos][newpos]);
ypos += 2;
idou--;
}
else {
swap(jut[ypos][xpos], jut[ypos][newpos]);
xpos--;
idou--;
}
}
break;

case 's':
if (xpos == 0 && ypos < 6) {
int newpos = ypos + 1;

if (idou == 1 && jut[newpos][xpos] != 'O') {
char target = (koma == koma1) ? koma2 : koma1;
cout << target << "잡음" << '\n';
swap(jut[6][0], jut[newpos][xpos]);
}

if (ypos == 2) {
newpos++;
swap(jut[ypos][xpos], jut[newpos][xpos]);
ypos += 2;
idou--;
}
else {
swap(jut[ypos][xpos], jut[newpos][xpos]);
ypos++;
idou--;
}
}
break;

case 'd':
if (ypos == 6 && xpos < 6) { // 하단
int newpos = xpos + 1;

if (idou == 1 && jut[ypos][newpos] != 'O' && jut[ypos][newpos] != ' ') { // 마지막 이동 위치에 상대 말이 있을 경우
char target = (koma == koma1) ? koma2 : koma1;
cout << target << "잡음" << '\n';
swap(jut[6][0], jut[ypos][newpos]);
}

if (xpos == 2) {
newpos += 1;
swap(jut[ypos][xpos], jut[ypos][newpos]);
idou--;
xpos += 2;
}
else {
swap(jut[ypos][xpos], jut[ypos][newpos]);
idou--;
xpos++;
}
}
break;

case 'i':
if (xpos == 6 && ypos == 6) {
int newXpos = xpos - 1;
int newYpos = ypos - 1;

swap(jut[ypos][xpos], jut[newYpos][newXpos]);
xpos--;
ypos--;
idou--;
}
break;

case 'k':
if ((xpos == 6 && ypos == 0) || (xpos == 3 && ypos == 3)) {
int newXpos = xpos - 1;
int newYpos = ypos + 1;

swap(jut[ypos][xpos], jut[newYpos][newXpos]);
xpos--;
ypos++;
idou--;
}
break;

case 'l':
if (xpos == 0 && ypos == 0) {
int newXpos = xpos + 1;
int newYpos = ypos + 1;

swap(jut[ypos][xpos], jut[newYpos][newXpos]);
xpos++;
ypos++;
idou--;
}
break;

default:
break;
}
printBoard();
}

if ((aStarted > 0 || bStarted > 0) && jut[6][0] == koma) {
cout << "게임 종료 - 플레이어 " << koma << "의 승리!" << '\n';
exit(0);
}
}

int main()
{
Jut jut;
jut.printMenu();

while (true) {
char start;
cin >> start;

if (start == 'p') {
cout << "게임 시작!" << '\n';
jut.run();
}
}
}