今天小编给大家分享一下如何用C语言实现简易三子棋游戏的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
1、头文件
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>//标识符定义#define ROW 3#define COL 3//函数定义//棋盘初始化void init_board(char board[ROW][COL], int row, int col);//打印棋盘void display_board(char board[ROW][COL], int row, int col);//玩家下棋void player_game(char board[ROW][COL], int row, int col);//电脑下棋void comeputer_game(char board[ROW][COL], int row, int col);//判断输赢char is_win(char board[ROW][COL], int row, int col);
2、游戏函数
#include "game.h"//棋盘初始化void init_board(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 display_board(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; int k = 0; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { printf(" %c ",board[i][j]); if (j < COL - 1) { printf("|"); } } printf("\n"); if (i < ROW - 1) { for (k = 0; k < COL; k++) { printf("---"); if (k < COL - 1) { printf("|"); } } } printf("\n"); }}//玩家下棋void player_game(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; while (1) { printf("请输入你要落子的坐标>:"); scanf("%d %d", &x, &y); if (x <= row && y <= col && x >= 1 && y >= 1) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("你输入的坐标被占用!\n"); } } else { printf("你输入的坐标有误,请重新输入!\n"); } } }//电脑下棋void comeputer_game(char board[ROW][COL], int row, int col) { while (1) { int x = rand() % row; int y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }//判断输赢//1.玩家赢返回*//2.电脑赢返回#//3.双方都不赢,棋盘没有位置平局返回p//4.双方都不赢,棋盘还有位置继续下棋返回cchar is_win(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; //判断行 for (x = 0; x < row; x++) { if (board[x][0] == board[x][1] && board[x][1] == board[x][2] && board[x][1] != ' ') { return board[x][1]; } } //判断列 for (y = 0; y < col; y++) { if (board[0][y] == board[1][y] && board[1][y] == board[2][y] && board[1][y] != ' ') { return board[1][y]; } } //判断对角线 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') { return board[1][1]; } if (board[2][0] == board[1][1] && board[1][1] == board[1][2] && board[1][1] != ' ') { return board[1][1]; } //判断棋盘是否有空位 for (x = 0; x < row; x++) { for (y = 0; y < col; y++) { if (board[x][y] == ' ') { return 'c'; } } } return 'p';}
3、游戏实现
#include "game.h"//打印菜单void menu() { printf("***************************\n"); printf("********* 三 子 棋 ********\n"); printf("********* 1. play ********\n"); printf("********* 0. exit ********\n"); printf("***************************\n");}void game() { char win = 0; //定义棋盘存储 char board[ROW][COL]; //棋盘初始化 init_board(board, ROW, COL); //打印棋盘 display_board(board, ROW, COL); while (1) { //玩家下棋 player_game(board, ROW, COL); display_board(board, ROW, COL); //判断输赢 win = is_win(board, ROW, COL); if (win != 'c') { break; } //电脑下棋 comeputer_game(board, ROW, COL); display_board(board, ROW, COL); //判断输赢 win = is_win(board, ROW, COL); if (win != 'c') { break; } } if (win == '*') { printf("恭喜玩家胜利!\n"); display_board(board, ROW, COL); system("pause"); } else if (win == '#') { printf("电脑胜利,继续加油哟!\n"); display_board(board, ROW, COL); system("pause"); } else { printf("平局\n"); display_board(board, ROW, COL); system("pause"); }} int main(void) { int num = 0; srand((unsigned)time(NULL)); do { //打印菜单 menu(); //选择菜单 printf("请选择菜单>:"); scanf("%d", &num); switch (num) { case 0: break; case 1: game(); break; default: printf("你的选择错误,请重新选择!\n"); system("pause"); system("cls"); break; } } while (num); system("pause"); return 0;}
效果图片
以上就是“如何用C语言实现简易三子棋游戏”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注编程网行业资讯频道。