c语言三字棋

1.三字棋存在只能算法,虽然程序较笨,希望大佬指导。

2.利用for循环嵌套if判断,每一行有三种堵棋的情况。

3.玩家或者电脑每下一步棋都要进行判断输赢和打印棋盘


game.h 头文件


#ifndef __GAME_H__
#define __GAME_H__

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

#define ROW 3
#define COL 3

void PrintMenu();
void PrintBoard(char board[ROW][COL],int row,int col);
void InitBoard(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 CheckWin(char board[ROW][COL], int row, int col);

#endif __GAME_h__



test.c

#include"game.h"

void game()
{
	char board[ROW][COL] = {0};
	char win = ' ';
	InitBoard(board,ROW,COL);
	PrintBoard(board,ROW,COL);
	do
	{
		PlayerMove(board, ROW, COL);//玩家走		
		PrintBoard(board, ROW, COL);//打印棋盘
		win = CheckWin(board, ROW, COL);//判断胜负
		if (win != ' ')
			break;
		system("CLS");//清屏,优化界面
		ComputerMove(board, ROW, COL;//电脑走
		PrintBoard(board, ROW, COL);//打印棋盘
		win=CheckWin(board, ROW, COL);
		if (win!=' ')
			break;
		
     } while (1);
	if (win == 'X')
	{
		printf("玩家赢\n");
	}
	else if (win == '0')
	{
		printf("电脑赢\n");
	}
	else if (win == 'Q')
	{
		printf("平局\n");
	}
	PrintBoard(board, ROW, COL);
	
	} 
int  main()
{
	int choose = 0;
	srand((unsigned)time(NULL));
	do{
		PrintMenu();
		scanf_s("%d", &choose);
		switch (choose)
		{
		case 1:game();
			break;
		case 0:
			break;
		}
	} while (choose);
	
	system("pause");
}
 
 
game.c
#include"game.h"
void PrintMenu()
{
	printf("**************************\n");
	printf("******1.start 0.exit*******\n");
}
void PrintBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	printf("*******游戏开始**********\n");
	for (i = 0; i < row; i++)
	{
		printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
		if (i < row-1)
		{	
			printf("---|---|---\n");
		}
	}
	printf("---------------\n");
}
void InitBoard(char board[ROW][COL], int row, int col)
{
	int i, j;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}
static int Isfull(char board[ROW][COL], int row, int col)
{
	int i, j;
	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, y;
	printf("玩家走-->");
	while (1)
	{
		printf("请选择坐标-->\n");
		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] = 'X';
				break;
			}
			else
			{
				printf("该位置已经被占用");
			}

		}
		else
		{
			printf("intput error");
		}
	}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
	int i;
	int count = 0;
	for (i = 0; i < row; i++)
	{
		
		if ((board[i][0] == board[i][1] || board[i][0] == board[i][2] || board[i][1] == board[i][2]) && (board[i][0] == 'X' || board[i][1] == 'X' || board[i][2] == 'X'))
		{
			if (board[i][0] == board[i][1] && board[i][0] == 'X')
			{
				board[i][2] = '0';
				count++;
			}
			else if (board[i][1] == board[i][2] && board[i][1] == 'X')
			{
				board[i][0] = '0';
				count++;
			}
			else if (board[i][0] == board[i][2] && board[i][0] == 'X')
			{
				board[i][1] = '0';
				count++;
			}
		} //行堵棋
		if ((board[0][i] == board[1][i] || board[0][i] == board[2][i] || board[1][i] == board[2][i]) && (board[0][i] == 'X' || board[1][i] == 'X' || board[2][i] == 'X'))
		{
			if (board[0][i] == board[1][i] && board[0][i] == 'X')
			{
				board[2][i] = '0';
				count++;
			}
			else if (board[1][i] == board[2][i] && board[1][i] == 'X')
			{
				board[0][i] = '0';
				count++;
			}
			else if (board[0][i] == board[2][i] && board[0][i] == 'X')
			{
				board[1][i] = '0';
				count++;
			}
		}//列堵棋
		if ((board[0][0] == board[1][1] || board[0][0] == board[2][2] || board[1][1] == board[2][2]) && (board[0][0] == 'X' || board[1][1] == 'X' || board[2][2] == 'X'))
		{
			if (board[0][0] == board[1][1] && board[0][0] == 'X')
			{
				board[2][2] = '0';
				count++;
			}
			else if (board[1][1] == board[2][2] && board[1][1] == 'X')
			{
				board[0][0] = '0';
				count++;
			}
			else if (board[0][0] == board[2][2] && board[0][0] == 'X')
			{
				board[1][1] = '0';
				count++;
			}
		}//主对角线堵棋
		if ((board[0][2] == board[1][1] || board[1][1] == board[2][0] || board[0][2] == board[2][0]) && (board[0][2] == 'X' || board[1][1] == 'X' || board[2][0] == 'X'))
		{
			if (board[0][2] == board[1][1] && board[0][2] == 'X')
			{
				board[2][0] = '0';
				count++;
			}
			else if (board[2][0] == board[1][1] && board[1][1] == 'X')
			{
				board[0][2] = '0';
				count++;
			}
			else if (board[0][2] == board[2][0] && board[0][2] == 'X')
			{
				board[1][1] = '0'; 
				count++;

			}
		}//非主对角线堵棋
		if (count == 0)
		{
			int x, y;
			x = rand() % row;
			y = rand() % col;
			if (board[x][y] == ' ')
			{
				board[x][y] = '0';
				break;
			}
		}

	}
}

	char CheckWin(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][0] != ' ')
			{
				return board[i][0];
			}
		}
		for (i = 0; i < row; 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 (Isfull(board, row, col) == 1)
		{
			return 'Q';
		}
		return ' ';
	}










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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值