dev-C++五子棋

学搜索的时候写了练手的。。


/*
 --------------------------------
 *        c++ code              *
 * by Lytning 2013/08/01 17:50  *
 --------------------------------
*/
#include <iostream>
#include <conio.h>

#define clr system("cls")
#define inside(x,y) ((x>0)&&(y>0)&&(x<16)&&(y<16))

using namespace std;

int pan[16][16],nums[4],x,y,oldx,oldy;
bool isBlack;

void instruction(int i)
{
    if(i==1) 
    {
        cout<<"Dev-C++ 五子棋 - Welcome"<<endl; 
        cout<<"---------------------------"<<endl;
        cout<<"· 该程序为双人五子棋,无人机对战"<<endl; 
        cout<<"· 输入“S”开始游戏"<<endl;
        cout<<"· 需要指令控制游戏,输入“?”获取帮助"<<endl;
        cout<<"                    powered by Lytning"<<endl;
    }
    if(i==2) 
    {
        cout<<"---------------------------"<<endl;
        cout<<"Dev-C++ 五子棋 - 指令"<<endl;
        cout<<"---------------------------"<<endl;
        cout<<"由于是控制台程序,需要输入指令控制游戏"<<endl;
        cout<<"指令列表:"<<endl;
        cout<<"·N(空格)x(空格)y   :将棋子下在(x,y)位置。"<<endl;
        cout<<"棋盘中坐标以16进制显示,输入时请输入10进制坐标。(A=10,F=15)"<<endl;
        cout<<"·A(空格)指令(空格)n:将棋子下在当前棋子附近n个单位"<<endl;
        cout<<"指令: R-右侧  L-左侧  U-上方  D-下方"<<endl;
        cout<<"RD-右下 RU-右上 LD-左下 LU-左上"<<endl; 
        cout<<"例如:A R 1:将棋子下在当前棋子 右侧 1 个单位"<<endl;
        cout<<" A LD 3:将棋子下在当前棋子左下方 3 个单位"<<endl; 
        cout<<"tips:开局时可随意输入一个n为0的指令在棋盘中央下棋"<<endl; 
        cout<<"         例如: A L 0"<<endl; 
    }
}

char makech(int n)
{
    if(n<10)
        return (char)n+48;
    return (char)n+55;
}

void print()
{
    cout<<"   ";
    for(int i=1;i<16;i++)
        cout<<makech(i)<<" ";
    cout<<endl<<"  ------------------------------"<<endl;
    for(int i=1;i<16;i++)
    {
        for(int j=1;j<16;j++)
        {
            if(j==1)
                cout<<makech(i)<<"| ";
            switch(pan[i][j])
            {
                case 9:
                    cout<<"- ";
                    break;
                case 0:
                    cout<<"0 ";
                    break;
                case 1:
                    cout<<"1 ";
                    break;
            }
        }
        cout<<endl;
    }
}

void dfs2(int x,int y,int fx)
{
    int trues=isBlack?1:0;
    if(pan[x][y]==trues)
        {nums[fx]++; }
    else
        {return;}
    switch(fx)
    {
        case 0:
            if(inside(x+1,y))
                {dfs2(x+1,y,0);}
            else
                {return;}
            break;
        case 1:
            if(inside(x,y+1))
                {dfs2(x,y+1,1);}
            else
                {return;}
            break;
        case 2:
            if(inside(x+1,y+1))
                {dfs2(x+1,y+1,2);}
            else
                {return;}
            break;
        case 3:
            if(inside(x+1,y-1))
                {dfs2(x+1,y-1,3);}
            else
                {return;}
            break;
    }
}

void dfs1(int x,int y,int fx)//0 上下 1 左右 2 \  3/ 
{
    int trues=isBlack?1:0;
    switch(fx)
    {
        case 0:
            if((pan[x-1][y]==trues)&&inside(x-1,y))
            {
                dfs1(x-1,y,0);
                return;
            }else
            {
                dfs2(x,y,0);
            }
            break;
        case 1:
            if((pan[x][y-1]==trues)&&inside(x,y-1))
            {
                dfs1(x,y-1,1);
                return;
            }else
            {
                dfs2(x,y,1);
            }
            break;
        case 2:
            if((pan[x-1][y-1]==trues)&&inside(x-1,y-1))
            {
                dfs1(x-1,y-1,2);
                return;
            }else
            {
                dfs2(x,y,2);
            }
            break;
        case 3:
            if((pan[x-1][y+1]==trues)&&inside(x-1,y+1))
            {
                dfs1(x-1,y+1,3);
                return;
            }else
            {
                dfs2(x,y,3);
            }
            break;
    }
}

void resetAll()
{
    for(int i=1;i<16;i++)
        for(int j=1;j<16;j++)
            pan[i][j]=9;
    x=8;
    y=8;
    isBlack=true;
}

int main()
{
    instruction(1);
    resetAll();
    while(1)
    {
        char t;
        cin>>t;
        oldx=x;oldy=y;
        if(t=='N')
        {
            
            cin>>x>>y;
        }
        else if(t=='A')
        {
            string act;
            int num;
            cin>>act>>num;
            if(act=="R")
                y+=num;
            if(act=="L")
                y-=num;
            if(act=="U")
                x-=num;
            if(act=="D")
                x+=num;
            if(act=="RU")
                x-=num,y+=num;
            if(act=="RD")
                x+=num,y+=num;
            if(act=="LU")
                y-=num,x-=num;
            if(act=="LD")
                y-=num,x+=num; 
        }
        else if(t=='S')
        {
            print();
            continue;
        }
        else if(t=='?')
        {
            instruction(2); 
            continue; 
        } 
        else if(t=='i')
        {
            instruction(3);
            continue; 
        } 
        else
        {
            cout<<"未知指令."<<endl;
            continue; 
        }
        if((inside(x,y))&&(pan[x][y]==9) )
        {
            pan[x][y]=(isBlack)?1:0;
            clr;
            print();
            for(int i=0;i<4;i++)
            {
                nums[i]=0;
                dfs1(x,y,i);
                if(nums[i]>=5)
                {
                    string winner=!isBlack?"白棋":"黑棋";
                    cout<<winner<<"获得胜利!"<<endl<<"是否重新开始?(输入Y/N)";
                    string temp;cin>>temp;
                    if(temp=="Y") 
                    {
                        resetAll();
                        continue;
                    }else{break;}
                }
            }
            isBlack=!isBlack;
            continue;
        }else
        {
            cout<<"坐标有误或该处已有棋子。"<<endl;
            x=oldx;y=oldy;
            continue;
        }
    }
    return 0;
}
 


  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值