三子棋

main.c

#define _CRT_SECURE_NO_WARNINGS 0
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"game.h"
void menu()
{
 printf("****************************\n");
 printf("********0:退出游戏*********\n");
 printf("********1:进入游戏*********\n");
 printf("********2:游戏介绍*********\n");
 printf("****************************\n");
}
void say()
{
 printf("游戏名称:      三子棋\n");
 printf("游戏类型:      益智类\n");
 printf("游戏规则: 人先走,电脑后走,每次没人走一步!\n");
 printf("           当一方率先有一行或者一列或者对角\n");
 printf("           线有三个棋子时,获胜!游戏结束。\n\n\n\n");
}
void ganme()
{   
 int count = 0;
 char play[ROWS][LOWS];
 srand((unsigned int)time(NULL));
 init_board(play, ROWS, LOWS);
 display_board(play, ROWS, LOWS);
 while (1)
 {
  player_move(play, ROWS, LOWS);
  display_board(play, ROWS, LOWS);
  if (check_win(play, ROWS, LOWS) == 1)
   break;
  computer_move(play, ROWS, LOWS);
  display_board(play, ROWS, LOWS);
  if (check_win(play, ROWS, LOWS) == 1)
   break;
  
 }
}
int main()
{
 int input = 0;
 int m = 0;
 do
 {
  menu();
  printf("输入选项:\n");
  scanf("%d", &input);
  switch (input)
  {
  case 0:
   break;
  case 1:
   m = 0;
   ganme();
   break;
  case 2:
   say();
   break;
  default:
   printf("请输入正确的选项!!!");
   break;
  }
 } while (input);
 system("pause");
 return 0;
}


game.h

#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ROWS 3
#define LOWS 3

void init_board(char play[ROWS][LOWS], int row, int low);
void display_board(char play[ROWS][LOWS], int row, int low);
void player_move(char play[ROWS][LOWS], int row, int low);
void computer_move(char play[ROWS][LOWS], int row, int low);
int check_win(char play[ROWS][LOWS], int row, int low);
#endif //__GAME_H


game.c


#define _CRT_SECURE_NO_WARNINGS 0
#include "game.h"
void init_board(char play[ROWS][LOWS], int row, int low)
{
 memset(play, ' ', row*low*sizeof(char));
}
void display_board(char play[ROWS][LOWS], int row, int low)
{
 int i = 0;
 printf("|---|---|---|\n");
 for (i = 0; i < row; i++)
 {
  printf("| %c | %c | %c |\n", play[i][0], play[i][1], play[i][2]);
  printf("|---|---|---|\n");
 }
}
void player_move(char play[ROWS][LOWS], int row, int low)
{
 int x = 0;
 int y = 0;
 while (1)
 {
  printf("请玩家输入坐标:>\n");
  scanf("%d %d", &x, &y);
  x--;
  y--;
  if ((x >= 0) && (x <= 2) && (y >= 0) && (y <= 2))
  {
   if (play[x][y] == ' ')
   {
    play[x][y] = 'X';
    break;
   }
   else
   {
    printf("输入的下标有误!请重新输入!");
   }
  }
  else
  {
   printf("输入的坐标有误!请重新输入!");
  }
 }
}
void computer_move(char play[ROWS][LOWS], int row, int low)
{
 int x = 0;
 int y = 0;
 static int i = 0;
 static int j = 0;
 static int m = 0;
 printf("电脑走:>\n");
 if ( ((play[1][0] == 'O') && (play[2][0] == 'O') && play[0][0] == ' ') || ((play[0][1] == 'O') && (play[0][2] == 'O') && play[0][0] == ' ') || ((play[1][1] == 'O') && (play[2][2] == 'O') && play[0][0] == ' '))
 {
  play[0][0] = 'O';
 }
 else if (((play[0][0] == 'O') && (play[0][2] == 'O') && play[0][1] == ' ') || ((play[1][1] == 'O') && (play[2][1] == 'O') && play[0][1] == ' '))
 {
  play[0][1] = 'O';
 }
 else if (((play[0][0] == 'O') && (play[0][1] == 'O') && play[0][2] == ' ') || ((play[1][2] == 'O') && (play[2][2] == 'O') && play[0][2] == ' ') || ((play[1][1] == 'O') && (play[2][0] == 'O') && play[0][2] == ' '))
 {
  play[0][2] = 'O';
 }
 else if (((play[0][0] == 'O') && (play[2][0] == 'O') && play[1][0] == ' ') || ((play[1][1] == 'O') && (play[1][2] == 'O') && play[1][0] == ' ') )
 {
  play[1][0] = 'O';
 }
 else if (((play[0][0] == 'O') && (play[2][2] == 'O') && play[1][1] == ' ') || ((play[0][2] == 'O') && (play[2][0] == 'O') && play[1][1] == ' ') || ((play[1][0] == 'O') && (play[1][2] == 'O') && play[1][1] == ' ') || ((play[0][1] == 'O') && (play[2][1] == 'O') && play[1][1] == ' ') )
 {
  play[1][1] = 'O';
 }
 else if (((play[1][0] == 'O') && (play[1][1] == 'O') && play[1][2] == ' ') || ((play[0][2] == 'O') && (play[2][2] == 'O') && play[1][2] == ' ') )
 {
  play[1][2] = 'O';
 }
 else if (((play[0][0] == 'O') && (play[1][0] == 'O') && play[2][0] == ' ') || ((play[0][2] == 'O') && (play[1][1] == 'O') && play[2][0] == ' ') || ((play[2][1] == 'O') && (play[2][2] == 'O') && play[2][0] == ' ') )
 {
  play[2][0] = 'O';
 }
 else if (((play[2][0] == 'O') && (play[2][2] == 'O') && play[2][1] == ' ') || ((play[0][1] == 'O') && (play[1][1] == 'O') && play[2][1] == ' ') )
 {
  play[2][1] = 'O';
 }
 else if (((play[2][0] == 'O') && (play[2][1] == 'O') && play[2][2] == ' ') || ((play[0][2] == 'O') && (play[1][2] == 'O') && play[2][2] == ' ') || ((play[0][0] == 'O') && (play[1][1] == 'O') && play[2][2] == ' '))
 {
  play[2][2] = 'O';
 }
 else if ( ((play[1][0] == 'X') && (play[2][0] == 'X') && play[0][0] == ' ') || ((play[0][1] == 'X') && (play[0][2] == 'X') && play[0][0] == ' ') || ((play[1][1] == 'X') && (play[2][2] == 'X') && play[0][0] == ' '))
 {
  play[0][0] = 'O';
 }
 else if ( ((play[0][0] == 'X') && (play[0][2] == 'X') && play[0][1] == ' ') || ((play[1][1] == 'X') && (play[2][1] == 'X') && play[0][1] == ' '))
 {
  play[0][1] = 'O';
 }
 else if ( ((play[0][0] == 'X') && (play[0][1] == 'X') && play[0][2] == ' ') || ((play[1][2] == 'X') && (play[2][2] == 'X') && play[0][2] == ' ') || ((play[1][1] == 'X') && (play[2][0] == 'X') && play[0][2] == ' '))
 {
  play[0][2] = 'O';
 }
 else if ( ((play[0][0] == 'X') && (play[2][0] == 'X') && play[1][0] == ' ') || ((play[1][1] == 'X') && (play[1][2] == 'X') && play[1][0] == ' '))
 {
  play[1][0] = 'O';
 }
 else if ( ((play[0][0] == 'X') && (play[2][2] == 'X') && play[1][1] == ' ') || ((play[0][2] == 'X') && (play[2][0] == 'X') && play[1][1] == ' ') || ((play[1][0] == 'X') && (play[1][2] == 'X') && play[1][1] == ' ') || ((play[0][1] == 'X') && (play[2][1] == 'X') && play[1][1] == ' '))
 {
  play[1][1] = 'O';
 }
 else if (((play[1][0] == 'X') && (play[1][1] == 'X') && play[1][2] == ' ') || ((play[0][2] == 'X') && (play[2][2] == 'X') && play[1][2] == ' '))
 {
  play[1][2] = 'O';
 }
 else if ( ((play[0][0] == 'X') && (play[1][0] == 'X') && play[2][0] == ' ') || ((play[0][2] == 'X') && (play[1][1] == 'X') && play[2][0] == ' ') || ((play[2][1] == 'X') && (play[2][2] == 'X') && play[2][0] == ' '))
 {
  play[2][0] = 'O';
 }
 else if ( ((play[2][0] == 'X') && (play[2][2] == 'X') && play[2][1] == ' ') || ((play[0][1] == 'X') && (play[1][1] == 'X') && play[2][1] == ' '))
 {
  play[2][1] = 'O';
 }
 else if ( ((play[2][0] == 'X') && (play[2][1] == 'X') && play[2][2] == ' ') || ((play[0][2] == 'X') && (play[1][2] == 'X') && play[2][2] == ' ') || ((play[0][0] == 'X') && (play[1][1] == 'X') && play[2][2] == ' '))
 {
  play[2][2] = 'O';
 }
 else if (play[1][1] == ' ')
 {
  play[1][1] = 'O';
 }
 else if ((play[0][0] != 'O')&&(play[0][1] != 'O')&&(play[0][2] != 'O') && (play[1][0] != 'O')&&(play[1][1] != 'O')&&(play[1][2] != 'O')&&(play[2][0] != 'O')&&(play[2][1] != 'O')&&(play[2][2] != 'O'))
 {
  while (1)
  {
   int x = rand() % 3;
   int y = rand() % 3;
   i = x;
   j = y;
   if (play[x][y]==' ')
   {
    play[x][y] = 'O';
    break;
   }
  }
 }
 else
 {
  if (play[i][0] == ' ')
  {
   play[i][0] = 'O';
  }
  else if (play[i][2] == ' ')
  {
   play[i][2] = 'O';
  }
  else if (play[0][j] == ' ')
  {
   play[0][j] = 'O';
  }
  else if (play[2][j] == ' ')
  {
   play[2][j] = 'O';
  }
 }
}
int check_win(char play[ROWS][LOWS], int row, int low)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < row; i++)
 {
  for (j = 0; j < low; j++)
  {
  
     if ((play[i][0] == 'X') && (play[i][1] == 'X') && (play[i][2] == 'X') || (play[0][j] == 'X') && (play[1][j] == 'X') && (play[2][j] == 'X') || (play[0][0] == 'X') && (play[1][1] == 'X') && (play[2][2] == 'X') || (play[0][2] == 'X') && (play[1][1] == 'X') && (play[2][0] == 'X'))
        {
            printf("恭喜玩家赢了!\n");
            return 1;
         }
     }
 }
 for (i = 0; i < row; i++)
 {
  for (j = 0; j < low; j++)
  {
   if ((play[i][0] == 'O') && (play[i][1] == 'O') && (play[i][2] == 'O') || (play[0][j] == 'O') && (play[1][j] == 'O') && (play[2][j] == 'O') || (play[0][0] == 'O') && (play[1][1] == 'O') && (play[2][2] == 'O') || (play[0][2] == 'O') && (play[1][1] == 'O') && (play[2][0] == 'O'))
   {
    printf("遗憾电脑赢了!\n");
    return 1;
   }
  }
 }
 if ((play[0][0] != ' ') && (play[0][1] != ' ') && (play[0][2] != ' ') && (play[1][0] != ' ') && (play[1][1] != ' ') && (play[1][2] != ' ') && (play[2][0] != ' ') && (play[2][1] != ' ') && (play[2][2] != ' '))
 {
  printf("平局!1请重新开始\n");
  return 1;
 }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lao_wine

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值