简易井字棋

先设置头文件,各个功能接口

three.h 文件

#pragma once

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


#define ROW 3
#define COL 3

//初始化
void InitBoard(char arr[ROW][COL], int row, int col);
//打印
void PrintBoard(char arr[ROW][COL], int row, int col);
//玩家下棋
void PlayerMove(char arr[ROW][COL], int row, int col);
//电脑下棋
void ComputerMove(char arr[ROW][COL], int row, int col);
//判断胜平
char Win(char arr[ROW][COL], int row, int col);

再设置各个接口功能,three.c 文件

#include"three.h"

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

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

void PlayerMove(char arr[ROW][COL], int row, int col)
{
	while (true)
	{
		int i = 0, j = 0;
		scanf("%d%d",&i,&j);
		if (i > row || j > col || i < 0 || j < 0)
			printf("move err\n");
		else if (' ' == arr[i][j])
		{
			arr[i][j] = '*';
			break;
		}
		else 
			printf("move err\n");
	}
}

void ComputerMove(char arr[ROW][COL], int row, int col)
{
	while (true)
	{
		srand((unsigned)time(NULL));
		int i = rand() % ROW;
		srand((unsigned)time(NULL));
		int j = rand() % COL;
		if (' ' == arr[i][j])
		{
			arr[i][j] = '#';
			break;
		}
	}
}

char Win(char arr[ROW][COL], int row, int col)
{
	//判断行
	for (int col = 0; col < COL-1; col++)
	{
		int countrow = 0;
		for (int row = 0; row < ROW-1; row++)
		{
			if (arr[row][col] == arr[row + 1][col] && ' ' != arr[row][col]);
			else countrow++;
		}
		if (countrow == 0)
			return 'w';
	}
	//判断列
	for (int row = 0; row < ROW-1; row++)
	{
		int countcol = 0;
		for (int col = 0; col < COL - 1; col++)
		{
			if (arr[row][col] == arr[row][col + 1] && ' ' != arr[row][col]);
			else countcol++;
		}
		if (countcol == 0)
			return 'w';
	}
	//判断斜(仅3*3格)
	int count = 0;
	for (int row = 0, col = 0; row< ROW - 1 && col< COL - 1; row++, col++)
	{
		if (arr[row][col] == arr[row + 1][col + 1] && ' ' != arr[row][col]);
		else count++;
	}
	if (count == 0)
		return 'w';

	count = 0;
	for (int row = ROW - 1, col = 0; row> 0 && col< COL - 1; row--, col++)
	{
		if (arr[row][col] == arr[row - 1][col + 1] && ' ' != arr[row][col]);
		else count++;
	}
	if (count == 0)
		return 'w';

	//平局
	count = 0;
	for (int row = 0; row < ROW; row++)
	{
		for (int col = 0; col < COL; col++)
		{
			if (arr[row][col] == ' ')
				count++;
		}
	}
	if (count == 0)
		return 't';
}

最后设置菜单

main.c 文件

#define _CRT_SECURE_NO_WARNINGS

#include"three.h"

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



void game()
{
	int input = 0;
	do
	{
		mean();
		printf("请选择->");
		scanf("%d",&input);
		char arr[ROW][COL];
		InitBoard(arr, ROW, COL);
		char win = '\0';
		switch (input)
		{
		case 0:
			break;
		case 1:
			while ('w' != win||'t'!=win)
			{
				printf("player move->");
				PlayerMove(arr, ROW, COL);
				printf("\n");
				PrintBoard(arr, ROW, COL);
				win=Win(arr, ROW, COL);
				if ('w' == win)
				{
					printf("player win\n");
					break;
				}
				if ('t' == win)
				{
					printf("平局\n");
					break;
				}
				printf("computer move");
				ComputerMove(arr, ROW, COL);
				printf("\n");
				PrintBoard(arr, ROW, COL);
				win = Win(arr, ROW, COL);
				if ('w' == win)
				{
					printf("computer win\n");
					break;
				}
				if ('t' == win)
				{
					printf("平局\n");
					break;
				}
			}
		default:
			printf("请重新选择\n");
			break;
		}
	} while (input);
	
}

int main()
{
	game();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值