这篇文章主要介绍如何使用C语言代码实现简单三子棋游戏,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
编译软件VS2019
c语言实现三子棋运用到了二维数组的知识,在编写三子棋程序时用到了多文件编译(不是必须的,但是使用多文件编译会增加代码的可读性),首先我们先建立一个头文件和两个源文件·:
test.c//主程序 函数实现game.c//主要用于自定义函数的定义game.h//声明自定义函数
主要代码实现
test.c
调用各个功能的函数
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h> //#include<>:编译器直接从系统类库目录里查找头文件:#include"game.h" //#include"":默认从项目当前目录查找头文件,所谓项目当前目录,就是项目工程文件所在的目录void menu() { printf("*********************************\n"); printf("************* 1.play **********\n"); printf("************* 0.exit **********\n"); printf("*********************************\n");}void game() { //三子棋过程 char board[row][col];//棋盘数组 //初始化棋盘--board的元素都给成空格 InitBoard(board, row, col); //打印棋盘 DisplayBoard(board, row, col); //下棋 char ret = 0; while (1) { PlayMove(board,row,col); DisplayBoard(board, row, col); //判断输赢 ret=IsWin(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("和局\n"); DisplayBoard(board, row, col);}int main() { int input = 0; srand((unsigned int)time(NULL)); 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;}
game.c
主程序当中的自定义函数的定义
#define _CRT_SECURE_NO_WARNINGS #include "game.h" void InitBoard(char board[row][col], int ROW, int COL) { for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { board[i][j] = ' '; } }}void DisplayBoard(char board[row][col], int ROW, int COL) { for (int i = 0; i < ROW; i++) { //打印数据 for (int j = 0; j < COL; j++) { printf(" %c ", board[i][j]); if (j < COL - 1) printf("|"); } printf("\n"); //打印分割行 if (i < ROW - 1) { for (int j = 0; j < COL; j++) { printf("---"); if (j < COL - 1) printf("+"); } } printf("\n"); }} void PlayMove(char board[row][col], int ROW, int COL) { printf("玩家下棋:>\n"); int x = 0; int y = 0; while (1) { printf("请输入坐标:>"); 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("坐标被占用,请重新输入\n"); } } else printf("坐标非法,超出范围\n"); } } void ComputerMove(char board[row][col], int ROW, int COL) { int x = 0; int y = 0; printf("电脑走:>\n"); while (1) { x = rand() % ROW; y = rand() % COL; if (board[x][y] == ' ') { board[x][y] = '#'; break; } }} //判断棋盘是否已满int full(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 IsWin(char board[row][col], int ROW, int COL) { //1.判断输赢 //三行 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 < COL; 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[0][0] != ' ') { return board[0][0]; } else if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[2][0] != ' ') return board[1][1]; //2.判断平局 else if (full(board, ROW, COL) == 1) return 'Q'; //3.游戏继续 else return 'C'; }
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 PlayMove(char board[row][col], int ROW, int COL); //电脑下棋void ComputerMove(char board[row][col], int ROW, int COL); //判断输赢//要返回四种不同的状态//玩家赢-返回“*”,电脑赢返回“#”,和局返回“Q”//继续游戏返回'C' char IsWin(char board[row][col], int ROW, int COL); //判断棋盘是否已满int full(char board[row][col], int ROW, int COL);
以上是“如何使用C语言代码实现简单三子棋游戏”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!