目录
一.game.h(游戏头文件)
二.test.c(游戏调试模块)
三.game.c(游戏模块)
四.实验结果
一.game.h(游戏头文件)
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#define COW 6
#define VER 6
void InitBoard(char board[COW][VER], int cow, int ver);
void Displayboard(char board[COW][VER], int cow, int ver);
void Playermove(char board[COW][VER], int cow, int ver);
void Computermove(char board[COW][VER], int cow, int ver);
char Judgegame(char board[COW][VER], int cow, int ver);
int Draw(char board[COW][VER], int cow, int ver);
二、test.c(测试代码)
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("************************************\n");
printf("*******1.开始游戏 0.退出游戏********\n");
printf("************************************\n");
}
void game()
{
int ret = 0;
char board[COW][VER] = { 0 };
InitBoard(board, COW, VER);
Displayboard(board, COW, VER);
while (1)
{
Playermove(board, COW, VER);
Displayboard(board, COW, VER);
char ret = Judgegame(board, COW, VER);
if (ret == '*')
{
printf("玩家赢\n");
break;
}
Computermove(board, COW, VER);
Displayboard(board, COW, VER);
ret = Judgegame(board, COW, VER);
if (ret == '#')
{
printf("电脑赢\n");
break;
}
if (ret == 'P')
{
printf("平局\n");
}
}
printf("游戏结束\n");
}
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("游戏结束\n");
break;
default:
printf("输入错误,请重新输入!!!!!\n");
break;
}
} while (input);
return 0;
}
三、game.c(游戏模块)
1.游戏操作
2.游戏输赢判断
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char board[COW][VER], int cow, int ver)
{
int i = 0;
int j = 0;
for(i=0;i<cow;i++)
{
for (j=0;j<ver;j++)
{
board[i][j] = ' ';
}
}
}
void Displayboard(char board[COW][VER],int cow,int ver)
{
int i = 0;
int j = 0;
for (i = 0; i < cow;i++)
{
for (j = 0; j < ver; j++)
{
printf(" %c ",board[i][j]);
if(j<ver-1)
printf("|");
}
printf("\n");
for (j = 0; j < ver; j++)
{
if(i<cow-1){
printf("---");
if (j < ver - 1)
printf("|");
}
}
printf("\n");
}
}
void Playermove(char board[COW][VER],int cow,int ver)
{
while (1)
{
int x = 0;
int y = 0;
printf(">>>>>>>>>>>>>>>>>>>玩家走<<<<<<<<<<<<<<<<\n");
printf("请输入一个坐标比如(x y)括号不用:>>>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= cow && y >= 1 && y <= ver)
{
if (board[x-1][y-1] == ' ')
{
board[x - 1][y - 1] ='*';
break;
}
else
{
printf("该位置已经被占用!!!!\n");
}
}
else
{
printf("所输入的坐标超出棋盘范围,请重新输入!\n");
}
}
}
void Computermove(char board[COW][VER], int cow, int ver)
{
printf(">>>>>>>>>>>>>>>>>>>电脑走<<<<<<<<<<<<<<<<\n");
while(1)
{
int x = 0;
int y = 0;
x = rand() % cow;
y = rand() % ver;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int Draw(char board[COW][VER], int cow, int ver)
{
int i = 0;
int j = 0;
for (i = 0; i < cow; i++)
{
for (j = 0; j < ver; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char Judgegame(char board[COW][VER], int cow, int ver)
{
int i = 0;
int j = 0;
for (i = 0; i < cow; i++)
{
for (j = 0; j < ver; j++)
{
if (board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j + 2] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
for (j = 0; j < ver; j++)
{
for (i = 0; i < cow; i++)
{
if (board[i][j] == board[i + 1][j] && board[i + 1][j] == board[i + 2][j] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
for (i = 0; i < cow; i++)
{
for (j = 0; j < ver; j++)
{
if (board[i][j] == board[i + 1][j + 1] && board[i + 1][j + 1] == board[i + 2][j + 2] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
for (i = 0; i < cow; i++)
{
for (j = 0; j < ver; j++)
{
if (board[i][j] == board[i - 1][j - 1] && board[i - 1][j - 1] == board[i - 2][j - 2] && board[i][j] != ' ')
{
return board[i][j];
}
}
for (i = 0; i < cow; i++)
{
for (j = 0; j < ver; j++)
{
if (board[i][j] == board[i - 1][j + 1] && board[i - 1][j + 1] == board[i - 2][j + 2] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
for (i = 0; i < cow; i++)
{
for (j = 0; j < ver; j++)
{
if (board[i][j] == board[i +1][j - 1] && board[i + 1][j - 1] == board[i + 2][j - 2] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
}
char n = Draw(board, COW, VER);
if(n==1)
{
return 'P';
}
}
实验结果
?Github代码(链接)
GITHUB代码链接
|