自己写的五子棋

存一下代码而已:

#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>

using namespace std;
struct point
{
    int cursor_x,cursor_y;
};
class fivechess
{
public:
    void RunGame();
    void init();
    void print_chess();
    string getstyle(int ,int );
    bool move_cursor();
    int who_win(int x,int y); // 返回 0代表没有人胜利,如果为1 则黑子胜利,2则为白棋;
private:
    int chessboard[15][15];
    int count;
    int winner;
    point now;
    const int SIZE=15;
};
/*******************************************/
//   五子棋的流程  核心部分

/*******************************************/
void fivechess::RunGame()
{
    init();
    system("title 简易五子棋 ——ACM制作");//设置标题
    system("mode con cols=63 lines=33");//设置窗口大小
    system("color E0");//设置颜色
    print_chess();
    while(1)
    {
        if(move_cursor())
        {
            winner=who_win(now.cursor_x,now.cursor_y);
            printf(" %d",winner);
        }
        print_chess();
        if(winner) break;
    }
}
/********************************************/
int fivechess::who_win(int x,int y)
{
    int i,z28,z46,z19,z73;
    int wanjia=!(count%2)+1;
    z28=z46=z19=z73=1;

    for(i=1; i<5; i++) if(y+1<15&&chessboard[x][y+i]==wanjia) z46++;
        else break; //  横着是否有五子连珠
    for(i=1; i<5; i++) if(y-1>=0&&chessboard[x][y-i]==wanjia) z46++;
        else break;

    for(i=1; i<5; i++) if(x+1<15&&chessboard[x+i][y]==wanjia) z28++;
        else break;
    for(i=1; i<5; i++) if(x-1>=0&&chessboard[x-i][y]==wanjia) z28++;
        else break;

    for(i=1; i<5; i++) if(x+1<15&&y+1<15&&chessboard[x+i][y+i]==wanjia) z73++;
        else break;
    for(i=1; i<5; i++) if(x-1>=0&&y-1>=0&&chessboard[x-i][y-i]==wanjia) z73++;
        else break;

    for(i=1; i<5; i++) if(x+1<15&&y-1>=0&&chessboard[x+i][y-i]==wanjia) z19++;
        else break;
    for(i=1; i<5; i++) if(y+1<15&&x-1>=0&&chessboard[x-i][y+i]==wanjia) z19++;
        else break;

    if(z28>=5||z46>=5||z19>=5||z73>=5) return wanjia;
    else
    {
        if(count==15*15-1) return 3;
        return 0;
    }
}

bool fivechess::move_cursor()  // 输入操作
{
    char opt;
    opt=_getch();
    if(opt=='2'&&now.cursor_x!=14) now.cursor_x++;
    else if(opt=='8'&&now.cursor_x!=0) now.cursor_x--;
    else if(opt=='4'&&now.cursor_y!=0) now.cursor_y--;
    else if(opt=='6'&&now.cursor_y!=14) now.cursor_y++;
    else if(opt=='5')
    {
        if(chessboard[now.cursor_x][now.cursor_y]==0)
        {
            chessboard[now.cursor_x][now.cursor_y]=(count%2)+1;
            count++;
        }
    }
    else if(opt=='~') exit(0);
    return opt=='5';
}
void fivechess::init()  // 初始化所有信息
{
    int i,j;
    for(i=0; i<SIZE; i++)
    {
        for(j=0; j<SIZE; j++)
            chessboard[i][j]=0;
    }
    count=0;
    winner=0;
    now.cursor_x=now.cursor_y=7;
}
string fivechess::getstyle(int i,int j)//获得棋盘中指定坐标交点位置的字符,通过制表符拼成棋盘
{
    if(chessboard[j][i]==1)//1为黑子
        return "●";
    else if(chessboard[j][i]==2)//2为白子
        return "○";
    else if(i==0&&j==0)//以下为边缘棋盘样式
        return "┏";
    else if(i==SIZE-1&&j==0)
        return "┓";
    else if(i==SIZE-1&&j==SIZE-1)
        return "┛";
    else if(i==0&&j==SIZE-1)
        return "┗";
    else if(i==0)
        return "┣";
    else if(i==SIZE-1)
        return "┫";
    else if(j==0)
        return "┯";
    else if(j==SIZE-1)
        return "┷";
    return "╋";//中间的空位
}
void fivechess::print_chess()
{
    system("cls");
    int MAXSIZE=SIZE*2+1;
    int i,j,x,y;
    x=now.cursor_x*2+1,y=now.cursor_y*2+1;
    for(i=0; i<MAXSIZE; i++)
    {
        for(j=0; j<MAXSIZE; j++)
        {
            if(i%2==1&&j%2==1)
            {
                cout<<getstyle(j/2,i/2);
            }
            else if(i==16&&j==13&&winner)
            {
                if(winner==1) cout<<"黑棋获胜";
                else if(winner==2) cout<<"白棋获胜";
                else cout<<" 平  局 ";
                j+=3;
            }
            else if(i%2==0&&j%2==0)
            {
                if(i+1==x&&j+1==y) cout<<"┏";
                else if(i-1==x&&j+1==y) cout<<"└";
                else if(i-1==x&&j-1==y) cout<<"┘";
                else if(i+1==x&&j-1==y) cout<<"┐";
                else cout<<"  ";
            }
            else if(i==0||i==SIZE*2||j==0||j==SIZE*2) cout<<"  ";
            else if(i%2==1&&j%2==0&&i!=0&&i!=SIZE*2&&j!=0&&j!=SIZE*2)
            {
                cout<<"━";
            }
            else if(i%2==0&&j%2==1&&i!=0&&i!=SIZE*2&&j!=0&&j!=SIZE*2)
            {
                cout<<"│";
            }
            else cout<<"  ";
        }
        printf("\n");
    }
}

int main()
{
    fivechess a;
    a.RunGame();
    getchar();
    return 0;
}



posted @ 2016-05-02 16:18  Code-dream  阅读(211)  评论(0编辑  收藏  举报