C语言三子棋的实现

完成三子棋的编程后,作者对数组有了更深入的理解,game函数的编写过程虽复杂但也增强了信心,认为更大的挑战正等待着自己。
摘要由CSDN通过智能技术生成

三子棋写完之后,感觉加深了自己对数组的感悟与理解,尤其是写game函数,感觉要链接的东西太多,写完之后觉得这游戏也不是那么难,更大的挑战还在后面,加油!!!

game.h

#ifndef __GAME_H__
#define __GAME_H__
#include<stdlib.h>
#include<stdio.h>
#include<stdio.h>
#include<string.h>
#include<time.h>

enum OPTION
{
	EXIT,
	PLAY
};
#define COLS 3
#define ROWS 3
void init_board(char board[ROWS][COLS], int rows, int cols);
void printf_board(char board[ROWS][COLS], int rows, int cols);
void player_move(char board[ROWS][COLS], int rows, int cols);
void computer_move(char board[ROWS][COLS], int rows, int cols);
int check_win(char board[ROWS][COLS], int rows, int cols);
#endif //__GAME_H__
game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init_board(char board[ROWS][COLS], int rows, int cols)
{
	memset(board,' ', sizeof(char)*ROWS*COLS);
}
void printf_board(char board[ROWS][COLS], int rows, int cols)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
		if (i != rows - 1)
		{
			printf("---|---|---\n");
		}

	}
	
}
void player_move(char board[ROWS][COLS], int rows, int cols)
{
	int x = 0;
	int y = 0;
	printf("请玩家输入坐标:");
	scanf("%d %d", &x, &y);
	while (1)
	{
		if ((x >= 1) && (x <= rows))
		{
			if ((y >= 1) && (y <= cols))
			{
				if (board[x-1][y-1] == ' ')
				{
					board[x-1][y-1] = 'X';
					break;
				}
			}
		}
		else
			printf("坐标输入错误,请重新输入");
	}

}
void computer_move(char board[ROWS][COLS], int rows, int cols)
{
	int x = 0;
	int y = 0;
	
	while(1)
	{
		x = rand() % rows;
		y = rand() % cols;
		if(board[x][y]==' ')
	   {
		 board[x][y] = '0';
		 break;
	   }
		
     }
}
	
int is_full(char board[ROWS][COLS], int rows, int cols)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			if (board[i][j] == ' ')
			{
				return 0;//棋盘未满
			}
		}
	}
	return 1;//棋盘满
}
int check_win(char board[ROWS][COLS], int rows, int cols)
{
	int i = 0;
   for(i = 0; i < rows; i++)
	{
	   if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][1] != ' '))//行相等
	   {
		   return board[i][1];
	   }
	   if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[1][i] != ' '))//列相等
	   {
		   return board[1][i];
	   }
	   if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && (board[1][1] != ' '))//左对角线相等
	   {
		   return board[1][1];
	   }
	   if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[1][1] != ' '))//左对角线相等
	   {
		   return board[1][1];
	   }
   }
   if (is_full(board, ROWS, COLS))
   {
	   return 'q';//棋盘满
   }
   else
	   return ' ';//棋盘未满
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
	printf("---------------------------\n");
	printf("      1.play   2.exit      \n");
	printf("---------------------------\n");
}
void game()
{

	int ret = 0;
	srand((unsigned int)time(NULL));
	char board[ROWS][COLS] = { 0 };
	init_board(board, ROWS, COLS);
	printf_board(board, ROWS, COLS);
	do
	{
	  player_move(board, ROWS, COLS);
	  printf_board(board, ROWS, COLS);
	  ret = check_win(board, ROWS, COLS);
	  if (ret != ' ')
	  {
		  break;
	  }
	  printf("电脑下棋:\n");
	  computer_move(board, ROWS, COLS);
	  printf_board(board, ROWS, COLS);
	  ret = check_win(board, ROWS, COLS);
	} while (ret == ' ');
	if (ret == 'X')
	{
		printf("恭喜你,你赢啦!\n");
	}
	else if (ret == '0')
	{
		printf("真遗憾,电脑赢了\n");
	}
	else if (ret == 'q')
	{
		printf("平局\n");
	}
}
int main()
{
	int input = 0;
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case EXIT:
			break;
		case PLAY:
			game();
			break;
		default:
			printf("输入有误,请重新输入.\n");
		}

	} while (input);
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值