五子棋 test10

###自制五子棋
(注.本五子棋游戏完全由C语言编写)
####简单思路:
1.定义二维数组
2.构建棋盘并初始化
3.输出棋盘
4.建立一个死循环:
玩家走(判断落点合法性),
输出棋盘
判断玩家赢、平局,
电脑走(判断是否已经走过),
输出棋盘
判断电脑赢、平局。
当玩家赢或电脑赢或平局就跳出循环,否则继续

####上代码:
game.h

#ifndef __GAME_H__
#define __GAME_H__      //放置头文件,声明函数


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define ROWS 10  
#define COLS 10  


void   menu();
void   game();
void  Init_board(char board[ROWS][COLS],int row,int col);
void show_board(char board[ROWS][COLS],int row,int col);
void player_move(char board[ROWS][COLS],int row,int col);
void com_move(char board[ROWS][COLS],int row,int col);
char check_win(char board[ROWS][COLS],int row,int col);
int check_full(char board[ROWS][COLS],int row,int col);
#endif 

game.c

#define _CRT_SECURE_NO_WARNINGS 1  //游戏的核心代码
#include"game.h"

void menu(){
  printf("************************************\n");
  printf("*********欢迎来到五子棋游戏*********\n");
  printf("****  1.play            0.exit  ****\n");
  printf("************************************\n");
  
}
void Init_board(char board[ROWS][COLS],int row,int col){
	//将二维数组中每个元素附成‘ ’ 
memset (board,' ',row*col*sizeof(char) );

}
//输出棋盘 
void show_board(char board[ROWS][COLS],int row,int col){
	int i=0;
	int j=0;
	int m=0;
	for (m=0;m<row;m++){
		printf("%3d ",m+1);
		}
	printf("\n");
	for (i=0;i<row;i++){
		
		for (j=0;j<col;j++){

	printf(" %c |",board[i][j]);
		}
        printf(" %d\n",i+1);
		
		for (j=0;j<col;j++){
		printf(" ▔ ");
		}
		printf("\n");	
	}
	printf("\n\n\n");
}
void player_move(char board[ROWS][COLS],int row,int col){
     int x=0;
	 int y=0;
	 while (1){
	 printf("请输入落子的坐标\n");
	 scanf("%d%d",&x,&y);
	 x--;
	 y--;
	 if(( (x >= 0) && (x<=row)) &&( (y>= 0) && (y<=col) ))//判断坐标是否合法
	 {
		 if (board[x][y] ==' ')
		 {
	        board[x][y]='X';
	         break;
	     }
	 else 
		 printf("你输入的坐标有误\n");//该坐标已经有子
	 }
	 else
         printf("你输入的坐标有误\n");//该坐标超过地图边界
	 }
}
void com_move(char board[ROWS][COLS],int row,int col){
	int i=0;
	int j=0;
	while(1){
	i=rand()%row;
	j=rand()%row;
	if (board[i][j]==' '){
	board[i][j]='O';
	break;
	}
	}
}
char check_win(char board[ROWS][COLS],int row,int col){
   int i=0;
    int j=0;
	//一行五子
	for (i=0;i<row;i++){

	for (j=0;j<=col-5;j++){      
     if (board[i][j]==board[i][j+1]
	 &&board[i][j+1]==board[i][j+2]
	 &&board[i][j+2]==board[i][j+3]
	 &&board[i][j+3]==board[i][j+4]
	 &&board[i][j+4] != ' ')
	 {
		return board[i][j];
	 }
	}
	}
	 //一列五子
	 for (i=0;i<row;i++){
		 for (j=0;j<=col-5;j++){
		  if ( board[i][j]==board[i+1][j]
		  &&board[i+1][j]==board[i+2][j]
		  &&board[i+2][j]==board[i+3][j]
		  &&board[i+3][j]==board[i+4][j]
		  &&board[i+4][j]!=' '){
		  return board[i][j];
		  }	 
		 }
	 }
	//左斜线五子
	 for (i=0;i<=row-5;i++){
		 for (j=0;j<=row-5;j++){
		 if (board[i][j]==board[i+1][j+1]
		 &&board[i+1][j+1]==board[i+2][j+2]
		 &&board[i+2][j+2]==board[i+3][j+3]
		 &&board[i+3][j+3]==board[i+4][j+4]
		 &&board[i+4][j+4]!=' '){
		  return board[i][j];
		 }
		 }
	 }
	 //右斜线五子
	 for (i=0;i<=row-5;i++){
		 for (j=col;j>=col-5;j--){
		 if (board[i][j]==board[i+1][j-1]&&
			 board[i+1][j-1]==board[i+2][j-2]&&
			 board[i+2][j-2]==board[i+3][j-3]&&
			 board[i+3][j-3]==board[i+4][j-4]&&
			 board[i][j]!=' '){
		 return board[i][j];
		 }
		 }
	 
	 }
	 if ( check_full(board,row,col)  ){
	 return 'P';
	 }
	 return ' ';
	}

int check_full(char board[ROWS][COLS],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;
}
void game(){
    //定义二维数组
	char board[ROWS][COLS]={0};
	char ch=0;
	srand((unsigned int) time(NULL));
	//构建棋盘并初始化
	Init_board(board,ROWS,COLS);
	/*输出棋盘*/
	show_board(board,ROWS,COLS);
	/*建立一个死循环:玩家走(判断落点合法性),
	                 输出棋盘
	                 判断玩家赢、平局,
				     电脑走(判断是否已经走过),
				     输出棋盘
				     判断电脑赢、平局。
				     当玩家赢或电脑赢或平局就跳出循环,否则继续*/
	while(1)
	{
		player_move(board,ROWS,COLS);
		system("cls");//清屏
		show_board(board,ROWS,COLS);
		if ( ( (ch=check_win(board,ROWS,COLS)) =='X' ) || ( (ch=check_win(board,ROWS,COLS)) =='P')  )
		{
		   break;
		}
		com_move(board,ROWS,COLS);
		system("cls");
		show_board(board,ROWS,COLS);
		if (  ( (ch=check_win(board,ROWS,COLS) ) =='O') ||( (ch=check_win(board,ROWS,COLS)) =='P'))
		{
		   break;
		}
           
	}
	if (ch=='X'){
	printf("恭喜玩家获得比赛胜利!\n");
	}
	if (ch=='O'){
	printf("恭喜电脑获得比赛胜利!(滑稽)\n");
	}
	if (ch=='P'){
	printf("平局!\n");

	}
}


test.c

#define _CRT_SECURE_NO_WARNINGS 1     //放置游戏操作流程,用于测试
#include "game.h"

int main(){
     int i=0;
	do {
		//打印棋盘
		menu();
		scanf("%d",&i);
		//根据输入选择分支
		switch(i){
		case 1:
			game ();//进入游戏程序
			break;
		case 0:
			//退出程序
			break;
		default:
			printf("输出有误!\n");
			break;
		}
	}
		while (i);
	
	

	
     return 0;
	
}

####总结:本五子棋有以下不足:
####1.没有创建一个落子计时器用于倒计时(太懒= =,而且清屏会显得界面明了,取舍下放弃了计时器)
####2.不支持玩家对弈,电脑也不够智能落子(还是太懒= =)

####有意见建议欢迎评论,代码较挫…望多多包涵。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值