小白的第一个程序

上了大概一个半月的网课,写出了第一个可执行文件,一个简单的三子棋,尽管电脑下得不怎么样,但也玩得很开心(^_^)

代码上传到gitee上了,感觉有点bug,恳请各位看官大大给提点意见

https://gitee.com/Alptraum76/tic-tac-toe-

#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
int playerWin = 0;
int computerWin = 0;
int tie = 0;
int m = 0, n = 0;
int*p = &m, *q = &n;
void game(int select)
{
	char ret = 0;
	char board[ROW][COL] = { 0 };
	//初始化棋盘
	initBoard(board, ROW, COL);
	//打印棋盘
	printBoard(board, ROW, COL);
	//判断先手
	if (select == 2)
	{
		//电脑走棋
		computerMove(board,ROW, COL, m, n);
		//打印棋盘
		printBoard(board, ROW, COL);
	}
	while (1)
	{
		//玩家走棋
		playerMove(board, ROW, COL, p, q);
		//打印棋盘
		printBoard(board, ROW, COL);
		//判断输赢
		ret =isWin(board, ROW, COL);
		//玩家获胜
		if (ret == 'X')
		{
			playerWin++;
			printf("玩家获胜!\n");
			break;
		}
		//平局
		else if(ret == 't')
		{
			tie++;
			printf("平局!\n");
			break;
		}
		//电脑走棋
		computerMove(board, ROW, COL, m, n);
		//打印棋盘
		printBoard(board, ROW, COL);
		//判断输赢
		ret = isWin(board, ROW, COL);
		//电脑获胜
		if (ret == '@')
		{
			computerWin++;
			printf("电脑获胜!\n");
			break;
		}
		//平局
		else if (ret == 't')
		{
			tie++;
			printf("平局!\n");
			break;
		}
	}
}
//显示菜单
void menu()
{
	printf("********欢迎来到三子棋游戏!********\n");
	printf("************1.开始游戏**************\n");
	printf("************0.退出游戏**************\n");
}
void menu2()
{
	printf("**************谁先下?**************\n");
	printf("**************1.玩家****************\n");
	printf("**************2.电脑****************\n");
	printf("************0.返回菜单**************\n");
}
void select()
{
	
	int select = 0;
	do
	{
		//显示菜单
		menu2();
		printf("请输入您的选择:");
		scanf_s("%d", &select);
		switch (select)
		{
		case 1:
			printf("玩家先手!\n");
			game(1);
			break;
		case 2:
			printf("电脑先手!\n");
			game(2);
			break;
		case 0:
			printf("返回菜单!\n");
			break;
		default:
			printf("输入错误!\n");
			break;
		}
	} while (select);
}
int main()
{
	int m=0, n=0;
	int*p = &m, *q = &n;
	int choice = 0;
	do
	{
		//显示菜单
		menu();
		printf("请输入您的选择:");
		scanf_s("%d", &choice);
		switch (choice)
		{
		case 1:
			printf("游戏开始!\n");
			select();
			break;
		case 0:
			printf("游戏结束!\n");
			printf("玩家胜利:%d局\n", playerWin);
			printf("电脑胜利:%d局\n", computerWin);
			printf("平局:%d局\n", tie);
			break;
		default:
			printf("输入错误!\n");
			break;
		}
	} while(choice);
	system("pause");
	return 0;
}
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define ROW 3
#define COL 3
//初始化棋盘
void initBoard(char board[ROW][COL], int row, int col);
//打印棋盘
void printBoard(char board[ROW][COL], int row, int col);
//玩家走棋
void playerMove(char board[ROW][COL], int row, int col,int* p,int* q);
//电脑走棋
void computerMove(char board[ROW][COL], int row, int col,int m,int n);
//判断游戏是否结束
char isWin(char board[ROW][COL], int row, int col);
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
//初始化棋盘
void initBoard(char board[ROW][COL], int row, int col)
{
    int i = 0, j = 0;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            board[i][j] = ' ';
        }
    }
}
//打印棋盘
void printBoard(char board[ROW][COL], int row, int col)
{
    int i = 0, j = 0;
    printf(" ");
    for (j = 1; j < col; j++)
    {
        printf(" %d  ", j);
    }
    printf(" %d\n", col);
    for (i = 0; i < row-1; i++)
    {
        printf("%d", i+1);
        for (j = 0; j < col-1; j++)
        {
            printf(" %c |", board[i][j]);
        }
        printf(" %c\n", board[i][j]);
        printf(" ---+");
        for (j = 1; j < col-1; j++)
        {
            printf("---+");
        }
        printf("---\n");
    }
    printf("%d", i + 1);
    for (j = 0; j < col-1; j++)
    {
        printf(" %c |", board[i][j]);
    }
    printf(" %c\n", board[i][j]);
}
//玩家走棋
void playerMove(char board[ROW][COL], int row, int col,int* p,int* q)
{
    int x = 0, y = 0;
    printf("玩家落子:\n");
    printf("请输入坐标:");
    scanf_s("%d%d", &x, &y);
    //判断输入坐标是否合法
    while (x < 1 || x > row || y < 1 || y > col || board[x-1][y-1] != ' ')
    {
        printf("输入坐标有误,请重新输入:");
        scanf_s("%d%d", &x, &y);
    }
    board[x-1][y-1] = 'X';
    *p = x;
    *q = y;
}
//电脑走棋
void computerMove(char board[ROW][COL], int row, int col,int m,int n)
{
    srand(time(NULL));
    printf("电脑落子:\n");
    int a = 0, b = 0;
    a = row / 2;
    b = col / 2;
    //判断坐标是否合法
    if (board[a][b] == ' ')
    {
        board[a][b] = '@';
        return;
    }
    int count = 20;
    do
    {
        //判断周围是否有空格
        a = m + 1 - (2 * (rand() % 2));
        b = n + 1 - (2 * (rand() % 2));
        //判断坐标是否合法
        if (a < 0 || a >= row || b < 0 || b >= col)
        {
            continue;
        }
        else
        {
            if (board[a][b] == ' ')
            {
                board[a][b] = '@';
                return;
            }
        }
        count--;
    } while (count);
    //随机选择一个空格
    a = rand() % row;
    b = rand() % col;
    while (board[a][b] != ' ')
    {
        a = rand() % row;
        b = rand() % col;
    }
    board[a][b] = '@';
}
//判断游戏是否结束
char isWin(char board[ROW][COL], int row, int col)
{
    int i = 0, j = 0;
    //横向判断
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col - 2; j++)
        {
            if (board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j + 2])
            {
                return board[i][j];
            }
        }
    }
    //纵向判断
    for (i = 0; i < row - 2; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (board[i][j] == board[i + 1][j] && board[i + 1][j] == board[i + 2][j])
            {
                return board[i][j];

            }
        }
    }
    //左上到右下斜向判断
    for (i = 0; i < row - 2; i++)
    {
        for (j = 0; j < col - 2; j++)
        {
            if (board[i][j] == board[i + 1][j + 1] && board[i + 1][j + 1] == board[i + 2][j + 2])
            {
                return board[i][j];
            }
        }
    }
    //右上到左下斜向判断
    for (i = 0; i < row - 2; i++)
    {
        for (j = 2; j < col; j++)
        {
            if (board[i][j] == board[i + 1][j - 1] && board[i + 1][j - 1] == board[i + 2][j - 2])
            {
                return board[i][j];
            }
        }
    }
    //平局判断
    int flag = 0;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (board[i][j] == ' ')
            {
                flag = 1;
                break;
            }
        }
    }
    if (flag == 0)
    {
        return 't';
    }
    return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值