在VS编译器的小黑框框里实现5x5棋盘的的三子棋
程序包括test.c(游戏测试)game.c(游戏源文件)和game.h(头文件)三个部分
game.h
#ifndef __GAME_H__
#define __GAME_H__
#define ROWS 5 //定义棋盘的行数
#define COLS 5 //定义棋盘的列数
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void init_board(char board[ROWS][COLS], int row, int col); //初始化棋盘
void display(char board[ROWS][COLS], int row, int col); //打印5*5的棋盘
void player_move(char board[ROWS][COLS], int row, int col); //玩家下棋
void computer_move(char board[ROWS][COLS], int row, int col);//电脑下棋
char check_win(char board[ROWS][COLS], int row, int col); //判断是否赢
#endif //__GAME_H__
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
#include <string.h>
void init_board(char board[ROWS][COLS], int row, int col) //初始化棋盘
{
memset(board, ' ', sizeof(board[0][0])*row*col);
}
void display(char board[ROWS][COLS], int row, int col) //打印棋盘
{
int i = 0;
for (i = 0; i < row; i++)
{
printf(" %c | %c | %c | %c | %c \n", board[i][0], board[i][1], board[i][2], board[i][3], board[i][4]);
if (i < row - 1)
{
printf("---|---|---|---|---\n");
}
}
}
void player_move(char board[ROWS][COLS], int row, int col) //玩家下棋
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标:> ");
scanf("%d%d", &x, &y);
if (((x>=1)&&(x<=row)) && ((y>=1) && (y<=col))) //row-3
{
if (board[x-1][y-1] == ' ')
{
board[x-1][y-1] = 'X';
break;
}
else
{
printf("该坐标已被用,请重新输入\n");
}
}
else
{
printf("输入坐标有误\n");
}
}
}
void computer_move(char board[ROWS][COLS], int row, int col) //电脑下棋
{
while (1)
{
int x = rand() % 5;
int y = rand() % 5;
if (board[x][y] == ' ')
{
board[x][y] = '$';
break;
}
}
}
int is_full(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < row; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char check_win(char board[ROWS][COLS], 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] == board[i][j+1]) && (board[i][j+1] == board[i][j+2]) && (board[i][j+1] != ' '))
{
return board[i][j+1];
}
}
}
for (i = 0; i < row; i++) //列相同
{
for (j = 0; j < col; j++)
{
if ((board[j][i] == board[j+1][i]) && (board[j+1][i] == board[j+2][i]) && (board[j+1][i] != ' '))
{
return board[j+1][i];
}
}
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if ((board[i][j] == board[i+1][j+1]) && (board[i+1][j+1] == board[i+2][j+2]) && (board[i+1][j+1] != ' '))//斜相同
{
return board[i+1][j+1];
}
}
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if ((board[i][j+2] == board[i+1][j+1]) && (board[i+1][j+1] == board[i+2][j]) && (board[i+1][j+1] != ' '))
{
return board[i+1][j+1];
}
}
}
if (is_full(board, row, col) == 1) //如果棋盘没有空,返回q
{
return 'q';
}
return ' '; //没输没赢,返回空格,继续下棋
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu() //菜单打印
{
printf("***************************\n");
printf("**** 1.play 0.exit ****\n");
printf("***************************\n");
}
void game() //游戏函数
{
char board[ROWS][COLS] = { 0 };
char ret = 0;
init_board(board, ROWS, COLS); //初始化棋盘
while (1)
{
printf("玩家移动,");
player_move(board, ROWS, COLS); //玩家下棋
display(board, ROWS, COLS); //打印棋盘
ret = check_win(board, ROWS, COLS); //判断是否赢
if (ret != ' ')
{
break;
}
printf("电脑移动\n");
computer_move(board, ROWS, COLS);
display(board, ROWS, COLS);
ret = check_win(board, ROWS, COLS);
if (ret != ' ')
{
break;
}
}
if (ret == 'X')
{
printf("玩家赢\n");
}
else if (ret == '$')
{
printf("电脑赢\n");
}
else if (ret == 'q')
{
printf("平局\n");
}
}
void test() //测试函数
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请输入你的选择:> "); //选择是否开始游戏
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("输入有误\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
运行结果: