文章目
目录
前言
使用C语言完成三子棋游戏。
一、三子棋介绍
使用c语言中二维数组相关的知识实现,选择通过三个文件test.c game.c game.h来实现
game.h中完成函数的声明,game.c中完成对函数的实现
二、
1.打印菜单
代码如下:
在test.c文件中创建
void menu()
{
printf("*********************\n");
printf("***** 1. play *****\n");
printf("***** 0. exit *****\n");
printf("*********************\n");
}
2.选择是否进入游戏
选择会多次进行,选择使用do while循环。
do
{
menu();
printf("请选择:>\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入.\n");
break;
}
} while (input);
3.完善game函数
选择1后会进入game函数当中开始游戏,首先棋盘的内容需要一个二维数组来接收存放,
并通过InitBoard函数对数组内容进行初始化,在头文件game.h中对InitBoard函数进行声明
同时为了方便打印不同大小棋盘,使用宏定义ROW和COL,方便修改棋盘
3.1棋盘初始化
//game.h
#define ROW 3
#define COL 3
void InitBoard(char board[ROW][COL], int row, int col);
char board[ROW][COL] = { 0 };//test.c
InitBoard(board,ROW,COL);//对数组进行初始化
在game.c中实现棋盘初始化,棋盘被初始化为空格
void InitBoard(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] = ' ';
}
}
}
3.2打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col) //打印棋盘
{
int i = 0;
for (i = 0; i < row; i++)
{
//1.打印数据
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if(j<col-1)
printf("|");
}
printf("\n");
//2.打印分割行
if (i < row - 1)
{
for (int j = 0; j < row; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
3.3下棋的实现
3.3.1玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x;
int y;
printf("玩家下棋,请输入坐标;>");
while (1)
{
scanf("%d %d", &x, &y);
if ((x >= 1 && x <=row) && (y >= 1 && y <= col))
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标被占用,不能落子");
}
}
else
{
printf("非法坐标,请重新输入");
}
}
}
3.3.2电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x=0;
int y = 0;
printf("电脑下棋:\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
4.输赢的判断
//判断输赢,在每一次玩家与电脑下棋之后都进行一次判断。
1.玩家赢 返回*
2.电脑赢 返回#
3.平局 返回Q
4.继续 返回C
int IsFull(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;
}
char IsWin(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];
}
}
//列
int j = 0;
for (j = 0; j < col; j++)
{
if ((board[0][j] == board[1][j]) && (board[1][j] == board[2][j]) && board[0][j] != ' ')
{
return board[0][j];
}
}
//对角线1
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && board[0][0] != ' ')
return board[0][0];
//对角线2
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';
}
game.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void InitBoard(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 DisplayBoard(char board[ROW][COL], int row, int col) //打印棋盘
{
int i = 0;
for (i = 0; i < row; i++)
{
//1.打印数据
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if(j<col-1)
printf("|");
}
printf("\n");
//2.打印分割行
if (i < row - 1)
{
for (int j = 0; j < row; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
//玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x;
int y;
printf("玩家下棋,请输入坐标;>");
while (1)
{
scanf("%d %d", &x, &y);
if ((x >= 1 && x <=row) && (y >= 1 && y <= col))
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标被占用,不能落子");
}
}
else
{
printf("非法坐标,请重新输入");
}
}
}
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x=0;
int y = 0;
printf("电脑下棋:\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//判断输赢
int IsFull(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;
}
char IsWin(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];
}
}
//列
int j = 0;
for (j = 0; j < col; j++)
{
if ((board[0][j] == board[1][j]) && (board[1][j] == board[2][j]) && board[0][j] != ' ')
{
return board[0][j];
}
}
//对角线1
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && board[0][0] != ' ')
return board[0][0];
//对角线2
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
#include"game.h"
void menu()
{
printf("*********************\n");
printf("***** 1. play *****\n");
printf("***** 0. exit *****\n");
printf("*********************\n");
}
void game()//游戏函数
{
char board[ROW][COL] = { 0 };
char ret = 0;
InitBoard(board,ROW,COL);//对数组进行初始化
//打印棋盘
DisplayBoard(board, ROW, COL);
//下棋
while (1)
{
ret = IsWin(board, ROW, COL);
//玩家下棋
PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
//判断输赢
if (ret != 'C')//游戏结束
break;
//电脑下棋
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
//判断输赢
ret=IsWin(board, ROW, COL);
if (ret != 'C')//游戏结束
break;
}
//判断
if (ret == '*')
printf("玩家胜利\n");
else if (ret == '#')
printf("电脑胜利\n");
else
printf("平局");
}
int main()
{
int input = 0;
srand((unsigned int) time(NULL));
do
{
menu();
printf("请选择:>\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入.\n");
break;
}
} while (input);
return 0;
}
game.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
//数组内容初始化
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);
//判断输赢
//1.玩家赢 返回*
//2.电脑赢 返回#
//3.平局 返回Q
//4.继续 返回C
char IsWin(char board[ROW][COL], int row, int col);
int IsFUll(char board[ROW][COL], int r
总结
以上就是三子棋实现的过程