三子棋

//头文件
#ifndef THREE_CHESS_H
#define THREE_CHESS_H

#include <stdio.h>
#include <windows.h>
#include <time.h>
#pragma warning(disable:4996)

#define ROW 3
#define COL 3

void ShowUl();
void Game();
void ComputerMove(char board[][COL], int row, int col);
void ShowBorad(char board[][COL], int row, int col);
char Judge(char board[][COL], int row, int col);
void PlayerMove(char board[][COL], int row, int col);

#endif

//函数
#define _CRT_SECURE_NO_WARNINGS 1
#include “three_chess.h”

void ShowUl()
{
printf("#######################################################\n");
printf("##### 1、Play 2、Exit ######\n");
printf("#######################################################\n");
printf(“Please Slect:>”);
}

void ComputerMove(char board[][COL], int row, int col)
{
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x-1][y-1] == ’ ')
{
board[x-1][y-1] = ‘O’;
break;
}
}
}
void PlayerMove(char board[][COL], int row, int col)
{
int x, y;
while (1)
{
printf(“Please Enter Your Pos(x,y):>\n”);
scanf("%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(“Enter Pos Is OK,Try Again!\n”);
}
}
else
{
printf(“Enter Error,Try Again!\n”);
}
}
}
char Judge(char board[][COL], int row, int col)
{
int i = 0;
int j = 0;
for (; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][2] == board[i][1] && 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[1][1] != ’ ')
{
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != ’ ')
{
return board[1][1];
}

for (i = 0; i < row; i++)
{
	for (j = 0; j < col; j++)
	{
		if (board[i][j] == ' ')
		{
			return 'N';
		}
	}
}
return 'E';

}
void ShowBoard(char board[][COL], int row, int col)
{
printf(" 1 2 3\n");
printf("---------------\n");
int i = 0;
int j = 0;
for (; i < row; i++)
{
printf("%d |", i + 1);
for (j = 0; j < col; j++)
{
printf(" %c |", board[i][j]);
}
printf("\n---------------\n");
}
printf("\n");
}
void Game()
{
char board[ROW][COL];
memset(board, ’ ', sizeof(board));
char result = ‘N’;
srand((unsigned long)time(NULL));

while (1)
{
	system("cls");
	ComputerMove(board,ROW,COL);
	ShowBoard(board, ROW, COL);
	result = Judge(board, ROW, COL);
	if (result != 'N')
	{
		break;
	}
	PlayerMove(board, ROW, COL);
	ShowBoard(board, ROW, COL);
	result = Judge(board, ROW, COL);
	if (result != 'N')
	{
		break;
	}
}
switch (result)
{
case 'X':
	printf("You Win!\n");
	break;
case 'O':
	printf("You Lose,Computer Win!\n");
	break;
case 'E':
	printf("恭喜,是平局!\n");
	break;
}

}

//主函数
#define _CRT_SECURE_NO_WARNINGS 1
#include “three_chess.h”

int main()
{
int select = 0;
int quit = 0;
while (!quit)
{
ShowUl();
scanf("%d", &select);
switch (select)
{
case 1:
Game();
break;
case 2:
quit = 1;
printf(“Bye,Bye!\n”);
break;
default:
printf(“Please Enter Again!\n”);
break;
}
}
system(“pause”);
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值