2021-07-31

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


void game()
{
	char ret = 0;
	char board[ROW][COL] = { 0 };
	Initboard(board, ROW, COL);
	displayboard(board, ROW, COL);

	while (1)
	{
		playermove(board, ROW, COL);
		displayboard(board, ROW, COL);
		ret = Iswin(board, ROW, COL);
		if (ret != 'c')
		{
			break;
		}
		computermove(board, ROW, COL);
		displayboard(board, ROW, COL);
		ret = Iswin(board, ROW, COL);
		if (ret != 'c')
		{
			break;
		}
	}
	if (ret == '*')
	{
		printf("玩家赢\n");
	}
	else if (ret == '#')
	{
		printf("电脑赢\n");
	}
	else
	{
		printf("平局\n");
	}
}
void menu()
{
	printf("************************************\n");
	printf("************************************\n");
	printf("******1.进入游戏 0.退出游戏*********\n");
	printf("************************************\n");
	printf("************************************\n");
}
void test()
{
	int i = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择>:");
		scanf_s("%d", &i);
		switch (i)
		{
		case 1 :
		{
			printf("进入游戏\n");
			game();
			break;
		}
		case 0 :
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误,请重新输入\n");
			
		}
	} while (i);
}
int main()
{
	test();
	return 0;
}

game.h

#define ROW 3
#define COL 3
#include<stdlib.h>
#include <stdio.h>
#include<time.h>

void Initboard(char board[ROW][COL], int row, int col);
void displayboard(char board[ROW][COL], int row, int col);
void playermove(char board[ROW][COL],int row,int col);
void computermove(char board[ROW][COL], int row, int col);
char Iswin(char board[ROW][COL], int row, int col);

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void Initboard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}
//棋盘
//   |   |    
//---|---|---
//   |   |   
//---|---|---
//   |   |
void displayboard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if (j<col-1)
			printf("|");	
		}
		printf("\n");
		if (i < row - 1)
			//printf("---|---|---\n");
		{
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j<col-1)
				printf("|");
			}
			printf("\n");
		}
	}
}
int isfull(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
			{
				return 0;
			}
		}
	}
return 1;
}
//玩家走
void playermove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家走\n");
	while (1)
	{
		printf("请输入一个坐标>:");
		scanf_s("%d%d", &x,&y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("坐标已被占用\n");
			}
		}
		else
			printf("输入错误,请重新输入\n");
	}
}

void computermove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("电脑走\n");
	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		//if (x >= 1 && x <= row && y >= 1 && y <= col)  此处和玩家走是不一样的,生成的本来就是 012
		
			if (board[x][y] == ' ')
			{
				board[x][y] = '#';
				break;
			}
		
	}
}
char Iswin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; 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 < col; i++)
	{
		if (board[0][i] = board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[0][i];
			
		}
	}
	if (board[0][0] = board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	if (board[2][0] = board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	if (1 == isfull(board, ROW, COL))
	{
		return 'Q';
	}
	return 'c';
}

上一篇:井字游戏


下一篇:leetcode723