三子棋
  73f0du7lUeyY 2023年11月02日 60 0

test.c

#include"game.h"

void menu()

{

printf("       三子棋\n");

printf("     1.开始游戏\n");

printf("     0.退出游戏\n");

printf("游戏规则:电脑胜利则游戏胜利,否则游戏失败\n");

printf("用空格隔开两个数字,例如,坐标(0,0)应写成“0 0”\n");

}  


void game()

{

char board[ROW][COL] = { 0 };

InitBoard(board,ROW,COL);//初始化棋盘

DisplayBoard(board,ROW,COL);//打印棋盘

char ret;

while (1)

{

 //玩家下棋

 PlayerMove(board, ROW, COL);

 DisplayBoard(board, ROW, COL);

 ret=Iswin(board,ROW,COL);//判断游戏状态 'O.胜利  X.失败  J.继续

 if (ret != 'J')

  break;

 //电脑下棋

 ComputerMove(board, ROW, COL);

 DisplayBoard(board, ROW, COL);

 if (ret != 'J')

  break;

}

if (ret == 'X')

 printf("失败\n");

if (ret == 'O')

 printf("胜利\n");

}

void test()

{

int input = 0;

srand((unsigned)time(NULL));

do

{

 menu();

 scanf_s("%d", &input);

 switch (input)

 {

 case 1:

  game();

  break;

 case 0:

  break;

 default:

  printf("输入错误,请重新输入:>");

  break;

 }

}

while (input!=0);

}

int main()

{

test();

return 0;

}


game.h

#define ROW 3

#define COL 3

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

//声明

void InitBoard(char board[ROW][COL], int row, int col);

void DisplayBoard(char board[ROW][COL], int row, int col);

void PlayerMove(char board[ROW][COL], int row, int col);

void ComputerMove(char board[ROW][COL], int row, int col);

char Iswin(char board[ROW][COL], int row, int col);


game.h

#include"game.h"

void InitBoard(char board[ROW][COL], int row, int col)

{

int i = 0;

for (i = 0; i < row; i++)

{

 int j = 0;

 for (j = 0; j < col; j++)

 {

  board[i][j] = ' ';

 }

}


}


void DisplayBoard(char board[ROW][COL], int row, int col)

{

int i = 0;

for (i = 0; i < row; i++)

{

 int j = 0;

 for (j = 0; j < col; j++)//打印一行

 {

  printf(" %c ", board[i][j]);

  if (j < col - 1)

   printf("|");

 }

 printf("\n");//换行

 if (i < row - 1)

 {

  for (j = 0; j < col; j++)

  {

   printf("---");

   if (j < col - 1)

    printf("|");

  }

  printf("\n");//换行

 }

}

}


void PlayerMove(char board[ROW][COL], int row, int col)

{

int x;

int y;

printf("玩家走\n");

printf("请输入坐标:");

while (1)

{

 scanf_s("%d%d", &x, &y);

 if (x >0 && x <= row && y >0 && y <=col)

 {

  if (board[x - 1][y - 1] == ' ')

  {

   board[x - 1][y - 1] = 'X';

   break;

  }

  else

   printf("该坐标被占用,请重新输入:");

 }

 else

  printf("坐标非法,请重新输入:");

}

}


void ComputerMove(char board[ROW][COL], int row, int col)

{

int x = 0;

int y = 0;

printf("电脑走\n");

while (1)

{

 x = rand() % 3;

 y = rand() % 3;

 if (board[x][y] == ' ')

 {

  board[x][y] = 'O';

  break;

 }

}

}


int IsFull(char board[ROW][COL], int row, int col)

{

int i = 0;

for (i = 0; i < row; i++)

{

 int j = 0;

 for (j = 0; j < col; j++)

 {

  if (board[i][j] == ' ')

   return 0;

}

}

return 1;

}


char Iswin(char board[ROW][COL], int row, int col)

{

int i = 0;

int j = 0;

for (i = 0; i < row; i++)

{

 if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][2] != ' ')

 {

  return board[i][2];

 }

}

for (j = 0; j < col; j++)

{

 if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[2][j] != ' ')

 {

  return board[2][j];

 }

}

if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] != ' ')

{

 return board[2][2];

}

if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != ' ')

 return board[2][0];

if (IsFull(board,ROW,COL) == 1)

 return 'X';

else

 return 'J';

}

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论