用C语言实现三字棋程序(可简易扩展成五字棋等)

三字棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。

这个小游戏相信在座的各位大佬们,在自己小时候免不了与自己的小伙伴么比试一二,再退一步说没玩过,总是看到别人玩过!在这里插入图片描述

思维过程:

  1. 初始化棋盘
  2. 展示棋盘
  3. 玩家下棋
  4. 判断输赢
  5. 电脑下棋
  6. 判断输赢(如果输赢未判断出来,则循环执行步骤3、4、5、6,直到判断出输赢或者棋盘已下满,结束游戏)

主函数代码(main.c):

#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){
		peoplePlay(board, Row, Col);
		system("cls");
		displayBoard(board, Row, Col);
		ret = judgeWin(board, Row, Col);
		if (ret != 'C')
			break;
		computerPlay(board, Row, Col);
		system("cls");
		displayBoard(board, Row, Col);
		ret = judgeWin(board, Row, Col);
		if (ret != 'C')
			break;
	}
	if (ret == 'x')
		printf("玩家赢\n");
	else if (ret == '#')
		printf("电脑赢\n");
	else if (ret == 'Q')
		printf("平局\n");
}

int main(){
	int input = 0;
	srand((unsigned int)time(NULL));//产生随机数

	do{
		menu();
		printf("请选择:");
		scanf("%d", &input);
		system("cls");
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("游戏已关闭!");
			break;
		default:
			printf("选择错误,请重新选择");
			break;
		}
	} while (input);

	system("pause");
	return 0;
}

(game.h)头文件代码:
这里面存放的是我在整个程序中应用到的一些数据库,及其我所定义的宏,和使用到的函数的声明。
通过修改行(row)、列(col)、相连棋子数(link)的数可以对棋盘的大小,和胜利条件做更改! 感兴趣的可以试试五字棋(row = 10, col = 10,link = 5)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//三字棋

#define Row 3	//棋盘的行数
#define Col 3	//棋盘的列数
#define link 3  //用来判赢的相连棋子数

//初始化
void initBoard(char board[Row][Col], int row, int col);
//展示棋盘
void displayBoard(char board[Row][Col], int row, int col);
//玩家下棋
void peoplePlay(char board[Row][Col], int row, int col);
//电脑下棋
void computerPlay(char board[Row][Col], int row, int col);
//判断胜利
char judgeWin(char board[Row][Col], int row, int col);

//"x"玩家赢
//"#"电脑赢
//"C"未满 —— 继续下棋
//"Q"满了 —— 平局

头文件对应的源文件(game.c):

#include "game.h"

void initBoard(char board[Row][Col], int row, int col){
	int i, j;
	
	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, j;
	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 (j = 0; j < col; j++){
				printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
	}
}

void peoplePlay(char board[Row][Col], int row, int col){
	int i, j;

	printf("玩家走:>\n");
	printf("请输入要下棋的位置:");
	while (1){
		scanf("%d%d", &i, &j);
		if (i <= row && i >= 1 && j <= col && j >= 1){
			if (board[i - 1][j - 1] != ' ')//判断棋盘的这个点是否已有落子
				printf("位置已被占用,请重新输入!");
			else{
				board[i - 1][j - 1] = 'x';
				break;
			}
		}
		else
			printf("输入错误,请重新输入!");
	}
}

void computerPlay(char board[Row][Col], int row, int col){
	int i, j;

	printf("电脑走:>\n");
	while (1){
		i = rand() % row + 1;
		j = rand() % col + 1;
		if (board[i - 1][j - 1] == ' '){//判断棋盘的这个点是否已有落子
			board[i - 1][j - 1] = '#';
			break;
		}
	}
}

static int isFull(char board[Row][Col], int row, int col){
	int i, j;

	for (i = 0; i < row; i++){
		for (j = 0; j < col; j++){
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;//棋盘已下满
}

// 这里舍弃一种难于扩展的三字棋代码
// 单凭理解来看,被注释的代码更易于理解

//char judgeWin(char board[Row][Col], int row, int col){
//	int i;
//
//	for (i = 0; i < row; i++){
//		if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][1] != ' ')
//			return board[i][0];
//	}
//	for (i = 0; i < col; i++){
//		if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[i][1] != ' ')
//			return board[0][i];
//	}
//	if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[1][1] != ' ')
//		return board[1][1];
//	if (board[0][2] == board[1][1] && board[2][0] == board[1][1] && board[1][1] != ' ')
//		return board[1][1];
//	if (isFull(board, row, col) == 1){
//		return 'Q';
//	}
//	return 'C';
//}

char judgeWin(char board[Row][Col], int row, int col){
	int i, j, a, b, c, d;
	int count = 1;
	for (i = 0; i < row; i++){//每一行的胜利条件
		for (j = 0; j < col - 1; j++){
			if (board[i][j] != ' ' && board[i][j] == board[i][j + 1]){
				count++;
				if (count == link){
					count = 1;
					return board[i][j];
				}
			}
			else{
				count = 1;
			}
		}
		count = 1;
	}

	for (j = 0; j < col; j++){//每一列的胜利条件
		for (i = 0; i < row - 1; i++){
			if (board[i][j] != ' ' && board[i][j] == board[i + 1][j]){
				count++;
				if (count == link){
					count = 1;
					return board[i][j];
				}
			}
			else{
				count = 1;
			}
		}
		count = 1;
	}
	
	for (a = 0; a < row - link + 1; a++){	//按行查找左对角线
		for (i = a, j = 0; j < col - 1; i++, j++){
			if (board[i][j] != ' ' && board[i][j] == board[i + 1][j + 1]){
				count++;
				if (count == link){
					count = 1;
					return board[i][j];
				}
			}
			else{
				count = 1;
			}
		}
		count = 1;
	}
	
	for (c = 0; c < col - link + 1; c++){	//按列查找左对角线
		for (i = 0, j = c; i < row - 1; i++, j++){
			if (board[i][j] != ' ' && board[i][j] == board[i + 1][j + 1]){
				count++;
				if (count == link){
					count = 1;
					return board[i][j];
				}
			}
			else{
				count = 1;
			}
		}
		count = 1;
	}
	
	for (b = 0; b < row - link + 1; b++){	//按行查找右对角线
		for (i = b, j = col - 1; j > 0; i++, j--){
			if (board[i][j] != ' ' && board[i][j] == board[i + 1][j - 1]){
				count++;
				if (count == link){
					count = 1;
					return board[i][j];
				}
			}
			else{
				count = 1;
			}
		}
		count = 1;
	}
	
	for (d = row - 1; d >= link - 1; d--){	//按列查找右对角
		for (i = 0, j = d; i < row - 1; i++, j--){
			if (board[i][j] != ' ' && board[i][j] == board[i + 1][j - 1]){
				count++;
				if (count == link){
					count = 1;
					return board[i][j];
				}
			}
			else{
				count = 1;
			}
		}
		count = 1;
	}

	if (isFull(board, row, col) == 1){
		return 'Q';
	}
	return 'C';
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值