c语言—三子棋

test.c:

#define _CRT_SECURE_NO_WARNINGS 1 
#include"game.h"
void menu()
{
	printf("*************************************\n");
	printf("****** 1.play game. 0.exit.  ********\n");
	
}
int main()
{   
	int num=2;
	char chess_road[3][3]={0};
	menu();
	while(num!=1||num!=0)
	{
	printf("请选择:");
    scanf("%d",&num);
	if(num==1)
	{
	game_road(chess_road);
		  while(is_full(chess_road))
		  {
	computer_game(chess_road);
	is_win(chess_road);
	player_game(chess_road);
	is_win(chess_road);
	}
	}
	else if(num==0)
		return;
	else
		printf("请重新输入:");
	}
     
}

   game.h:

#ifndef _GAME_H_
#define _GAME_H_



#include<stdio.h>  
#include<malloc.h>  
#include<time.h>  
#include<stdlib.h> 

void game_road(char chess_road[3][3]);
void printf_road(char chess_road[3][3]);
int is_full(char chess_road[3][3]);
void computer_game(char chess_road[3][3]);
void player_game(char chess_road[3][3]);
int is_win(char chess_road[3][3]);


#endif
game.c:

#define _CRT_SECURE_NO_WARNINGS 1 
#include"game.h"
void game_road(char chess_road[3][3])//设置游戏棋盘
 
{
	int i=0;
	int j=0;
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			chess_road[i][j]=' ';
		}
	}
for(i=0;i<3;i++)
	{
		printf("%c|%c|%c\n",chess_road[i][0],chess_road[i][1],chess_road[i][2]);
		if(i!=2)
			printf("-|-|-\n");
	}
}
void printf_road(char chess_road[3][3])//打印游戏中的棋盘
{
	int i=0;
    printf("\n");
	for(i=0;i<3;i++)
	{
		printf("%c|%c|%c\n",chess_road[i][0],chess_road[i][1],chess_road[i][2]);
		if(i!=2)
			printf("-|-|-\n");
	}

}

int is_full(char chess_road[3][3])//判断棋盘中的棋子是否满
{
	int i=0,j=0;
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			if (chess_road[i][j]==' ')
				return 1;
		}
	}
	return 0;
}
void computer_game(char chess_road[3][3])//电脑开始落子
{
	int line=0;
	int column=0;
	int count=0;
	printf("电脑开始游戏...\n");
	if(is_full(chess_road)==0)
	{
		exit(0);
	}
	while(line=rand()%3,column=rand()%3,chess_road[line][column]==' ')
	{
		count++;
	chess_road[line][column]='*';
	if(count==1)
		break;
}
	printf_road(chess_road);
}
void player_game(char chess_road[3][3])//玩家开始落子
{
	int line=-1;
	int column=-1;
	if(is_full(chess_road)==0)
	{
		exit(0);
	}
	do
	{   printf("玩家开始游戏...\n");
		printf("请输入坐标:");
	scanf("%d%d",&line,&column);
	}
	while(line>3||line<1||column<1||column>3||chess_road[line-1][column-1]!=' ');	
       chess_road[line-1][column-1]='+';
	   printf_road(chess_road);
}
int is_win(char chess_road[3][3])//判断胜负
{
	int i=0;
	if(chess_road[0][0]==chess_road[1][1]&&chess_road[1][1]==chess_road[2][2])
		
	{
		if(chess_road[0][0]=='*')
		{
         printf("电脑赢\n");
		  exit(0);
		}
		if(chess_road[0][0]=='+')
		{
			printf("玩家赢\n");
			exit(0);
		}
	}
		for( i=0;i<3;i++)
		{
			if(chess_road[i][0]==chess_road[i][1]&&chess_road[i][1]==chess_road[i][2])
			{
				if(chess_road[i][0]=='*')
				{
					printf("电脑赢\n");
					exit(0);
				}
				 if(chess_road[i][0]=='+')
				 {
					printf("玩家赢\n");
					exit(0);
					}
			}
		}
             if(is_full(chess_road)==0)
			 {
           printf("平局\n");
		      exit(0);
			 }
			 return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值