扫雷 三子棋的实现



三子棋


game.h

#ifndef __GAME_H__
#define __GAME_H__

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>

#define ROWS 3
#define COLS 3

void init_board(char board[ROWS][COLS],int row,int col);
void display_board(char board[ROWS][COLS],int row,int col);
void player_move(char board[ROWS][COLS],int row,int col);
void computer_move(char board[ROWS][COLS],int row,int col);
char check_win(char board[ROWS][COLS],int row,int col);


#endif  //__GAME_H__



头文件,定义函数名称


game.c

#include"game.h"

#define _CRT_SECURE_NO_WARNINGS 1


void init_board(char board[ROWS][COLS],int row,int col)
{
 int i = 0;
 int j = 0;
 for (i = 0;i < row;i++)
 {
  for (j = 0; j < col; j++)
   board[i][j] = ' ';
 }
}


void display_board(char board[ROWS][COLS],int row,int col)
{
 int i = 0;
 for (i = 0; i < row;i++)
 {
  printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
     if (i < 2)
   printf("---|---|---\n");
 }

}


void player_move(char board[ROWS][COLS],int row,int col)
{
 int x = 0;
 int y = 0;
 while(1)
 {
  printf ("玩家走:> ");
     scanf_s ("%d%d",&x,&y);
      x--;
     y--;
     if (x >= 0 && x <= 2 && y >= 0 && y <= 2)
     {
      if (board[x][y] == ' ')
      {
       board[x][y] = '$';
    break;
      }
         else
      {
          printf("输入错误,请重新输入:>\n");
      }
     }
     else
      printf ("输入错误,请重新输入:>\n");
 }
}


void computer_move(char board[ROWS][COLS],int row,int col)
{
 while(1)
 {
  int x = rand()%3;
     int y = rand()%3;

     if (board[x][y] == ' ')
  {
      board[x][y] = '*';
   break;
  }
 }
}

static int is_full(char board[ROWS][COLS],int row,int col)
{
     int i = 0;
  int j = 0;
  for (i = 0;i < row;i++)
  {
   for (j = 0;j < col;j++)
   {
    if (board[i][j] == ' ')
     return 0;
   }
  }
  return 1;
}


char check_win(char board[ROWS][COLS],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][1] != ' '))
   return board[i][1];
 }
 for (j = 0;j < col;j++)
 {
  if((board[0][j] == board[1][j])&&(board[1][j] == board[2][j])&&(board[1][j] != ' '))
         return board[1][j]; 
 }

    if((board[0][0] == board[1][1])&&(board[1][1] == board[2][2])&&(board[1][1] != ' '))
 {
  return board[1][1];
 }
 if((board[0][2] == board[1][1])&&(board[1][1] == board[2][0])&&(board[1][1] != ' '))
 {
  return board[1][1];
 }           
 if(is_full(board,row,col) == 1)
 {
  return 'q';
 }
  return ' ';
}


game.c 文件实现对棋盘的初始化,打印,还有输入坐标后的输出,以及 对于 输赢的判断

test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>

#include"game.h"


void game()
{
 char board[ROWS][COLS] = {0};
 char ret = 0;
 init_board(board,ROWS,COLS);
 display_board(board,ROWS,COLS);
 srand((unsigned int)time(NULL));

 while(1)
 {
     player_move(board,ROWS,COLS);
  if ((ret = check_win(board,ROWS,COLS))!=' ')
   break;
     display_board(board,ROWS,COLS);
     computer_move(board,ROWS,COLS);
  if ((ret = check_win(board,ROWS,COLS))!=' ')
   break;
     printf ("电脑走:> \n");
     display_board(board,ROWS,COLS); 
 }

 printf ("\n");
 display_board(board,ROWS,COLS);
 if (ret == '$')
 {
  printf ("\n恭喜你!玩家赢!!!!!\n\n");
 }
 else if (ret == '*')
 {
  printf ("\n很遗憾,电脑赢。\n\n");
 }
 else if (ret == 'q')
 {
  printf ("\n平局\n\n");
 }
   
}

void menu()
{
 printf("****************************\n");
 printf("***    1.play   0.exit   ***\n");
 printf("****************************\n");
}


int main()
{
    int input = 0;
 do
 {
  menu();
  printf("请选择:>");
  scanf("%d",&input);
  switch(input)
  {
  case 1:
   game();
   break;
  case 0:
   printf ("退出游戏!\n");
   break;
  default:
   printf ("选择错误!\n");
   break;
  }
 }while(input);
 return 0;
}

test.c文件主要是对于整个函数的应用,







扫雷游戏

game.h

#ifndef __GAME_H__
#define __GAME_H__

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>


enum Option
{
 EXIT,
    PLAY
};

#define ROW 10
#define COL 10
#define ROWS ROW+2
#define COLS COL+2

#define EASY_COUNTS 10

void InitBoard(char arr[ROWS][COLS],int row,int col,char set);
void DisplayBoard(char arr[ROWS][COLS],int row,int col);
void SetMine(char arr[ROWS][COLS],int row,int col,char count);
int GetMineCount(char arr[ROWS][COLS],int x,int y);
int check_set(char arr[ROWS][COLS],int x,int y);
void First_Input(char arr[ROWS][COLS],int x, int y);
//void Test_set(char arr[ROWS][COLS],int x, int y);
//int showAroundBlock(char arr[ROWS][COLS],int x,int y);//这里就是打印该点周围八个格子的数字;


#endif //__GAME_H__


头文件,对于函数名称的定义,以及在库函数的引用


game.c

#define _CRT_SECURE_NO_WARNINGS 1
 
#include "game.h"
#include<string.h>

void InitBoard(char arr[ROWS][COLS],int row,int col,char set)
{
 int i = 0;
 int j = 0;
 for (i = 0;i < row; i++)
 {
  for(j = 0;j < col; j++)
  {
   arr[i][j] = set;
  }
 }
}

void DisplayBoard(char arr[ROWS][COLS],int row,int col)
{
    int i = 0;
 int j = 0;
 printf ("   ");
 for (i = 1; i <= row;i++)
 {
  printf ("%2d ",i);
 }
 printf ("\n");

 for (i = 1;i <= row;i++)
 {
  printf ("%2d ",i);
  for (j = 1;j <=col;j++)
  {
   printf("%2c ",arr[i][j]);
  }
  printf ("\n");
 }  
}

void SetMine(char arr[ROWS][COLS],int row,int col,char count)
{
 int x = 0;
 int y = 0;
 char mine[ROWS][COLS] = {0};
 while(count)
 {
  x = rand()%10+1;
  y = rand()%10+1;
  if (arr[x][y] == '0')
  {
      arr[x][y] = 'a';
         count --;
  }
 }
}

int GetMineCount(char arr[ROWS][COLS],int x,int y)
{
 int count = 0;
 if (arr[x - 1][y - 1] == 'a')
 {
  count ++;
 }
 if (arr[x - 1][y] == 'a')
 {
  count ++;
 }
    if (arr[x - 1][y + 1] == 'a')
 {
  count ++;
 }
 if (arr[x][y + 1] == 'a')
 {
  count ++;
 }
 if (arr[x + 1][y + 1] == 'a')
 {
  count ++;
 }
 if (arr[x + 1][y] == 'a')
 {
  count ++;
 }
 else if (arr[x + 1][y - 1] == 'a')
 {
  count ++;
 }
 if (arr[x][y - 1] == 'a')
 {
  count ++;
 }
 return count;
}

int check_set(char arr[ROWS][COLS],int x,int y)
{
 
 if ( arr[x][y] == 'a')
 {
  printf ("\n WHAT A PITY ! GAME OVER ! \n\n\n");
  return 0;
 }
}

void First_Input(char arr[ROWS][COLS],int x, int y)
{
 if (arr[x][y] == 'a')
 {
  x = rand()%10+1;
  y = rand()%10+1;
  if (arr[x][y] == '0')
  {
      arr[x][y] = 'a';
  }
 }
}


初始化定义扫雷盘,然后打印两个棋盘,随机输入雷,之后,保证第一次点击,不结束游戏,然后判断是否踩到雷,是否结束游戏



test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"
#include<time.h>


void menu()
{
 printf ("*************************\n");
 printf ("***  1.play   0.exit  ***\n");
 printf ("*************************\n");
}

int game()
{
 int x = 0;
 int y = 0;
 int count = 0;
 char show[ROWS][COLS] = {0};
 char mine[ROWS][COLS] = {0};
 srand((unsigned int) time (NULL));

    InitBoard(show,ROWS,COLS,'*');
    InitBoard(mine,ROWS,COLS,'0');
    DisplayBoard(show,ROW,COL);
 SetMine(mine,ROWS,COLS,EASY_COUNTS);
    //DisplayBoard(mine,ROW,COL);
 printf ("请输入坐标:>");
 scanf ("%d%d",&x,&y);
    First_Input(show,x, y);
 show[x][y] = '0'+count;
    DisplayBoard(show,ROW,COL);
 while(1)
 {
        //DisplayBoard(show,ROW,COL);
     printf ("请输入坐标:>");
     scanf ("%d%d",&x,&y);
        count = GetMineCount(mine,x,y);
     show[x][y] = '0'+count;
  //Test_set(x,y);
        DisplayBoard(show,ROW,COL);
  if ((check_set(mine,x,y)) == 0)
      return 0;
 }
 return 0;
}


int main ()
  {
 int input = 0;
 do
 {
  menu();
     printf ("请选择:>");
     scanf ("%d",&input);
  switch(input)
  {
  case 1:
   {
    game();
    break;
   }
  case 0:
   {
    printf ("游戏结束\n");
    break;
   }
  default:
   {
    printf ("选择错误\n");
                break;
   }
  }
 }
 while (input);

 return 0;
}


对于函数的引用,以及整个游戏的正常运作


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值