数组(2)

2024-01-22 17:47:33 浏览数 (1)

9. 三子棋

类似于之前的猜数字小游戏,我们需要先把整个游戏的大体结构在主函数中实现出来:

代码语言:javascript复制
//test.c

#include <stdio.h>

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

int main()
{
	int input = 0;
	
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);//1 0 4
		
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏n");
			break;
		default:
			printf("选择错误,重新选择!n");
			break;
		}

	} while (input);

	return 0;
}

接下来,我们需要对游戏内容进行具体地实现:

  1. 初始化棋盘

棋盘上一开始什么都没有,说明里面放的都是空格

代码语言:javascript复制
//test.c

#include "game.h"

void game()
{
	char board[3][3] = { 0 };
	InitBoard(board, 3, 3);
}

在game.h中对函数进行声明

代码语言:javascript复制
//game.h

//初始化棋盘
void InitBoard(char board[3][3], int row, int col);

这里我们思考一下:如果我们不想玩三子棋,而想玩四子棋,那么我们是不是需要把所有是3的地方全部改成4,这样就太繁琐了,那么要如何解决呢?

代码语言:javascript复制
//game.h

#define ROW 3
#define COL 3

//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);
代码语言:javascript复制
//test.c

#include "game.h"

void game()
{
	char board[ROW][COL] = { 0 };
	InitBoard(board, ROW, COL);
}

在game.c中对函数进行定义

代码语言:javascript复制
//game.c

#include "game.h"

//初始化棋盘为空格
void InitBoard(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] = ' ';
		}

	}

}

  1. 打印棋盘
代码语言:javascript复制
//test.c

#include "game.h"

void game()
{
	char board[ROW][COL] = { 0 };
	InitBoard(board, ROW, COL);
	//打印棋盘
	DisplayBoard(board, ROW, COL);
}
代码语言:javascript复制
//game.h

#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);

因为在game.c中实现打印棋盘,要引用头文件stdio.h,而test.c中也需要引用stdio.h;同时,test.c和game.c又都引用了game.h,所以我们可以直接在game.h中引用stdio.h

代码语言:javascript复制
//game.h

#include <stdio.h>

#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);
代码语言:javascript复制
//game.c

#include "game.h"

//版本一
void DisplayBoard(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]);
		}

		printf("n");
	}

}

以上打印方法打印出来屏幕上什么也没有,这是因为我们打印的是空格,所以我们还需要加一下分界线

代码语言:javascript复制
//game.c

#include "game.h"

//版本2
void DisplayBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;

	for (i = 0; i < row; i  )
	{
		//1. 打印数据
		printf(" %c | %c | %c n", board[i][0], board[i][1], board[i][2]);
		//2. 打印分割线
		if (i < row - 1)
		{
			printf("---|---|---n");
		}

	}

}

以上打印方法不具有普适性,如果改变行和列,打印出来的棋盘就不对了

代码语言:javascript复制
//game.c

#include "game.h"

//版本3
void DisplayBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;

	for (i = 0; i < row; i  )
	{
		//1. 打印数据
		int j = 0;

		for (j = 0; j < col; j  )
		{
			printf(" %c ", board[i][j]);

			if (j < col - 1)
			{
				printf("|");
			}

		}

		printf("n");
		//2. 打印分割线
		if (i < row - 1)
		{
			//printf("---|---|---n");
			int j = 0;

			for (j = 0; j < col; j  )
			{
				printf("---");
				
				if (j < col - 1)
				{
					printf("|");
				}

			}

			printf("n");
		}

	}

}

  1. 玩家下棋
代码语言:javascript复制
//test.c

#include "game.h"

void game()
{
	char board[ROW][COL] = { 0 };
	InitBoard(board, ROW, COL);
	//打印棋盘
	DisplayBoard(board, ROW, COL);
	//下棋
	//玩家下棋
	PlayerMove(board, ROW, COL);
	DisplayBoard(board, ROW, COL);
}
代码语言:javascript复制
//game.h

#include <stdio.h>

#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 PlayerMove(char board[ROW][COL], int row, int col);
代码语言:javascript复制
//game.c

#include "game.h"

void PlayerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家下棋>:n");
	
	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");
		}

	}

}

  1. 电脑下棋

我们先让玩家和电脑一直下棋

代码语言:javascript复制
//test.c

#include "game.h"

void game()
{
	char board[ROW][COL] = { 0 };
	InitBoard(board, ROW, COL);
	//打印棋盘
	DisplayBoard(board, ROW, COL);
	//下棋
	while (1)
	{
		//玩家下棋
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//电脑下棋
		ComputerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
	}
	
}

int main()
{
	srand((unsigned int)time(NULL));
	return 0;
}
代码语言:javascript复制
//game.h

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

#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 PlayerMove(char board[ROW][COL], int row, int col);
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col);
代码语言:javascript复制
//game.c

#include "game.h"

//电脑随机下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;//0~row-1
	int y = 0;//0~col-1
	printf("电脑下棋>:n");
	
	while (1)
	{
		x = rand() % row;
		y = rand() % col;

		if (' ' == board[x][y])
		{
			board[x][y] = '#';
			break;
		}

	}

}

  1. 判断输赢

玩家赢 - ‘*’ 电脑赢 - ‘#’ 平局 - ‘Q’ 继续 - ‘C’

代码语言:javascript复制
//test.c

#include "game.h"

void game()
{
	char board[ROW][COL] = { 0 };
	InitBoard(board, ROW, COL);
	//打印棋盘
	DisplayBoard(board, ROW, COL);
	//下棋
	char ret = 0;
	while (1)
	{
		//玩家下棋
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);

		if (ret != 'C')
		{
			break;
		}

		//电脑下棋
		ComputerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		
		if (ret != 'C')
		{
			break;
		}

	}

	if ('*' == ret)
	{
		printf("玩家赢n");
	}
	else if ('#' == ret)
	{
		printf("电脑赢n");
	}
	else
	{
		printf("平局n");
	}

}
代码语言:javascript复制
//game.h

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

#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 PlayerMove(char board[ROW][COL], int row, int col);
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col);


//判断输赢
//玩家赢 - '*'
//电脑赢 - '#'
//平局 - 'Q'
//继续 - 'C'

char IsWin(char board[ROW][COL], int row, int col);
代码语言:javascript复制
//game.c

#include "game.h"

int IsFull(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  )
		{

			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][0]!=' '))
		{
			return board[i][0];
		}

	}

	//列
	for (i = 0; i < col; i  )
	{

		if ((board[0][i]==board[1][i]) && (board[1][i]==board[2][i]) && (board[0][i]!=' '))
		{
			return board[0][i];
		}

	}

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

	if ((board[0][2]==board[1][1]) && (board[1][1]==board[2][0]) && (board[1][1]!=' '))
	{
		return board[1][1];
	}

	//平局
	if (1 == IsFull(board, row, col))
	{
		return 'Q';
	}

	//继续
	return 'C';
}

以上写法也不具有普适性

代码语言:javascript复制
//game.c

#include "game.h"

int IsFull(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  )
		{

			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  )
	{
		int j = 0;
		int count = 0;

		for (j = 0; j < col - 1; j  )
		{

			if ((board[i][j] != ' ') && (board[i][j] == board[i][j   1]))
			{
				count  ;
			}

		}

		if ((col - 1) == count)
		{
			return board[i][0];
		}

	}

	//列
	for (i = 0; i < col; i  )
	{
		int j = 0;
		int count = 0;

		for (j = 0; j < row - 1; j  )
		{

			if ((board[j][i] != ' ') && (board[j][i] == board[j   1][i]))
			{
				count  ;
			}

		}

		if ((row - 1) == count)
		{
			return board[0][i];
		}

	}

	//对角线
	int count = 0;
	for (i = 0; i < row - 1; i  )
	{

		if ((board[i][i]!=' ') && (board[i][i]==board[i   1][i   1]))
		{
			count  ;
		}

	}

	if ((row - 1) == count)
	{
		return board[0][0];
	}

	count = 0;

	for (i = 0; i < row - 1; i  )
	{

		if ((board[i][col-1-i]!=' ') && (board[i][col-1-i]==board[i 1][col-2-i]))
		{
			count  ;
		}

	}

	if ((row-1) == count)
	{
		return board[0][col - 1];
	}

	//平局
	if (1 == IsFull(board, row, col))
	{
		return 'Q';
	}

	//继续
	return 'C';
}

以下为完整代码:

代码语言:javascript复制
//game.h

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

#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 PlayerMove(char board[ROW][COL], int row, int col);
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col);


//判断输赢
//玩家赢 - '*'
//电脑赢 - '#'
//平局 - 'Q'
//继续 - 'C'

char IsWin(char board[ROW][COL], int row, int col);
代码语言:javascript复制
//game.c

#include "game.h"

//初始化棋盘为空格
void InitBoard(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 DisplayBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;

	for (i = 0; i < row; i  )
	{
		//1. 打印数据
		int j = 0;

		for (j = 0; j < col; j  )
		{
			printf(" %c ", board[i][j]);

			if (j < col - 1)
			{
				printf("|");
			}

		}

		printf("n");
		//2. 打印分割线
		if (i < row - 1)
		{
			//printf("---|---|---n");
			int j = 0;

			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;
	printf("玩家下棋>:n");
	
	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 ComputerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;//0~row-1
	int y = 0;//0~col-1
	printf("电脑下棋>:n");
	
	while (1)
	{
		x = rand() % row;
		y = rand() % col;

		if (' ' == board[x][y])
		{
			board[x][y] = '#';
			break;
		}

	}

}

int IsFull(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  )
		{

			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  )
	{
		int j = 0;
		int count = 0;

		for (j = 0; j < col - 1; j  )
		{

			if ((board[i][j] != ' ') && (board[i][j] == board[i][j   1]))
			{
				count  ;
			}

		}

		if ((col - 1) == count)
		{
			return board[i][0];
		}

	}

	//列
	for (i = 0; i < col; i  )
	{
		int j = 0;
		int count = 0;

		for (j = 0; j < row - 1; j  )
		{

			if ((board[j][i] != ' ') && (board[j][i] == board[j   1][i]))
			{
				count  ;
			}

		}

		if ((row - 1) == count)
		{
			return board[0][i];
		}

	}

	//对角线
	int count = 0;
	for (i = 0; i < row - 1; i  )
	{

		if ((board[i][i]!=' ') && (board[i][i]==board[i   1][i   1]))
		{
			count  ;
		}

	}

	if ((row - 1) == count)
	{
		return board[0][0];
	}

	count = 0;

	for (i = 0; i < row - 1; i  )
	{

		if ((board[i][col-1-i]!=' ') && (board[i][col-1-i]==board[i 1][col-2-i]))
		{
			count  ;
		}

	}

	if ((row-1) == count)
	{
		return board[0][col - 1];
	}

	//平局
	if (1 == IsFull(board, row, col))
	{
		return 'Q';
	}

	//继续
	return 'C';
}
代码语言:javascript复制
//test.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 };
	InitBoard(board, ROW, COL);
	//打印棋盘
	DisplayBoard(board, ROW, COL);
	//下棋
	char ret = 0;
	while (1)
	{
		//玩家下棋
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);

		if (ret != 'C')
		{
			break;
		}

		//电脑下棋
		ComputerMove(board, ROW, COL);
		DisplayBoard(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()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);//1 0 4
		
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏n");
			break;
		default:
			printf("选择错误,重新选择!n");
			break;
		}

	} while (input);

	return 0;
}

0 人点赞