三子棋

头文件:

Tic_Tac_Toe.h

#ifndef  __Tic_Tac_Toe_h_
#define  __Tic_Tac_Toe_h_

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

#define ROW 3
#define COL 3

void start(char chess[ROW][COL], int row, int col);

void displayboard(char chess[ROW][COL], int row, int col);

void player(char chess[ROW][COL], int row, int col);

void computer(char chess[ROW][COL], int row, int col);

int  check_win(char chess[ROW][COL], int row, int col);


#endif


game.c

#define _CRT_SECURE_NO_WARNINGS
#include "Tic_Tac_Toe.h"

void start(char chess[ROW][COL], int row, int col)
{
	int i=0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			chess[i][j] = ' ';
		}
	}
	

}
void displayboard(char chess[ROW][COL], int row, int col)
{
	int i;
	printf("|---|---|---|\n");
	for (i = 0; i < row; i++)
	{

		printf("|%c  | %c | %c |\n", chess[i][0], chess[i][1], chess[i][2]);
		
		printf("|---|---|---|\n");
	}
	
}
void player(char chess[ROW][COL], int row, int col)
{
	int a, b;
	do
	{
		printf("  Player move(2,2):  \n");
		scanf("%d,%d", &a, &b);
		getchar();
		if (chess[a - 1][b - 1] == ' ')
		{
			chess[a - 1][b - 1] = 'X';
			break;
		}

	} while (1);
}
void computer(char chess[ROW][COL], int row, int col)
{
	do
	{
		row=rand() % 3 + 0;
		col = rand() % 3 + 0;
		
		if (chess[row ][col] == ' ')
		{
			chess[row ][col] = '0';
			break;
		}

	} while (1);



}

int  check_win(char chess[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	int count = 0;
	
	for (i = 0; i < row;i++)
	{
	     
		
			 if (chess[i][0] != ' '&& chess[i][1] == chess[i][0] && chess[i][0] == chess[i][2])
			 {
				 return 1;
			 }
		
		
	}
	
	for (i = 0; i < col; i++)
	{
		   if ( (chess[0][i] != ' ') && chess[0][i] == chess[2][i] && (chess[0][i] == chess[1][i]) )
		{
			return 1;
		}
	}

	
	
	if ((chess[0][0] != ' ') &&(chess[0][0] == chess[2][2])&&(chess[0][0] == chess[1][1]))

	    {
		   return 1;
      	}
		

   if ((chess[0][2] != ' ') + (chess[0][2] == chess[1][1]) +(chess[0][2] == chess[2][0])==3)

		{
			return 1;
		}
	
	
   for (i = 0; i < row; i++)
		
   {
		for (j = 0; j < col; j++)
		{
			if (chess[i][j] != ' ')
			{
				count++;
			}
		}
	}
	
   if (count == 9)
	{
		printf(" Dogfall!\n");
		return 2;
	}

	return 0;
}

test.c

#define _CRT_SECURE_NO_WARNINGS
#include "Tic_Tac_Toe.h"

void menu()
{
	printf("  ****************************************************************\n");
	printf("  ********** Do you want to play the Tic_Tac_Toe Game?  **********\n");
	printf("  ***********                                          ***********\n");
	printf("  *************   1.play                2.exit        ************\n");
	printf("  ****************************************************************\n");



}

void game(void)
{
	int i = 0;
	char chessboard[ROW][COL] = { '0'};
	int row = ROW;
	int col = COL;
	int ret;
	start(chessboard, row, col);
	displayboard(chessboard, row, col);
	do
	{

	player(chessboard, row, col);

	displayboard(chessboard, row, col);
	
	ret=check_win(chessboard, row, col);
	if (ret == 1)
	{
		printf("Player Win!\n");
		break;
	}
	else if (ret == 2)
	{
		break;
	}
	computer(chessboard, row, col);

	displayboard(chessboard, row, col);

	ret=check_win(chessboard, row, col);
	if (ret == 1)
	{
		printf("Computer  Win!\n");
		break;
	}
	else if (ret == 2)
	{
		break;
	}

	} while (1);


}


int main()
{
	int n;
	do

	{
		menu();
		scanf("%d", &n);
		if (n == 1)
		{
			printf("  ~~~Game start~~~\n");
			game();

		}

		else if (n == 2)
		{
			break;
		}

		else
		{
			printf("  Input error! \n");
		}
	} while (1);
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值