< [C++ 더 쉽게 더 깊게 실습과제] 1인용 틱택토 게임

프로그래밍 공부/Jumping into C++

[C++ 더 쉽게 더 깊게 실습과제] 1인용 틱택토 게임

Rocketbabydolls 2020. 11. 11. 20:35
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

void display(int array[][3], int size)
{
    for(int i = 0 ; i < size ; i++)
    {
        for(int j = 0 ; j < size ; j++)
        {
            if (array[i][j] == -1) // -1일때 공백 출력
            {
                cout << " ";
            }
            else if (array[i][j] == 0) // 0일때 O 출력
            {
                cout << "O";
            }
            else if(array[i][j] == 1) // 1일때 X 출력
                cout << "X";

        }

        cout << "\n";
    }

    cout << "---\n";

}
void location_input_man(int t[][3])
{
    int x,y;

    cout << "좌표를 입력해 주세요(가장 왼쪽 위가 (0,0)입니다.) : ";

    //사용자의 입력
    cin >> x >> y;

    if ( t[x][y] == -1 )
        t[x][y] = 0;
    else
    {
        cout << "입력 오류입니다! 빈칸을 고르세요.";
        location_input_man(t);
    }
}

void location_input_com(int t[][3])
{
    int c_x = (rand()%10)/2;
    int c_y = (rand()%10)/2;

        //컴퓨터의 입력, 랜덤함수 사용
    if ( t[c_x][c_y] == -1 )
      t[c_x][c_y] = 1;
    else
    {
        location_input_com(t);
    }
}

int status_hor(int t[][3],int size)
{
 //가로 조건

    for(int i = 0 ; i < size; i++)
    {
        int tmp = t[i][0];

        for(int j = 0; j < size ; j++)
        {
            if( tmp == t[i][j])
            {
                continue;
            }
            else
                return -1;

        }
        return tmp;
    }



}

int status_ver(int t[][3], int size)
{
        //세로조건

    for(int i = 0 ; i < size; i++)
    {
        int tmp = t[0][i];

        for(int j = 0; j < size ; j++)
        {
            if( tmp == t[j][i])
            {
                continue;
            }
            else
                return -1;
        }

        return tmp;
    }


}

int status_dia(int t[][3], int size)
{

    //대각선 조건
    int tmp = t[0][0];

    for (int i = 0 ; i < size ; i++)
    {
        if (tmp == t[i][i])
            continue;
        else
            return -1;
    }
    if(tmp == 1) return 1;
    if(tmp == 0) return 0;
    if(tmp == -1) return -1;



}
int status_dia2(int t[][3], int size)
{

int tmp;

    tmp = t[0][size-1];

    for (int i = 0 ; i < size  ;i++)
    {
        if (tmp == t[i][size - i - 1])
            continue;
        else
            return -1;
    }

        if(tmp == 1) return 1;
        if(tmp == 0) return 0;
        if(tmp == -1) return -1;

}

int game_status(int t[][3], int size)
{
    //게임 승리 조건은 모두 8개, 가로 3, 세로 3, 대각 2
    if (status_hor(t, size) == -1)
    {
        if (status_ver(t, size) == -1)
        {
            if(status_dia(t, size) == -1)
            {
                if(status_dia2(t, size) == -1)
                {
                    return -1;
                }
                else return status_dia2(t,size);

            }
            else status_dia(t, size);
        }
        else return status_ver(t, size);
    }
    else return status_hor(t, size);

}



int main()
{
    //틱택토 게임 크기는 3X3으로 고정
    //1인 사용자의 입력은 O로 통일
    //컴퓨터의 입력은 X로 통일



    int t[3][3]={
        {-1, -1, -1},

        {-1, -1, -1},

        {-1, -1, -1}
        }; //-1로 초기화

    srand(time(NULL));



    cout << "틱택토 게임을 시작합니다.\n";

    display(t, 3);

    int tmp;

    while(true)
    {

    location_input_man(t); //사용자 입력

    tmp = game_status(t, 3);

    location_input_com(t); //컴퓨터 입력

    display(t, 3);

    tmp = game_status(t, 3);

    if (tmp == 1)
    {
        cout << "컴퓨터의 승리입니다.\n";
        break;
    }
    else if (tmp == 0)
    {
        cout << "사용자의 승리입니다.\n";
        break;
    }
    else
    {
        continue;
    }

        cout << "tmp 값 : " << tmp << endl;

    }

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






    return 0;
}#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

void display(int array[][3], int size)
{
    for(int i = 0 ; i < size ; i++)
    {
        for(int j = 0 ; j < size ; j++)
        {
            if (array[i][j] == -1) // -1일때 공백 출력
            {
                cout << " ";
            }
            else if (array[i][j] == 0) // 0일때 O 출력
            {
                cout << "O";
            }
            else if(array[i][j] == 1) // 1일때 X 출력
                cout << "X";

        }

        cout << "\n";
    }

    cout << "---\n";

}
void location_input_man(int t[][3])
{
    int x,y;

    cout << "좌표를 입력해 주세요(가장 왼쪽 위가 (0,0)입니다.) : ";

    //사용자의 입력
    cin >> x >> y;

    if ( t[x][y] == -1 )
        t[x][y] = 0;
    else
    {
        cout << "입력 오류입니다! 빈칸을 고르세요.";
        location_input_man(t);
    }
}

void location_input_com(int t[][3])
{
    int c_x = (rand()%10)%3;
    int c_y = (rand()%10)%3;

        //컴퓨터의 입력, 랜덤함수 사용
    if ( t[c_x][c_y] == -1 )
    {
      t[c_x][c_y] = 1;
      cout << c_x << " " << c_y <<endl;
    }
      else
    {
        cout << "올바른 입력 계산중...\n";
        location_input_com(t);
    }
}

int status_hor(int t[][3],int size)
{
 //가로 조건

    for(int i = 0 ; i < size; i++)
    {
        int tmp = t[i][0];

        for(int j = 0; j < size ; j++)
        {
            if( tmp == t[i][j])
            {
                continue;
            }
            else
                return -1;

        }
        return tmp;
    }



}

int status_ver(int t[][3], int size)
{
        //세로조건

    for(int i = 0 ; i < size; i++)
    {
        int tmp = t[0][i];

        for(int j = 0; j < size ; j++)
        {
            if( tmp == t[j][i])
            {
                continue;
            }
            else
                return -1;
        }

        return tmp;
    }


}

int status_dia(int t[][3], int size)
{

    //대각선 조건
    int tmp = t[0][0];

    for (int i = 0 ; i < size ; i++)
    {
        if (tmp == t[i][i])
            continue;
        else
            return -1;
    }
    if(tmp == 1) return 1;
    if(tmp == 0) return 0;
    if(tmp == -1) return -1;



}
int status_dia2(int t[][3], int size)
{

int tmp;

    tmp = t[0][size-1];

    for (int i = 0 ; i < size  ;i++)
    {
        if (tmp == t[i][size - i - 1])
            continue;
        else
            return -1;
    }

        if(tmp == 1) return 1;
        if(tmp == 0) return 0;
        if(tmp == -1) return -1;

}

int game_status(int t[][3], int size)
{
    //게임 승리 조건은 모두 8개, 가로 3, 세로 3, 대각 2
    if (status_hor(t, size) == -1)
    {
        if (status_ver(t, size) == -1)
        {
            if(status_dia(t, size) == -1)
            {
                if(status_dia2(t, size) == -1)
                {
                    return -1;
                }
                else return status_dia2(t,size);

            }
            else status_dia(t, size);
        }
        else return status_ver(t, size);
    }
    else return status_hor(t, size);

}




int main()
{
    //틱택토 게임 크기는 3X3으로 고정
    //1인 사용자의 입력은 O로 통일z
    //컴퓨터의 입력은 X로 통일



    int t[3][3]={
        {-1, -1, -1},

        {-1, -1, -1},

        {-1, -1, -1}
        }; //-1로 초기화

    srand(time(NULL));



    cout << "틱택토 게임을 시작합니다.\n";

    display(t, 3);

    int tmp;


    int i;

   for(i = 0 ; i < 9; i++){

    location_input_man(t); //사용자 입력


    tmp = game_status(t, 3);

       if (tmp == 1)
    {
        cout << "컴퓨터의 승리입니다.\n";
        break;
    }
    else if (tmp == 0)
    {
        cout << "사용자의 승리입니다.\n";
        break;
    }

    location_input_com(t); //컴퓨터 입력

    display(t, 3);

    tmp = game_status(t, 3);


    if (tmp == 1)
    {
        cout << "컴퓨터의 승리입니다.\n";
        break;
    }
    else if (tmp == 0)
    {
        cout << "사용자의 승리입니다.\n";
        break;
    }
    else
    {
        continue;
    }

    }

    if (i == 9 && tmp == -1)
    {
        cout << "무승부입니다."<<endl;
    }

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






    return 0;
}