三子棋游戏

头文件

#ifndef _GAME_H_
#define _GAME_H_
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include <string.h>
#include<time.h>
#pragma warning(disable:4996)
#define ROW 3
#define COL 3

void game();

#endif

main.c

#include<stdio.h>
#pragma warning(disable:4996)
void menu()
{
	printf("****************************\n");
	printf("**1. play     2. exit     **\n");
	printf("****************************\n");
	printf("Please Select:");
}
int main()
{
	int select = 0;

	//这里是为了让游戏一直可以玩下去。若选择错误会一直循环,直到选择1或2时循环跳出.

	do{
		menu();
		scanf("%d", &select);
		switch (select){
		case 1:
			game();//选择1进入游戏!
			break;
		case 2:
			exit(0);//选择2退出游戏!
		default:
			break;
		}

	} while (1);
	system("pause");
	return 0;
}

game.c

#include "game.h"


static void displayBoard(char board[ROW][COL], int row)
{
	int i = 0;
	for (; i < row; i++){
		printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
		if (i != 2){
			printf("---|---|---\n");
		}
	}
	printf("\n");
}

static void playMove(char board[][COL], int row)
{
	int x, y;
	do{
		printf("Please Enter(x,y):\n");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= 3 && y >= 1 && y <= 3)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = 'x';
				break;
			}
			else
			{
				printf("这个位置被占用,try again!\n");
			}
		}
		else
		{
			printf("Select errors,Please try again!\n");
		}
	} while (1);
}

static void computerMove(char board[ROW][COL], int row)
{
	srand((unsigned long)time(NULL));
	do{
		int x = rand() % row;
		int y = rand() % COL;
		if (board[x][y] == ' ')
		{
			board[x][y] = 'o';
			break;
		}
	} while (1);
}

static int isFull(char board[][COL], int row)
{
	int i = 0;
	for (; i < row; i++){
		int j = 0;
		for (; j < COL; j++){
			if (board[i][j] == ' '){
				return 0;
			}
		}
	}
	return 1;
}

static char isWin(char board[][COL], int row)
{
	int i = 0;
	for (; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
		{
			return board[i][0];
		}
	}

	for (i = 0; i < COL; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
		{
			return board[0][i];
		}
	}

	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
	{
		return board[0][0];
	}


	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	if (isFull(board, row))
	{
		return 'q';
	}

	return ' ';
}

void game()
{
	char board[ROW][COL];
	memset(board, ' ', COL*ROW);
	char ret;
	do{
		system("cls");
		displayBoard(board, ROW);
		playMove(board, ROW);
		ret = isWin(board, ROW);
		if (ret != ' '){
			break;
		}
		computerMove(board, ROW);
		ret = isWin(board, ROW);
	} while (ret == ' ');
	if (ret == 'q'){
		printf("平局!\n");
	}
	else if (ret == 'x'){
		printf("恭喜你,你赢了!\n");
	}
	else if (ret == 'o'){
		printf("不好意思,你输了!\n");
	}
	else{
		printf("debug!\n");
	}

}

结果图显示:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值