四连子棋

Contributor:游客141498472 Type:代码 Date time:2020-11-01 10:48:57 Favorite:96 Score:0.6
返回上页 Report
请选择举报理由:




Collection Modify the typo
#include<stdio.h>
#include<stdlib.h>
#define ROW 7
#define COL 8
#define O 1
#define X 2
#define WHITE 0
#define COUNT 4
int map[ROW][COL]={0};
void PrintMap(int map[][COL],int row,int col);
int PlayChess(int map[][COL],int col,int piece);
int IsWin(int map[][COL],int row,int col,int piece);
int main()
{
int piece=O;
int row;
int col;
int Winner=0;
int count_piece=0;
while(1)
{
/*清屏*/
system("cls");
PrintMap(map,ROW-1,COL-1);
if(Winner!=0||count_piece==(ROW-1)*(COL-1))
{
break;
}
printf("现在%d号玩家下子\n",piece);
printf("落子位置(1~%d):",COL-1);
scanf("%d",&col);
if(col<1||col>=COL)
{
printf("下子位置无效!\n");
continue;
}
row=PlayChess(map,col,piece);
if(row==0)
{
printf("该列已经满了!\n");
}
else
{
count_piece++;
Winner=IsWin(map,row,col,piece);
if(Winner!=0)
{
}
else
{
if(piece==O)
{
piece=X;
}
else if(piece==X)
{
piece=O;
}
}
}
}
if(Winner!=0)
{
printf("%d号玩家获胜!\n",Winner);
}
else
{
printf("平局!\n");
}
return 0;
}
void PrintMap(int map[][COL],int row,int col)
{
int number=1;
while(number<=2*COL-2)
{
if(number%2==0)
{
printf("%d",number/2);
}
else
{
printf(" ");
}
number++;
}
printf("\n");
int i=1;
while(i<=2*ROW-1)
{
int j=1;
while(j<=2*COL-1)
{
if(i%2==1)
{
printf("-");
}
else if(j%2==1)
{
printf(" ");
}
else
{
int h=i/2;
int z=j/2;
if(map[h][z]==O)
{
printf("O");
}
else if(map[h][z]==X)
{
printf("X");
}
else
{
printf(" ");
}
}
j++;
}
printf("\n");
i++;
}
}
//下子
int PlayChess(int map[][COL],int col,int piece)
{
int i=ROW-1;
while(i>=1)
{
if(map[i][col]==WHITE)
{
map[i][col]=piece;
return i;
}
i--;
}
return 0;
}
int IsWin(int map[][COL],int row,int col,int piece)
{
int countH=1,countS=1,countP=1,countN=1;
int i;
/*计算countH*/
/*左边*/
i=col-1;
while(i>=1)
{
if(map[row][i]==piece)
{
countH++;
}
else
{
break;
}
i--;
}
/*右边*/
i=col+1;
while(i<=COL-1)
{
if(map[row][i]==piece)
{
countH++;
}
else
{
break;
}
i++;
}
/*计算countS*/
/*上边*/
i=row-1;
while(i>=1)
{
if(map[i][col]==piece)
{
countS++;
}
else
{
break;
}
i--;
}
/*下边*/
i=row+1;
while(i<=ROW-1)
{
if(map[i][col]==piece)
{
countS++;
}
else
{
break;
}
i++;
}
int j;
/*计算countN*/
/*左上*/
i=row-1;
j=col-1;
while(i>=1&&j>=1)
{
if(map[i][j]==piece)
{
countN++;
}
else
{
break;
}
i--;
j--;
}
/*右下*/
i=row+1;
j=col+1;
while(i<=ROW-1&&j<=COL-1)
{
if(map[i][j]==piece)
{
countN++;
}
else
{
break;
}
i++;
j++;
}
/*计算countP*/
/*右上*/
i=row-1;
j=col+1;
while(i>=1&&j<=COL-1)
{
if(map[i][j]==piece)
{
countP++;
}
else
{
break;
}
i--;
j++;
}
/*左下*/
i=row+1;
j=col-1;
while(i<=ROW-1&&j>=1)
{
if(map[i][j]==piece)
{
countP++;
}
else
{
break;
}
i++;
j--;
}
if(countH>=COUNT||countS>=COUNT||countP>=COUNT||countN>=COUNT)
{
return piece;
}
else
{
return 0;
}
}
声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。
Hot degree:
Difficulty:
quality:
Description: the system according to the heat, the difficulty, the quality of automatic certification, the certification of the article will be involved in typing!

This paper typing ranking TOP20

登录后可见

用户更多文章推荐