#include <iostream>
#include <algorithm>
using namespace std;
template<typename T>
struct printCB{
void operator()(T c){
cout << c << " ";
}
};
struct Chess{
char sign;
Chess(char s):sign(s){}
};
template<typename C> //传棋子结构体
class ChessBoard{
private:
char *base;
int board;
public:
ChessBoard(int b = 10):board(b){
base = new char[board*board];
fill(base,base+board*board,'+'); //初始化棋盘
}
~ChessBoard(){
delete []base;
}
void luozi(int r,int c,C chess);
void printChessBoard();
bool march(int c,int r,int dic,char s);//dic显示是怎样匹配·棋子
bool CheckChess(int ,int ,char);
};
int checkN(int,int,char *base,int board,char);
int checkE(int,int,char *base,int board,char);
int checkS(int,int,char *base,int board,char);
int checkW(int,int,char *base,int board,char);
int checkNE(int,int,char *base,int board,char);
int checkNW(int,int,char *base,int board,char);
int checkSE(int,int,char *base,int board,char);
int checkSW(int,int,char *base,int board,char);
template<typename C>
void ChessBoard<C>::printChessBoard(){
int count=0;
for(;count<board;count++){
for_each(base+count*board,base+count*board+board,printCB<char>());
printf("\n");
}
}
template<typename C>
void ChessBoard<C>::luozi(int r,int c,C chess){
if('+'==*(base+r*board+c)){
*(base+r*board+c)=chess.sign;
}
}
template<typename C>
bool ChessBoard<C>::march(int row,int col,int dic,char s){
switch(dic){
case 0:return checkN(row,col,base,board,s)+checkS(row,col,base,board,s)>=5;
case 1:return checkW(row,col,base,board,s)+checkE(row,col,base,board,s)>=5;
case 2:return checkNW(row,col,base,board,s)+checkSE(row,col,base,board,s)>=5;
case 3:return checkNE(row,col,base,board,s)+checkSW(row,col,base,board,s)>=5;
}
}
//template<typename C>
int checkN(int row,int col,char *base,int board,char s){
int count=1;
while(row-1>0 && *(base+(row-1)*board+col)==s)
count++;
return count;
}
//template<typename C>
int checkS(int row,int col,char *base,int board,char s){
int count=1;
while(row+1<board && *(base+(row+1)*board+col)==s)
count++;
return count;
}
//template<typename C>
int checkE(int row,int col,char *base,int board,char s){
int count=1;
while(col+1<board && *(base+row*board+col+1)==s)
count++;
return count;
}
//template<typename C>
int checkW(int row,int col,char *base,int board,char s){
int count=1;
while(col-1>0 && *(base+row*board+col-1)==s)
count++;
return count;
}
//template<typename C>
int checkNW(int row,int col,char *base,int board,char s){
int count=1;
while((row+1<board && col-1>0) && *(base+(row+1)*board+col-1)==s)
count++;
return count;
}
//template<typename C>
int checkNE(int row,int col,char *base,int board,char s){
int count=1;
while((row+1<board && col+1<board) && *(base+(row+1)*board+col+1)==s)
count++;
return count;
}
//template<typename C>
int checkSW(int row,int col,char *base,int board,char s){
int count=1;
while((row-1>0 && col-1>0) && *(base+(row-1)*board+col-1)==s)
count++;
return count;
}
//template<typename C>
int checkSE(int row,int col,char *base,int board,char s){
int count=1;
while((row+1<board && col+1<board) && *(base+(row+1)*board+col+1)==s)
count++;
return count;
}
int main()
{
bool win_flag=false;
int count=0,col,row;
ChessBoard<struct Chess> chessboard;
while(!win_flag){
if(count%2==0){
cout << "请白棋落子" << endl;
count++;
cin >> row >> col;
chessboard.luozi(row,col,Chess('0'));
win_flag=chessboard.march(row,col,0,'0');
win_flag=chessboard.march(row,col,1,'0');
win_flag=chessboard.march(row,col,2,'0');
win_flag=chessboard.march(row,col,3,'0');
chessboard.printChessBoard();
}
else{
cout << "请黑棋落子" << endl;
count++;
cin >> row >> col;
chessboard.luozi(row,col,Chess('@'));
win_flag=chessboard.march(row,col,0,'@');
win_flag=chessboard.march(row,col,1,'@');
win_flag=chessboard.march(row,col,2,'@');
win_flag=chessboard.march(row,col,3,'@');
chessboard.printChessBoard();
}
}
chessboard.printChessBoard();
return 0;
}
编译器是dev c++