黑白棋

· · 个人记录

请勿举办谢谢的说。

随手写点小博弈。

需要 windows.h。

规则可以自己去百度。

分数会显示在最后一排,最后一排中间是下一位玩家的颜色,以及显示最后的结果。

结束后按 R 键可以重开,按 E 键可以退出。

因为没有学过怎么写 UI,整个游戏比较简陋。

颜色主题和设置可以自己在代码上段改。

欢迎 Debug。

#include <bits/stdc++.h>
#include <conio.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <windows.h>
#include <algorithm>
#include <random>
using namespace std;

// Please Set Char Size to 8 X 16 !!!

// Basic Settings
// #define UseDefaultColorset
#define UseAnotherColorset
const int WhiteText = ' ';
const int ChessBoradLines = 8;
const int ChessBoradColumns = 8;
const int ScoreBoardWidth = 3;
const int DetectTime = 50; // ms
const int DisableHint = 0;
const int ScreenLines = 2 * ChessBoradLines + 2;
const int ScreenColumns = 4 * ChessBoradColumns + 2;

// Colors
HANDLE nHANDLE = GetStdHandle(STD_OUTPUT_HANDLE);
#define BLACK 0x0   // Empty
#define BLUE 0x1    // FOREGROUND_BLUE
#define GREEN 0x2   // FOREGROUND_GREEN
#define CYAN 0x3    // 0x1 | 0x2
#define RED 0x4     // FOREGROUND_RED
#define PURPLE 0x5    // 0x1 | 0x4
#define YELLOW 0x6  // 0x2 | 0x4
#define WHITE 0x7   // 0x1 | 0x2 | 0x4
#define GREY 0x8    // FOREGROUND_INTENSITY
#define OXBLUE 0x9    // 0x8 | 0x1
#define OXGREEN 0xA   // 0x8 | 0x2
#define OXCYAN 0xB    // 0x8 | 0x1 | 0x2
#define OXRED 0xC     // 0x8 | 0x4
#define OXPINK 0xD    // 0x8 | 0x1 | 0x4
#define OXYELLOW 0xE  // 0x8 | 0x2 | 0x4
#define OXWHITE 0xF   // 0x8 | 0x1 | 0x2 | 0x4
short Merge(short TextCol,short BackgroundCol) { return TextCol|(BackgroundCol<<4); }
void SetColor(short Col) { SetConsoleTextAttribute(nHANDLE, Col); return; }
void SetColor(short TextCol,short BackgroundCol) { SetConsoleTextAttribute(nHANDLE, TextCol|(BackgroundCol<<4)); return; }

#ifdef UseDefaultColorset
const int DefaultBackgroundColor = GREEN;
const int WhiteColor = OXWHITE;
const int BlackColor = BLACK;
const int ChoosedColor = GREY;
const int ChangedColor = GREY;
const int DefaultColor = OXGREEN;
const int TextColor = OXWHITE;
#else
#ifdef UseAnotherColorset
const int DefaultBackgroundColor = GREY;
const int WhiteColor = OXWHITE;
const int BlackColor = BLACK;
const int ChoosedColor = RED;
const int ChangedColor = OXRED;
const int DefaultColor = WHITE;
const int TextColor = BLACK;
#else  // For Customization , Change Yourself
const int DefaultBackgroundColor = GREEN;
const int WhiteColor = OXWHITE;
const int BlackColor = BLACK;
const int ChoosedColor = GREY;
const int ChangedColor = GREY;
const int DefaultColor = OXGREEN;
const int TextColor = BLACK;
#endif
#endif

// Do not Change This!!!
const int PlayerWhite = 2;
const int PlayerBlack = 1;

// char BackgroundText[ScreenLines][ScreenColumns];  // Text Disabled
int BackgroundColor[ScreenLines][ScreenColumns];

void Place(const int x, const int y) {  // v:x ->:y
    COORD PlaceCursorHere;
    PlaceCursorHere.X = y;
    PlaceCursorHere.Y = x;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), PlaceCursorHere);
    return;
}

void outColor(int x, int y, WORD col) {
    if(BackgroundColor[x][y] == col) return ;
    Place(x, y);
    SetColor(TextColor,col);
    putchar(WhiteText);
    BackgroundColor[x][y] = col;
    return;
}

void initBackground(void) {
    SetColor(TextColor,DefaultBackgroundColor);
    for(int i=0; i<ScreenLines; ++i) {
        for(int u=0; u<ScreenColumns; ++u) {
            putchar(WhiteText);
            BackgroundColor[i][u]=DefaultBackgroundColor;
        }
    }
}

// Click & Type
HWND H;
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x0001) ? 1 : 0)
#define KEYBDOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1 : 0)
POINT GetClick(void) {
    POINT P;
    while (1) {
        GetCursorPos(&P);
        ScreenToClient(H, &P);
        if (KEY_DOWN(MOUSE_MOVED)) return P;
    }
    return P;
}

// Cursors
HANDLE CSR = GetStdHandle(-11);
CONSOLE_CURSOR_INFO Cursor;

void HideC(void) {
    Cursor.bVisible = 0;
    Cursor.dwSize = 1;
    SetConsoleCursorInfo(CSR, &Cursor);
    return;
}

void ShowC(void) {
    Cursor.bVisible = 1;
    Cursor.dwSize = 1;
    SetConsoleCursorInfo(CSR, &Cursor);
    return;
}

// Type Transfer
inline string int_to_string(int x) {
    string res="";
    bool negative= (x < 0);
    if(negative) x=-x;
    res=(char)(x%10+'0')+res;
    x/=10;
    while(x) {
        res=(char)(x%10+'0')+res;
        x/=10;
    }
    return res;
}

// Game Main
int ChessBoard[ChessBoradLines][ChessBoradColumns];  // 0 : uncovered  1 : Black  2 : White
int Choosed[ChessBoradLines][ChessBoradColumns];  // 0 : uncovered  1 : Choose  2 : To be Changed
int NextPlayer = 1;

bool InBoard(int x,int y) {
    if(x < 0) return 0;
    if(x >= ChessBoradLines) return 0;
    if(y < 0) return 0;
    if(y >= ChessBoradColumns) return 0;
    return 1;
}

const int Directions[8][2] = {{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1}};
int PreSelect(int x,int y,int col) {  // col :  1 : Black  2 : White  return changed chesses count
    if(ChessBoard[x][y]) return 0;
    int res=0;
    for(int dr=0; dr<8; ++dr) {
        int px=x+Directions[dr][0],py=y+Directions[dr][1];
        while(InBoard(px,py)) {
            if(!ChessBoard[px][py]) {
                break;
            }
            if(ChessBoard[px][py] == col) {
                px-=Directions[dr][0];
                py-=Directions[dr][1];
                while(px!=x||py!=y) {
                    Choosed[px][py]=2; ++res;
                    px-=Directions[dr][0];
                    py-=Directions[dr][1];
                }
                break;
            }
            px+=Directions[dr][0];
            py+=Directions[dr][1];
        }
    }
    Choosed[x][y]=1;
    return res;
}

void DeSelect(void) {
    for(int i=0; i<ChessBoradLines; ++i) {
        for(int u=0; u<ChessBoradColumns; ++u) {
            Choosed[i][u]=0;
        }
    }
}

bool CheckMovable(int Player) {
    bool res = 0;
    for(int x=0; x<ChessBoradLines; ++x) {
        for(int y=0; y<ChessBoradColumns; ++y) if(ChessBoard[x][y] == 0) {
            if(res) break;
            if(PreSelect(x,y,Player)) res = 1;
        }
    }
    DeSelect();
    return res;
}

void SelectPoint(int x,int y,int col) { // without check. Please use Preselect to check first
    for(int dr=0; dr<8; ++dr) {
        int px=x+Directions[dr][0],py=y+Directions[dr][1];
        while(InBoard(px,py)) {
            if(!ChessBoard[px][py]) break;
            if(ChessBoard[px][py] == col) {
                px-=Directions[dr][0];
                py-=Directions[dr][1];
                while(px!=x||py!=y) {
                    ChessBoard[px][py]=col;
                    px-=Directions[dr][0];
                    py-=Directions[dr][1];
                }
                break;
            }
            px+=Directions[dr][0];
            py+=Directions[dr][1];
        }
    }
    ChessBoard[x][y]=col;
}

inline void PaintChessBoard(bool PaintScoreBoard = 1) {
    int scoreBlack = 0;
    int scoreWhite = 0;
    for(int i=0; i<ChessBoradLines; ++i) {
        for(int u=0; u<ChessBoradColumns; ++u) {
            if(Choosed[i][u] == 0 || DisableHint) {
                switch(ChessBoard[i][u]) {
                    case 0:
                        outColor(2*i+1,4*u+2,DefaultColor);
                        outColor(2*i+1,4*u+3,DefaultColor);
                        break;
                    case 1:
                        ++scoreBlack;
                        outColor(2*i+1,4*u+2,BlackColor);
                        outColor(2*i+1,4*u+3,BlackColor);
                        break;
                    case 2:
                        ++scoreWhite;
                        outColor(2*i+1,4*u+2,WhiteColor);
                        outColor(2*i+1,4*u+3,WhiteColor);
                        break;
                    default :
                        exit(1);
                        break;
                }
            } else if(Choosed[i][u] == 1) {
                outColor(2*i+1,4*u+2,ChoosedColor);
                outColor(2*i+1,4*u+3,ChoosedColor);
            } else if(Choosed[i][u] == 2) {
                outColor(2*i+1,4*u+2,ChangedColor);
                outColor(2*i+1,4*u+3,ChangedColor);
            }
        }
    }
    if(PaintScoreBoard) {
        SetColor(TextColor,DefaultBackgroundColor);
        for(int i=0; i<ScoreBoardWidth; ++i) {
            Place(ScreenLines-1, 5 + ScoreBoardWidth - i);
            putchar(scoreBlack%10+'0');
            scoreBlack/=10;
        }
        for(int i=0; i<ScoreBoardWidth; ++i) {
            Place(ScreenLines-1, ScreenColumns - 7 - i);
            putchar(scoreWhite%10+'0');
            scoreWhite/=10;
        }
    }
}

void GameInit(void) {
    memset(ChessBoard, 0, sizeof ChessBoard);
    int L=ChessBoradColumns/2;
    int U=ChessBoradLines/2;
    ChessBoard[L-1][U-1]=1;
    ChessBoard[L][U]=1;
    ChessBoard[L-1][U]=2;
    ChessBoard[L][U-1]=2;
}

int PotentialX,PotentialY;
pair <int,int> EmptyReturn={-1923623,-8633854};
inline pair <int,int> GetPosition(POINT P) {
    if(4<=(P.x&31)&&(P.x&31)<12) return EmptyReturn;
    if(4<=(P.y&31)&&(P.y&31)<12) return EmptyReturn;
    PotentialX = (P.y-4)/32;
    PotentialY = (P.x-4)/32;
    if(InBoard(PotentialX,PotentialY)) return make_pair(PotentialX,PotentialY);
    else return EmptyReturn;
}

void GameWork(void) {
    GameInit();
    initBackground();
    PaintChessBoard();
    // Score Board
    Place(ScreenLines-1,0); printf("BLACK");
    Place(ScreenLines-1,ScreenColumns-5); printf("WHITE");
    // Start
    POINT P;
    pair <int,int> LastPos = EmptyReturn;
    int Selectcount = 0;
    while(true) {  // enum step

        LastPos = EmptyReturn;
        Selectcount = 0;

        if(!CheckMovable(NextPlayer)) {
            if(CheckMovable(NextPlayer^3))
                goto CHANGEPLAYER;
            else break;
        }

        if(NextPlayer == 1) {
            outColor(ScreenLines-1, ScreenColumns/2-1, BlackColor);
            outColor(ScreenLines-1, ScreenColumns/2  , BlackColor);
        } else {
            outColor(ScreenLines-1, ScreenColumns/2-1, WhiteColor);
            outColor(ScreenLines-1, ScreenColumns/2  , WhiteColor);
        }

        while(true) {
            GetCursorPos(&P);
            ScreenToClient(H, &P);
            pair <int,int> Pos = GetPosition(P);

            if(Pos == EmptyReturn) {
                KEY_DOWN(MOUSE_MOVED);
                continue;
            }

            if(Pos != LastPos) {
                DeSelect();
                Selectcount = PreSelect(Pos.first, Pos.second, NextPlayer);
                PaintChessBoard(0);
                LastPos = Pos;
            }

            if(KEY_DOWN(MOUSE_MOVED)) {
                if(Selectcount == 0) goto Sleep;
                if(Pos == EmptyReturn) goto Sleep;
                SelectPoint(Pos.first,Pos.second,NextPlayer);
                DeSelect();
                PaintChessBoard(1);
                break;
            }
            Sleep:;
            Sleep(DetectTime);
        }       

        CHANGEPLAYER:;
        NextPlayer ^= 3; 
    }

    int scoreBlack=0,scoreWhite=0;
    for(int i=0; i<ChessBoradLines; ++i) {
        for(int u=0; u<ChessBoradColumns; ++u) {
            if(ChessBoard[i][u]==PlayerBlack) ++scoreBlack;
            if(ChessBoard[i][u]==PlayerWhite) ++scoreWhite;
        }
    }
    if(scoreBlack > scoreWhite) {
        SetColor(BlackColor,WhiteColor);
        Place(ScreenLines-1, ScreenColumns/2-1);
        printf(" Win");
        SetColor(WhiteColor,BlackColor);
        Place(ScreenLines-1, ScreenColumns/2-3);
        printf("  ");
    }
    if(scoreWhite > scoreBlack) {
        SetColor(WhiteColor,BlackColor);
        Place(ScreenLines-1, ScreenColumns/2-1);
        printf(" Win");
        SetColor(BlackColor,WhiteColor);
        Place(ScreenLines-1, ScreenColumns/2-3);
        printf("  ");
    }
    if(scoreWhite == scoreBlack) {
        SetColor(BLACK,DefaultColor);
        Place(ScreenLines-1, ScreenColumns/2-2);
        printf("Deul");
    }
    KEY_DOWN(82);
    KEY_DOWN(69); // Refresh
    while(1) {
        if(KEY_DOWN(82)) return ;  // KeyR
        if(KEY_DOWN(69)) exit(0);  // KeyE
        Sleep(DetectTime);
    }
}

void Initation(void) {
    HideC();
    system("title \"Black and White\"");
    system(("mode con lines="+int_to_string(ScreenLines)+" cols="+int_to_string(ScreenColumns)).c_str());
    initBackground();
    H = GetForegroundWindow();
}

signed main(void) {
    Initation();
    while(1) GameWork();
}

// To-do List
// 1. Revoke
// 2. Save & Load