C语言实现N子棋

学习了数组之后,我尝试写了一段N子棋代码,采用了面向对象的编程思想,将绘画棋盘,落子,规则分别作为对象,编程以实现功能,作为初学者,编程过程中可能有一些不正确的地方或者又可以改进的地方,请大家批评指正。

主函数:

#define _CRT_SECURE_NO_WARNINGS
#include
#include"game.h"

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

void game()
{
	char board[ROW][COL] = {0};		//创建棋盘
	InitBoard(board, ROW, COL);		//初始化棋盘
	DisplayBoard(board,ROW,COL);	//打印棋盘
	//   |   |   
	//---|---|---
	//   |   |   
	//---|---|---
	//   |   |   
	while (1)
	{
		int a = 0;
		PlayerMove(board,ROW,COL);
		DisplayBoard(board, ROW, COL);
		a=Judge(board,ROW,COL,WIN);
		switch (a)
		{
		case 1:
			printf("你赢了!\n");
			break;
		case 2:
			printf("你输了!\n");
			break;
		case 3:
			printf("平局!\n");
			break;
		case 4:
			break;
		}
		if (a != 4)
			break;
		ComputerMove(board,ROW,COL);
		DisplayBoard(board, ROW, COL);
		a = Judge(board, ROW, COL,WIN);
		switch (a)
		{
		case 1:
			printf("你赢了!\n");
			break;
		case 2:
			printf("你输了!\n");
			break;
		case 3:
			printf("平局!\n");
			break;
		case 4:
			break;
		}
		if (a != 4)
			break;
	}
}

void test()
{
	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);
}

int main()
{
	test();
	return 0;
}

头文件:

#define _CRT_SECURE_NO_WARNINGS
#define ROW 3
#define COL 3
#define WIN 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);
int Judge(char board[ROW][COL], int row, int col,int win);

在头文件中可以更改棋盘的大小和N子棋中N的数值

棋盘行数定义在ROW中,列数定义在COL中

声明的函数InitBoard可以初始化棋盘,将数组每一个元素都赋值为空格(‘ ’)

函数DisplayBoard可以将棋盘实时绘画出来

函数PlayerMove和ComputerMove分别为玩家落子和电脑落子操作函数

函数Judge为裁判函数,每一次落子后,检验游戏是否结束

game函数:

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#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("|");
			else
				printf("\n");
		}
		for (int j = 0; j < col; j++)
		{
			if (i < row - 1)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
				else
					printf("\n");
			}
		}
	}
}

void PlayerMove(char board[ROW][COL], int row, int col)
{
	int a = 1;
	do
	{
		printf("玩家走:>\n");
		printf("请输入坐标:>\n");
		int x = 0, y = 0;
		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] = '*';
				a = 0;
			}
			else
			{
				printf("该坐标被占用\n");
			}
		}
		else
		{
			printf("坐标非法,请重新输入\n");
		}
	} while (a);
}

void ComputerMove(char board[ROW][COL], int row, int col)
{
	printf("电脑走:>\n");
	int a = 1;
	do
	{
		int x = 0, y = 0;
		srand((unsigned)time(NULL));
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '1';
			a = 0;
		}
	} while (a);
}

int Judge(char board[ROW][COL], int row, int col,int win)
{
	for (int x = 0; x < row; x++)//判断行
	{
		for (int y = 0; y < col - win+1; y++)
		{
			int count = 1;
			for (int k = 1; k < win; k++)
			{
				if (board[x][y] == board[x][y + k] && board[x][y] !=' ')
					count++;
			}
			if (count == win && board[x][y] == '*')
				return 1;
			else if (count == win && board[x][y] == '1')
				return 2;
		}
	}
	for (int y = 0; y < col; y++)//判断列
	{
		for (int x = 0; x < row - win+1; x++)
		{
			int count = 1;
			for (int k = 1; k < win; k++)
			{
				if (board[x][y] == board[x + k][y] && board[x][y] != ' ')
					count++;
			}
			if (count == win && board[x][y] == '*')
				return 1;
			else if (count == win && board[x][y] == '1')
				return 2;
		}
	}
	for (int x = 0; x < row - win + 1; x++)//判断右斜
	{
		for (int y = 0; y < col - win + 1; y++)
		{
			int count = 1;
			for (int k = 1; k < win; k++)
			{
				if (board[x][y] == board[x + k][y + k] && board[x][y] != ' ')
					count++;
			}
			if (count == win && board[x][y] == '*')
				return 1;
			else if (count == win && board[x][y] == '1')
				return 2;
		}
	}
	for (int x = win; x < row + 1; x++)//判断左斜
	{
		for (int y = 0; y < col - win + 1; y++)
		{
			int count = 1;
			for (int k = 1; k < win; k++)
			{
				if (board[x][y] == board[x - k][y + k] && board[x][y] != ' ')
					count++;
			}
			if (count == win && board[x][y] == '*')
				return 1;
			else if (count == win && board[x][y] == '1')
				return 2;
		}
	}
	int count = 0;
	for (int x = 0; x < row; x++)
	{
		for (int y = 0; y < col;y++)
		if (board[x][y] != ' ')
			count++;
	}
	if (count == row*col)
		return 3;
	return 4;
}

你可能感兴趣的:(C语言实现N子棋)