c/c++井字棋

  1. c++版本的井字棋
    首先先上代码,下面是主函数,我们把功能用函数实现,通过主函数我们可以知道游戏流程。
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>

int chess[5][5] = {
    
    '\0'}, n = 0;                                //定义初始化棋盘数组和步数
bool end = false;                                               //定义初始化游戏结束判别标志

int main()
{
    
    

    int in, first = 0;
    int x, y;

    printf("欢迎来到Jack井字棋\n");
    showMenu();
    //根据输入判断游戏进行或退出
    std::cin >> in;

    while (in == 1)
    {
    
    
        printf("谁先手?你先,请输入0;电脑先,请输入1\n");
        std::cin >> first;
        //根据先后手开始分组,用if-else语句分段
        if (first % 2 == 0)
            while (1)
            {
    
    
                draw();
                printf("请输入位置x y:\n");
                std::cin >> x >> y;
                chess[x - 1][y - 1] = 1;
                randomDrop();
                n += 2;
                draw();
                switch (judge())
                {
    
    
                case 0:
                    break;
                case 1:
                    end = true;
                    printf("你赢了!\n");
                    break;
                case 2:
                    end = true;
                    printf("你输了。。。。。。\n");
                    break;
                case 3:
                    end = true;
                    printf("平局\n");
                    break;
                default:
                    break;
                }
                if (end)
                    break;
            }
        else
        {
    
    
            draw();
            randomDrop();
            printf("请输入位置x y:\n");
            std::cin >> x >> y;
            chess[x - 1][y - 1] = 1;
            n += 2;
            draw();
            switch (judge())
            {
    
    
            case 0:
                break;
            case 1:
                end = true;
                printf("你赢了!\n");
                break;
            case 2:
                end = true;
                printf("你输了。。。。。。\n");
                break;
            case 3:
                end = true;
                printf("平局\n");
                break;
            default:
                break;
            }
            if (end)
                break;
        }
        printf("还想继续吗?(1.yes  2.Thanks)\n");
        int willings;
        std::cin >> willings;
        if (willings != 1)
            break;
    }
    printf("Bye.\n");
    system("pause");
    return 0;
}

输出菜单函数

//简单输出开始菜单
void showMenu()
{
    
    

    printf("##################################################\n");
    printf("##                                              ##\n");
    printf("##     1.start                  2.exit          ##\n");
    printf("##                                              ##\n");
    printf("##################################################\n");
}

棋盘绘制函数

//根据棋盘数组中数值进行输出,值为1则输出X,值为2则输出O,否则输出空
int draw()
{
    
    

    system("cls");
    printf("");
    for (int i = 0; i < 5; i++)
    {
    
    
        printf("|");
        for (int j = 0; j < 5; j++)
        {
    
    
            if (chess[i][j] == 1)
                printf("X");
            else if (chess[i][j] == 2)
                printf("O");
            else
                printf(" ");
            printf("|");
        }
        printf("\n-------------\n");
    }
    return 0;
}

电脑随机落子函数

//电脑随机落子,根据赋予的随机数进行棋盘数组赋值
void randomDrop()
{
    
    

    int x, y;
    while (1)
    {
    
    
        x = rand() % 5;
        y = rand() % 5;
        if (chess[x][y] == '\0')
        {
    
    
            chess[x][y] = 2;
            break;
        }
    }
    draw();
}

当然,还有最后的游戏结果判定函数

//判别输赢和平局
//我们赢了,返回1;电脑赢了,返回2;平局则返回3
//存在还没走完的情况,所以返回0
int judge()
{
    
    

    int i;
    if (n != 25)
    {
    
    
        for (i = 0; i < 5; i++)
            if ((chess[i][0] == 1 && chess[i][1] == 1 && chess[i][2] == 1 && chess[i][3] == 1 && chess[i][4] == 1) ||
                (chess[0][i] == 1 && chess[1][i] == 1 && chess[2][i] == 1 && chess[3][i] == 1 && chess[4][i] == 1))
                return 1;
            else if ((chess[i][0] == 2 && chess[i][1] == 2 && chess[i][2] == 2 && chess[i][3] == 2 && chess[i][4] == 2) ||
                     (chess[0][i] == 2 && chess[1][i] == 2 && chess[2][i] == 2 && chess[3][i] == 2 && chess[4][i] == 2))
                return 2;
            else
                continue;
        //斜对
        if ((chess[0][0] == 1 && chess[1][1] == 1 && chess[2][2] == 1 && chess[3][3] == 1 && chess[4][4] == 1) || (chess[0][4] == 1 && chess[1][3] == 1 && chess[2][2] == 1 && chess[3][1] == 1 && chess[4][0] == 1))
            return 1;
        else if ((chess[0][0] == 2 && chess[1][1] == 2 && chess[2][2] == 2 && chess[3][3] == 2 && chess[4][4] == 2) || (chess[0][4] == 2 && chess[1][3] == 2 && chess[2][2] == 2 && chess[3][1] == 2 && chess[4][0] == 2))
            return 2;
    }
    //看是否平局
    else
        return 3;
    return 0;
}

由于函数实现有清屏,一张张图片截屏起来很麻烦,我又不想整动态图,这里就不演示结果了。(注:输入要用英文,不然输入缓存有误)
2. c语言实现井字棋

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

void menu();								//输出菜单
void draw(char chess[][3]);					//描绘棋盘
char judge(char chess[][3]);				//输赢判定函数
void random_move();							//电脑落子

int main(){
    
    

	int a, count = 0;
	char chess[3][3] = {
    
    '\0'};
	menu();
	printf("请选择:\n");
	scanf("%d", &a);
	if(a != 1)
		return 0;

	draw(chess);
	srand((unsigned)time(NULL));

	while(1){
    
    
		int user_x, user_y;
		printf("请输入<x/y>: \n");
		scanf("%d,%d", &user_x, &user_y);

		if((user_x >=1 && user_x <=3) && (user_y >= 1 && user_y <= 3)\
			&&chess[user_x - 1][user_y - 1] == '\0'){
    
    
			chess[user_x - 1][user_y - 1] = 'X';
			count++;
			if(judge(chess) == 'X'){
    
    
				draw(chess);
				printf("你赢了!\n");
				break;
			}
			if(count == 9){
    
    
				draw(chess);
				printf("平手。\n");
				break;
			}

			random_move(chess);
			count++;
			if(judge(chess) == 'O'){
    
    
				printf("你输了!\n");
				break;
			}
		}
		else
			continue;
	}
	return 0;
}

void menu(){
    
    

	printf("############################################################\n");
	printf("##		1.play		2.Exit		    	##\n");
	printf("############################################################\n");
}

void draw(char chess[][3]){
    
    

	system("cls");
	printf(" %c | %c | %c		Player:X\n", chess[0][0], chess[0][1], chess[0][2]);
	printf(" -----------		Computer:O\n");
	printf(" %c | %c | %c \n", chess[1][0], chess[1][1], chess[1][2]);
	printf(" -----------  \n");
	printf(" %c | %c | %c \n", chess[2][0], chess[2][1], chess[2][2]);
}

char judge(char chess[][3]){
    
    

	int i;
	//循环判断三行中是否有一行不为空且同为x或o
	for(i=0; i < 3; i++){
    
    
		if(chess[i][0] == chess[i][1] && chess[i][1] == chess[i][2] && chess[i][2] != '\0')
			return chess[i][0];
	}
	//循环判断三列中是否有一行不为空且同为x或o
	for(i=0; i < 3; i++){
    
    
		if(chess[0][i] == chess[1][i] && chess[1][i] == chess[2][i] && chess[2][i] != '\0')
			return chess[0][i];
	}
	//判断斜对行是否有相同且不为空的
	if(chess[0][0] == chess[1][1] && chess[1][1] == chess[2][2] && chess[2][2] != '\0')
		return chess[0][0];
	if(chess[0][2] == chess[1][1] && chess[1][1] == chess[2][0] && chess[2][0] != '\0')
		return chess[0][2];
	return "";
}

void random_move(char chess[][3]){
    
    

	int x, y;
	while(1){
    
    
		x = rand() %3;
		y = rand() %3;
		if(chess[x][y] == '\0'){
    
    
			chess[x][y] = "O";
			break;
		}
	}
	draw(chess);
}

猜你喜欢

转载自blog.csdn.net/weixin_44948269/article/details/119292027