/************************************************************************************
Copyright(c),
FileName: 前 井字棋
Author:某学员 Version 1.0Date:XXX
Description:C语言简单程序
Version:2.0
Function List:
History:
<Author><time><version>
浪子2015-12-15Ver 1.0
***********************************************************************************
//思路:进入程序先选择人机 还是人人
//进入后开始画棋盘,用小键盘确定下子位置
//判断下子情况 开始对战
//AI编写
//变颜色永远是重点
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include "nanhu.h"
//定义变量
int P_RowMax = 10;
int P_ColMax = 16;
int Games[3][3];//棋盘
int iPlayer;//当前 0 1 2 3 11 12 13//人人和局6,人机和局7
int GameType;//0人机先手,1人机后手,2人人,
void TitleTop3Line();//游戏标题
void ShowFirstView();//显示选择目录
void DrawTable();//画表格
void DrawTableCell(int);//画单元格
BOOL is_PressOk(int, int AndP = 1);//判断当前选择是否可以
int is_Over(int);//判断游戏是否结束
void CleanGame();//清空棋盘
void EasyAI();
void main()
{
int iKeyWord;//获得的按键数字
int iKeyError = 10;//允许10次出错
int iPlayFirst = 1;//1,3 电脑或者玩家1
srand((unsigned)time(NULL));
STEP1://状态进入目录
//进入界面,选择对战目录
ShowFirstView();//选择目录
scanf("%d", &GameType);//获取
if (GameType == 2)
{
iPlayFirst = 1;//选择玩家1先手
}
else if (GameType == 1)
{
iPlayFirst = 3;//选择电脑AI先手
}
else
{
iPlayFirst = 1;//选择玩家1先手
}
//开始对战
//画棋盘,提示下子
//initTableAndGames();
STEP2://状态开始游戏
CleanGame();//清空
iPlayer = iPlayFirst;//默认玩家1开始
while (1)//死循环
{
STEP3://状态运行中
DrawTable();//画棋盘
if (iPlayer == 1 || iPlayer == 2)//玩家
{
scanf("%d", &iKeyWord);//获取输入数字
if (!is_PressOk(iKeyWord))//无效位置
{
iKeyError--;
if (iKeyError > 0)
goto STEP3;//正常出错
else
{//超负荷出错
iKeyError = 10;
goto STEP8;
}
}
}
else if (iPlayer == 3)//电脑
{
EasyAI();
}
if (!is_Over(iPlayer))//如果未结束
{
if (iPlayer == 1)
{
iPlayer = 2;
if (GameType != 2)
{
iPlayer = 3;
}
}
else
{
iPlayer = 1;
}
}
else
{
iPlayer = is_Over(iPlayer);
DrawTable();
goto STEP10;
}
printf("");
}STEP8://输入连续错误,就不玩
my_RED;
printf("\n");
printf("多次输入出错,游戏结束\n");
printf("\n");
my_WHITE;
goto STEPEND;
STEP10://感谢语
my_YELLOW;
printf("\n");
printf("游戏结束,谢谢参与.\n");
printf("\n");
my_WHITE;
STEPTRY://回味区域
printf("1.再来一局.\n");
printf("2.回到菜单.\n");
my_SKYBLUE;
printf("请选择:");
int iTry;
scanf("%d", &iTry);
if (iTry == 1)
{
goto STEP2;
}
if (iTry == 2)
{
goto STEP1;
}
my_WHITE;
STEPEND://我还会回来的!
system("pause");
}
//---------------------------------------------------------------------------
//函数名称:TitleTop3Line
//作用:输出标题
//输入:暂无
//输出:暂无
//---------------------------------------------------------------------------
void TitleTop3Line()
{
my_WHITE;
printf("┏━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃ ");
my_YELLOW;
printf("井字棋(圈叉棋)");
my_WHITE;
printf(" ┃\n");
printf("┣━━━━━━━━━━━━━━━━━━━━━━┫\n");
}
//---------------------------------------------------------------------------
//函数名称:ShowFirstView
//作用:显示目录页
//输入:暂无
//输出:暂无
//---------------------------------------------------------------------------
void ShowFirstView()
{
system("cls");
my_WHITE;
TitleTop3Line();
printf("┃ ┃\n");
printf("┃ ┃\n");
printf("┃ 请选择游戏模式: ┃\n");
printf("┃ ┃\n");
printf("┃ ");
MAGENTA;
printf("0. 选择人机先手.");
my_WHITE;
printf(" ┃\n");
printf("┃ ┃\n");
printf("┃ ");
my_BLUE;
printf("1. 选择人机后手.");
my_WHITE;
printf(" ┃\n");
printf("┃ ┃\n");
printf("┃ ");
my_GREEN;
printf("2. 选择人人对战.");
my_WHITE;
printf(" ┃\n");
printf("┃ ┃\n");
printf("┗━━━━━━━━━━━━━━━━━━━━━━┛\n");
my_SKYBLUE;
printf("请选择: ");
my_WHITE;
}
//---------------------------------------------------------------------------
//函数名称:DrawTable
//作用:刷新棋盘
//输入:暂无
//输出:暂无
//---------------------------------------------------------------------------
void DrawTable()
{
system("cls");
my_WHITE;
TitleTop3Line();
printf("┃ ┃\n");
printf("┃ ┃\n");
printf("┃ ");
DrawTableCell(Games[0][0]);//这里输入第1个[0][0]
printf("┃");
DrawTableCell(Games[0][1]);//这里输入第2个[0][1]
printf("┃");
DrawTableCell(Games[0][2]);//这里输入第3个[0][2]
printf(" 7 8 9 ┃\n");
printf("┃ ━╋━╋━ ┃\n");
printf("┃ ");
DrawTableCell(Games[1][0]);//这里输入第1个[1][0]
printf("┃");
DrawTableCell(Games[1][1]);//这里输入第2个[1][1]
printf("┃");
DrawTableCell(Games[1][2]);//这里输入第3个[1][2]
printf(" 4 5 6 ┃\n");
printf("┃ ━╋━╋━ ┃\n");
printf("┃ ");
DrawTableCell(Games[2][0]);//这里输入第1个[2][0]
printf("┃");
DrawTableCell(Games[2][1]);//这里输入第2个[2][1]
printf("┃");
DrawTableCell(Games[2][2]);//这里输入第3个[2][2]
printf(" 1 2 3 ┃\n");
printf("┃ ┃\n");
printf("┃ ┃\n");
printf("┃ ");
if (iPlayer >= 11 && iPlayer <= 12)
{
printf("恭喜 ");
DrawTableCell(iPlayer -10);
printf("玩家%d 获得胜利! ┃\n", iPlayer-10);
}
else if(iPlayer >= 1 && iPlayer <= 2)
{
printf("现在由 ");
DrawTableCell(iPlayer);
printf("玩家%d 下子 ┃\n",iPlayer);
}
else if (iPlayer == 13)
{
printf("抱歉,您输了,请继续努力。 ┃\n");
}
else if (iPlayer == 6)
{
printf("和局了,握握手好朋友,请继续努力。 ┃\n");
}
else if (iPlayer == 7)
{
printf("恭喜恭喜,您平局了AI,请继续努力。 ┃\n");
}
printf("");
//printf(" ┃\n");
printf("┗━━━━━━━━━━━━━━━━━━━━━━┛\n");
my_SKYBLUE;
printf("请选择: 对应的位置开始下棋: ");
my_WHITE;
}
//---------------------------------------------------------------------------
//函数名称:DrawTableCell
//作用:画棋子
//输入:int iCell
//输出:暂无
//---------------------------------------------------------------------------
void DrawTableCell(int iCell)
{
switch (iCell)
{
case 0:
{
printf(" ");
}break;
case 1:
{
my_RED;
printf("〇");
my_WHITE;
}break;
case 2:
case 3:
{
my_SKYBLUE;
printf("Ⅹ");
my_WHITE;
}
}
}
//---------------------------------------------------------------------------
//函数名称:is_PressOk
//作用:判断当前选择是否可以
//输入:int iPress,int AndP
//输出:可以就true 否则false
//---------------------------------------------------------------------------
BOOL is_PressOk(int iPress,int AndP )
{
if (iPress >= 1 && iPress <= 9)
{
if (Games[2 - (iPress - 1 ) / 3][(iPress - 1) % 3 ] == 0)//0为 未占用
{
if (AndP)//如果参数下子 就下
{
Games[2 - (iPress - 1) / 3][(iPress - 1) % 3 ] = iPlayer;
}
return true;
}
}
return false;
}
//---------------------------------------------------------------------------
//函数名称:is_Over
//作用:判断是否结束
//输入:int iPlay
//输出:int 赢家
//---------------------------------------------------------------------------
int is_Over(int iPlay)
{
int iReturn;
//行相同 //列相同
for (int i = 0; i < 3;i )
{
//行相同 //列相同
if ((Games[i][0] == iPlay && Games[i][1] == iPlay && Games[i][2] == iPlay)
|| (Games[0][i] == iPlay && Games[1][i] == iPlay && Games[2][i] == iPlay))
{
iReturn = iPlay 10;//胜负已分
return iReturn;
}
}
if ((Games[0][0] == iPlay && Games[1][1] == iPlay && Games[2][2] == iPlay)
|| (Games[0][2] == iPlay && Games[1][1] == iPlay && Games[2][0] == iPlay))
{
iReturn = iPlay 10;//胜负已分
return iReturn;
}
//放满了 和局
int iFull = 0;
for (int i = 0; i < 9; i )
{
if(*(&Games[0][0] i) != 0)
iFull ;
}
if (iFull == 9)
{
if (GameType == 2)
{
iReturn = 6;
return iReturn;
}
else
{
iReturn = 7;
return iReturn;
}
}
return 0;
}
//---------------------------------------------------------------------------
//函数名称:CleanGame
//作用:清空棋盘
//输入:暂无
//输出:暂无
//---------------------------------------------------------------------------
void CleanGame()
{
//int *p = &Games[0][0];
for (int i = 0; i < 9; i )
{
*(&Games[0][0] i) = 0;
}
}
//---------------------------------------------------------------------------
//函数名称:EasyAI
//作用:简单人工智能
//输入:暂无
//输出:暂无
//---------------------------------------------------------------------------
void EasyAI()
{
//抢胜
for (int i = 0; i < 9; i )
{
if (*(&Games[0][0] i) == 0)
{
*(&Games[0][0] i) = 3;
if (is_Over(3) != 13)
{
*(&Games[0][0] i) = 0;
}
else
{
return;
}
}
}
//抢救
for (int i = 0; i < 9; i )
{
if (*(&Games[0][0] i) == 0)
{
*(&Games[0][0] i) = 1;
if (is_Over(1) != 11)
{
*(&Games[0][0] i) = 0;
}
else
{
*(&Games[0][0] i) = 3;
return;
}
}
}
//抢中心
if (Games[1][1] == 0)
{
Games[1][1] = 3;
return;
}
//抢四角
int Tmp[4] = { 0, 2, 6, 8 };
int Tmp2[4];
int iCount = 0;
for (int i = 0; i < 4; i )
{
if (*(&Games[0][0] Tmp[i]) == 0)
{
Tmp2[iCount ] = Tmp[i];
}
}
if (iCount)
{
iCount = rand() % iCount;
*(&Games[0][0] Tmp2[iCount]) = 3;
return;
}
//随便丢
int Tmp3[4] = { 1, 3, 5, 7 };
memcpy(Tmp, Tmp3, 9 * sizeof(int));
iCount = 0;
for (int i = 0; i < 4; i )
{
if (*(&Games[0][0] Tmp[i]) == 0)
{
Tmp2[iCount ] = Tmp[i];
}
}
if (iCount)
{
iCount = rand() % iCount;
*(&Games[0][0] Tmp2[iCount]) = 3;
return;
}
}
赶紧关注来报名更多小项目实战吧
,