目录
一、头文件(game.h)
#define Row 3
#define Col 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//头文件中声明函数
//棋盘初始化
void chessboard_initialize(char board[Row][Col], int row, int col);
//打印棋盘
void print_board(char board[Row][Col], int row, int col);
//玩家下棋
void player_time(char board[Row][Col], int row, int col);
//判断输赢
char is_win(char board[Row][Col], int row, int col);
//电脑下棋
void computer_time(char board[Row][Col], int row, int col);
二、主程序(test.c)
#include "三子棋game.h"
void menu()
{
printf("****************************\n");
printf("***** 开始游戏--1 ****\n");
printf("***** 结束游戏--0 ****\n");
printf("****************************\n");
}
void game()
{
char ch = 0;
//棋盘初始化
char board[Row][Col];
chessboard_initialize(board, Row, Col);
//打印棋盘
print_board(board, Row, Col);
while (1)
{
//玩家下棋
player_time(board, Row, Col);
print_board(board, Row, Col);
//判断输赢
ch = is_win(board, Row, Col);
if (ch != 'C')
{
break;
}
//电脑下棋
computer_time(board, Row, Col);
print_board(board, Row, Col);
if (ch != 'C')
{
break;
}
}
if (ch == '*')
printf("恭喜您获得胜利!\n");
else if (ch == '#')
printf("很遗憾,您输了,再接再厉\n");
else if (ch == 'P')
printf("平局了,再来一把吧!\n");
}
int main()
{
while (getchar != EOF)
{
srand((unsigned int)time(NULL));
int input = 0;
//打印开始目录菜单
menu();
printf("是否开始三子棋游戏(1/0):");
scanf("%d", &input);
//开始游戏
if (input == 1)
{
//进入游戏,打印棋盘
game();
}
//结束游戏
else if(input==0)
{
printf("游戏已结束!\n");
break;
}
else
{
printf("选择错误,请重新选择\n");
}
}
}
三、调用函数程序(game.c)
#include "三子棋game.h"
//初始化棋盘
void chessboard_initialize(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++)
{
board[i][j] = ' ';
}
}
}
//打印棋盘
void print_board(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("|");
}
}
else
{
for (j = 0; j < col; j++)
{
printf(" ");
if (j < col - 1)
printf("|");
}
}
printf("\n");
}
}
//玩家下棋
void player_time(char board[Row][Col], int row, int col)
{
int x, y;
printf("玩家下棋(请输入棋盘坐标):");
scanf("%d %d", &x, &y);
while (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标非法,请重新输入\n");
break;
}
}
}
//电脑下棋
void computer_time(char board[Row][Col], int row, int col)
{
printf("电脑下棋:\n");
while(1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//判断输赢
//判断棋盘是否满了
int is_full(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++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
//玩家赢返回'*'
//电脑赢返回'#'
//平局返回'P'
//未结束返回'C'继续游戏
char is_win(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][0] != ' ')
{
return board[i][0];
break;
}
}
//判断列
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
break;
}
}
//判断对角线(1)
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[0][0];
//判断对角线(2)
if (board[0][2] == board[2][2] && board[2][0] == board[2][2] && board[2][2] != ' ')
return board[2][2];
//平局
if (is_full(board, row, col) == 1)
return 'P';
//继续
else
return 'C';
}
本人是初学C的小白,这个小游戏有很多地方有待优化,感谢大佬们指出!!!