算法思路在上一篇[C++之简单五子棋的设计思路](http://blog.csdn.net/black_kyatu/article/details/79293392)中描述的较为清晰了。接下来则是设计数据类型和语言实现部分。
类及类的实现
#ifndef RENJU_H
#define RENJU_H
#include <iostream>
#include <windows.h>
#include <string>
#define hor 7
#define ver 4
using namespace std;
//用于记录坐标
struct position
{
int x;
int y;
position()
{
x = 0;
y = 0;
}
position(int a,int b)
{
x = a;
y = b;
}
};
//用于记录棋子颜色和节点状态
enum state
{
blank=0,black=1,white=2
};
//用于存储棋局分析信息:未完赛,犯规,平局,黑方胜,白方胜
enum result
{
go_on,error,draw,black_win,white_win
};
// 设置光标
void setpos(COORD a)
{
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(out, a);
}
// 设置光标
void setpos(int i, int j)
{
COORD pos = { i, j };
setpos(pos);
}
//绘图函数,用于在指定坐标输出指定字符
inline void gps(int x,int y,char c)
{
setpos(y,x);
cout<<c;
}
//绘图函数,用于在指定坐标输出整数
inline void gps(int x,int y,int i)
{
setpos(y,x);
if(i>=10)
cout<<i;
else
cout<<0<<i;
}
//绘图函数,用于在指定坐标输出字符串
inline void gps(int x,int y,string s)
{
setpos(y,x);
cout<<s;
}
//绘图函数,用于在给定坐标打印棋盘中的一格
void tab(int x,int y,state str)
{
string s;
switch (str)
{
case blank:
s=" ";
break;
case black:
s="黑";
break;
case white:
s="白";
break;
default:
break;
}
setpos(y,x);
cout<<" ------ ";
setpos(y,x+1);
cout<<"| |";
setpos(y,x+2);
cout<<"| "<<s<<" |";
setpos(y,x+3);
cout<<"| |";
setpos(y,x+4);
cout<<" ------ ";
}
//查找最大值
int MAX(const int *a,int n)
{
int max = a[0];
for(int i =1; i < n ;i++)
{
if(a[i] > max)
max = a[i];
}
return max;
}
//检测是否符合胜利条件
//棋子类
class chess
{
public:
inline chess(int x=0,int y=0,state c=blank)
{ point.x=x,point.y=y;
color=c;
};
inline chess(chess &ch)
{ point=ch.drop_point();
color=ch.get_