井字棋游戏C++

井字棋,英文名叫Tic-Tac-Toe,是一种在3*3格子上进行的连珠游戏,和五子棋类似,由于棋盘一般不画边框,格线排成井字故得名。游戏需要的工具仅为纸和笔,然后由分别代表O和X的两个游戏者轮流在格子里留下标记(一般来说先手者为X),任意三个标记形成一条直线,则为获胜。
编写程序,实现简单的井字棋游戏。基本功能包括:先手选择(计算机或玩家先走)、打印棋盘、输入坐标来落子、判断输赢或平局、有棋子的位置不能落子等。

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<stdlib.h>
using namespace std;
void insepct(int a[3][3])
{
    if(a[0][0]==a[0][1]&&a[0][0]==a[0][2]&&a[0][0]==1)
    {
        cout<<"YOU WIN"<<endl;
        exit(0);
    }
    if(a[1][0]==a[1][1]&&a[1][0]==a[1][2]&&a[1][0]==1)
    {
        cout<<"YOU WIN"<<endl;
        exit(0);
    }
    if(a[2][0]==a[2][1]&&a[2][0]==a[2][2]&&a[2][0]==1)
    {
        cout<<"YOU WIN"<<endl;
        exit(0);
    }
    if(a[0][0]==a[1][0]&&a[0][0]==a[2][0]&&a[0][0]==1)
    {
        cout<<"YOU WIN"<<endl;
        exit(0);
    }
    if(a[0][1]==a[1][1]&&a[0][1]==a[2][1]&&a[0][1]==1)
    {
        cout<<"YOU WIN"<<endl;
        exit(0);
    }
    if(a[0][2]==a[1][2]&&a[0][2]==a[2][2]&&a[0][2]==1)
    {
        cout<<"YOU WIN"<<endl;
        exit(0);
    }
    if(a[0][0]==a[1][1]&&a[0][0]==a[2][2]&&a[0][0]==1)
    {
        cout<<"YOU WIN"<<endl;
        exit(0);
    }
    if(a[0][2]==a[1][1]&&a[0][2]==a[2][0]&&a[0][2]==1)
    {
        cout<<"YOU WIN"<<endl;
        exit(0);
    }
    if(a[0][0]==a[0][1]&&a[0][0]==a[0][2]&&a[0][0]==2)
    {
        cout<<"YOU FAIL"<<endl;
        exit(0);
    }
    if(a[1][0]==a[1][1]&&a[1][0]==a[1][2]&&a[1][0]==2)
    {
        cout<<"YOU FAIL"<<endl;
        exit(0);
    }
    if(a[2][0]==a[2][1]&&a[2][0]==a[2][2]&&a[2][0]==2)
    {
        cout<<"YOU FAIL"<<endl;
        exit(0);
    }
    if(a[0][0]==a[1][0]&&a[0][0]==a[2][0]&&a[0][0]==2)
    {
        cout<<"YOU FAIL"<<endl;
        exit(0);
    }
    if(a[0][1]==a[1][1]&&a[0][1]==a[2][1]&&a[0][1]==2)
    {
        cout<<"YOU FAIL"<<endl;
        exit(0);
    }
    if(a[0][2]==a[1][2]&&a[0][2]==a[2][2]&&a[0][2]==2)
    {
        cout<<"YOU FAIL"<<endl;
        exit(0);
    }
    if(a[0][0]==a[1][1]&&a[0][0]==a[2][2]&&a[0][0]==2)
    {
        cout<<"YOU FAIL"<<endl;
        exit(0);
    }
    if(a[0][2]==a[1][1]&&a[0][2]==a[2][0]&&a[0][2]==2)
    {
        cout<<"YOU FAIL"<<endl;
        exit(0);
    }
}
void output(int a[3][3])
{
    for (int x = 0;x < 3;x++)
    {
        cout << "-------" << endl;
        cout << "|";
        for (int y = 0;y < 3;y++)
        {
            if (a[x][y] == 0)
            {
                cout << " ";
            }
            if (a[x][y] == 1)
            {
                cout << "O";
            }
            if (a[x][y] == 2)
            {
                cout << "X";
            }
            cout << "|";
        }
        cout << endl;
    }
    cout << "-------" << endl;
}
void chess()
{
    int b, c, n, x, y, i;
    int a[3][3] = {
    0 }
    ;
    cout << "请选择先手(1 - 电脑  0 - 玩家)" << endl;
    cin >> n;
    cout <<"-------"<<"n"<< "| | | |" << "n" << "-------" << "n" << "| | | |" << "n" << "-------" << "n" << "| | | |" <<"n"<< "-------" << endl;
    if (n == 0)
    {
        for(i=0;i<9;i++)
        {
            cout << "请输入棋子坐标" << endl;
            cin >> b >> c;
            a[b][c] = 1;
            //1为玩家O,2为电脑X
            output(a);
            insepct(a);
            b=rand()%3;
            c=rand()%3;
            while(a[b][c]==1)
            {
                b=rand()%3;
                c=rand()%3;
            }
            a[b][c]=2;
            output(a);
            insepct(a);
        }
    }
    if (n == 1)
    {
        for(i=0;i<9;i++)
        {
            b=rand()%3;
            c=rand()%3;
            while(a[b][c]==1)
            {
                b=rand()%3;
                c=rand()%3;
            }
            a[b][c]=2;
            output(a);
            insepct(a);
            cout<<"请输入棋子坐标"<<endl;
            cin>>b>>c;
            a[b][c]=1;
            output(a);
            insepct(a);
        }
    }
}
int main()
{
    int h, b;
    cout << "********************" << endl;
    cout << "*****1.开始游戏*****" << endl;
    cout << "*****0.结束游戏*****" << endl;
    cout << "********************" << endl;
    srand((unsigned)time(NULL));
    cin >> h;
    if (h == 0)
    {
        return 0;
    }
    if (h == 1)
    {
        chess();
    }
    return 0;
}

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇
下一篇>>