| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
- #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++ 
我用vs给你编译没什么问题。 
只有一个警告 
“ChessBoard<Chess>::march”: 不是所有的控件路径都返回值 
棋盘可以下棋很正常。。 
 
 
 |   
 
 
 
 |