c语言实现三子棋!

 前言 :用c语言实现三子棋可分为六个步骤!

1:首先是小游戏开始的准备工作-----

      首先我们要进入小游戏至少要选择一次,我们用一个do--while语句来实现!

       然后建立一个简易菜单-----

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

 之后再用一个选择语句供玩家选择-----

	do
	{
		menu();
		printf("请输入选项->\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1: 
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误,请重新输入->\n");
			break;
		}
	} while (input);

 此时已经成型,进入下一个步骤即可!

2:创建棋盘(一个二维数组),对其初始化----  

//初始化棋盘
	init(board, ROW, COL);

对棋盘上的棋子都赋予空格

void init(char board[ROW][COL],int row,int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}

进行下一个步骤!

3:打印棋盘----

//打印棋盘
	display(board, ROW, COL);

顾名思义,要打印一个3*3的“井”类图样,并且对其棋子赋值----

表格示例----

         |   |   
	  ---|---|---
	     |   |   
	  ---|---|---
		 |   |   
	     

代码实现如图:

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

进入下一个步骤!

4:玩家开始下棋,且打印棋盘---

	//玩家下棋
		playermove(board, ROW, COL);
		display(board, ROW, COL);

代码实现如图-----

void playermove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("玩家下棋->\n");
		scanf("%d%d", &x, &y);
		if (x <= row && y <= col)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else printf("位子被占,请重新输入->\n");
		}
		else printf("输入非法,请重新输入->\n");
	}
}

进入下一个步骤!

5:轮到电脑开始下棋,且打印棋盘---

	//电脑下棋
		compmove(board, ROW, COL);
		display(board, ROW, COL);

代码实现如图----

void compmove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("电脑下棋->\n");
	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}

因为电脑是无规则下棋,所以其中采用了---“时间戳”!

rand()----会生成随机数。

rand函数的使用使用需要引----"srand"!

srand((unsigned int)time(NULL));

使用“rand”函数只需引用一次"srand"----所以将其放首位即可!

int main()
{
	srand((unsigned int)time(NULL));

同时需要引头文件----

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

完成即进入下一步!

6:玩家或电脑每次下完棋都要判断输赢---

//判断输赢
		ret=iswin(board, ROW, COL);

共有4种结果---平局---玩家赢---电脑赢---继续---

使用不同的返回值来进行判断---

while (1)
	{
		//玩家下棋
		playermove(board, ROW, COL);
		display(board, ROW, COL);
		//判断输赢
		ret=iswin(board, ROW, COL);
		if (ret != 'c')
			break;
		//电脑下棋
		compmove(board, ROW, COL);
		display(board, ROW, COL);
		ret = iswin(board, ROW, COL);
		if (ret != 'c')
			break;
	}
	if (ret == '*')
		printf("玩家赢\n");
	else if (ret == "#")
		printf("电脑赢\n");
	else printf("平局\n");

函数代码实现如图所示--

int flat(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 iswin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	//行
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
			return board[i][1];
	}
	//列
	int j = 0;
	for (j = 0; j < col; j++)
	{
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
			return board[1][j];
	}
	//对角
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
		return board[1][1];
	else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
		return board[1][1];
	else if (flat(board, ROW, COL) == 1)
		return 'q';
	else return 'c';
}

然后就ok了!

主要还是细心,一步一步来,切记不可出错!

可以在写作的过程中多运用几个文件分工会更加明确!

此代码我分为了三个文件!

以下是总代码的实现---

-----test.c---文件名--起始文件-

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
	printf("***************************\n");
	printf("******    1.play     ******\n");
	printf("******    0.exit       ****\n");
	printf("***************************\n");
}
void game()
{
	char board[ROW][COL] = { 0 };
	//初始化棋盘
	init(board, ROW, COL);
	//打印棋盘
	display(board, ROW, COL);
	int ret = 0;
	while (1)
	{
		//玩家下棋
		playermove(board, ROW, COL);
		display(board, ROW, COL);
		//判断输赢
		ret=iswin(board, ROW, COL);
		if (ret != 'c')
			break;
		//电脑下棋
		compmove(board, ROW, COL);
		display(board, ROW, COL);
		ret = iswin(board, ROW, COL);
		if (ret != 'c')
			break;
	}
	if (ret == '*')
		printf("玩家赢\n");
	else if (ret == "#")
		printf("电脑赢\n");
	else printf("平局\n");
}
int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("请输入选项->\n");
		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<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
void init(char board[ROW][COL],int row,int col);
void display(char board[ROW][COL], int row, int col);
void playermove(char board[ROW][COL], int row, int col);
void compmove(char board[ROW][COL], int row, int col);
char iswin(char board[ROW][COL], int row, int col);

-----game.c----文件名---负责函数的实现

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init(char board[ROW][COL],int row,int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}

void display(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		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)
		{
			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;
	while (1)
	{
		printf("玩家下棋->\n");
		scanf("%d%d", &x, &y);
		if (x <= row && y <= col)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else printf("位子被占,请重新输入->\n");
		}
		else printf("输入非法,请重新输入->\n");
	}
}

void compmove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("电脑下棋->\n");
	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}


int flat(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 iswin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	//行
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
			return board[i][1];
	}
	//列
	int j = 0;
	for (j = 0; j < col; j++)
	{
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
			return board[1][j];
	}
	//对角
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
		return board[1][1];
	else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
		return board[1][1];
	else if (flat(board, ROW, COL) == 1)
		return 'q';
	else return 'c';
}

以上就是用c语言对三子棋的实现---如果对你有启发的话------就给哥们来个一键三连!

拜拜了您嘞!

猜你喜欢

转载自blog.csdn.net/m0_71676870/article/details/130544107
今日推荐