栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c语言版本实现的五子棋

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c语言版本实现的五子棋

#include 
#include 

#define N    5

int chessboard[N + 1][N + 1] = { 0 };

int whoseTurn = 0;

void initGame();
void printChessboard();
void playChess();
int judge(int, int);

int main()
{
    initGame();
    while (1)
    {
 ++whoseTurn;
 playChess();
    }
    return 0;
}

void initGame(void)
{
    char c;

    printf("Please input 'y' to enter the game:");
    c = getchar();
    if ('y' != c && 'Y' != c)
 exit(0);

    system("cls");
    printChessboard();
}

void printChessboard()
{
    int i, j;

    for (i = 0; i <= N; i++)
    {
 for (j = 0; j <= N; j++)
 {
     if (0 == i)
  printf("%3d", j);
     else if (j == 0)
  printf("%3d", i);
     else if (1 == chessboard[i][j])
  printf("  O");
     else if (2 == chessboard[i][j])
  printf("  X");
     else
  printf("  *");
 }
 printf("n");
    }
}

void playChess(void)
{
    int i, j;

    if (1 == (whoseTurn & 1))
    {
 printf("Turn to player 1, please input the position:");
 scanf_s("%d %d", &i, &j);

 while (chessboard[i][j] != 0)
 {
     printf("This position  has been occupied, please input the position again:");
     scanf_s("%d %d", &i, &j);
 }

 chessboard[i][j] = 1;
    }
    else
    {
 printf("Turn to player 1, please input the position:");
 scanf_s("%d %d", &i, &j);

 while (chessboard[i][j] != 0)
 {
     printf("This position  has been occupied, please input the position again:");
     scanf_s("%d %d", &i, &j);
 }

 chessboard[i][j] = 2;
    }

    system("cls");
    printChessboard();

    if (judge(i, j))
    {
 if (1 == (whoseTurn & 1))
 {
     printf("Winner is player 1!n");
     exit(0);
 }
 else
 {
     printf("Winner is player 2!n");
     exit(0);
 }
    }
}

int judge(int x, int y)
{
    int i, j;
    int t = 2 - (whoseTurn & 1);

    for (i = x - 4, j = y; i <= x; i++)
    {
 if (i >= 1 && i <= N - 4 && t == chessboard[i][j] && t == chessboard[i + 1][j] && t == chessboard[i + 2][j] && t == chessboard[i + 3][j] && t == chessboard[i + 4][j])
     return 1;
    }
    for (i = x, j = y - 4; j <= y; j++)
    {
 if (j >= 1 && j <= N - 4 && t == chessboard[i][j] && t == chessboard[i][j + 1] && t == chessboard[i][j + 2] && t == chessboard[i][j + 3] && t == chessboard[i][j + 4])
     return 1;
    }
    for (i = x - 4, j = y - 4; i <= x, j <= y; i++, j++)
    {
 if (i >= 1 && i <= N - 4 && j >= 1 && j <= N - 4 && t == chessboard[i][j] && t == chessboard[i + 1][j + 1] && t == chessboard[i + 2][j + 2] && t == chessboard[i + 3][j + 3] && t == chessboard[i + 4][j + 4])
     return 1;
    }
    for (i = x + 4, j = y - 4; i >= 1, j <= y; i--, j++)
    {
 if (i >= 1 && i <= N - 4 && j >= 1 && j <= N - 4 && t == chessboard[i][j] && t == chessboard[i - 1][j + 1] && t == chessboard[i - 2][j + 2] && t == chessboard[i - 3][j + 3] && t == chessboard[i - 4][j + 4])
     return 1;
    }

    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/233029.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号