一、rand随机函数的使用

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

//随机洗牌程序
int main()
{
	int i = 0;
	int arr[54] = { 0 };
	int mark[55] = { 0 };
	srand((unsigned int)time(NULL));
	while (i < 54) {
		int x = rand() % 54 + 1;
		if (mark[x] == 1) {
			continue;
		}
		arr[i] = x;
		mark[x] = 1;
		printf("%d\t", arr[i]);
		i++;
	}

	return 0;
}

三子棋游戏实现

test.c

//三子棋游戏
#include "game.h"
void game()
{
	int flag = 1;//标记输赢
	char board[ROW][COL] = { '0' };
	//初始化棋盘
	InitBoard(board);
	//展示棋盘
	DisplayBoard(board, ROW, COL);
	//下棋
	while (1) {
		//电脑下棋
		ComputerMove(board, ROW, COL);
		//棋盘展示
		DisplayBoard(board, ROW, COL);
		//判断电脑是否赢
		flag = isWin(board, ROW, COL);
		if (flag == 1) {
			printf("电脑赢!\n");
			break;
		}
		else if (flag == 0) {
			printf("平局!\n");
			break;
		}

		//玩家下棋
		PlayMove(board);
		//棋盘展示
		DisplayBoard(board, ROW, COL);
		//判断玩家是否赢
		flag = isWin(board, ROW, COL);
		if (flag == 1) {
			printf("玩家赢!\n");
			break;
		}
		else if (flag == 0) {
			printf("平局!\n");
			break;
		}
	}

}

//进入游戏
void Begin() 
{
	int input = 0;
	do {
		printf("*****************************\n");
		printf("****  1、play   0、exit  ****\n");
		printf("*****************************\n");
		printf("请输入您的选择:>");
		scanf("%d", &input);
		switch (input) {
		case 1:
			game();
			break;
		case 0:
			printf("游戏结束,欢迎下次再来!\n");
			break;
		default:
			printf("您的输入有误,请重新输入!");
			break;
		}
	} while (input);
	
}


int main()
{
	Begin();

	return 0;
}

game.h

#pragma once

#define ROW 4
#define COL 4
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void InitBoard(char board[ROW][COL]);
void DisplayBoard(char board[ROW][COL], int row, int col);
void PlayMove(char board[ROW][COL]);
void ComputerMove(char board[ROW][COL], int row, int col);
int isWin(char board[ROW][COL], int row, int col);


game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

//初始化棋盘函数
void InitBoard(char board[ROW][COL])
{
	for (int i = 0; i < ROW; i++) {
		for (int j = 0; j < COL; j++) {
			board[i][j] = ' ';
		}
	}
}

//显示棋盘信息函数
void DisplayBoard(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++) {
		//打印一行
		for (int j = 0; j < col; j++) {
			if (i == 0) {
				printf(" %d ", j);
				if (j < col - 1) {
					printf("|");
				}
			}
			else if (j == 0) {
				printf(" %d ", i);
				if (j < col - 1) {
					printf("|");
				}
			}
			else {
				printf(" %c ", board[i][j]);
				if (j < col - 1) {
					printf("|");
				}
			}
		}
		printf("\n");

		if (i < row - 1) {
			for (int j = 0; j < col; j++) {
				printf("---");
				if (j < col - 1) {
					printf("|");
				}
			}
			printf("\n");
		}
	}
}

//玩家下棋函数
void PlayMove(char board[ROW][COL])
{
	int x, y;
	printf("玩家下棋\n");
	printf("玩家请输入下棋坐标:");
	while (1) {
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= ROW-1 && y >= 1 && y <= COL-1) {
			if (board[x][y] == ' ') {
				board[x][y] = '*';
				break;
			}
			else {
				printf("您输入的位置已被占用,请重新输入: >");
			}
		}
		else {
			printf("您输入的坐标超过棋盘范围,请重新输入:>");
		}
	}
}

//电脑下棋函数
void ComputerMove(char board[ROW][COL], int row, int col)
{
	int x, y;
	printf("电脑下棋\n");
	srand((unsigned int)time(NULL));
	while (1) {
		x = rand() % (row - 1) + 1;
		y = rand() % (col - 1) + 1;

		if (board[x][y] == ' ') {
			board[x][y] = '#';
			break;
		}
	}
}

//判断是否平局
int isPing(char board[ROW][COL], int row, int col)
{
	for (int i = 1; i < row; i++) {
		for (int j = 1; j < col; j++) {
			if (board[i][j] == ' ') {
				return 2;
			}
		}
	}
	return 0;
}

//判断输赢
int isWin(char board[ROW][COL], int row, int col)
{
	for (int i = 1; i < row; i++) {
		//行
		if ((board[i][1] == board[i][2] && board[i][2] == board[i][3] && board[i][1] != ' ')) {
			return 1;
		}
		//列
		if ((board[1][i] == board[2][i] && board[2][i] == board[3][i]) && board[1][i] != ' ') {
			return 1;
		}
		//斜着
		if ((board[1][1] == board[2][2] && board[2][2] == board[3][3] && board[1][1] != ' ')||(board[1][3] == board[2][2] && board[2][2] == board[3][1]) && board[1][3] != ' ') {
			return 1;
		}		
	}

	return isPing(board, ROW, COL);	
}