三子棋简易思路

此版本的三子棋很适合新手上手学习,用到的知识也比较简单,主要是函数和数组,基本上学了c语言一个月左右就可以尝试。

源文件 test.c

包含

1.起始菜单打印函数(menu())

2.游戏函数(play())

3.主函数(main())

#include "game.h" //此头文件后面会注明
void menu()
{
	printf("1.play\n");
	printf("0.exit\n");
}

//此处的play()函数并未完整,仅表示框架
void play()
{
	char res;
	char board[ROW][COL];
	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));
    //用时间戳来作为srand()函数的种子,使得生成的数为随机值
	int n = 1;
	while (n)
	{
		menu();

		scanf("%d", &n);
		switch (n)
		{
		case 1:
			play();
			break;
		case 0:
			break;
		default:
			printf("输入错误,请重新输入>");
			break;
		}
	}
	return 0;
}
源文件 game.c

包含

1.初始化棋盘函数(Initboard(char board[ROW][COL], int row, int col))

2.显示棋盘(Displayboard(char board[ROW][COL], int row, int col))

3.玩家动函数(Playermove(char board[ROW][COL], int row, int col))

4.电脑动函数(Computermove(char board[ROW][COL], int row, int col))

5.判断输赢(Iswin(char board[ROW][COL], int row, int col))

#include "game.h"//此头文件后面会注明
void Initboard(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++)
			board[i][j] = ' ';
	}
}

void Displayboard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (j = 0; j < row; j++)
	{
		for (i = 0; i < col; i++)
		{
			printf(" %c ", board[j][i]);
			if (i < col - 1)
				printf("|");
		}
		printf("\n");
		if (j < row - 1)
		{
			for (i = 0; i < col; i++)
			{
				printf("---");
				if (i < col - 1)
					printf("|");
			}
		}
		printf("\n");
	}
}

void Playermove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	
	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("该坐标被占有,请重新输入");
		}
		else
			printf("该坐标非法,请重新输入");
	}
}

void Computermove(char board[ROW][COL], int row, int col)
{
	printf("电脑下棋\n");
	int x = 0;
	int y = 0;

	while (1)
	{
		x = rand() % row + 1;
		y = rand() % col + 1;
        //随机生成坐标,并判断是否被占有
		if (board[x - 1][y - 1] == ' ')
		{
			board[x - 1][y - 1] = '#';
			break;
		}
	}
}

static int 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 0;
		}
	}
	return 1;
}

//判断输赢
//玩家赢返回"*"
//电脑赢返回"#"
//平局返回"Q"
//继续返回"C"

char Iswin(char board[ROW][COL], int row, int col)
 {
	for (int i = 0; i < row; i++)
	{
		if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][1] != ' '))
			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[1][j] != ' '))
			return board[0][j];
	}

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

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

	if (Isfull(board, row, col))
		return 'Q';//平局判断
	return 'C';//当游戏继续时,返回'C'
}
补充test.c中的play()函数
void play()
{
	char res;
	char board[ROW][COL];
	Initboard(board, ROW, COL);
	Displayboard(board, ROW, COL);
	while (1)
	{
		Playermove(board, ROW, COL);
		Displayboard(board, ROW, COL);
		res = Iswin(board, ROW, COL);
		if (res != 'C')
			break;
		Computermove(board, ROW, COL);
		Displayboard(board, ROW, COL);
		res = Iswin(board, ROW, COL);
		if (res != 'C')
			break;
	}
	if ((Iswin(board, ROW, COL)) == '*')
		printf("玩家赢\n\n");
	else if ((Iswin(board, ROW, COL)) == '#')
		printf("电脑赢\n\n");
	else
		printf("平局\n\n");
}

为使test.c能使用到game.c中的函数,将game.c中的函数在game.h中声明

头文件game.h
#pragma once
#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 Playermove(char board[ROW][COL], int row, int col);

void Computermove(char board[ROW][COL], int row, int col);

char Iswin(char board[ROW][COL], int row, int col);
运行效果

三子棋简易思路_第1张图片

 

你可能感兴趣的:(c语言,linux,服务器)