#ifndef _GAME_H_
#define _GAME_H_
#include
#include"game.h"
#include
#include
#include
#pragma warning(disable:4996)
typedef unsigned int uint;
enum OPTION
{
EXIT,
START
};
#define ROW 3
#define COL 3
void menu();
void init_board(char board[ROW][COL], int row, int col);
void print_board(char board[ROW][COL], int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
void paiyer_move(char board[ROW][COL], int row, int col);
int is_full(char board[ROW][COL], int row, int col);
char is_win(char board[ROW][COL], int row, int col);
#endif //_GAME_H_
#include"game.h"
void menu()
{
printf("***** 《三子棋游戏》*****\n");
printf("*************************\n");
printf("****** 1.play *******\n");
printf("****** 0.exit *******\n");
printf("*************************\n");
printf("*************************\n");
}
void game()
{
char ret;
char board[ROW][COL];
init_board(board, ROW, COL);
print_board(board, ROW, COL);
while (1)
{
printf("玩家走:\n");
paiyer_move(board, ROW, COL);
print_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != ' ')
break;
printf("电脑走:\n");
computer_move(board, ROW, COL);
print_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != ' ')
break;
}
if (ret == '#')
{
printf("恭喜你赢了!\n");
}
else if (ret == '*')
{
printf("很遗憾,你输了\n");
}
else if (ret == 'Q')
{
printf("平局\n");
}
}
int main()
{
int input = 0;
srand((uint)time(NULL));
do {
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case START:
game();
break;
case EXIT:
break;
default:
break;
}
} while (input);
return 0;
}
【函数实现】:
①初始化棋盘
void init_board(char board[ROW][COL], int row, int col)
{
memset(board, ' ', sizeof(board[0][0] )* row*col);
}
//memset函数用来对一段内存空间全部设置为某个字符
不知道memset函数作用的朋友请戳——> memset
void print_board(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
if (i != row-1)
{
printf("---|---|---\n");
}
}
}
打印效果图:
③玩家下棋
void paiyer_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("请输入落子的坐标:\n");
while (1)
{
scanf("%d%d", &x, &y);
//保证输入坐标的合法性,玩家输入的坐标是从1开始的
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 computer_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;
y = rand() % col;//利用rand函数生成随机坐标
if (board[x][y] == ' ')
{
board[x][y] = '*';
break;
}
}
}
不知道rand函数作用的朋友请戳——>rand
static int is_full(char board[ROW][COL], int row, int col)
//函数is_full只在is_win中被调用,可以加上static改变其链接属性
{
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 is_win(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][2] != ' '))
{
return board[i][1];
}
}
for (j = 0; j < col; j++)
{
if ((board[0][j] == board[1][j]) && (board[1][j] == board[2][j]) && (board[2][j] != ' '))
{
return board[1][j];
}
}
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && (board[2][2] != ' '))
{
return board[1][1];
}
else if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[1][1] != ' '))
{
return board[1][1];
}
else if (is_full(board,ROW,COL) == 1)
{
return 'Q';//表示棋盘下满了
}
else
return ' ';
}