要实现三子棋游戏,首先要了解三子棋的规则,方便我们的编程和理解。
规则如下:
在九宫格棋盘上,只要将三个棋子走成一条线(横、竖、对角线),对方就算输了。
首先,我们的游戏程序应该先有个菜单页面,这个菜单页面用来让用户操作选择玩游戏。我们可以用函数去实现,先建立一个game.h作为头文件,在定义一个game.c文件来实现具体的函数功能(创建棋盘,初始化棋盘等。),最后定义一个main.c文件主要编写实现这个游戏。
1.game.h(头文件)
game.h(头文件)
#ifndef __GAME_H__
#define __GAME_H__
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void InitBoard(char board[][3],int row,int col);
void ShowBoard(char board[][3],int row,int col);
void PlayerMove(char board[][3],int row,int col);
char IsWin(char board[][3],int row,int col);
void ComputerMove(char board[][3],int row,int col);
#endif//__GAME_H__
2.main.c(主函数)
#include "game.h"
void menu()
{
printf("*********************\n");
printf("*******1.play********\n");
printf("*******0.exit*********\n");
printf("*********************\n");
}
void game()
{
char ret = 0;
char board[3][3] = {0};
InitBoard(board,3,3);
ShowBoard(board,3,3);
while(1)
{
PlayerMove(board,3,3);
ret = IsWin(board,3,3);
if(ret != ' ')
{
break;//玩家胜出或者平局
}
ShowBoard(board,3,3);
ComputerMove(board,3,3);
ret = IsWin(board,3,3);
if(ret != ' ')
{
break;//电脑胜出或者平局
}
ShowBoard(board,3,3);
}
if(ret == 'Q')
{
printf("平局\n");
}
else if(ret == 'X')
{
printf("玩家胜出\n");
}
else if(ret == 'O')
{
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;
}
3.game.c
#include "game.h"
void InitBoard(char board[][3],int row,int col)
{
int i = 0;
for(i = 0;i < row;i++)
{
int j = 0;
for(j = 0;j < col;j++)
{
board[i][j] = ' ';
}
}
//memset(board,' ',row*col*sizeof(char));
}
void ShowBoard(char board[][3],int row,int col)
{
int i = 0;
int j = 0;
for(i = 0;i < row;i++)
{
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[][3],int row,int col)
{
int x = 0;
int y = 0;
while(1)
{
printf("请输入1-3的坐标:");//X
scanf("%d%d",&x,&y);//1,1
if(x>=1 && x<=3 && y>=1 && y <= 3)
{
if(board[x-1][y-1] == ' ')
{
board[x-1][y-1] = 'X';
break;
}
else
{
printf("此位置已经被占用了\n");
}
}
else
{
printf("坐标位置不合法\n");
}
}
}
static int IsFull(char board[][3],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] == ' ')
{
return 0;//没有满
}
}
}
return 1;//满
}
char IsWin(char board[][3],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][0]!= ' ')
{
return board[i][0];
}
//列
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[0][0] != ' ')
{
return board[0][2];
}
//平局
if(IsFull(board,row,col) == 1)
{
return 'Q';
}
}
//即没有赢,也没有平局
return ' ';
}
void ComputerMove(char board[][3],int row,int col)
{
int x = 0;
int y = 0;
printf("电脑移动\n");
while(1)
{
x = rand()%row+1;//3 [0-3) [1-3]
y = rand()%col+1;
if(board[x-1][y-1] == ' ')
{
board[x-1][y-1] = 'O';
break;
}
}
}