初学者,还在学,写的差,谅解
Game.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#define ROW 3
#define COL 3
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);
//打印棋盘
void DispalyBoard(char board[ROW][COL], int row, int col);
//玩家下棋
void PalyerMove(char board[ROW][COL], int row, int col);
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col);
//判断输赢
//玩家赢 - '*'
//电脑赢 - '#'
//平局 - 'Q'
//继续 - 'C'
char WinningBoard(char board[ROW][COL], int row, int col);
Test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
printf("*******************************\n");
printf("***********1.play**************\n");
printf("***********0.exlt**************\n");
printf("*******************************\n");
}
void game()
{
char ret = 0;
char board[ROW][COL] = { 0 };
//初始化棋盘的函数
InitBoard(board,ROW,COL);
DispalyBoard(board, ROW, COL);
//开始下棋
while (1)
{
PalyerMove(board,ROW,COL);
//判断输赢
ret = WinningBoard(board, ROW, COL);
if (ret != 'C')
{
break;
}
DispalyBoard(board, ROW, COL);
ComputerMove(board,ROW,COL);
//判断输赢
ret = WinningBoard(board, ROW, COL);
if (ret != 'C')
{
break;
}
DispalyBoard(board, ROW, COL);
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else
{
printf("平局\n");
}
DispalyBoard(board, ROW, COL);
}
int main()
{
srand((unsigned int )time(NULL));//设置随机数的生成
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;
}
Game.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void InitBoard(char board[ROW][COL], 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 DispalyBoard(char board[ROW][COL], 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<row-1)
// printf("---|---|---\n");
// }
//}
//第二个版本
void DispalyBoard(char board[ROW][COL], 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]);*/
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
//打印分割线
/*printf("---|---|---\n");*/
if (i < row - 1)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void PalyerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋\n");
while (1)
{
printf("坐标行范围%d,坐标列范围%d,注意XY数值之间添加一个空格\n", ROW, COL);
printf("请输入坐标:>");
scanf("%d %d", &x, &y);
//坐标范围合法判断
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标被占用请选择其他位置\n");
}
}
else
{
printf("坐标输入非法请重新输入\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
//{
// printf("电脑下棋\n");
//
// int x = 0;
// int y = 0;
//
//
// while (1)
// {
// x = rand() % row;
// y = rand() % col;
// if (board[x][y] == ' ')
// {
// board[x][y] = '#';
// break;
// }
// }
//}
{
int x = 0;
int y = 0;
int z = 0;
printf("电脑下棋:\n\n");
z = check_computer(board, row, col);//电脑下棋(能赢就赢,玩家要赢就拦截玩家)
//第一次落子
while (0 == z)
{
x = rand() %row;
y = rand() %col;
if (board[1][1] == ' ')
{
//如果中心位置玩家没有下则优先下到中心位置
board[1][1] = '#';
z = 1;
break;
}
if (board[x][y] == ' ')
{
//没有的话就随机下一个位置
board[x][y] = '#';
z = 1;
break;
}
}
}
char judge_player(char board[ROW][COL], int row, int col, int k)//电脑优化算法,堵截玩家
{
int i = 0;
int j = 0;
while (0 == k)
{
for (i = 0; i < row; i++) //判断玩家在横行上是否会赢,
{
if ((board[i][0] == board[i][1])
&& (board[i][1] == '*')
&& (board[i][2] == ' '))
{
board[i][2] = '#';
k = 1;
break;
}
if ((board[i][0] == board[i][2])
&& (board[i][0] == '*')
&& (board[i][1] == ' '))
{
board[i][1] = '#';
k = 1;
break;
}
if ((board[i][1] == board[i][2])
&& (board[i][1] == '*')
&& (board[i][0] == ' '))
{
board[i][0] = '#';
k = 1;
break;
}
}
if (k != 0)
break;
for (j = 0; j < col; j++) //判断玩家在竖列上是否会赢
{
if ((board[0][j] == board[1][j])
&& (board[1][j] == '*')
&& (board[2][j] == ' '))
{
board[2][j] = '#';
k = 1;
break;
}
if ((board[0][j] == board[2][j])
&& (board[2][j] == '*')
&& (board[1][j] == ' '))
{
board[1][j] = '#';
k = 1;
break;
}
if ((board[1][j] == board[2][j])
&& (board[2][j] == '*')
&& (board[0][j] == ' '))
{
board[0][j] = '#';
k = 1;
break;
}
}
break;
}
while (0 == k) //判断玩家在对角线上是否会赢
{
if ((board[0][0] == board[1][1])
&& (board[1][1] == '*')
&& (board[2][2] == ' '))
{
board[2][2] = '#';
k = 1;
break;
}
if ((board[0][0] == board[2][2])
&& (board[2][2] == '*')
&& (board[1][1] == ' '))
{
board[1][1] = '#';
k = 1;
break;
}
if ((board[1][1] == board[2][2])
&& (board[1][1] == '*')
&& (board[0][0] == ' '))
{
board[0][0] = '#';
k = 1;
break;
}
if ((board[0][2] == board[1][1])
&& (board[0][2] == '*')
&& (board[2][0] == ' '))
{
board[2][0] = '#';
k = 1;
break;
}
if ((board[0][2] == board[2][0])
&& (board[2][0] == '*')
&& (board[1][1] == ' '))
{
board[1][1] = '#';
k = 1;
break;
}
if ((board[1][1] == board[2][0])
&& (board[2][0] == '*')
&& (board[0][2] == ' '))
{
board[0][2] = '#';
k = 1;
break;
}
break;
}
return k;
}
int check_computer(char board[ROW][COL], int row, int col) //电脑判断自己是否会赢
{
int i = 0;
int j = 0;
int k = 0;
//判断电脑自己在横行、纵行、对角线上是否能赢(3颗棋子能否连成一条线),能赢就落子
while (0 == k)
{
for (i = 0; i < row; i++)//判断电脑横行上是否会赢
{
if ((board[i][0] == board[i][1])
&& (board[i][1] == '#')
&& (board[i][2] == ' '))
{
board[i][2] = '#';
k = 1;
break;
}
if ((board[i][0] == board[i][2])
&& (board[i][0] == '#')
&& (board[i][1] == ' '))
{
board[i][1] = '#';
k = 1;
break;
}
if ((board[i][1] == board[i][2])
&& (board[i][1] == '#')
&& (board[i][0] == ' '))
{
board[i][0] = '#';
k = 1;
break;
}
}
if (k != 0)
{
break;
}
for (j = 0; j < col; j++)//判断电脑列上是否会赢
{
if ((board[0][j] == board[1][j])
&& (board[1][j] == '#')
&& (board[2][j] == ' '))
{
board[2][j] = '#';
k = 1;
break;
}
if ((board[0][j] == board[2][j])
&& (board[2][j] == '#')
&& (board[1][j] == ' '))
{
board[1][j] = '#';
k = 1;
break;
}
if ((board[1][j] == board[2][j])
&& (board[2][j] == '#')
&& (board[0][j] == ' '))
{
board[0][j] = '#';
k = 1;
break;
}
}
break;
}
while (0 == k)
{
if ((board[0][0] == board[1][1])//判断对角线上是否会赢
&& (board[1][1] == '#')
&& (board[2][2] == ' '))
{
board[2][2] = '#';
k = 1;
break;
}
if ((board[0][0] == board[2][2])
&& (board[2][2] == '#')
&& (board[1][1] == ' '))
{
board[1][1] = '#';
k = 1;
break;
}
if ((board[1][1] == board[2][2])
&& (board[1][1] == '#')
&& (board[0][0] == ' '))
{
board[0][0] = '#';
k = 1;
break;
}
if ((board[0][2] == board[1][1])
&& (board[0][2] == '#')
&& (board[2][0] == ' '))
{
board[2][0] = '#';
k = 1;
break;
}
if ((board[0][2] == board[2][0])
&& (board[2][0] == '#')
&& (board[1][1] == ' '))
{
board[1][1] = '#';
k = 1;
break;
}
if ((board[1][1] == board[2][0])
&& (board[2][0] == '#')
&& (board[0][2] == ' '))
{
board[0][2] = '#';
k = 1;
break;
}
break;
}
k = judge_player(board, row, col, k);
return k;
}
//如果满了返回1 不满返回0
int isFull(char board[ROW][COL], 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 WinningBoard(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] == board[i][j - 1] && board[i][j - 1] == board[i][j + 1] && board[i][j - 1] != ' ')
{
return board[i][j - 1];
}
}
/*if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}*/
}
//列
int j = 0;
for (j = 0; j < col; j++)
{
int i = 0;
for (i = 0; i < row; i++)
{
if (board[i][j] == board[i - 1][j] && board[i - 1][j] == board[i + 1][j] && board[i - 1][j] != ' ')
{
return board[i - 1][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 (isFull(board, row, col))
{
return 'Q';
}
//游戏继续
return 'C';
}
扫雷
game.h
#pragma once
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);
void DispalyBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS],int row,int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//void MarkMine(char show[ROWS][COLS],char mark[ROWS][COLS], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include"game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DispalyBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("-----------------扫雷游戏---------------\n");
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-----------------扫雷游戏---------------\n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int get_mine_count(char board[ROWS][COLS], int x, int y)
{
return (board[x - 1][y] +
board[x - 1][y - 1] +
board[x][y - 1] +
board[x + 1][y - 1] +
board[x + 1][y] +
board[x + 1][y + 1] +
board[x][y + 1] +
board[x - 1][y + 1] - 8 * '0');
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0; int y = 0;
int win = 0;//找到非雷的个数
while (win<row*col-EASY_COUNT)
{
printf("请输入坐标: ");
scanf("%d%d",&x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*')
{
printf("改坐标已经被排查过");
}
else
{
//如果是雷
if (mine[x][y] == '1')
{
printf("很遗憾你被炸死了\n");
DispalyBoard(mine, ROW, COL);
break;
}
//如果不是雷
else
{
win++;
//统记mine数组中x,y坐标周围有几个雷
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';//转换成字符数字
DispalyBoard(show, ROW, COL);
}
}
}
else
{
printf("输入非法请重新输入");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你成功排雷\n");
DispalyBoard(mine, ROW, COL);
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include"game.h"
void menu()
{
printf("***********************\n");
printf("********1.paly*********\n");
printf("********0.exit*********\n");
printf("***********************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };//存放布置好的雷
char show[ROWS][COLS] = { 0 };//存放排查出的雷的
char mark[ROWS][COLS] = { 0 };
//初始化数组的内容
//mine没有布置雷的时候都是‘0‘
InitBoard(mine,ROWS,COLS,'0');
//show数组没有排查雷的时候都是’*‘
InitBoard(show, ROWS, COLS,'*');
//DispalyBoard(mine, ROW, COL);
//设置雷
SetMine(mine,ROW,COL);
DispalyBoard(show, ROW, COL);
//标记雷
//MarkMine(show,mark, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
//设置随机数的生成
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏");
break;
default:
printf("选择错误");
break;
}
} while (input);
return 0;
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容