三子棋

本文介绍了一个使用C语言实现的简易版井字游戏。该程序包括玩家与电脑对战的功能,通过简单的文本界面进行交互。游戏实现了基本的规则判断,如胜负条件及平局情况,并具备了电脑随机下棋的智能行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

game.h
#ifndef __GAME_H__
#define __GAME_H__

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

#define ROWS  3
#define COLS  3

void game();
char check_win(char arr[ROWS][COLS], int rows, int cols);
static int is_full(char arr[ROWS][COLS], int rows, int cols);
void  init_board(char [ROWS][COLS], int rows, int cols);
void player_move(char arr[ROWS][COLS], int rows, int cols);
void computer_move(char arr[ROWS][COLS], int rows, int cols);

#endif

game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"

void init_board(char arr[ROWS][COLS], int rows,int cols)
{
	int i = 0;
	{
		for (i = 0; i <= rows; i++)
		{
			printf("-------\n");
			if (i < rows)
			{
				printf("|%c|%c|%c|\n", arr[i][0], arr[i][1], arr[i][2]);
			}
		}
	}

}
static int  is_full(char arr[ROWS][COLS], int rows, int cols)
{
	int i = 0, j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			if (arr[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}
char check_win(char arr[ROWS][COLS], int rows, int cols)
{
	int ret = 0;
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		if ((arr[i][0] == arr[i][1]) 
			&&( arr[i][1] == arr[i][2])
			&&( arr[i][1] != ' '))
			return arr[i][0];
	}
	for (i = 0; i < cols; i++)
	{
		if ((arr[0][i] == arr[1][i])
			&&( arr[1][i] == arr[2][i])
			&&( arr[1][i] != ' '))
			return arr[0][i];
	}
	if ((arr[0][0] == arr[1][1])
		&&( arr[1][1] == arr[2][2])
		&&( arr[1][1] != ' '))
		return arr[1][1];
	if ((arr[0][2] == arr[1][1])
		&& (arr[1][1] == arr[2][0])
		&&( arr[1][1] != ' '))
		return arr[1][1];
	if (is_full(arr, rows, cols))
		return 'q';
	return ' ';
}
void computer_move(char arr[ROWS][COLS], int rows, int cols)
{
	while (1)
	{
		int x = rand() % 3;
		int y = rand() % 3;
		if (arr[x][y] == ' ')
		{
			arr[x][y] = '0';
			break;
		}
		
	}
}
void player_move(char arr[ROWS][COLS], int rows, int cols)
{
	while (1)
	{

		int i = 9;
		int j = 5;

		printf("输入坐标:");
		scanf("%d %d", &i, &j);//i,j前面要加&
		if ((i <= 3) && (j <= 3))
		{
			i--;
			j--;

			if (arr[i][j] == ' ')
			{
				arr[i][j] = '*';
				break;
			}
			else
			{
				printf("重新输入这有棋子\n");
			}
		}
		else
		{
			printf("输入有误重新输入\n");
		}
	}
}


test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<time.h> #include"game.h" void menu() {  printf("*******1.play**********\n");  printf("*******0.exit**********\n"); } void game() {  char ret=1;  int rows = 3;  int cols = 3;  /*  char arr[ROWS][COLS] = { ' ' };*/   /*{ { ' ', ' ', ' ' },  { ' ', ' ', ' ' },  { ' ', ' ', ' ' }, };*///初始化数组  char arr[ROWS][COLS] = { ' '};  for (int i = 0; i < 3; i++)  {   for (int j = 0; j < 3; j++)   {    arr[i][j] = ' ';   }  }  srand((unsigned int)time(NULL));  //init_board( arr,rows, cols);//显示数组  while (1)  {   printf("电脑走:\n");   computer_move(arr, rows, cols);   if ((ret = check_win(arr, rows, cols)) != ' ')    break;   init_board(arr, rows, cols);   printf("玩家走:\n");   player_move(arr, rows, cols);   if ((ret =check_win(arr, rows, cols)) != ' ')    break;   init_board(arr, rows, cols);  }  if (ret == '0')   printf("电脑赢\n");  else if (ret == '*')   printf("玩家赢\n");  else if (ret == 'q')   printf("平局\n");  init_board(arr, rows, cols); } int main() {  int input=0;  do  {   menu(); printf("请输入\n");   scanf("%d", &input);   switch (input)   {   case 1:       game(); break;   case 0:      ; break;   default:       printf("输入有误\n"); break;   }  } while (input);
   return 0; }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值