利用C语言实现人机三子棋游戏
三子棋实现效果
1.首先游戏界面是一个选择进入游戏或者退出游戏的简单界面。
2.进入游戏之后出现的是一个3*3的一个棋盘。
3.玩家通过输入棋盘坐标下棋,机器随机落子。
4.游戏结束后可以选择继续游戏或者退出游戏。
实现代码
先上代码。
头文件: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 CheckWin(char board[Row][Col], int row, int col);
源文件:game.c
#define _CRT_SECURE_NO_WARNINGS 1
#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++)
{
//打印数据
//printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
for (int j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
//打印分割的行
if (i < row - 1)
//printf("---|---|---\n");
{
for (int 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] != ' ')
{
printf("坐标被占用,请重新输入!\n");
}
else
{
//单引号与双引号结果不同
board[x - 1][y - 1] = '*';
break;
}
}
else
{
printf("坐标非法,请重新输入!\n");
}
}
}
void ComputerMove(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;
}
}
}
static 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 CheckWin(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];
}
}
//三列
for (i = 0; i < row; i++)
{
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[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
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("**********************\n");
printf("***** 1.play ******\n");
printf("***** 0.exit ******\n");
printf("**********************\n");
}
void game()
{
char ret = 0;
//数据的存储是二维数组
char board[Row][Col];
//初始化棋盘—初始化为空格
InitBoard(board, Row, Col);
//打印棋盘
DisplayBoard(board, Row, Col);
while (1)
{
//玩家下棋
PlayerMove(board, Row, Col);
ret = CheckWin(board, Row, Col);
if (ret != 'C')
{
break;
}
DisplayBoard(board, Row, Col);
//电脑下棋
ComputerMove(board,Row,Col);
ret = CheckWin(board, Row, Col);
if (ret != 'C')
{
break;
}
DisplayBoard(board, Row, Col);
}
if (ret == '*')
{
printf("玩家赢了\n");
}
else if (ret == '#')
{
printf("电脑赢了\n");
}
else if (ret == 'Q')
{
printf("平局\n");
}
DisplayBoard(board, Row, Col);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
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;
}