三子棋游戏

        本程序创建了Game.c、Chess_Three、Game.h三个文件。

首先Chess_Three.c的文件:、

#define _CRT_SECURE_NO_WARNINGS 1

//三子棋游戏
#include "Game.h"

//选项显示
void menu()
{
	printf("########################\n");
	printf("####1.Play    0.Exit####\n");
	printf("########################\n");
}

//游戏的显示
void game()
{
	char ret;
	char board[ROW][COL] = { 0 };
	//棋盘初始化
	InitBoard(board, ROW, COL);
	//棋盘打造
	Dis_Board(board, ROW, COL);
	//下棋
	while (1)
	{
		PlayerMove(board, ROW, COL);
		Dis_Board(board, ROW, COL);
		ret = Is_Win(board, ROW, COL);
		if (ret == '*')
		{
			printf("玩家赢了\n");
			break;
		}
		else if (ret == '#')
		{
			printf("电脑赢了\n");
			break;
		}
		else if (ret == 'Q')
		{
			printf("平局\n");
			break;
		}
		ComputerMove(board, ROW, COL);
		Dis_Board(board, ROW, COL);
		ret = Is_Win(board, ROW, COL);
		if (ret == '*')
		{
			printf("玩家赢了\n");
			break;
		}
		else if (ret == '#')
		{
			printf("电脑赢了\n");
			break;
		}
		else if (ret == 'Q')
		{
			printf("平局\n");
			break;
		}
			
	}
}

//选择是否开启游戏
void game_judge()
{
	int input;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请输入:");
		scanf("%d", &input);
		switch (input)
		{
		case 1: 
			printf("游戏开始\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输出错误\n");
			break;
		}
	} while(input);
	
}

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

接下来是Game.c的文件:

#define _CRT_SECURE_NO_WARNINGS 1

#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 Dis_Board(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 < row; j++)
			{
				printf("---");
				if (j < row - 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("槽位已使用,请重选\n");
		}
		else
			printf("非法落子,请重下\n");
	}
}

//很傻的电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("电脑落子:");
	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			printf("%d,%d\n", x+1, y+1);
			break;
		}
	}
	
}

//判断棋盘是否还存有位子
int Is_Full(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 Is_Win(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 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];
	}
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
			return board[1][i];
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
		return board[1][1];
	if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
		return board[1][1];

	if (1 == Is_Full(board, ROW, COL))
	{
		return 'Q';
	}
	return 'C';
}

最后为Game.h的文件:

#define _CRT_SECURE_NO_WARNINGS 1

#ifndef __GAME_H__
#define __GAME_H__

#include
#include
#include

#define ROW	 3
#define COL	 3


void InitBoard(char board[ROW][COL], int row, int col);
void Dis_Board(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 Is_Win(char board[ROW][COL], int row, int col);
void Stop();
#endif

以上就是全部内容。

        本代码只是我写来练手的,中间会有很多可以优化的地方,请见谅,还在学习中,哈哈,感兴趣可以复制了玩,只是这个游戏建议目的为让机器赢,不然它太傻了,哈哈。

 

你可能感兴趣的:(c语言)