实现人机对战三子棋,其文件组成形式为头文件和源文件形式,具体如下:
game.h文件中放入函数的声明,以及用到的宏定义,以及头文件的包括.
game.c文件中放的是各部分函数功能的实现.
test.c文件中是测试函数,放的是整个程序实现的主逻辑.
各部分代码如下:
game.h
#ifndef __GANE_H__
#define __GANE_H__ // 头文件组合形式
#define ROWS 3 //行数
#define COLS 3 //列数
#include <stdio.h> //所用的头文件
#include <stdlib.h>
#include <time.h>
#include<string.h>
void print_board (char board[ROWS][COLS],int rows,int cols );//打印表格函数
void init_board (char board[ROWS][COLS],int rows,int cols ); //初始化函数
void player_move (char board[ROWS][COLS],int rows,int cols); //玩家移动函数
void computer_move (char board[ROWS][COLS],int rows,int cols); //电脑移动函数
char f_win(char board[ROWS][COLS],int rows,int cols); //检验函数
#endif //__GANE_H__
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "stdio.h"
#include "game.h"
#include<string.h>
void init_board(char board[ROWS][COLS],int rows,int cols )
{
memset(board,' ',rows*cols*(sizeof(char)));
}
void print_board (char board[ROWS][COLS],int rows,int cols )
{
int i = 0;
for (i=0;i<cols;i++)
{
printf (" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
if (i!=(cols-1))
printf ("---|---|---\n");
}
}
void player_move (char board[ROWS][COLS],int rows,int cols)
{
int x = 0;
int y = 0;
printf ("请输入两个下标>:");
scanf ("%d%d",&x,&y);
x--;
y--;
if ((x>=0)&&(x<=rows-1)&&(y>=0)&&(y<=cols-1))
{
if (board[x][y]==' ')
{
board[x][y]='X';
}
else
{
printf ("下标错误,请重新输入!");
}
}
else
{
printf ("输入有误!");
}
}
void computer_move (char board[ROWS][COLS],int rows,int cols)
{
while (1)
{
int x=rand()%3;
int y=rand()%3;
if (board[x][y]==' ')
{
board[x][y]='0';
break ;
}
}
}
static int f_full(char board[ROWS][COLS],int rows,int cols)
{
int i = 0;
int j = 0;
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
if (board[i][j]==' ')
return 0;
}
return 1;
}
char f_win(char board[ROWS][COLS],int rows,int cols)
{
int i =0;
for (i=0;i<rows;i++)
{
if ((board[i][0]==board[i][1])&&(board[i][1]==board[i][2])&&(board[i][0])!=' ')
return board[i][0];
}
//判断每行的元素是否相同
for (i=0;i<cols;i++)
{
if ((board[0][i]==board[1][i])&&(board[1][i]==board[2][i])&&(board[0][i])!=' ')
return board[0][i];
}
if ((board[0][0]==board[1][1])&&(board[1][1]==board[2][2])&&(board[0][0])!=' ')
return board [0][0];
if ((board[0][2]==board[1][1])&&(board[1][1]==board[2][0])&&(board[1][1])!=' ')
return board [0][2];
if (f_full(board,rows,cols))
{
return 'p';
}
return ' ';
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.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);
print_board(board,ROWS,COLS);
srand((unsigned int)time(NULL));
while (1)
{
printf ("玩家走>\n");
player_move(board,ROWS,COLS);
print_board(board,ROWS,COLS);
if ((ret=f_win(board,ROWS,COLS))!=' ')
break;
printf ("电脑走>\n");
computer_move(board,ROWS,COLS);
print_board(board,ROWS,COLS);
if ((ret=f_win(board,ROWS,COLS))!=' ')
break;
}
ret=f_win(board,ROWS,COLS);
if (ret=='X')
{
printf ("恭喜你,赢了!\n");
}
else if (ret=='0')
{
printf ("不好意思,你输了!\n");
}
else if (ret=='p')
{
printf ("平局!\n");
}
print_board(board,ROWS,COLS);
}
void menu ()
{
printf ("****************************\n");
printf ("***** 1.play 0.exit *****\n");
printf ("****************************\n");
}
int main()
{
int input = 0;
menu();
do
{
printf ("请选择>:");
scanf ("%d",&input);
switch (input)
{
case 1:
{
game();
break ;
}
case 0:
{
break;
}
default:
{
printf("请重新输入>:");
break ;
}
}
}while (input);
return 0;
}
运行结果如下:
希望能帮助你,明天继续,加油!!!