C语言实现三子棋

 本次程序涉及到的函数声明

#define  _CRT_SECURE_NO_WARNINGS
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define COL 3
#define ROW 3
void game();//游戏函数
void Initboard(char board[ROW][COL], int x, int y);//格式化棋盘 将每个位置初始化成“空格”
void displayboard(char board[ROW][COL], int row,int col);//打印棋盘函数
void player_move(char board[ROW][COL], int row, int col);//玩家下棋操作函数
void computer_move(char board[ROW][COL], int row, int col);//电脑下棋函数
char judge(char board[ROW][COL], int row, int col);//判断游戏结束函数
int Full(char board[ROW][COL], int row, int col);//判断棋盘是否满了

 函数体

#include"game.h"
void game()
{
	/*int cow = 0;
	int col = 0;
	scanf("%d %d", &cow, &col);*/
	char board[ROW][COL] = {0};
	Initboard(board,ROW,COL);
	 displayboard(board,ROW,COL);
	 
	 while (1)
	 {
		 player_move(board, ROW, COL);
		 displayboard(board, ROW, COL);
		 if (judge(board, ROW, COL) == '*')
		 {
			 printf("玩家胜利\n");
			 break;
		 }
		  if (judge(board, ROW, COL) == '#')
		 {
			 printf("电脑胜利\n");
			 break;
		 }
		  if (judge(board, ROW, COL) == 'Q')
		 {
			 printf("平局\n");
			 break;
		 }
		 else
		 {
			 printf("继续游戏\n");
			 
		 }
		 computer_move(board, ROW, COL);
		 displayboard(board, ROW, COL);
		 if (judge(board, ROW, COL) == '*')
		 {
			 printf("玩家胜利\n");
			 break;
		 }
		  if (judge(board, ROW, COL) == '#')
		 {
			 printf("电脑胜利\n");
			 break;
		 }
		  if (judge(board, ROW, COL) == 'Q')
		 {
			 printf("平局\n");
			 break;
		 }
		 else
		 {
			 printf("继续游戏\n");

		 }
	 }
	 
}
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++)
	{
		int j = 0;
		for(j = 0; j < COL; j++)
		{
			printf(" %c ", board[i][j]);
			if(j<COL-1)
			{
				printf("|");
			}

		}
		printf("\n");
		for (j = 0; j < COL; j++)
		{
			printf("---");
			if (j < COL - 1)
			{
				printf("|");
			}
		}
		printf("\n");
	}
}
void player_move(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		
		printf("输入坐标\n");
		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 computer_move(char board[ROW][COL],int row,int col)
{
	printf("电脑下棋\n");
	while (1)
	{
		
		int x = rand() % ROW;
		int y = rand() % COL;
		
		
			if (board[x][y] == ' ')
			{
				board[x][y] = '#';
				break;
			}
			
		
	}
}
char judge(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] != ' ')
		{
			if (board[i][1] == '*')
			{
				return '*';

				break;
			}
			else
			{
				return '#';
				break;
			}
		}
	}
	for (i = 0; i < COL; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
		{
			if (board[0][i] == '*')
			{
				return '*';

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

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

			
		}
		if(board[1][1]=='#')
		{
			return '#';
			
		}
	}
		 if (Full(board,row,col))
		{
			return 'Q';
		}
	
		
			return 'C';
		
	
}
int Full(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;
}
#define  _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu ()
{
	printf("************************************\n");
	printf("***********1.paly 0.exit************\n");
	printf("************************************\n");
}

int main()
{

	srand((unsigned int)time(NULL));
		int input = 0;
	do
	{  
		menu();
		printf("请输入1/0\n");
		scanf("%d", &input);
		switch( input)
		{
		case 1:
			printf("游戏开始 >\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
		


	} while (input);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值