首先简述一下:九宫格的棋盘,连成三个连续的即为胜
现在拆分三子棋的步骤
(1)打印菜单,1开始 0退出
(2)初始化棋盘
(3)打印棋盘
(4)玩家下棋,子为’*’
(5)判断
(6)电脑下棋,为‘#’
(7)判断
(8)返回步骤三
现在分析过后,对其进行编写
写game.h(头文件) game.c(游戏主体,函数文件) test.c(测试)
我们创建的棋盘大致为
下边先写game.h
#define ROW 3
#define COL 3
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void InitBoard(char board[ROW][COL],int row,int col);
void DisplayBoard(char board[ROW][COL],int row,int col);
void PlayerMove(char board[ROW][COL],int row,int col);
void ComputerMove(char board[ROW][COL],int row,int col);
char IsWin(char board[ROW][COL],int row,int col);
game.c
#include<stdio.h>
#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 DisplayBoard(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++)
{
printf(" %c ",board[i][j]);
if(j<col-1)
printf("|");
}
printf("\n");
if(i<row-1)
{
for(j=0;j<col;j++)
{
printf("---");
if(j<col-1)
printf("|");
}
printf("\n");
}
}
}
//玩家下
void PlayerMove(char board[ROW][COL],int row,int col)
{
int x=0;
int y=0;
printf("玩家走:>\n");
while(1)
{
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)
{
int x=0;
int y=0;
printf("电脑走:>\n");
while(1)
{
x=rand()%row;
y=rand()%col;
if(board[x][y]==' ')
{
board[x][y]='#';
break;
}
}
}
//判断是否下满
//下满则返回1,为判断是否平局
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 IsWin(char board[ROW][COL],int row,int col)
{
int i=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(i=0;i<col;i++)
{
if(board[0][i]==board[1][i]&&board[1][i]==board[2][i]&&board[1][i]!=' ')
return board[1][i];
}
//对角线
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)==1)
{
return 'Q';
}
return 'c';
}
test.c
#include"game.h"
#include <unistd.h>
//菜单
void menu()
{
printf("******************************\n");
printf("*****play:1 not play:0 *****\n");
printf("******************************\n");
}
//游戏过程
void game()
{
char ret = 0;
char board[ROW][COL]={0};
InitBoard(board,ROW,COL);
DisplayBoard(board,ROW,COL);
while(1)
{
PlayerMove(board,ROW,COL);
system("cls");
DisplayBoard(board,ROW,COL);
ret=IsWin(board,ROW,COL);
if(ret!='c')
{
break;
}
ComputerMove(board,ROW,COL);
sleep(1);
system("cls");
DisplayBoard(board,ROW,COL);
ret=IsWin(board,ROW,COL);
if(ret!='c')
{
break;
}
}
if(ret=='*')
printf("玩家胜!\n");
else if(ret=='#')
printf("电脑胜!\n");
else
printf("平局\n");
sleep(2);
system("cls");
}
void test()
{
int input=0;
srand((unsigned int)time(NULL));//为给电脑提供随机数字的准备
do
{
menu();
printf("请选择:>");
scanf("%d",&input);
switch(input)
{
case 1:
system("cls");
printf("三子棋开始\n");
game();
break;
case 0:
printf("游戏结束\n");
break;
default:
printf("选择错误,请重新选择\n");
break;
}
}
while(input);
}
int main( )
{
test();
return 0;
}
现在井字棋已经实现了
如有错误,请指出,也欢迎讨论