三子棋游戏

介绍下三子棋游戏规则,方便接下来编程和理解。规则如下:
在九宫格棋盘上,只要任意一方棋子连成一线(横线,竖线,斜线)就赢了,游戏结束可选择是否继续 。

嘿嘿,具体操作是什么嘞?咱一点点来->
 1.对于玩家开说,我们的游戏程序应该先有个菜单页面,用来让用户操作选择玩游戏,还是退出。界面如下,只需几条printf语句即可。


菜单界面:


游戏本身应该允许反复玩耍,且刚开始就要执行一次,所以用do-while循环更合适。先循环后判断,所以这里选择do-while:

do
	{
		menu();
		printf("请选择:<");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			break;
		default:
			printf("输入有误,请重新输入!\n");
			break;
		}

	} while (input);


3.接下来需要考虑如何让电脑或用户进行轮番下棋操作 ,电脑走需要让电脑产生随机坐标,在数组这个坐标位置放一个‘#’作为电脑棋子,需用到rand()函数,这里还需在main中加一行代码:

 

srand((unsigned int)time(NULL));

 

注意生成随机数要用到time.h头文件。
其次考虑到用户习惯输入(1,1)(2,1)这种坐标,所以需给传递的参数减1。
4.在轮番下棋要进行判断输赢操作,这时需要编写一个check_win()函数,通过返回值判断输过返回值判断输赢。若棋盘已满但未分输赢,则是平局,需编写is_full()函数判断棋盘是否已满。

5.为了使显示的棋盘更美观,可用system("pause")清空屏幕,使之只在一个棋盘上显示每次玩的结果。
6.源代码如下:
 

game.h
#ifndef _GAME_H_
#define _GAME_H_
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<time.h>
//棋盘的行数和列数
#define ROWS 3
#define COLS 3
void init_board(char board[ROWS][COLS], int rows, int cols);//初始化棋盘
void display_board(char board[ROWS][COLS], int rows, int cols);//打印棋盘
void player_move(char board[ROWS][COLS], int rows, int cols);//玩家下棋
void compute_move(char board[ROWS][COLS], int rows, int cols);//电脑下棋
char 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)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ' ';
		}
	}
}
void display_board(char board[ROWS][COLS], int rows, int cols)
{
	int i;
	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;
	while (1)
	{
		printf("请输入坐标:");
		scanf("%d%d", &x, &y);
		x--;
		y--;
		if (((x >= 0) && (x <= rows - 1)) && ((y >= 0) && (y <= cols - 1)))
		{
			//if (board[x][y] == ' ')
			if (board[x][y] != '*' && board[x][y] != '#')
			{
				board[x][y] = '*';
				break;
			}
			else
			{
				printf("坐标错误,请重新输入!\n");
			}
		}
	}
}
//电脑走
void compute_move(char board[ROWS][COLS], int rows, int cols)
{
	while(1)
	{
		int x = rand() % rows;
		int y = rand() % cols;
		//if (board[x][y] == ' ')
		if (board[x][y] != '*' && board[x][y] != '#')
		{
			board[x][y] = '#';
			break;
		}
	}
}
static 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] != '*' && board[i][j] != '#')
				return 0;
		}
	}
	return 1;
}
char 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];
	}
	for (i  = 0;i < cols; i++)
	{
		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';
	}
	return ' ';
}


 

test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
//菜单显示
void menu()
{
	printf("*************************************\n");
	printf("**********    1.play    *************\n");
	printf("**********    0.exit    *************\n");
	printf("*************************************\n");
}
void game()
{
	char board[ROWS][COLS] = { 0 };
	int ret = '0';
	init_board(board, ROWS, COLS);//初始化棋盘
	display_board(board, ROWS, COLS);
	while (1)
	{
		player_move(board, ROWS, COLS);
		ret = check_win(board, ROWS, COLS);
		if (ret == '*' || ret == '#'|| ret == 'q')
		{
			display_board(board, ROWS, COLS);
			break;
		}
		compute_move(board, ROWS, COLS);
		system("cls");
		ret = check_win(board, ROWS, COLS);

		if (ret == '*' || ret == '#' || ret == 'q')
		{
			display_board(board, ROWS, COLS);
			break;
		}
		display_board(board, ROWS, COLS);
	}
	if (ret == '*')
	{
		printf("玩家赢\n");
	}
	if (ret == '#')
	{
		printf("电脑赢\n");
	}
	if (ret == 'q')
	{
		printf("平局\n");
	}

}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择:<");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			break;
		default:
			printf("输入有误,请重新输入!\n");
			break;
		}

	} while (input);
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值