linux控制臺(tái)單人五子棋

五子棋

getch()函數(shù)linux,沒(méi)有,所以前面從網(wǎng)上找了一個(gè)替代實(shí)現(xiàn)。廢話不多說(shuō),直接上截圖 !


五子棋.png

####五子棋源代碼

#pragma once

/*
常用的ANSI控制碼如下(有些不支持):
\033[0m 關(guān)閉所有屬性
\033[1m 高亮
\033[2m 亮度減半
\033[3m 斜體
\033[4m 下劃線
\033[5m 閃爍
\033[6m 快閃
\033[7m 反顯
\033[8m 消隱
\033[9m 中間一道橫線
10-19 關(guān)于字體的
21-29 基本與1-9正好相反
30-37 設(shè)置前景色
40-47 設(shè)置背景色
30:黑
31:紅
32:綠
33:黃
34:藍(lán)色
35:紫色
36:深綠
37:白色
38 打開下劃線,設(shè)置默認(rèn)前景色
39 關(guān)閉下劃線,設(shè)置默認(rèn)前景色
40 黑色背景
41 紅色背景
42 綠色背景
43 棕色背景
44 藍(lán)色背景
45 品紅背景
46 孔雀藍(lán)背景
47 白色背景
48 不知道什么東西
49 設(shè)置默認(rèn)背景色
50-89 沒(méi)用
90-109 又是設(shè)置前景背景的,比之前的顏色淺
\033[nA 光標(biāo)上移n行
\033[nB 光標(biāo)下移n行
\033[nC 光標(biāo)右移n行
\033[nD 光標(biāo)左移n行
\033[y;xH設(shè)置光標(biāo)位置
\033[2J 清屏
\033[K 清除從光標(biāo)到行尾的內(nèi)容
\033[s 保存光標(biāo)位置
\033[u 恢復(fù)光標(biāo)位置
\033[?25l 隱藏光標(biāo)
\033[?25h 顯示光標(biāo)
*/

#include <termio.h>

/*
 * 得到用戶輸入的一個(gè)字符
 *        : 返回得到字符
 */
int getch(void) {
    int cr;
    struct termios nts, ots;

    if (tcgetattr(0, &ots) < 0) // 得到當(dāng)前終端(0表示標(biāo)準(zhǔn)輸入)的設(shè)置
        return EOF;

    nts = ots;
    cfmakeraw(&nts); // 設(shè)置終端為Raw原始模式,該模式下所有的輸入數(shù)據(jù)以字節(jié)為單位被處理
    if (tcsetattr(0, TCSANOW, &nts) < 0) // 設(shè)置上更改之后的設(shè)置
        return EOF;

    cr = getchar();
    if (tcsetattr(0, TCSANOW, &ots) < 0) // 設(shè)置還原成老的模式
        return EOF;

    return cr;
}

#include <iostream>
#include <chrono>
#include <vector>
#include <functional>
#include <sstream>

using namespace std;

namespace LB
{

    class GoBang
    {
    public:
        enum color
        {
            black=1,
            white=2
        };
        static void start_play()
        {
            pair<uint32_t,uint32_t> position=make_pair(7,7);
            color c=black;
            GoBang gobang;
            gobang.clear_chessBoard();

            for (char input;;)
            {
                gobang.show(position,c);
                input=getch();
                switch(input)
                {
                case 'w':
                    if(position.first>0)
                        position.first-=1;
                    break;
                case 'a':
                    if(position.second>0)
                        position.second-=1;
                    break;
                case 's':
                    if(position.first<gobang.chess_board.size()-1)
                        position.first+=1;
                    break;
                case 'd':
                    if(position.second<gobang.chess_board.size()-1)
                        position.second+=1;
                    break;
                case ' ':
                    if(gobang.add_chess(position.first,position.second,c))
                    {
                        auto result=gobang.check(position.first,position.second);
                        switch(result)
                        {
                        case 0:
                            if(c==white)
                            {
                                c=black;
                                break;
                            }
                            if(c==black)
                                c=white;
                            break;
                        case GoBang::white:
                            cout<<"\033[31mwhite win \033[0m"<<endl;
                            return;
                            break;
                        case GoBang::black:
                            cout<<"\033[31mblack win \033[0m"<<endl;
                            return;
                            break;
                        }
                    }
                    break;
                case 'q':
                    return;
                default:
                    continue;
                }


            }
        }
    protected:
        stringstream output;
        vector<pair<uint32_t,uint32_t>> steps;
        std::vector< vector<uint8_t> > chess_board;
        function<void(pair<uint32_t,uint32_t>,color c)> show=[this](pair<uint32_t,uint32_t> position,color c)
        {
            output.str("");
            // clear the format
            // hight light
            // blue title
            output<<"\033[0m\033[1m\033[32mWelcome to play GoBang\n";
            output<<"\033[0m\033[1m\033[34mw:move up\na:move left\ns:move down\nd:move right\nspace:put\nq:quit\n";
            for(auto& row:chess_board)
            {
                for(auto& each:row)
                {

                    output<<"\033[0m";
                    if(&each==&chess_board[position.first][position.second])
                    {
                        if(c==GoBang::white)
                            output<<"\033[37m";
                        else
                            output<<"\033[30m";
                        if(each==0)
                            output<<"\033[43m";   // ██
                        if(static_cast<color>(each)==GoBang::black)
                            output<<"\033[40m";
                        if(static_cast<color>(each)==GoBang::white)
                            output<<"\033[47m";
                        output<<"\033[2m? ";
                        continue;
                    }
                    if(each==0)
                    {
                        output<<"\033[43m  ";   // ██
                        continue;
                    }
                    if(static_cast<color>(each)==GoBang::black)
                    {
                        output<<"\033[40m  ";
                        continue;
                    }
                    if(static_cast<color>(each)==GoBang::white)
                    {
                        output<<"\033[47m  ";
                        continue;
                    }


                }
                output<<"\033[0m\n";
            }
            cout<<output.str()<<std::endl;
        };

        void clear_chessBoard(uint32_t size=15)
        {
            chess_board.resize(0);
            chess_board.resize(size);
            for(auto&row : chess_board)
            {
                row.resize(size);
                for(auto&column : row)
                    column=0;
            }
            steps.resize(size*size);
            steps.resize(0);
        }

        uint8_t check(uint32_t row,uint32_t column)
        {
            if(row+1>chess_board.size()||column+1>chess_board[row].size())
                return 0;
            auto& target=chess_board[row][column];
            if(target==0)
                return 0;
            static int total;
            total =1;
            // check the line (-)
            for(int r=row,c=column-1;c>=0&&chess_board[r][c]==target;c--)
                total+=1;
            for(int r=row,c=column+1;c<chess_board.size()&&chess_board[r][c]==target;c++)
                total+=1;
            if(total>=5)
                return target;
            // check the column (|)
            total =1 ;
            for(int r=row-1,c=column;r>=0&&chess_board[r][c]==target;r--)
                total+=1;
            for(int r=row+1,c=column;r<chess_board.size()&&chess_board[r][c]==target;r++)
                total+=1;
            if(total>=5)
                return target;
            // check the right up && left down (/)
            total =1 ;
            for(int r=row-1,c=column+1;r>=0&&c<chess_board.size()&&chess_board[r][c]==target;r--,c++)
                total+=1;
            for(int r=row+1,c=column-1;r<chess_board.size()&&c>=0&&chess_board[r][c]==target;r++,c--)
                total+=1;
            if(total>=5)
                return target;
            // check the left up && right down (\)
            total =1 ;
            for(int r=row-1,c=column-1;r>=0&&c>=0&&chess_board[r][c]==target;r--,c--)
                total+=1;
            for(int r=row+1,c=column+1;r<chess_board.size()&&c<chess_board.size()&&chess_board[r][c]==target;r++,c++)
                total+=1;
            if(total>=5)
                return target;
            // no body win
            return 0;
        }
        bool add_chess(uint32_t r,uint32_t c,color player)
        {
            if(chess_board.size()<=r)
                return false;
            if(chess_board[r].size()<=c)
                return false;
            if(chess_board[r][c]!=0)
                return false;
            chess_board[r][c]=static_cast<uint8_t>(player);
            steps.push_back(make_pair(r,c));
            return true;
        }
    };
}

main函數(shù)調(diào)用

#include <iostream>
#include <vector>
#include <sstream>
#include <funny_code/sudoko.hpp>
#include <utility/timer.hpp>
#include <thread>
#include <funny_code/chess_add.hpp>
#include <stdlib.h>
using namespace std;
using namespace LB;

int main()
{
    GoBang::start_play();
    return 0;
}

我用steps存了每一步,你們可以自行添加悔棋功能。

  • 小功能封裝好,GoBang類里面提供很全的小功能。
  • 把大功能在GoBang的靜態(tài)函數(shù)start_play()里面通過(guò)調(diào)用小功能來(lái)集成實(shí)現(xiàn)。
  • 界面繪制要獨(dú)立出來(lái),要不然很亂。最好是邏輯跟界面分開吧,界面就用show單獨(dú)來(lái)畫。

其實(shí)沒(méi)什么吊難度,就是繪制花了點(diǎn)功夫。
看看以后能不能用vulkan繪制出來(lái),然后加個(gè)網(wǎng)絡(luò)對(duì)戰(zhàn)還有AI?
那都是后話了,看心情。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容