五子棋C++代码

引用其他人的代码,Mark一下!

  1 #include<iostream>
  2 //#include"data.h"
  3 #define N 10//棋盘规格
  4 using namespace std;
  5 static int chessboard[N][N];//棋盘
  6 struct chess{//棋子
  7     int x,y;
  8     int color;//0为无子,1为白,-1为黑
  9 };
 10 void init_chessboard()//初始化棋盘函数;
 11 {
 12     int i,j;
 13     for(i=0;i<N;i++)
 14         for(j=0;j<N;j++)
 15         {
 16             chessboard[i][j]=0;
 17         }
 18 }
 19 bool is_win(struct chess che)//判断胜负函数,ture 未分出胜负,可以继续下棋;false分出胜负
 20 {
 21     int x,y,num;
 22     int xmin,xmax,ymin,ymax,min,max;
 23     //begin判断左右方向
 24     num=0;
 25     xmin=(che.y-4>0)?che.y-4:0;//左边界
 26     xmax=(che.y+4<N-1)?che.y+4:N-1;//右边界
 27     //cout<<xmin<<"左右"<<xmax<<endl;
 28     for(y=xmin;y<=xmax;y++)
 29     {
 30         if(chessboard[che.x][y]==che.color&&num<5)
 31         {
 32             num++;
 33             //cout<<num<<endl;
 34             if(num==5)
 35                 return false;
 36         }
 37         else
 38             num=0;
 39     }
 40     //end 判断左右方向
 41     //begin判断上下方向
 42     num=0;
 43     ymin=(che.x-4>0)?che.x-4:0;//上边界
 44     ymax=(che.x+4<N-1)?che.x+4:N-1;//下边界
 45     //cout<<ymin<<"上下"<<ymax<<endl;
 46     for(x=ymin;x<=ymax;x++)
 47     {
 48         if(chessboard[x][che.y]==che.color&&num<5)
 49         {
 50             num++;
 51             //cout<<num<<endl;
 52             if(num==5)
 53                 return false;
 54         }
 55         else
 56             num=0;
 57     }
 58     //end 判断上下方向
 59     //begin判断135度方向
 60     num=0;
 61     xmin=(che.y<4)?che.y:4;//左边界到点的距离
 62     xmax=(N-1-che.y<4)?N-1-che.y:4;//右边界到点的距离
 63     ymin=(che.x<4)?che.x:4;//上边界到点的距离
 64     ymax=(N-1-che.x<4)?N-1-che.x:4;//下边界到点的距离
 65     min=xmin<ymin?xmin:ymin;//左上方边界到点的距离
 66     max=xmax<ymax?xmax:ymax;//右下放边界到点的距离
 67     //cout<<"左上边界距离"<<min<<endl;
 68     //cout<<"右下边界距离"<<max<<endl;
 69     for(x=che.x-min,y=che.y-min;x<=che.x+max;x++,y++)//左上到右下遍历
 70     {
 71         if(chessboard[x][y]==che.color&&num<5)
 72         {
 73             num++;
 74         //    cout<<num<<endl;
 75             if(num==5)
 76                 return false;
 77         }
 78         else
 79             num=0;
 80     }
 81     //end判断135度方向
 82 //begin判断45度方向
 83     num=0;
 84     min=ymin<xmax?ymin:xmax;//右上距离
 85     max=xmin<ymax?xmin:ymax;//左下距离
 86     //cout<<"右上距离"<<min<<endl;
 87     //cout<<"左下距离"<<max<<endl;
 88     for(x=che.x-min,y=che.y+min;x<=che.x+max;x++,y--)//由右上到左下判断
 89     {
 90         if(chessboard[x][y]==che.color&&num<5)
 91         {
 92             num++;
 93         //    cout<<num<<endl;
 94             if(num==5)
 95             return false;
 96         }
 97         else
 98             num=0;
 99     }
100     //end 45度方向
101     return true;
102 }
103 bool is_right_chess(struct chess che)
104 {
105     if(che.x>=0&&che.x<N&&che.y>=0&&che.y<N&&chessboard[che.x][che.y]==0){
106         chessboard[che.x][che.y]=che.color;
107         return true;
108     }
109     else
110     {
111         cout<<"落子不合法,重新下子!"<<endl;
112         return false;
113     }
114 }
115 void show_chessboard()
116 {
117     int i,j;
118     cout<<"   0 1 2 3 4 5 6 7 8 9"<<endl;
119     for(i=0;i<N;i++)
120     {
121         cout<<i<<"  ";
122         for(j=0;j<N;j++)
123         {
124             //cout<<chessboard[i][j]<<" ";
125             if(chessboard[i][j]==-1)
126                 cout<<"*"<<" ";
127             else if(chessboard[i][j]==1)
128                 cout<<"o"<<" ";
129             else
130                 cout<<"-"<<" ";
131         }
132         cout<<endl;
133     }
134 }
135 struct chess put_chess(int colo)
136 {
137     if(colo==1)
138         cout<<"白方下子"<<endl;
139     else if(colo==-1)
140         cout<<"黑方下子"<<endl;
141     struct chess che;
142     cin>>che.x;
143     cin>>che.y;
144     che.color=colo;
145     return che;
146 }
147 int renrenModle()
148 {
149     init_chessboard();
150     struct chess pre;
151     //int colo;
152     while(1)
153     {
154         show_chessboard();
155         do{//黑方下棋
156             pre=put_chess(-1);
157         }while(!is_right_chess(pre));//下子不合法,重下
158         show_chessboard();
159         if(!is_win(pre))//黑方胜,胜时返回0,未分胜负返回1;
160         {
161             cout<<"黑方胜"<<endl;
162             return -1;
163         }
164         do{//白方下棋
165             pre=put_chess(1);
166         }while(!is_right_chess(pre));//下子不合法,重下
167         show_chessboard();
168         if(!is_win(pre))//白方胜
169         {
170             cout<<"白方胜"<<endl;
171             return 1;
172         }
173     }
174 }
175 int main()
176 {
177     renrenModle();
178     return 0;
179 }
原文地址:https://www.cnblogs.com/jungsee/p/8158505.html