c语言实现三子棋

Posted ~千里之行,始于足下~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言实现三子棋相关的知识,希望对你有一定的参考价值。

用c语言实现三子棋小游戏。

game.h

#pragma once //防止头文件重复包含
#include<stdio.h>
#include<Windows.h>
#include<time.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#define ROW 3
#define COL 3
#define SPACE ' '  // 空格
#define WHITE '*'  // 白子
#define BLACK '#'  //黑子
#define DRAW 'D'   //平局
#define NEXT 'N'   //继续
extern void game();

game.c

#include"game.h"
static void showBoard(char board[][COL], int row, int col)
{
	system("cls");
	printf(" ");
	for (int i = 0; i < col; i++){
		printf("%4d", i + 1);
	}
	printf("\\n---------------\\n");
	for (int i = 0; i < row; i++){
		printf("%-2d", i + 1); //2
		for (int j = 0; j < col; j++){
			printf("| %c ", board[i][j]); //12
		}
		printf("|\\n");
		printf("\\n---------------\\n");
	}
}

static void playerMove(char board[][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("输入您落子的位置: \\n");
		scanf("%d %d", &x, &y);
		if (x < 1 || x > 3 || y < 1 || y > 3)
		{
			printf("输入有误!\\n");
			continue;
		}
		if (board[x - 1][y - 1] == SPACE)
		{
			board[x - 1][y - 1] = WHITE;
			break;
		}
	}
}

static char isEnd(char board[][COL], int row, int col)
{
	//判断行
	for (int i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != SPACE)
		{
			return board[i][0];
		}
	}
	//判断列
	for (int j = 0; j < col; j++)
	{
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != SPACE)
		{
			return board[0][j];
		}
	}
	//判断对角线
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != SPACE)
	{
		return board[0][0];
	}
	if (board[1][3] == board[2][2] && board[2][2] == board[3][1] && board[2][2] != SPACE)
	{
		return board[2][2];
	}
	//判满
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (board[i][j] == SPACE)
			{
				return NEXT;
			}
		}
	}
	return DRAW;
}

static void computerMove(char board[][COL], int row, int col)
{
	while (1)
	{
		int x = rand() % ROW;
		int y = rand() % COL;
		if (x < 0 || x > 2 || y < 0 || y > 2)
		{
			continue;
		}
		if (board[x][y] == SPACE)
		{
			board[x][y] = BLACK;
			break;
		}
	}
}

void game()
{
	srand((unsigned long)time(NULL));//随机数种子
	char board[ROW][COL] = { SPACE };//二维数组代表棋盘
	memset(board, SPACE, sizeof(board));//全部初始化为空格
	char result = SPACE;
	while (1)
	{
		showBoard(board, ROW, COL);     //打印棋盘
		playerMove(board, ROW, COL);    //玩家落子
	    result = isEnd(board, ROW, COL);//判断是否继续
		if (result != NEXT)
		{
			break;
		}
		computerMove(board, ROW, COL);  //电脑落子
		result = isEnd(board, ROW, COL);//判断是否继续
		if (result !=  NEXT)
		{
			break;
		}
	}
	showBoard(board, ROW, COL);        //打印棋盘
	switch (result)
	{
	case WHITE:
		printf("玩家赢\\n");
		break;
	case BLACK:
		printf("电脑赢\\n");
		break;
	case DRAW:
		printf("平局\\n");
		break;
	default:
		printf("bug!\\n");
	}
	system("pause");
	system("cls");

}

main.c

#include"game.h"

static void menu()
{
	printf("|+++++++++++++++++++++++++++++++++++|\\n");
	printf("|+++1. play               0. exit+++|\\n");
	printf("|+++++++++++++++++++++++++++++++++++|\\n");
}

int main()
{
	int quit = 0;
	while (!quit)
	{
		menu();
		int select = 0;
		printf("请输入您的选择: \\n");
		scanf("%d", &select);
		switch (select)
		{
		case 1: //printf("开始游戏\\n");
			game();
			break;
		case 0:
			quit = 1;
			printf("欢迎使用!\\n");
			break;
		default:
			printf("输入错误!\\n");
		}
	}
	system("pause");
	return 0;
}

以上是关于c语言实现三子棋的主要内容,如果未能解决你的问题,请参考以下文章

《C语言入门》三子棋C语言实现(详细版)

C语言游戏跟电脑battle三子棋

C语言实现三子棋游戏

详细解读C语言实现三子棋

三子棋(C语言实现)

c语言三子棋