三子棋游戏简单实现(C语言)

    小小的诈尸一波,快到期末了,事情真的非常多,非常的忙,实在是抽不出时间来写文章,所以这次就发一下自己写的三子棋源码供大家学习参考,小小的水一期(超小声)。

    废话不多说,直接看源码:

第一部分:头文件test.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3

/*菜单界面*/
void menu();

/*用户选择棋子*/
int Choice_Chess(char* Chess);

/*游戏棋盘*/
void Display_Board(char board[ROW][COL], int rows, int cols);

/*初始化棋盘*/
void Start_Board(char board[ROW][COL], int rows, int cols);

/*下棋操作(玩家)*/
void Player_1(char board[ROW][COL], int rows, int cols, int choice_2, char* Chess);

/*下棋操作(电脑)*/
void Player_2(char board[ROW][COL],int choice_2, char* Chess);

/*判断游戏是否结束*/
int Judgement(char board[ROW][COL], int rows, int cols, int choice_2, char* Chess);

第二部分:源文件test.c

#include "main.h"

/*菜单界面*/
void menu()
{
	printf("***********************************************\n");
	printf("***********************************************\n");
	printf("************        三子棋       **************\n");
	printf("************      1.开始游戏     **************\n");
	printf("************      2.退出游戏     **************\n");
	printf("***********************************************\n");
	printf("***********************************************\n");
}

/*用户选择棋子*/
int Choice_Chess(char* Chess)
{
	int choice_2 = 0;
	printf("请选择你想要的棋子:\n");
	printf("0.*              1.#\n");
	printf("注意:选择好棋子以后,不能更改,且您没选择的棋子归为电脑选择。\n");
	while (1)
	{
		scanf("%d", &choice_2);
		if (choice_2 != 0 && choice_2 != 1)
		{
			printf("您输入的信息有误,请重新选择!\n ");
		}
		else
		{
			printf("选择成功!您选择的棋子为:%c,游戏马上开始.....\n",Chess[choice_2]);
			Sleep(2000);
			system("cls");
			break;
		}
	}
	return choice_2;
}

/*初始化棋盘*/
void Start_Board(char board[ROW][COL], int rows, int cols)
{
	int i = 0, j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ' ';
		}
	}
}

/*游戏棋盘*/
void Display_Board(char board[ROW][COL], int rows, int cols)
{
	/*循环变量*/
	int i = 0;
	printf("0");
	for (i = 1; i <= rows; i++)
	{
		printf("  %d",i);
	}
	printf("\n");
	for (i = 1; i <= cols; i++)
	{
		printf("%d", i);
		if (i != 3)
		{
			printf(" %c | %c | %c  \n",board[i - 1][0], board[i - 1][1], board[i - 1][2]);
		    printf("  -----------\n");
		}
		else
		{
			printf(" %c | %c | %c  \n",board[i - 1][0], board[i - 1][1], board[i - 1][2]);
		}
	}
}

/*下棋操作(玩家)*/
void Player_1(char board[ROW][COL], int rows, int cols, int choice_2, char* Chess)
{
	/*存储玩家输入的行和列*/
	int row = 0, col = 0;
	printf("请输入您想要下棋的位置(先行后列):\n ");
	while (1)
	{
		scanf("%d%d", &row, &col);
		if ((row > 0 && row <= rows) && (col > 0 && col <= cols) && (board[row - 1][col - 1] == ' '))
		{
			board[row - 1][col - 1] = Chess[choice_2];
			break;
		}
		else
		{
			printf("您输入的信息有误,请重新输入!\n");
		}
	}
}

/*下棋操作(电脑)*/
void Player_2(char board[ROW][COL],int choice_2, char* Chess)
{
	srand((unsigned int)time(NULL));
	int row = 0;
	int col = 0;
	while (1)
	{
		row = rand() % 3 + 1;
		col = rand() % 3 + 1;
		if (board[row - 1][col - 1] == ' ')
		{
			board[row - 1][col - 1] = Chess[1 - choice_2];
			break;
		}
	}
}

/*判断游戏是否结束*/
int Judgement(char board[ROW][COL], int rows, int cols, int choice_2, char* Chess)
{
	int target = 0;
	int i = 0, j = 0;
	/*判断是否有一方获胜*/
	if (((board[1][1] == board[1][0] && board[1][1] == board[1][2]) || (board[1][1] == board[0][1] && board[1][1] == board[2][1]) || (board[1][1] == board[2][0] && board[1][1] == board[0][2]) || (board[0][0] == board[1][1] && board[1][1] == board[2][2])) && (board[1][1] != ' '))
	{
		if (board[1][1] == Chess[choice_2])
		{
			printf("游戏结束,玩家胜利!\n");
			Display_Board(board, rows, cols);
			system("pause");
			system("cls");
		}
		else
		{
			printf("游戏结束,电脑胜利!\n");
			Display_Board(board, rows, cols);
			system("pause");
			system("cls");
		}
		target = 1;
		return target;
	}
	return 0;
}

第三部分:源文件main.c

#include "main.h"

/*开始游戏整套操作*/
void game();

/*棋盘*/
char board[ROW][COL];
/*棋子*/
char Chess[2] = { '*','#' };
/*行数*/
int rows = ROW;
/*列数*/
int cols = COL;
/*存储玩家选择菜单选项*/
int choice_1 = 0;
/*存储玩家选择棋子的选项*/
int choice_2 = 0;
/*记录是否游戏结束*/
int target = 0;
/*记录已下棋子的个数*/
int num = 0;

int main()
{
	do
	{
		menu();
		printf("请选择: ");
		scanf("%d",&choice_1);
		switch (choice_1)
		{
		case 1:
			game();
			break;
		case 2:
			printf("感谢您的游玩,下次再见!\n");
			break;
		default:
			printf("您输入的信息有误,请重新选择!\n");
			system("pause");
			system("cls");
			break;
		}
	} while (choice_1 != 2);
	return 0;
}

void game()
{
	printf("请稍等,游戏马上开始.........\n");
	Sleep(2000);
	system("cls");
	/*初始化棋盘*/
	Start_Board(board, rows, cols);
	/*选择棋子*/
	choice_2 = Choice_Chess(Chess);
	/*正式开始游戏*/
	while (1)
	{
		Display_Board(board, rows, cols);
		Player_1(board, rows, cols, choice_2, Chess);
		target = Judgement(board, rows, cols, choice_2, Chess);
		num++;
		if (num == 9 || target == 1)
		{
			break;
		}
		Display_Board(board, rows, cols);
		Player_2(board, choice_2, Chess);
		target = Judgement(board, rows, cols, choice_2, Chess);
		num++;
		if (num == 9 || target == 1)
		{
			break;
		}
	}
	if (num == 9)
	{
		printf("游戏结束,本局为平局!\n");
		Display_Board(board, rows, cols);
		system("pause");
		system("cls");
	}
}

    以上代码是本人完全自主的实现的,希望对大家有所帮助。代码仍有可优化之处,供大家学习参考。

(顺带一提,明天有扫雷哦~)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值