五子棋
getch()函数linux,没有,所以前面从网上找了一个替代实现。废话不多说,直接上截图 !
![](https://img.haomeiwen.com/i15168051/f64cae228f10f797.png)
####五子棋源代码
#pragma once
/*
常用的ANSI控制码如下(有些不支持):
\033[0m 关闭所有属性
\033[1m 高亮
\033[2m 亮度减半
\033[3m 斜体
\033[4m 下划线
\033[5m 闪烁
\033[6m 快闪
\033[7m 反显
\033[8m 消隐
\033[9m 中间一道横线
10-19 关于字体的
21-29 基本与1-9正好相反
30-37 设置前景色
40-47 设置背景色
30:黑
31:红
32:绿
33:黄
34:蓝色
35:紫色
36:深绿
37:白色
38 打开下划线,设置默认前景色
39 关闭下划线,设置默认前景色
40 黑色背景
41 红色背景
42 绿色背景
43 棕色背景
44 蓝色背景
45 品红背景
46 孔雀蓝背景
47 白色背景
48 不知道什么东西
49 设置默认背景色
50-89 没用
90-109 又是设置前景背景的,比之前的颜色浅
\033[nA 光标上移n行
\033[nB 光标下移n行
\033[nC 光标右移n行
\033[nD 光标左移n行
\033[y;xH设置光标位置
\033[2J 清屏
\033[K 清除从光标到行尾的内容
\033[s 保存光标位置
\033[u 恢复光标位置
\033[?25l 隐藏光标
\033[?25h 显示光标
*/
#include <termio.h>
/*
* 得到用户输入的一个字符
* : 返回得到字符
*/
int getch(void) {
int cr;
struct termios nts, ots;
if (tcgetattr(0, &ots) < 0) // 得到当前终端(0表示标准输入)的设置
return EOF;
nts = ots;
cfmakeraw(&nts); // 设置终端为Raw原始模式,该模式下所有的输入数据以字节为单位被处理
if (tcsetattr(0, TCSANOW, &nts) < 0) // 设置上更改之后的设置
return EOF;
cr = getchar();
if (tcsetattr(0, TCSANOW, &ots) < 0) // 设置还原成老的模式
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函数调用
#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的静态函数start_play()里面通过调用小功能来集成实现。
- 界面绘制要独立出来,要不然很乱。最好是逻辑跟界面分开吧,界面就用show单独来画。
其实没什么吊难度,就是绘制花了点功夫。
看看以后能不能用vulkan绘制出来,然后加个网络对战还有AI?
那都是后话了,看心情。
网友评论