三子棋(C语言)

目录

0.文件

一、步骤

二、所用函数

三.代码。


0.文件

test.c 测试三子棋游戏

game.c 游戏函数的实现

game.h 游戏的函数声明


一、步骤

1建立一个游戏菜单

2创建一个棋盘并将其初始化

3将初始化后的棋盘进行打印

4玩家落子并打印棋盘(玩家输入行列坐标的方式来落子)

5判定胜负关系(玩家是否获胜?有则直接游戏结束无则继续进行)

6电脑落子并打印棋盘(随机位置落子)

7判定胜负关系(电脑是否获胜?有则直接游戏结束无则继续进行)

8若无胜负关系则回到1.4 继续交替下棋直至出现胜负或和棋。

//

//玩家赢 - '*'

//电脑赢 - '#'

//平局 - 'Q'

//继续 - 'C'

//

二、所用函数

//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);
//打印棋盘
void DispalyBoard(char board[ROW][COL], int row, int col);
//玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col);
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col);
//判赢
char IsWin(char board[ROW][COL], int row, int col);

.1初始化棋盘

void InitBoard(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}

创建二维数组,初始化为空格

2打印棋盘

//void DispalyBoard(char board[ROW][COL], int row, int col)
//{
//	for (int i = 0; i < row; i++)
//	{
//		printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
//		if (i < row - 1)
//			printf("---------\n");
//	}
//}

void DispalyBoard(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		int j = 0;
		for ( j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if (j < col - 1)
				printf("|");
		}
		printf("\n");
		if (i < row - 1)
		{
			int j = 0;
			for (j = 0; j < col; j++)
			{
				printf("---");
                if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
		
	}
}

3玩家下棋

void PlayerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家下棋>\n");
	while (1)
	{
		printf("请输入坐标>\n");
		scanf("%d %d", &x, &y);
		//坐标合法性
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{

			//坐标被占用
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}

			else
				printf("坐标被占用,请重新输入\n");
		}
		else
			printf("输入非法,请重新输入\n");
	}
	
}

a.坐标合法性

b.坐标被占用,重新输入坐标

4电脑下棋

void ComputerMove(char board[ROW][COL], int row, int col)
{
	printf("电脑下棋>\n");
	int x = 0;
	int y = 0;
     
	 while (1)
	 {
		 x = rand() % row;
		 y = rand() % col;
		 if (board[x][y] == ' ')
		 {
			 board[x][y] = '#';
			 break;
		 }
	 }

}

a.生成随机数

b.坐标被占用,重新生成随机数

5判赢

//判满
int IsFull(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i <= row; i++)
	{
		for (int j = 0; j <= col; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}
char IsWin(char board[ROW][COL], int row, int col)
{
	//行
	for (int i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][2] == board[i][1] && board[i][0] != ' ')
			return board[i][0];
	}
	//列
    for (int j = 0; j < col; j++)
	{
		if (board[0][j] == board[1][j] && board[2][j] == board[1][j] && board[0][j] != ' ')
			return board[0][j];
	}
	//对角线
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1]!=' ')
		return board[1][1];
	if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
		return board[1][1];
	//平局
	if (IsFull(board, row, col) == 1)
		return 'Q';
	//继续
	return 'C';
}

a.行

b.列

c.对角线

d.若满,则平局

未满,则继续


三.代码。

game.c

#define _CRT_SECURE_NO_WARNINGS

#include"game.h"

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

//打印棋盘
//void DispalyBoard(char board[ROW][COL], int row, int col)
//{
//	for (int i = 0; i < row; i++)
//	{
//		printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
//		if (i < row - 1)
//			printf("---------\n");
//	}
//}

void DispalyBoard(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		int j = 0;
		for ( j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if (j < col - 1)
				printf("|");
		}
		printf("\n");
		if (i < row - 1)
		{
			int j = 0;
			for (j = 0; j < col; j++)
			{
				printf("---");
                if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
		
	}
}

//玩家下棋
//坐标合法性,坐标被占用
void PlayerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家下棋>\n");
	while (1)
	{
		printf("请输入坐标>\n");
		scanf("%d %d", &x, &y);
		//坐标合法性
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{

			//坐标被占用
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}

			else
				printf("坐标被占用,请重新输入\n");
		}
		else
			printf("输入非法,请重新输入\n");
	}
	
}

//电脑下棋
//找没有下棋的随机下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
	printf("电脑下棋>\n");
	int x = 0;
	int y = 0;
     
	 while (1)
	 {
		 x = rand() % row;
		 y = rand() % col;
		 if (board[x][y] == ' ')
		 {
			 board[x][y] = '#';
			 break;
		 }
	 }

}

//判满
int IsFull(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i <= row; i++)
	{
		for (int j = 0; j <= col; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}
char IsWin(char board[ROW][COL], int row, int col)
{
	//行
	for (int i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][2] == board[i][1] && board[i][0] != ' ')
			return board[i][0];
	}
	//列
    for (int j = 0; j < col; j++)
	{
		if (board[0][j] == board[1][j] && board[2][j] == board[1][j] && board[0][j] != ' ')
			return board[0][j];
	}
	//对角线
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1]!=' ')
		return board[1][1];
	if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
		return board[1][1];
	//平局
	if (IsFull(board, row, col) == 1)
		return 'Q';
	//继续
	return 'C';
}

game.h

 

#pragma once


#include 
#include 
#include 

#define ROW 3
#define COL 3

//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);

//打印棋盘
void DispalyBoard(char board[ROW][COL], int row, int col);

//玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col);

//电脑下棋
//找没有下棋的随机下棋
void ComputerMove(char board[ROW][COL], int row, int col);

//
//玩家赢 - '*'
//电脑赢 - '#'
//平局 - 'Q'
//继续 - 'C'
//

char IsWin(char board[ROW][COL], int row, int col);

 test.c

#define _CRT_SECURE_NO_WARNINGS

#include
//自己创建的用“”引用
#include"game.h"

void meun()
{
    printf("******************************\n");
    printf("************  1.play  ********\n");
    printf("************  0.exit  ********\n");
    printf("******************************\n");

}

void game()
{
    char ret = 0;
    //创建数组
    char board[ROW][COL] = { 0 };
    //初识化棋盘
    InitBoard(board,ROW,COL);
    //打印棋盘
    DispalyBoard(board, ROW, COL);
    //判赢
    while (1)
    {
        PlayerMove(board, ROW, COL);
        ret = IsWin(board, ROW, COL);
        if (ret != 'C')
            break;
        DispalyBoard(board, ROW, COL);
        ComputerMove(board, ROW, COL);
        ret = IsWin(board, ROW, COL);
        if (ret != 'C')
            break;
        DispalyBoard(board, ROW, COL);
    }
    if (ret == '*')
        printf("玩家赢\n");
    else if (ret == '#')
        printf("电脑赢\n");
    else
        printf("平局\n");

    DispalyBoard(board, ROW, COL);

}

int main()
{
    //生成随机数
    srand((unsigned int)time(NULL));
    int input = 0;
    do
    {
        meun();
        printf("请输入选择>\n");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 2:
            printf("退出游戏\n");
            break;
        default:
            printf("输入不正确\n");
            break;
        }
    } while (input);
    return 0;
}

 

你可能感兴趣的:(c语言)