三子棋C语言实现(多种模式任你选)

头文件部分


//game.h

//'X':人的棋子
//'0':电脑的棋子

#ifndef __GAME_H__
#define __GAME_H__

#define _CRT_SECURE_NO_WARNINGS 1

#define ROWS 3
#define COLS 3

enum OP
{
	EXIT,
	PLAY
};

enum FIRST
{
	FIRST_PLAYER=1,
	FIRST_COMPUTER
};

enum ML
{
	SIMPLE=1,
	COMPLEX
};

void menu1();
void menu2();
void menu3();
void init_board(char board[ROWS][COLS]);
void printf_board(char board[ROWS][COLS]);
void player_move(char board[ROWS][COLS]);
void computer_move(char board[ROWS][COLS],int model);
int computer_consider(char board[ROWS][COLS]);
void computer_random(char board[ROWS][COLS]);
char judge_bunko(char board[ROWS][COLS]);
void player_first_move(char board[ROWS][COLS], int model);
void computer_first_move(char board[ROWS][COLS], int model);

#endif  //__GAME_H__

实现部分

//game.c

#include "game.h"
#include<stdio.h>
#include<stdlib.h>

char judge_bunko(char board[ROWS][COLS])
{
	int i = 0;
	int j = 0;
	int count=0;  //棋子数目


	for (i = 0; i < ROWS; 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 < COLS; 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[0][0] != ' ')) //判断主对角线是否有三颗相同的棋子
	{
		return board[0][0];
	}

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

	for (i = 0; i < ROWS; i++)  //判断棋盘是否满了
	{
		for (j = 0; j < COLS; j++)
		{
			if (' ' != board[i][j])
				count++;
		}
	}

	if (ROWS*COLS == count)
	{
		return 'M';
	}

	return '#';
}

void player_first_move(char board[ROWS][COLS], int model)
{
	char bunko;

	do
	{
		player_move(board);
		printf_board(board);
		bunko = judge_bunko(board);

		if ('X' == bunko)
		{
			printf("恭喜你赢了!^*^");
			break;
		}

		if ('M' == bunko)
		{
			printf("平局!^=^");
			break;
		}

		computer_move(board, model);
		printf_board(board);
		bunko = judge_bunko(board);

		if ('0' == bunko)
		{
			printf("你真菜!^&^");
			break;
		}

		if ('M' == bunko)
		{
			printf("平局!^=^");
			break;
		}
	} while ('#' == bunko);
}

void computer_first_move(char board[ROWS][COLS], int model)
{
	char bunko;

	do
	{
		computer_move(board, model);
		printf_board(board);
		bunko = judge_bunko(board);

		if ('0' == bunko)
		{
			printf("你真菜!^&^");
			break;
		}

		if ('M' == bunko)
		{
			printf("平局!^=^");
			break;
		}

		player_move(board);
		printf_board(board);
		bunko = judge_bunko(board);

		if ('X' == bunko)
		{
			printf("恭喜你赢了!^*^");
			break;
		}

		if ('M' == bunko)
		{
			printf("平局!^=^");
			break;
		}
	} while ('#' == bunko);
}

void computer_random(char board[ROWS][COLS])
{
	int x = 0;
	int y = 0;

	do
	{
		x = rand() % 3;
		y = rand() % 3;
	} while (' ' != board[x][y]);

	board[x][y] = '0';
}

int computer_consider(char board[ROWS][COLS])
{
	int i = 0;

	//*****判断行上是否有两颗相同的棋子*****
	for (i = 0; i < ROWS; i++) 
	{
		if ((board[i][0] == board[i][1]) && (' ' != board[i][0])&&(' '==board[i][2]))
		{
			board[i][2] = '0';
			return 1;         //表示此时电脑已经落子
		}
		
		if ((board[i][1] == board[i][2]) && (' ' != board[i][1]) && (' ' == board[i][0]))
		{
			board[i][0] = '0';
			return 1;
		}

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

	//*****判断列上是否有两颗相同的棋子*****
	for (i = 0; i < COLS; i++) 
	{
		if ((board[0][i] == board[1][i]) && (' ' != board[0][i]) && (' ' == board[2][i]))
		{
			board[2][i] = '0';
			return 1;
		}

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

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

	//*****判断主对角线上是否有两颗相同的棋子*****
	if ((board[0][0] == board[1][1]) && (' ' != board[0][0]) && (' ' == board[2][2]))
	{
		board[2][2] = '0';
		return 1;
	}

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

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

	//*****判断次对角线上是否有两颗相同的棋子*****
	if ((board[0][2] == board[1][1]) && (' ' != board[0][2]) && (' ' == board[2][0]))
	{
		board[2][0] = '0';
		return 1;
	}

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

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

	return 0;
}

void computer_move(char board[ROWS][COLS],int model)
{
	int ret = 0;

	printf("\n电脑出棋>:\n");

	if (SIMPLE == model)
	{
		computer_random(board);
	}

	if (COMPLEX == model)
	{
		ret = computer_consider(board);
		if (0 == ret)    //表示电脑没有出棋的最优位置,则随机落子
		{
			computer_random(board);
		}
	}
}

void player_move(char board[ROWS][COLS])
{
	int x = 0;
	int y = 0;

	printf("\n请出棋:>");
	fflush(stdin);
	scanf("%d%d", &x, &y);

	while ((x<0) || (y<0) || (x>ROWS) || (y>COLS)|| ' '!=board[x-1][y-1])
	{
		printf("\n输入位置不合法,请重新输入:>");
		scanf("%d%d", &x, &y);
	}

	board[x - 1][y - 1] = 'X';
}

void menu3()
{
	printf("----------------------------------------------\n");
	printf("-------------        1.简易模式      ---------\n");
	printf("-------------        2.高级模式      ---------\n");
	printf("----------------------------------------------\n");
}

void menu2()
{
	printf("----------------------------------------------\n");
	printf("-------------        1.玩家      -------------\n");
	printf("-------------        2.电脑      -------------\n");
	printf("----------------------------------------------\n");

}

void menu1()
{
	printf("----------------------------------------------\n");
	printf("-------------     1.开始游戏      ------------\n");
	printf("-------------     0.退出游戏      ------------\n");
	printf("----------------------------------------------\n");
}

void init_board(char board[ROWS][COLS])
{
	int i = 0;
	int j = 0;
	
	for (i = 0; i < ROWS;i++)
	for (j = 0; j < COLS; j++)
	{
		board[i][j] = ' ';
	}
}

void printf_board(char board[ROWS][COLS])
{
	int i = 0;
	int j = 0;
	
	for (i = 0; i < ROWS; i++)
	{
		printf(" %c | %c | %c\n",board[i][0],board[i][1],board[i][2]);
		if (i != 2)
		{
			printf("---|---|---\n");
		}
	}
}

测试部分

//text.c

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

void start_game()
{
	srand((unsigned)time(NULL));

	char board[ROWS][COLS] = { 0 };
	int first = 0;
	int model = 0;

	init_board(board);
	system("cls");

	menu3();
	printf("\n请选择模式:>");
	fflush(stdin);
	scanf("%d", &model);
	system("cls");

	menu2();
	printf("\n请选择谁先出棋:>");
	fflush(stdin);
	scanf("%d", &first);
	system("cls");
	printf_board(board);
	printf("\n****游戏开始****\n");
	switch(first)
	{
	case FIRST_PLAYER:
		player_first_move(board, model);
		break;
	case FIRST_COMPUTER:
		computer_first_move(board, model);
		break;
	default:
		break;
	}
}

void play_game()
{
	int again = 0;
	int choice = 0;

	menu1();
	printf("请选择:>");
	fflush(stdin);
	scanf("%d", &choice);

	do
	{
		switch(choice)
		{
		case PLAY:
		{
					 start_game();
					 break;
		}
		case EXIT:exit(0); break;
		default:
			break;
		}
		printf("\n\n再来一局?(Y/N):>");
		fflush(stdin);
		scanf("%c", &again);
		system("cls");
	} while (('Y' == again) || ('y'==again));
}

int main()
{
	play_game();

	printf("\n欢迎再来!\n");
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值