三子棋游戏

#include<stdio.h>
#include<stdlib.h>
#define MAX_ROW 3
#define MAX_COL 3
void Init_map(char show_map[MAX_ROW][MAX_COL]) {
 for (int row = 0; row < MAX_ROW; row++) {
  for (int col = 0; col < MAX_COL; col++) {
   show_map[row][col] = ‘*’;
  }
 }
}
void Print_map(char show_map[MAX_ROW][MAX_COL]) {
 printf("   |");
 for (int row = 0; row < MAX_ROW; row++) {
  printf("%d “, row);
 }
 printf(”\n");
 for (int col = 0; col < MAX_ROW; col++) {
  printf("  %d|", col);
  for (int row = 0; row < MAX_ROW; row++) {
   printf("%c “, show_map[row][col]);
  }
  printf(”\n");
 }
}
void All_scanf(char show_map[MAX_ROW][MAX_COL]) {
 printf(“请输入要落棋子的坐标:\n”);
 int row = 0;
 int col = 0;
 int i = 0;
 int j = 0;
 while (1) {
  scanf_s("%d,%d", &row, &col);
  if (row<0 || row>MAX_ROW - 1 || col<0 || col>MAX_COL - 1) {
   printf(“您的输入有误,请重新输入:\n”);
   continue;
  }
  show_map[row][col] = ‘+’;
  break;
 }
 while (1) {
  srand((unsigned)time(0));
  i = rand() % MAX_ROW;
  j = rand() % MAX_COL;
  if (show_map[i][j] == ‘+’) {
   continue;
  }
  show_map[i][j] = ‘o’;
  break;
 }
 system(“cls”);
}
int User_Win(char show_map[MAX_ROW][MAX_COL]) {
 if (show_map[0][0] == ‘+’ && show_map[0][0] == show_map[1][1] && show_map[0][0] == show_map[2][2]) {
  return 1;
 }
 if (show_map[1][1] == ‘+’ && show_map[1][1] == show_map[0][2] && show_map[1][1] == show_map[2][0]) {
  return 1;
 }
 for (int row = 0; row < MAX_ROW; row++) {
  if (show_map[row][0] == ‘+’ && show_map[row][0] == show_map[row][1] && show_map[row][0] == show_map[row][1]) {
   return 1;
  }
 }
 for (int col = 0; col < MAX_ROW; col++) {
  if (show_map[0][col] == ‘+’ && show_map[0][col] == show_map[1][col] && show_map[0][col] == show_map[2][col]) {
   return 1;
  }
 }
 return 0;
}
int Comp_Win(char show_map[MAX_ROW][MAX_COL]) {
 if (show_map[0][0] == ‘o’ && show_map[0][0] == show_map[1][1] && show_map[0][0] == show_map[2][2]) {
  return 1;
 }
 if (show_map[1][1] == ‘o’ && show_map[1][1] == show_map[0][2] && show_map[1][1] == show_map[2][0]) {
  return 1;
 }
 for (int row = 0; row < MAX_ROW; row++) {
  if (show_map[row][0] == ‘o’ && show_map[row][0] == show_map[row][1] && show_map[row][0] == show_map[row][1]) {
   return 1;
  }
 }
 for (int col = 0; col < MAX_ROW; col++) {
  if (show_map[0][col] == ‘o’ && show_map[0][col] == show_map[1][col] && show_map[0][col] == show_map[2][col]) {
   return 1;
  }
 }
 return 0;
}
void Game() {
 char show_map[MAX_ROW][MAX_COL];
 Init_map(show_map);
 while (1) {
  Print_map(show_map);
     All_scanf(show_map);
  if (User_Win(show_map)) {
   Print_map(show_map);
   printf(“胜利.\n”);
   break;
  }
  if (Comp_Win(show_map)) {
   Print_map(show_map);
   printf(“失败.\n”);
   break;
  }
 }
}
int main() {
 Game();
 system(“pause”);
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值