C语言实现简单的三子棋小游戏

作为一个编程的新手,做一个这样的小游戏其实挺有难度的,但是平时就要重在总结。现在就是我对这个游戏的成品以下的认知。

虽然这个完全可以用一个主函数直接写完,但是我还是想了解一下头文件的用法,还有多多的运用函数,一般对于这种新手编写的程序,创建一个头文件,然后再创建一个放你要自定义的函数的源文件,最后创建一个放主函数的源文件。这些基本就够用了。

1.头文件的格式。

现在我对头文件有一个简单的认知,首先它有严格的格式. 就和编程序必须要引头文件一样,他的格式:  比如我们的头文件叫做 liangliang.h 那么他的开始格式是.

#ifndef  _LIANGLIANG_H__    //注意这里必须是大写

#define _LIANGLIANG_H__

.

.

.        //这里是你声明的函数。

.

#endif   //LIANGLIANG_H__   一般#endif后面加上头文件的名字 好习惯~~

头文件里面可以定义函数吗?

 

 定义只能有一次,而声明的次数不限。任何标识符在使用前至少要有声明,将声明放在头文件中,就可以在需要使用到标识符时随时把头文件包含进来即可。如果把定义放进头文件中,那每包含一次头文件,标识符就定义了一次,这样在多文件的编译连接时很容易出问题的。
包含其实就相当于把被包含文件的内容加入到其中,和用被包含文件中的内容替换掉对应的包含语句没有区别。使用头文件的好处就是免除了重复劳动的麻烦。
反正最终编译器都是把被包含文件的内容合并到一块才编译的。
<span style="color:#ff0000">2.主要学习到的函数。</span>
<span style="color:#ff0000"> fflush(stdin)
</span>
 
清空输入缓冲区。 scanf("%d",&a); scanf("%c",&c); 经常如果这种情况的话,第一次输入的回车可能会被第二次输入操作所捕捉,这个的作用就是清空缓冲,这样就不会出现这种情况了。第一次输入的回车可能会被第二次输入操作所捕捉,这个的作用就是清空缓冲,这样就不会出现这种情况了。
 

枚举类型enum用法

枚举常量是枚举类型中的值,即枚举值。枚举类型是一种用户定义的类型,只有用户在程序中定义它后才能被使用。用户通常利用枚举类型定义程序中需要使用的一组相关的符号常量。枚举类型的定义格式为:   
    
  enum   <枚举类型名>   {<枚举表>};   
    
  它是一条枚举类型定义语句,该语句以enum保留字开始,接着为枚举类型名,它是用户命名的一个标识符,以后就直接使用它表示该类型,枚举类型名后为该类型的定义体,它是由一对花括号和其中的枚举表所组成,枚举表为一组用逗号分开的由用户命名的符号常量,每个符号常量又称为枚举常量或枚举值。如:   
    
  (1)   enum   color{red,   yellow,   blue};   
    
  (2)   enum   day{Sun,   Mon,   Tues,   Wed,   Thur,   Fri,   Sat};   
    
  第一条语句定义了一个枚举类型color,用来表示颜色,它包含三个枚举值red,yellow和blue,分别代表红色、黄色和兰色。   
    
  第二条语句定义了一个枚举类型day,用来表示日期,它包含7个枚举值,分别表示星期日、星期一至星期六。   
    
  一种枚举类型被定义后,可以象整型等预定义类型一样使用在允许出现数据类型的任何地方。如可以利用它定义变量。   
    
  (1)   enum   color   c1,   c2,c3;   
    
  (2)   enum   day   today,   workday;   
    
  (3)   c1=red;   
    
  (4)   workday=Wed;   
    
  第一条语句开始的保留字enum和类型标识符color表示上述定义的枚举类型color,其中enum可以省略不写,后面的三个标识符c1,c2和c3表示该类型的三个变量,每一个变量用来表示该枚举表中列出的任一个值。   
    
  第二条语句开始的两个成分(成分之间的空格除外)表示上述定义的枚举类型day,同样enum可以省略不写,后面的两个标识符today和workday表示该类型的两个变量,每一个变量用来表示该枚举表中列出的七个值中的任一个值。   
    
  第三条语句把枚举值red赋给变量c1,第四条语句把枚举值Wed赋给变量workday。   
    
  在一个枚举类型的枚举表中列出的每一个枚举常量都对应着一个整数值,该整数值可以由系统自动确认,也可以由用户指定。若用户在枚举表中一个枚举常量后加上赋值号和一个整型常量,则就表示枚举常量被赋予了这个整型常量的值。如:   
    
  enum   day{Sun=7,   Mon=0,   Tues,   Wed,   Thur,   Fri,   Sat};   
    
  用户指定了Sun的值为7,Mon的值为0。   
    
  若用户没有给一个枚举常量赋初值,则系统给它赋予的值是它前一项枚举常量的值加1,若它本身就是首项,则被自动赋予整数0。如对于上述定义的color类型,red,yellow和blue的值分别为0,1和2;对于刚被修改定义的day类型,各枚举常量的值依次为7,0,1,2,3,4,5,6。   
    
  由于各枚举常量的值是一个整数,所以可把它同一般整数一样看待,参与整数的各种运算。又由于它本身是一个符号常量,所以当作为输出数据项时,输出的是它的整数值,而不是它的标识符,这一点同输出其他类型的符号常量是一致的。 

Sleep()函数

 

功 能: 执行挂起一段时间  

用 法: unsigned sleep(unsigned seconds);  

注意:  

在VC中使用带上头文件#include <windows.h>,在Linux下,gcc编译器中,使用的头文件因gcc版本的不同而不同#include <unistd.h> 

在VC中,Sleep中的第一个英文字符为大写的"S" ,在linux下不要大写,在标准C中是sleep, 不要大写,简单的说VC用Sleep, 别的一律使用sleep

在VC中,Sleep()里面的单位,是以毫秒为单位,所以如果想让函数滞留1秒的话,应该是Sleep(1000); 在Linux下,sleep()里面的单位是秒,而不是毫秒。

delay()函数

 

delay()是循环等待,该进程还在运行,占用处理器。

sleep()不同,它会被挂起,把处理器让给其他的进程。

system("cls")函数

这个函数的作用就是清屏 你需要从哪里开始清理屏幕就放到哪里的后面。

没有啥说的 哈哈

3.源代码

game.h

#ifndef _GAME_H__ #define _GAME_H__ #define ROWS 3 #define COLS 3 void init_board(char board[ROWS][COLS], int rows, int cols); void display_board(char board[ROWS][COLS], int rows, int cols); void player_move(char board[ROWS][COLS], int rows, int cols); char check_win(char board[ROWS][COLS], int rows, int cols); void computer_move(char board[ROWS][COLS], int rows, int cols); #endif  //GAME_H__

 

game.c

# define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>


void init_board(char board[ROWS][COLS], int rows, int cols)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ' ';
		}
	}
	//memset(board, ' ', sizeof((char)*cols*rows);


}
void display_board(char board[ROWS][COLS], int rows, int cols)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
		if (i < rows - 1)
		{
			printf("---|---|---\n");
		}
	}
}
void player_move(char board[ROWS][COLS], int rows, int cols)
{
	int x = 0;
	int y = 0;
	printf("请输入坐标\n");
	while (1)
	{
		scanf("%d%d", &x, &y);
		fflush(stdin);
		x--;
		y--;
		if ((x >= 0) && (x <= rows ) && (y >= 0) && (y <= cols ))
		{
			if (board[x][y] == ' ')
			{
				board[x][y] = '*';
				break;
			}
			else
			{
				printf("我觉得你输入的有问题 再看看\n");
			}
		}
		else
			{
				printf("我觉得你输入的有问题 再看看\n");
			}
	}
}
void computer_move(char board[ROWS][COLS], int rows, int cols)
{
	printf("让电脑想想呀\n");
	Sleep(2000);
	rows = rand() % 3;
	cols = rand() % 3;
	while (board[rows][cols] != ' ')
	{
		rows = rand() % 3;
		cols = rand() % 3;
	}
	board[rows][cols] = '#';


}
char check_win(char board[ROWS][COLS], int rows, int cols)
{
	int i = 0;
	int j = 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[1][1] != ' '))  
	{
		return board[1][1];
	}
	if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[1][1] != ' '))  
	{
		return board[1][1];
	}
	if (is_full(board, rows, cols))
	{
		return 'T';  //平局
	}
	return ' ';


}
static int is_full(char board[ROWS][COLS], int rows, int cols)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;


}
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ' ';
		}
	}
	//memset(board, ' ', sizeof((char)*cols*rows);


}
void display_board(char board[ROWS][COLS], int rows, int cols)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
		if (i < rows - 1)
		{
			printf("---|---|---\n");
		}
	}
}
void player_move(char board[ROWS][COLS], int rows, int cols)
{
	int x = 0;
	int y = 0;
	printf("请输入坐标\n");
	while (1)
	{
		scanf("%d%d", &x, &y);
		fflush(stdin);
		x--;
		y--;
		if ((x >= 0) && (x <= rows ) && (y >= 0) && (y <= cols ))
		{
			if (board[x][y] == ' ')
			{
				board[x][y] = '*';
				break;
			}
			else
			{
				printf("我觉得你输入的有问题 再看看\n");
			}
		}
		else
			{
				printf("我觉得你输入的有问题 再看看\n");
			}
	}
}
void computer_move(char board[ROWS][COLS], int rows, int cols)
{
	printf("让电脑想想呀\n");
	Sleep(2000);
	rows = rand() % 3;
	cols = rand() % 3;
	while (board[rows][cols] != ' ')
	{
		rows = rand() % 3;
		cols = rand() % 3;
	}
	board[rows][cols] = '#';


}
char check_win(char board[ROWS][COLS], int rows, int cols)
{
	int i = 0;
	int j = 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[1][1] != ' '))  
	{
		return board[1][1];
	}
	if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[1][1] != ' '))  
	{
		return board[1][1];
	}
	if (is_full(board, rows, cols))
	{
		return 'T';  //平局
	}
	return ' ';


}
static int is_full(char board[ROWS][COLS], int rows, int cols)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;


}
<span style="color:#ff0000">test.c</span>
# define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"
#include<time.h>
#include<stdlib.h>
#include<Windows.h>
void menu()
{
	printf("**********************************\n");
	printf("************三子棋小游戏**********\n");
	printf("***************1.PLAY*************\n");
	printf("***************0.EXIT*************\n");
	printf("**********************************\n");
	printf("**********************************\n");
}
enum Option
{
	EXIT,
	PLAY
};
void game()
{
	char board[ROWS][COLS] = { 0 };
	char ret = ' ';
	init_board(board, ROWS, COLS);
	display_board(board, ROWS, COLS);
	while (1)
	{
		player_move(board, ROWS, COLS);
		display_board(board, ROWS, COLS);
		ret =(check_win(board, ROWS, COLS));
		if (ret != ' ')
		{
			break;
		}
		system("cls");
	     computer_move(board, ROWS, COLS);
		 display_board(board, ROWS, COLS);
		 ret = check_win(board, ROWS, COLS);
		 if (ret != ' ')
		 {
			 break;
		 }
	}
	if (ret == '*')
	{
		printf("恭喜你胜利了\n");


	}
	if (ret == '#')
	{


		printf("这你都赢不了 呵呵\n");
	}
	if (ret == 'T')
	{
		printf("平局\n");
	}


}


	int main()
	{
		int input = 0;


		do
		{
			menu();
			printf("是否要开始游戏\n");
			scanf("%d", &input);
			fflush(stdin);
			switch (input)
			{
			case PLAY:
				game();
				Sleep(3000);
				system("cls");
				break;
			case EXIT:
				break;
			default:
				printf("选择错误 请重新输入\n");
				break;


			}
		} while (input);
		return 0;
	}
	printf("**********************************\n");
	printf("************三子棋小游戏**********\n");
	printf("***************1.PLAY*************\n");
	printf("***************0.EXIT*************\n");
	printf("**********************************\n");
	printf("**********************************\n");
}
enum Option
{
	EXIT,
	PLAY
};
void game()
{
	char board[ROWS][COLS] = { 0 };
	char ret = ' ';
	init_board(board, ROWS, COLS);
	display_board(board, ROWS, COLS);
	while (1)
	{
		player_move(board, ROWS, COLS);
		display_board(board, ROWS, COLS);
		ret =(check_win(board, ROWS, COLS));
		if (ret != ' ')
		{
			break;
		}
		system("cls");
	     computer_move(board, ROWS, COLS);
		 display_board(board, ROWS, COLS);
		 ret = check_win(board, ROWS, COLS);
		 if (ret != ' ')
		 {
			 break;
		 }
	}
	if (ret == '*')
	{
		printf("恭喜你胜利了\n");


	}
	if (ret == '#')
	{


		printf("这你都赢不了 呵呵\n");
	}
	if (ret == 'T')
	{
		printf("平局\n");
	}


}


	int main()
	{
		int input = 0;


		do
		{
			menu();
			printf("是否要开始游戏\n");
			scanf("%d", &input);
			fflush(stdin);
			switch (input)
			{
			case PLAY:
				game();
				Sleep(3000);
				system("cls");
				break;
			case EXIT:
				break;
			default:
				printf("选择错误 请重新输入\n");
				break;


			}
		} while (input);
		return 0;
	}
这是我的一些积累,test.c的主函数框架,这就是循环的过程这个以后可以多用,具体每个函数的功能也差不多都懂了.

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。以下是一个简单C语言实现单人三子棋游戏的代码: ```c #include <stdio.h> #include <stdlib.h> #define BOARD_SIZE 3 void init_board(char board[][BOARD_SIZE]); void print_board(char board[][BOARD_SIZE]); int make_move(char board[][BOARD_SIZE], int row, int col, char player_char); int check_win(char board[][BOARD_SIZE], char player_char); int main() { char board[BOARD_SIZE][BOARD_SIZE]; int row, col, win; char player_char = 'X'; init_board(board); print_board(board); while (1) { printf("\nPlayer %c's turn.\n", player_char); printf("Enter row number: "); scanf("%d", &row); printf("Enter column number: "); scanf("%d", &col); if (make_move(board, row, col, player_char)) { print_board(board); win = check_win(board, player_char); if (win) { printf("\nPlayer %c wins!\n", player_char); break; } player_char = (player_char == 'X') ? 'O' : 'X'; } else { printf("Invalid move. Try again.\n"); } } return 0; } void init_board(char board[][BOARD_SIZE]) { int i, j; for (i = 0; i < BOARD_SIZE; i++) { for (j = 0; j < BOARD_SIZE; j++) { board[i][j] = '-'; } } } void print_board(char board[][BOARD_SIZE]) { int i, j; printf("\n"); for (i = 0; i < BOARD_SIZE; i++) { for (j = 0; j < BOARD_SIZE; j++) { printf("%c ", board[i][j]); } printf("\n"); } } int make_move(char board[][BOARD_SIZE], int row, int col, char player_char) { if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE || board[row][col] != '-') { return 0; } board[row][col] = player_char; return 1; } int check_win(char board[][BOARD_SIZE], char player_char) { int i, j; int row_win, col_win, diag_win1, diag_win2; for (i = 0; i < BOARD_SIZE; i++) { row_win = 1; col_win = 1; for (j = 0; j < BOARD_SIZE; j++) { if (board[i][j] != player_char) { row_win = 0; } if (board[j][i] != player_char) { col_win = 0; } } if (row_win || col_win) { return 1; } } diag_win1 = 1; diag_win2 = 1; for (i = 0; i < BOARD_SIZE; i++) { if (board[i][i] != player_char) { diag_win1 = 0; } if (board[i][BOARD_SIZE-1-i] != player_char) { diag_win2 = 0; } } if (diag_win1 || diag_win2) { return 1; } return 0; } ``` 这个程序实现了基本的单人三子棋游戏,并且包括了判断胜负的函数。您可以根据需要修改和扩展此代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值