C语言实现三子棋游戏
时间:2022-02-19 09:39:03|栏目:C代码|点击: 次
本文实例为大家分享了C语言实现三子棋游戏的具体代码,供大家参考,具体内容如下
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
实现三子棋游戏功能模块
#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]); int j = 0; for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) { printf("|"); } } printf("\n");//打印完数据换行 //打印分隔行 //if(i<row-1) // printf("---|---|---\n"); if (i < row - 1) { for (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] == ' ') { board[x - 1][y - 1] = '*';//落子 break; } else { printf("该坐标以被占用,请重新输入\n"); } } 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; int j = 0; int count = 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++) { count = 0; for (j = 0; j < col - 1; j++) { if (board[i][j] == board[i][j + 1] && board[i][j] != ' ') { count++; } else { break; } //if (count == col - 1) { // return board[i][0]; //} } if (count == col - 1) { 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]; // } //} for (i = 0; i < col; i++) { count = 0; for (j = 0; j < row - 1; j++) { if (board[j][i] == board[j+1][i] && board[j][i] != ' ') { count++; } else { break; } //if (count == row - 1) { // return board[0][i]; //} } if (count == row - 1) { return board[0][i]; } } //对角线 //if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') { // return board[0][0]; //} //if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ') { // return board[1][1]; //} //分别判断两条对角线 int count1 = 0; for (i = 0; i < row-1; i++) { //count = 0; if ((board[i][i] == board[i + 1][i + 1])&&(board[0][0]!=' ')) {//循环判断第一条对角线 如果相同计数器累加 不相同就跳出循环 count1++; } else { break; } //if (count1 == row - 1) {//到此时 第一条对角线上所有元素相同 游戏结束 返回胜利条件 count达不到row-1!! // return board[0][0]; //} } if (count1 == row - 1) {//到此时 需要在外层判断!! 第一条对角线上所有元素相同 游戏结束 返回胜利条件 return board[0][0]; } int count2 = 0; for (i = 0; i < row - 1; i++) { //count = 0; if (board[i][row-1-i] == board[i + 1][row-2-i] && board[row-1-i][0] != ' ') { count2++; } else { break; } //if (count2 == row - 1) { // return board[row-1][0]; //} } if (count2 == row - 1) { return board[row - 1][0]; } //判断平局 棋盘是否满了 是否有空格 if (IsFull(board,ROW,COL)==1) { return 'Q'; } //不是平局 return 'C'; }
test.c
主函数
#include"game.h" void game() { char ret = 0; //printf("game start"); char board[ROW][COL] = { 0 };//数组应该初始化为空格 InitBoard(board, ROW, COL);//初始化棋盘 //打印棋盘 DisplayBoard(board, ROW, COL); //分析: //1、玩家赢 * 2、电脑赢 # 3、平局 Q 4、继续 C //要判断输赢 while (1) { //玩家下棋 PlayerMove(board,ROW,COL); DisplayBoard(board, ROW, COL); //判断输赢 ret = CheckWin(board, ROW, COL); if (ret != 'C') { break; } //电脑下棋 ComputerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); //判断输赢 ret = CheckWin(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("玩家赢\n"); } else if(ret == '#') { printf("电脑赢\n"); } else { printf("平局\n"); } } void menu() { printf("##########################\n"); printf("######### 1、play ########\n"); printf("######### 0、exit ########\n"); printf("##########################\n"); } 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); }
上一篇:C语言结构体的全方面解读
栏 目:C代码
下一篇:C语言中冒泡排序算法详解
本文标题:C语言实现三子棋游戏
本文地址:http://www.codeinn.net/misctech/193850.html