|
|
//da mu mu de kechengsheji---jngziqi
|
|
|
//game.h 应放入文件中
|
|
|
#ifndef __GAME_H__
|
|
|
#define __GAME_H__
|
|
|
#define ROW 3
|
|
|
#define CLO 3
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <time.h>
|
|
|
#include <memory.h>
|
|
|
void menu(); //打印菜单
|
|
|
void game(char arr[ROW][CLO], int row, int clo); //执行菜单
|
|
|
void play(char arr[ROW][CLO], int row, int clo); //游戏主体
|
|
|
void InitBoard(char arr[ROW][CLO], int row, int clo); //打印棋盘
|
|
|
void Pmove(char arr[ROW][CLO], int row, int clo); //玩家下棋
|
|
|
void Cmove(char arr[ROW][CLO], int row, int clo); //电脑下棋
|
|
|
int Isfull(char arr[ROW][CLO], int row, int clo); //检测盘满
|
|
|
int Iswin(char arr[ROW][CLO], int row, int clo); //判断输赢
|
|
|
#endif
|
|
|
//
|
|
|
//test.c 此为游戏主逻辑部分
|
|
|
#include"stdio.h"
|
|
|
int main()
|
|
|
{
|
|
|
srand((unsigned int)time(NULL)); //产生随机数种子
|
|
|
char arr[ROW][CLO];
|
|
|
game(arr,ROW,CLO);
|
|
|
system("pause");
|
|
|
return 0;
|
|
|
}
|
|
|
//
|
|
|
void menu()
|
|
|
{
|
|
|
printf("******************************\n");
|
|
|
printf("******1.play(开始游戏)******\n");
|
|
|
printf("******2.exit(退出游戏)******\n");
|
|
|
printf("******************************\n");
|
|
|
printf("***(O:代表玩家,X:代表电脑)***\n");
|
|
|
}
|
|
|
|
|
|
//执行菜单
|
|
|
|
|
|
void game(char arr[ROW][CLO], int row, int clo)
|
|
|
{
|
|
|
int input = 0;
|
|
|
do
|
|
|
{
|
|
|
menu();
|
|
|
scanf("%d",&input);
|
|
|
memset(arr, ' ', ROW*CLO * sizeof(arr[0][0]));
|
|
|
switch (input)
|
|
|
{
|
|
|
case 1:
|
|
|
play(arr, row, clo); break;
|
|
|
case 2:
|
|
|
exit(0); break;
|
|
|
default:
|
|
|
printf("小可爱,你是不是按错键啦?\n");
|
|
|
}
|
|
|
} while (input);
|
|
|
}
|
|
|
|
|
|
//游戏主体
|
|
|
|
|
|
void play(char arr[ROW][CLO], int row, int clo)
|
|
|
{
|
|
|
int flag = 0;
|
|
|
InitBoard(arr, row, clo);
|
|
|
while (1)
|
|
|
{
|
|
|
printf("人类走\n");
|
|
|
Pmove(arr, row, clo);
|
|
|
InitBoard(arr, row, clo);
|
|
|
flag = Iswin(arr, row, clo);
|
|
|
if (flag == 1)
|
|
|
{
|
|
|
printf("亲爱的玩家,恭喜你赢啦!你可真是个小机灵鬼!\n");
|
|
|
break;
|
|
|
}
|
|
|
flag = Isfull(arr, row, clo);
|
|
|
if (flag == 4)
|
|
|
{
|
|
|
printf("棋盘满啦,平局平局,你挺不错哟!游戏结束\n");
|
|
|
break;
|
|
|
}
|
|
|
printf("电脑走\n");
|
|
|
Cmove(arr, row, clo);
|
|
|
InitBoard(arr, row, clo);
|
|
|
flag = Iswin(arr, row, clo);
|
|
|
if (flag == 1)
|
|
|
{
|
|
|
printf("居然是电脑赢了,哎~你有点菜哟?\n");
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//棋盘打印
|
|
|
|
|
|
void InitBoard(char arr[ROW][CLO], int row, int clo)
|
|
|
{
|
|
|
int i = 0;
|
|
|
int j = 0;
|
|
|
for (j = 0; j < row; j++)
|
|
|
{
|
|
|
for (i = 0; i < clo; i++)
|
|
|
{
|
|
|
printf(" %c ", arr[j][i]);
|
|
|
if (i < (clo - 1))
|
|
|
{
|
|
|
printf("|");
|
|
|
}
|
|
|
}
|
|
|
printf("\n");
|
|
|
if (j < row - 1)
|
|
|
{
|
|
|
for (i = 0; i < clo; i++)
|
|
|
{
|
|
|
printf("---");
|
|
|
if (i < (clo - 1))
|
|
|
{
|
|
|
printf("|");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
printf("\n");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//玩家下棋
|
|
|
|
|
|
void Pmove(char arr[ROW][CLO], int row, int clo)
|
|
|
{
|
|
|
printf("请输入坐标(空格隔开)\n");
|
|
|
int m = 0, n = 0;
|
|
|
while (1)
|
|
|
{
|
|
|
scanf("%d %d", &m, &n);
|
|
|
if (((m >= 1) && (m <= 3)) && ((n >= 1) && (n <= 3)))
|
|
|
{
|
|
|
if (arr[m - 1][n - 1] == ' ')
|
|
|
{
|
|
|
arr[m - 1][n - 1] = 'O';
|
|
|
break;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
printf("嘿!小伙子,这里可能已经有棋子了哦?不能再走啦!\n");
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
printf("再想想?可能你输错了哦!\n");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//判断输赢
|
|
|
|
|
|
int Iswin(char arr[ROW][CLO], int row, int clo)
|
|
|
{
|
|
|
//行
|
|
|
int i = 0;
|
|
|
for (i = 0; i < row; i++)
|
|
|
{
|
|
|
if (((arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2])) && ((arr[i][0] != ' ')))
|
|
|
{
|
|
|
return 1;
|
|
|
}
|
|
|
}
|
|
|
//列
|
|
|
for (i = 0; i < clo; i++)
|
|
|
{
|
|
|
if ((arr[0][i] == arr[1][i]) && (arr[1][i] == arr[2][i]) && ((arr[0][i] != ' ') ))
|
|
|
{
|
|
|
return 1;
|
|
|
}
|
|
|
}
|
|
|
//圈
|
|
|
if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) && ((arr[0][0] != ' ') ))
|
|
|
{
|
|
|
return 1;
|
|
|
}
|
|
|
if ((arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0]) && ((arr[0][2] != ' ')))
|
|
|
{
|
|
|
return 1;
|
|
|
}
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
//电脑下棋(核心)
|
|
|
|
|
|
void Cmove(char arr[ROW][CLO], int row, int clo)
|
|
|
{
|
|
|
int flag = 0;
|
|
|
int m = 0, n = 0;
|
|
|
for (m = 0; m < row; m++)
|
|
|
{
|
|
|
for (n = 0; n < row; n++)
|
|
|
{
|
|
|
if (arr[m][n] == ' ')
|
|
|
{
|
|
|
arr[m][n] = 'X';
|
|
|
flag = Iswin(arr, row, clo);
|
|
|
if (flag == 0)
|
|
|
{
|
|
|
arr[m][n] = ' ';
|
|
|
}
|
|
|
if (flag == 1)
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if (flag == 1)
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
if (flag == 0)
|
|
|
{
|
|
|
for (m = 0; m < row; m++)
|
|
|
{
|
|
|
for (n = 0; n < row; n++)
|
|
|
{
|
|
|
if (arr[m][n] == ' ')
|
|
|
{
|
|
|
arr[m][n] = 'O';
|
|
|
flag = Iswin(arr, row, clo);
|
|
|
if (flag == 0)
|
|
|
{
|
|
|
arr[m][n] = ' ';
|
|
|
}
|
|
|
if (flag == 1)
|
|
|
{
|
|
|
arr[m][n] = 'X';
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if (flag == 1)
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if ((m == 3) && (n == 3))
|
|
|
{
|
|
|
while (1)
|
|
|
{
|
|
|
m = rand() % 3;
|
|
|
n = rand() % 3;
|
|
|
if (arr[m][n] == ' ')
|
|
|
{
|
|
|
arr[m][n] = 'X';
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//判断盘满
|
|
|
|
|
|
int Isfull(char arr[ROW][CLO], int row, int clo)
|
|
|
{
|
|
|
int i = 0, j = 0;
|
|
|
int count = 0;
|
|
|
for (i = 0; i < row; i++)
|
|
|
{
|
|
|
for (j = 0; j < clo; j++)
|
|
|
{
|
|
|
if (arr[i][j] != ' ')
|
|
|
{
|
|
|
count ++;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if (count == 9)
|
|
|
{
|
|
|
return 4;
|
|
|
}
|
|
|
return 3;
|
|
|
}
|
|
|
|
|
|
|