zoukankan      html  css  js  c++  java
  • 简易五子棋 V1.1.0

    main.cpp

    #include "fivechess.cpp"
    
    int main()
    {
        fivechess a;
        a.RunGame();
        getchar();
        return 0;
    }
    



    fivechess.h

    #include <conio.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    using namespace std;
    const int MAX_SIZE=15;
    
    struct point
    {
        int cursor_x,cursor_y;
    };
    void gotoxy(int x,int y)//位置函数
    {
        COORD pos;
        pos.X=x;
        pos.Y=y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
    }
    void color(int a)//颜色函数
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
    }
    
    class fivechess
    {
    public:
        void RunGame();
    private:
        void init();
        void print_chess();
        string getstyle(int ,int );
        bool move_cursor();
        int who_win(int x,int y); // 返回 0代表没有人胜利,如果为1 则黑子胜利,2则为白棋;
        void change_chessboard();
        void change_chess();
        void show_winnwer();
        int chessboard[MAX_SIZE][MAX_SIZE];
        int count;
        int winner;
        point now,pre;
        char opt;
    };
    



    fivechess.cpp

    #include "fivechess.h"
    void fivechess::RunGame()
    {
        init();
        system("title 简易五子棋 ——ACM制作");//设置标题
        system("mode con cols=63 lines=33");//设置窗口大小
        system("color E0");//设置颜色
        print_chess();
        while(1)
        {
            if(move_cursor())
            {
                change_chess();
                winner=who_win(now.cursor_x,now.cursor_y);
            }
            change_chessboard();
            pre.cursor_x=now.cursor_x,pre.cursor_y=now.cursor_y;
            if(winner)
            {
                show_winnwer();
                break;
            }
        }
    }
    /********************************************/
    void fivechess::change_chess()
    {
        int x,y;
        x=now.cursor_x*2+1,y=now.cursor_y*2+1;
        count++;
        chessboard[now.cursor_x][now.cursor_y]=!(count%2)+1;
        gotoxy(y*2,x);
        if(count%2==1) cout<<"●";
        else cout<<"○";
        gotoxy(0,30);
    
    }
    void fivechess::show_winnwer()
    {
        gotoxy(25,14);
        if(winner==1)cout<<"黑棋获胜";
        else if(winner==2)  cout<<"白棋获胜";
        else cout<<" 平  局 ";
        gotoxy(0,30);
    }
    void fivechess::change_chessboard()
    {
        int x,y;
        int xx,yy;
        xx=pre.cursor_x*2+1,yy=pre.cursor_y*2+1;
        x=now.cursor_x*2+1,y=now.cursor_y*2+1;
        gotoxy(yy*2-2,xx-1);
        cout<<"  ";
        gotoxy(yy*2+2,xx-1);
        cout<<"  ";
        gotoxy(yy*2-2,xx+1);
        cout<<"  ";
        gotoxy(yy*2+2,xx+1);
        cout<<"  ";
    
        gotoxy(y*2-2,x-1);
        cout<<"┏";
        gotoxy(y*2+2,x-1);
        cout<<"┓";
        gotoxy(y*2-2,x+1);
        cout<<"┗";
        gotoxy(y*2+2,x+1);
        cout<<"┘";
        gotoxy(0,30);
    }
    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) return 3;
            return 0;
        }
    }
    
    bool fivechess::move_cursor()  // 输入操作
    {
        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=='~') exit(0);
        else if(opt=='5')
        {
            if(chessboard[now.cursor_x][now.cursor_y]==0) return true;
            else return false;
        }
        return false;
    }
    void fivechess::init()  // 初始化所有信息
    {
        int i,j;
        for(i=0; i<MAX_SIZE; i++)
        {
            for(j=0; j<MAX_SIZE; j++)
                chessboard[i][j]=0;
        }
        count=0;
        winner=0;
        now.cursor_x=now.cursor_y=7;
        pre.cursor_x=pre.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==MAX_SIZE-1&&j==0)
            return "┓";
        else if(i==MAX_SIZE-1&&j==MAX_SIZE-1)
            return "┛";
        else if(i==0&&j==MAX_SIZE-1)
            return "┗";
        else if(i==0)
            return "┣";
        else if(i==MAX_SIZE-1)
            return "┫";
        else if(j==0)
            return "┯";
        else if(j==MAX_SIZE-1)
            return "┷";
        return "╋";//中间的空位
    }
    void fivechess::print_chess()
    {
        system("cls");
        int MAXSIZE=MAX_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%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==MAX_SIZE*2||j==0||j==MAX_SIZE*2) cout<<"  ";
                else if(i%2==1&&j%2==0&&i!=0&&i!=MAX_SIZE*2&&j!=0&&j!=MAX_SIZE*2)
                {
                    cout<<"━";
                }
                else if(i%2==0&&j%2==1&&i!=0&&i!=MAX_SIZE*2&&j!=0&&j!=MAX_SIZE*2)
                {
                    cout<<"│";
                }
                else cout<<"  ";
            }
            printf("
    ");
        }
    }
    



  • 相关阅读:
    Rust Book 学习记录(Nightly)
    测试
    Web前端研发工程师编程能力飞升之路
    Nginx 部署 Django
    数组组合排列及排序
    nodeJS(windows)新手攻略
    虎说:简约不简单-瀑布流布局
    虎说:bootstrap源码解读(重置模块)
    捉Bug:易车注册页布局小臭虫 附demo
    虎扯:小众玩物 webkit家的滚动条
  • 原文地址:https://www.cnblogs.com/coded-ream/p/7207988.html
Copyright © 2011-2022 走看看