三子棋小游戏(c语言实现附详解)

今天给大家分享一个简单的三子棋小游戏,C语言实现,附详解

1.整体思路

首先我们以一个3x3的数组作为棋盘,先将数组中元素初始化为空格。然后,玩家下棋用*表示,电脑下棋用#号来表示。

每次玩家或电脑落子之后,都要判断一下胜负情况。详细判断规则见代码注释

2.代码实现

test.c

#include "game.h"

void game()
{
	//创建一个二维数组作为棋盘
	char board[ROW][COL] = { 0 };
	
	InitBoard(board, ROW, COL);//初始化棋盘
	DisplayBoard(board, ROW, COL);//打印棋盘

	char ret;
	while (1)
	{
		//开始下棋 
		player_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		ret = IsWon(board, ROW, COL);
		if (ret == '*')
		{
			printf("you won\n");
			break;
		}
		if (ret == 'D')
		{
			printf("平局\n");
			break;
		}
		computer_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		ret = IsWon(board, ROW, COL);
		if (ret == '#')
		{
			printf("you lost\n");
			break;
		}
		if (ret == 'D')
		{
			printf("平局\n");
			break;
		}
	}
}
void menu()
{
	printf("*************************\n");
	printf("********1. play  ********\n");
	printf("********0. exit  ********\n");
	printf("*************************\n");
}

int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	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.h

#pragma once
#include 
#include 
#include 
#include 

#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 player_move(char board[ROW][COL], int row, int col);//玩家下棋
void computer_move(char board[ROW][COL], int row, int col);//电脑下棋
char IsWon(char board[ROW][COL], int row, int col);//判断是否有人赢了 

game.c

#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 player_move(char board[ROW][COL], int row, int col)
{
	int x, y;
	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 computer_move(char board[ROW][COL], int row, int col)
{
	int x, y;
	printf("电脑下棋:>");
	while (1)
	{
		x = rand() % 3;
		y = rand() % 3;
	
		//判断坐标是否已被占用
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			printf("%d %d\n", x+1, y+1);
			break;
		}
	}

}

bool IsFull(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
			{
				return false;
			}
		}
	}
	return true;
}

//判断是否有人赢了
char IsWon(char board[ROW][COL], int row, int col)
{
	//玩家赢了,返回*。电脑赢了,返回#,平局返回D,否则返回C继续下棋
	for (int 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 (int j = 0; j < col; j++)
	{
		//判断三列
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ')
		{
			return board[0][j];
		}
	}

	//判断对角线
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
	{
		return board[0][0];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
	{
		return board[0][2];
	}

	//判断是否平局,即棋盘是否满了
	if (IsFull(board, ROW, COL))
	{
		return 'D';
	}
	
	//继续下棋
	return 'C';

}

你可能感兴趣的:(c语言复习宝典,c语言)