一担挑游戏

#include <iostream>
#include <iomanip>
using namespace std;

int chessboard[3][3];
int scan()    //判断结局
{
    int i=0,j=0;
    //横成一条
        for(i=0;i<3;i++)
        {
            if((chessboard[i][0]==chessboard[i][1])&&(chessboard[i][0]==chessboard[i][2])
                &&(chessboard[i][0]!=100))
            {
                if(chessboard[i][0]==0)
                {cout<<setw(15)<<setfill(' ')<<' '<<"You lose!"<<endl;}
                else {cout<<setw(15)<<setfill(' ')<<' '<<"You win!"<<endl;}
                return 0;
            }
        }
    //竖成一条
        for(i=0;i<3;i++)
        {
            if(chessboard[0][i]==chessboard[1][i]&&chessboard[0][i]==chessboard[2][i]
                &&chessboard[0][i]!=100)
            {
                if(chessboard[0][i]==0)
                {cout<<setw(15)<<setfill(' ')<<' '<<"You lose!"<<endl;}
                else {cout<<setw(15)<<setfill(' ')<<' '<<"You win!"<<endl;}
                return 0;
            }
        }
    //斜成一条
    if(chessboard[0][0]==chessboard[1][1]&&chessboard[0][0]==chessboard[2][2]
        &&chessboard[0][0]!=100)
    {
        if(chessboard[0][0]==0)
        {cout<<setw(15)<<setfill(' ')<<' '<<"You lose!"<<endl;}
        else {cout<<setw(15)<<setfill(' ')<<' '<<"You win!"<<endl;}
        return 0;
    }
     if(chessboard[0][2]==chessboard[1][1]&&chessboard[0][2]==chessboard[2][0]
        &&chessboard[0][2]!=100)
    {
        if(chessboard[0][2]==0)
        {cout<<setw(15)<<setfill(' ')<<' '<<"You lose!"<<endl;}
        else {cout<<setw(15)<<setfill(' ')<<' '<<"You win!"<<endl;}
        return 0;
    }
    //平局
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {if(chessboard[i][j]==100)return 1;}
    }
    cout<<setw(15)<<setfill(' ')<<"draw!"<<endl;
    return 0;

}
int next()  //计算机下一步,
{
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            if(chessboard[i][j]==100)
            {
                chessboard[i][j]=0;
                return 0;
            }
           
        }
    }
    return 0;
}
void print()    //打印状态
{
    int i=0,j=0;
    for(i=0;i<3;i++)
    {
        cout<<setw(25)<<setfill(' ')<<' ';
        for(j=0;j<3;j++)
        {
            switch(chessboard[i][j])
            {
            case 0: cout<<"  O";break;
            case 1: cout<<"  X";break;
            case 100: cout<<"  _";break;
            default: cout<<"error!"<<endl;
            }
        }
        cout<<endl;
    }
    cout<<endl;
}
void init()        //初始化
{
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            chessboard[i][j]=100;
}

int main()
{
    int x,y;
    init();
    while(1)
    {
        cout<<setw(15)<<setfill(' ')<<' '<<setw(50)<<setfill('*')<<' '<<endl;
        cout<<setw(20)<<setfill(' ')<<' ';
        cout<<"enter the position:";
        cin>>x>>y;
        if(x>2||y>2)
        {
            cout<<setw(15)<<setfill(' ')<<"ERROR!"<<endl;
            return 0;
        }
        cout<<endl;
        chessboard[x][y]=1;
        if(!scan())break;
        next();
        if(!scan())break;
        print();
    }
    print();
}
注释:这是最幼稚的一种写法,在后面会给出更出色的算法。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值