C语言"黑框框"下不忍直视的三子棋实现

今天发一个界面丑到爆,无比Low的三子棋程序


同样,win32 无图形界面


输入坐标进行交互,

X代表玩家下子,0代表电脑下子(rand()函数随机产生合法坐标落子),率先连成三子则胜利。棋盘满未连成三子则平局。


程序比较简单直接发源码.三个块,game.h代表程序所需头文件、宏定义、函数声明,test.c代表程序游戏逻辑,game.c代表程序定义函数实现.


代码未优化,尽请见谅.


game.h:

/*
此头文件包含test.c所需头文件及相关函数声明
*/

#ifndef __GAME_H__
#define __GAME_H__

#include <stdio.h>
#include<stdlib.h>
#include<time.h>
	
//定义棋盘的行数与列数
#define ROWS 3					
#define COLS 3

//打印菜单
void menu(void);

//初始化棋盘
void init(char board[][COLS], int rows);	

//打印棋盘
void print(char board[][COLS], int rows);

//游戏环节
void play(char board[][COLS], int rows);						

//通过检查棋盘是否已满判断是否和棋
int checkfull(char board[][COLS], int rows);

//检查是否有人获胜
int checkwin(char board[][COLS], int rows);

//玩家玩
void player(char board[][COLS], int rows);	

//电脑玩
void computer(char board[][COLS], int rows);	

//检查是否有人获胜or平局
int check(char board[][COLS], int rows);	


#endif



test.c:

/*
三子棋游戏逻辑
*/

#include "game.h"

int main(void)
{

	int input;								//存放用户输入选项 

	char board[ROWS][COLS];							//存放棋盘

	menu();									//打印菜单

	scanf("%d", &input);					<span style="white-space:pre">		</span>//接受用户输入

	while( input )
	{	
		init(board, ROWS);						//初始化棋盘

		print(board, ROWS);						//打印棋盘

		play(board, ROWS);						//进行游戏

		menu();								//打印菜单

		scanf("%d", &input);				<span style="white-space:pre">		</span>//再次接受用户输入
	}												
										//游戏结束

	printf("\nBye!\n");					<span style="white-space:pre">	</span>	//程序结束

	return 0;

}

game.c:


/*
此源代码包含test.c函数实现
*/

#include "game.h"

//打印菜单
void menu(void)
{
	printf("**********************\n");
	printf("*****   1.play   *****\n");
	printf("*****   0.exit   *****\n");
	printf("**********************\n");
}

//初始化棋盘
void init(char board[][COLS], int rows)
{
	int r;
	int c;

	for( r = 0; r < rows; r++ )
		for( c = 0; c < COLS; c++ )
			board[r][c] = ' ';							//游戏未开始时将棋子所在位置初始化为空格
}

//打印棋盘
void print(char board[][COLS], int rows)
{
	int r;
	int c;

	for( r = 0; r < rows; r++)
	{
		for( c = 0; c < COLS; c++)
		{
			printf(" %c ", board[r][c]);
			
			if( c < 2 )								//打印两次 "|"
				printf("|");
		}
		
		printf("\n");
	
		if( r == 2 )									//打印棋盘最后一行时规则不同
			printf("   |   |   ");
		else
			printf("___|___|___");

		printf("\n");
	}
}

//检查是否有人获胜or平局
int check(char board[][COLS], int rows)
{
		int ret;									//接受棋盘是否已满返回值
		int flag;									//接受是否获胜返回值

		flag = checkwin(board, rows);				                        //检查是否有人获胜

		if( flag == 1 )									//flag为1玩家胜
		{
			printf("你赢了!\n");
			return 1;
		}
		else if( flag == 0 )							        //flag为0电脑胜
		{
			printf("呵呵!\n");
			return 1;
		}

		ret = checkfull(board, rows);					                //如若前面为无人获胜,则此处通过检查棋盘是否已满判断是否和棋

		if( ret == 1 )									//ret为1 则棋盘满 和棋
		{
			printf("和棋!\n");
			return 1;
		}

		return 0;									//返回1 则此局结束, 返回0 则此局未结束!
}


//通过检查棋盘是否已满判断是否和棋
int checkfull(char board[][COLS], int rows)
{
	int r;
	int c;

	for( r = 0; r < rows; r++ )
		for( c = 0; c < COLS; c++ )
			if( ' ' == board[r][c])
				return 0;							//返回0 代表棋盘未满

	return 1;										//返回1 代表棋盘已满
}

//检查是否有人获胜
int checkwin(char board[][COLS], int rows)
{								
	int r;
	int c;

	for( r = 0; r < rows; r++ )							        //判断3行情况下是否有人获胜
	{
		if( (board[r][0] == board[r][1]) 
			&& (board[r][1] == board[r][2])
			&& (board[r][2] == 'x') )					<span style="white-space:pre">	</span>//x为玩家落子
			return 1;								//返回1 玩家胜

		if( (board[r][0] == board[r][1]) 
			&& (board[r][1] == board[r][2])
			&& (board[r][2] == '0') )				    <span style="white-space:pre">	</span> <span style="white-space:pre">	</span> //0为电脑落子
			return 0;								//返回0 电脑胜
	}

	for( c = 0; c < COLS; c++ )					    	                //判断3列情况下是否有人获胜
	{
		if( (board[0][c] == board[1][c]) 
			&& (board[1][c] == board[2][c])
			&& (board[2][c] == 'x') )				<span style="white-space:pre">	</span>	//x为玩家落子
			return 1;								//返回1 玩家胜

		if( (board[0][c] == board[1][c]) 
			&& (board[1][c] == board[2][c])
			&& (board[2][c] == '0') )				<span style="white-space:pre">	</span>	//0为电脑落子		
			return 0;								//返回0 电脑胜
	}

		if( (board[0][0] == board[1][1])				                //下面为判断对角线情况是否有人获胜
			&& (board[1][1] == board[2][2])
			&& (board[2][2] == 'x') )
			return 1;

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

		if( (board[0][2] == board[1][1])
			&& (board[1][1] == board[2][0])
			&& (board[2][0] == 'x') )
			return 1;

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

//玩家玩
void player(char board[][COLS], int rows)
{
	int x;
	int y;
	
	while( 1 )
	{
		printf("请输入坐标:");
		scanf("%d %d", &x, &y);

		if( board[x - 1][y - 1] == ' ' )				         //玩家会将数组位置 0.0 视为 1.1 所以此处对 x y 分别减1
		{
			board[x - 1][y - 1] = 'x';
			
			break;
		}
		else
		{
			printf("位置不合法!\n");
		}
	}//退出while循环

	print(board, rows);								//打印更新后棋盘
}

//电脑玩
void computer(char board[][COLS], int rows)
{
	int x;
	int y;
	
	srand((unsigned)time(NULL));

	while( 1 )
	{
		
		x = rand() % 3;							
		y = rand() % 3;

		if( board[x][y] == ' ' )
		{
			board[x][y] = '0';

			break;
		}

	}//退出循环

	printf("电脑玩!\n");
	print(board, rows);								//打印更新后的棋盘
}
	



//游戏环节
void play(char board[][COLS], int rows)
{	
	while(1)
	{

		player(board, rows);							//玩家玩
		
		if( check(board, rows) )						//检查是否有人获胜or平局
			break;								

		computer(board, rows);							//电脑玩

		if( check(board, rows) )						//检查是否有人获胜or平局
			break;										

	}//退出while循环
}




  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值