#include <iostream>
#include <conio.h>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iomanip>
using namespace std;
#define WIDE_AND_LONG 20
#define NAME_LEN 20
typedef class Gobang
{
public:
int InitPlayerName(char *, char *);
int CheckInput(char,int);
int CheckIndexInput(char, char, char);
int GetPlayerName();
int InitBoard();
int WriteBoard(char, char, bool);
int BeginOrNot();
int CheckRow();
int CheckColumn();
int CheckTopLeft();
int CheckTopRight();
int CheckDownLeft();
int CheckDownRight();
int CheckDraw();
int CheckFinish();
int Chess();
int ShowBoard();
private:
char acBoard[WIDE_AND_LONG][WIDE_AND_LONG];
char acPlayerOneName[NAME_LEN];
char acPlayerTwoName[NAME_LEN];
}GOBANG;
int GOBANG::InitPlayerName(char *pPlayerOne,char *pPlayerTwo)
{
strcpy(acPlayerOneName,pPlayerOne);
strcpy(acPlayerTwoName,pPlayerTwo);
return ;
}
int GOBANG::CheckInput(char ch, int iNameLen)
{
if(' ' == ch || '\t' == ch)
{
cout<<"含有非法字符!"<<endl;
return -;
}
if(iNameLen > NAME_LEN - )
{
cout<<"输入超出限定长度!"<<endl;
return -;
}
return ;
}
int GOBANG::WriteBoard(char cRow, char cColumn, bool bJudge)
{
int iRow = ;
int iColumn = ;
if(cRow >= '' && cRow <= '')
{
iRow = static_cast<int>(cRow - '');
}
else if(cRow >= 'A' && cRow <= static_cast<char>('A' + WIDE_AND_LONG - - ))
{
iRow = static_cast<int>((cRow - 'A') + );
}
else
{
iRow = static_cast<int>((cRow - 'a') + );
}
if(cColumn >= '' && cColumn <= '')
{
iColumn = static_cast<int>(cColumn - '');
}
else if(cColumn >= 'A' && cColumn <= static_cast<char>('A' + WIDE_AND_LONG - - ))
{
iColumn = static_cast<int>((cColumn - 'A') + );
}
else
{
iColumn = static_cast<int>((cColumn - 'a') + );
}
if('+' != acBoard[iRow][iColumn])
{
cout<<"此处已有棋子!"<<endl;
return -;
}
if(!bJudge)
{
acBoard[iRow][iColumn] = static_cast<char>();
}
else
{
acBoard[iRow][iColumn] = static_cast<char>();
}
return ;
}
int GOBANG::CheckIndexInput(char cRow,char cSeparator,char cColumn)
{
if(!cRow || !cSeparator || !cColumn)
{
return -;
}
if(!((cRow >= '' && cRow <= '') || \
(cRow >= 'A' && cRow <= static_cast<char>('A' + WIDE_AND_LONG - - )) || \
(cRow >= 'a' && cRow <= static_cast<char>('a' + WIDE_AND_LONG - - )))
)
{
return -;
}
if(' ' != cSeparator && '\t' != cSeparator && ',' != cSeparator)
{
return -;
}
if(!((cColumn >= '' && cColumn <= '') || \
(cColumn >= 'A' && cColumn <= static_cast<char>('A' + WIDE_AND_LONG - - )) || \
(cColumn >= 'a' && cColumn <= static_cast<char>('a' + WIDE_AND_LONG - - )))
)
{
return -;
}
return ;
}
int GOBANG::GetPlayerName()
{
fflush(stdin);
char cTemp = ;
int iNameLen = ;
cout<<"是否自己定义昵称?是(Y),否(任意键):";
cTemp = getch();
cout<<endl;
if(('y' != cTemp) && ('Y' != cTemp))
{
return ;
}
fflush(stdin);
memset(acPlayerOneName,,sizeof(acPlayerOneName));
memset(acPlayerTwoName,,sizeof(acPlayerTwoName));
cout<<"请玩家一输入昵称:";
while('\n' != (cTemp = getchar()))
{
if(- == CheckInput(cTemp,iNameLen))
{
fflush(stdin);
cout<<"请玩家一输入昵称:";
continue;
}
acPlayerOneName[iNameLen] = cTemp;
iNameLen++;
}
if( == iNameLen)
{
strcpy(acPlayerOneName,"玩家一");
}
iNameLen = ;
fflush(stdin);
cout<<"请玩家二输入昵称:";
while('\n' != (cTemp = getchar()))
{
if(- == CheckInput(cTemp,iNameLen))
{
fflush(stdin);
cout<<"请玩家二输入昵称:";
continue;
}
acPlayerTwoName[iNameLen] = cTemp;
iNameLen++;
}
if( == iNameLen)
{
strcpy(acPlayerTwoName,"玩家一");
}
return ;
}
int GOBANG::InitBoard()
{
int iRow = ;
int iColumn = ;
for(iRow = ; iRow < WIDE_AND_LONG; iRow++)
{
for(iColumn = ; iColumn < WIDE_AND_LONG; iColumn++)
{
acBoard[iRow][iColumn] = '+';
}
}
return ;
}
int GOBANG::CheckRow()
{
int iRow = ;
int iColumn = ;
int iPlayerOneLen = ;
int iPlayerTwoLen = ;
for(iRow = ; iRow < WIDE_AND_LONG; iRow ++)
{
while(iColumn < WIDE_AND_LONG)
{
if(static_cast<char>() == acBoard[iRow][iColumn])
{
iPlayerOneLen ++;
iPlayerTwoLen = ;
}
else if(static_cast<char>() == acBoard[iRow][iColumn])
{
iPlayerTwoLen ++;
iPlayerOneLen = ;
}
else
{
iPlayerTwoLen = ;
iPlayerOneLen = ;
}
iColumn ++;
}
if(iPlayerOneLen >= )
{
return ;
}
if(iPlayerTwoLen >= )
{
return ;
}
iPlayerOneLen = ;
iPlayerTwoLen = ;
iColumn = ;
}
return ;
}
int GOBANG::CheckColumn()
{
int iRow = ;
int iColumn = ;
int iPlayerOneLen = ;
int iPlayerTwoLen = ;
for(iColumn = ; iColumn < WIDE_AND_LONG; iColumn ++)
{
while(iRow < WIDE_AND_LONG)
{
if(static_cast<char>() == acBoard[iRow][iColumn])
{
iPlayerOneLen ++;
iPlayerTwoLen = ;
}
else if(static_cast<char>() == acBoard[iRow][iColumn])
{
iPlayerTwoLen ++;
iPlayerOneLen = ;
}
else
{
iPlayerTwoLen = ;
iPlayerOneLen = ;
}
iRow ++;
}
if(iPlayerOneLen >= )
{
return ;
}
if(iPlayerTwoLen >= )
{
return ;
}
iPlayerOneLen = ;
iPlayerTwoLen = ;
iRow = ;
}
return ;
}
int GOBANG::CheckTopLeft()
{
int iRow = ;
int iTempRow = ;
int iColumn = ;
int iPlayerOneLen = ;
int iPlayerTwoLen = ;
for(iRow = ; iRow < WIDE_AND_LONG; iRow ++)
{
iTempRow = iRow;
while(iTempRow >= )
{
if(static_cast<char>() == acBoard[iTempRow][iColumn])
{
iPlayerOneLen ++;
iPlayerTwoLen = ;
if(iPlayerOneLen >= )
{
return ;
}
}
else if(static_cast<char>() == acBoard[iTempRow][iColumn])
{
iPlayerTwoLen ++;
iPlayerOneLen = ;
if(iPlayerTwoLen >= )
{
return ;
}
}
else
{
iPlayerTwoLen = ;
iPlayerOneLen = ;
}
iTempRow --;
iColumn ++;
} iPlayerOneLen = ;
iPlayerTwoLen = ;
iColumn = ;
}
return ;
}
int GOBANG::CheckTopRight()
{
int iRow = ;
int iColumn = ;
int iTempColumn = ;
int iPlayerOneLen = ;
int iPlayerTwoLen = ;
for(iColumn = ; iColumn < WIDE_AND_LONG - ; iColumn ++)
{
iTempColumn = iColumn;
while(iTempColumn < WIDE_AND_LONG)
{
if(static_cast<char>() == acBoard[iRow][iTempColumn])
{
iPlayerOneLen ++;
iPlayerTwoLen = ;
if(iPlayerOneLen >= )
{
return ;
}
}
else if(static_cast<char>() == acBoard[iRow][iTempColumn])
{
iPlayerTwoLen ++;
iPlayerOneLen = ;
if(iPlayerTwoLen >= )
{
return ;
}
}
else
{
iPlayerTwoLen = ;
iPlayerOneLen = ;
}
iRow ++;
iTempColumn ++;
} iPlayerOneLen = ;
iPlayerTwoLen = ;
iRow = ;
}
return ;
}
int GOBANG::CheckDownLeft()
{
int iRow = ;
int iTempRow = ;
int iColumn = ;
int iPlayerOneLen = ;
int iPlayerTwoLen = ;
for(iRow = ; iRow < WIDE_AND_LONG - ; iRow ++)
{
iTempRow = iRow;
while(iTempRow < WIDE_AND_LONG)
{
if(static_cast<char>() == acBoard[iTempRow][iColumn])
{
iPlayerOneLen ++;
iPlayerTwoLen = ;
if(iPlayerOneLen >= )
{
return ;
}
}
else if(static_cast<char>() == acBoard[iTempRow][iColumn])
{
iPlayerTwoLen ++;
iPlayerOneLen = ;
if(iPlayerTwoLen >= )
{
return ;
}
}
else
{
iPlayerTwoLen = ;
iPlayerOneLen = ;
}
iTempRow ++;
iColumn ++;
} iPlayerOneLen = ;
iPlayerTwoLen = ;
iColumn = ;
}
return ;
} int GOBANG::CheckDownRight()
{
int iRow = ;
int iTempRow = ;
int iColumn = WIDE_AND_LONG - ;
int iPlayerOneLen = ;
int iPlayerTwoLen = ;
for(iRow = ; iRow < WIDE_AND_LONG - ; iRow ++)
{
iTempRow = iRow;
while(iTempRow < WIDE_AND_LONG)
{
if(static_cast<char>() == acBoard[iTempRow][iColumn])
{
iPlayerOneLen ++;
iPlayerTwoLen = ;
if(iPlayerOneLen >= )
{
return ;
}
}
else if(static_cast<char>() == acBoard[iTempRow][iColumn])
{
iPlayerTwoLen ++;
iPlayerOneLen = ;
if(iPlayerTwoLen >= )
{
return ;
}
}
else
{
iPlayerTwoLen = ;
iPlayerOneLen = ;
}
iTempRow ++;
iColumn --;
} iPlayerOneLen = ;
iPlayerTwoLen = ;
iColumn = WIDE_AND_LONG - ;
}
return ;
}
int GOBANG::CheckDraw()
{
int iRow = ;
int iColumn = ;
for(iRow = ; iRow < WIDE_AND_LONG; iRow++)
{
for(iColumn = ; iColumn < WIDE_AND_LONG; iColumn++)
{
if('+' == acBoard[iRow][iColumn])
{
return ;
}
}
}
return ;
}
int GOBANG::CheckFinish()
{
int iJudgeRow = ;
int iJudgeColumn = ;
int iJudgeTopLeft = ;
int iJudgeTopRight = ;
int iJudgeDownLeft = ;
int iJudgeDownRight = ;
int iJudgeDraw = ;
iJudgeRow = CheckRow();
iJudgeColumn = CheckColumn();
iJudgeTopLeft = CheckTopLeft();
iJudgeTopRight = CheckTopRight();
iJudgeDownLeft = CheckDownLeft();
iJudgeDownRight = CheckDownRight();
iJudgeDraw = CheckDraw();
if( == iJudgeRow || == iJudgeColumn || == iJudgeTopLeft || \
== iJudgeTopRight || == iJudgeDownLeft || == iJudgeDownRight)
{
cout<<"恭喜玩家<"<<acPlayerOneName<<">获胜!"<<endl;
return ;
}
if( == iJudgeRow || == iJudgeColumn || == iJudgeTopLeft || \
== iJudgeTopRight || == iJudgeDownLeft || == iJudgeDownRight)
{
cout<<"恭喜玩家<"<<acPlayerTwoName<<">获胜!"<<endl;
return ;
}
if( == iJudgeDraw)
{
cout<<"平局!"<<endl;
return ;
}
return ;
}
int GOBANG::ShowBoard()
{
int iRow = ;
int iColumn = ;
system("cls");
cout<<" ";
for(iRow = ; iRow < WIDE_AND_LONG; iRow++)
{
if( < iRow)
{
cout<<static_cast<char>('A' + iRow - )<<" ";
}
else
{
cout<<iRow<<" ";
}
}
cout<<endl;
for(iRow = ; iRow < WIDE_AND_LONG; iRow++)
{
if( < iRow)
{
cout<<static_cast<char>('A' + iRow - )<<" ";
}
else
{
cout<<iRow<<" ";
}
for(iColumn = ; iColumn < WIDE_AND_LONG; iColumn++)
{
cout<<acBoard[iRow][iColumn]<<' ';
}
cout<<endl;
}
return ;
}
int GOBANG::Chess()
{
bool bJudge = false;
while()
{
char cRow = ;
char cSeparator = ;
char cColumn = ;
char cTemp = ;
int iLen =;
fflush(stdin);
if(!bJudge)
{
cout<<"请<"<<acPlayerOneName<<">输入对应的行和列(格式: a,3 或 a 3 或 a 3):";
}
else
{
cout<<"请<"<<acPlayerTwoName<<">输入对应的行和列(格式: a,3 或 a 3 或 a 3):";
}
while('\n' != (cTemp = getchar()))
{
if(iLen > )
{
cout<<"输入有误!"<<endl;
fflush(stdin);
if(!bJudge)
{
cout<<"请<"<<acPlayerOneName<<">输入对应的行和列(格式: a,3 或 a 3 或 a 3):";
}
else
{
cout<<"请<"<<acPlayerTwoName<<">输入对应的行和列(格式: a,3 或 a 3 或 a 3):";
}
iLen = ;
continue;
}
if( == iLen)
{
cRow = cTemp;
}
else if( == iLen)
{
cSeparator = cTemp;
}
else
{
cColumn = cTemp;
}
iLen++;
}
if(- == CheckIndexInput(cRow,cSeparator,cColumn))
{
cout<<"输入有误!"<<endl;
continue;
}
if(- == WriteBoard(cRow,cColumn,bJudge))
{
continue;
}
ShowBoard();
if( == CheckFinish())
{
BeginOrNot();
}
bJudge = !bJudge;
}
return ;
}
int GOBANG::BeginOrNot()
{
char cTemp = ;
cout<<"是否继续?是(Y),退出(任意键):";
fflush(stdin);
cTemp = getch();
if('y' == cTemp || 'Y' == cTemp)
{
InitBoard();
ShowBoard();
Chess();
}
else
{
exit();
}
return ;
}
int main()
{
cout<<"Hello world!";
}

随机推荐

  1. 使用PushSharp进行IOS发布应用的消息推送

    在做.NET向IOS设备的App进行消息推送时候,采用的是PushSharp开源类库进行消息的推送,而在开发过程中,采用的是测试版本的app,使用的是测试的p12证书采用的是ApnsConfigura ...

  2. java service wrapper 级别为info导致内存剧增直至溢出

    上周,公司某一环境发生java service wrapper内存剧增导致最后被自动killed的情况,经过分析,确定导致java service wrapper(后续简称wrapper)守护进程内存 ...

  3. API的非向后兼容性无论如何通常代表着一种比较差的设计

    不管一个类库或者工具方法实现多么的好,如果无法做到向后兼容性,通常会给用户带来很大的升级成本,很多对此的依赖如果希望在后续的升级和维护期间使用该类库的其他新增特性或者好处,将不得不推迟升级亦或是被迫接 ...

  4. Dalvik VM和JVM 的比较

    dx:dx工具用于将.class字节码(bytecode)转换为Android字节码(保存在.dex文件中)这个字节码文件 是给 Android 的 Java 虚拟机(Dalvik虚拟机)运行用的. ...

  5. angular 指令——时钟范例

    <html> <head> <meta charset='utf-8'> <title>模块化</title> <script typ ...

  6. ABAP中正则表达式的简单使用方法 (转老白BLOG)

    在一个论坛上面看到有人在问正则表达式的问题,特举例简单说明一下.另外,REPLACE也支持REGEX关键字.最后:只能是ECC6或者更高版本才可以(ABAP supports POSIX regula ...

  7. 无法将类型为“Excel.ApplicationClass”的 COM 对象强制转换为接口类 型“Excel._Application”。此操作失败的原因是对 IID 为“{000208D5 -0000-0000-C000-000000000046}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 加载类型库/DLL 时出错。 (异常来 自 HRESULT:

    无法将类型为“Excel.ApplicationClass”的 COM 对象强制转换为接口类 型“Excel._Application”.此操作失败的原因是对 IID 为“{000208D5 -000 ...

  8. 转:Web应用程序项目XX已配置为使用IIS

    转:http://www.cnblogs.com/Joetao/articles/2392526.html 今天在看开源项目Umbraco是,出现一个项目加载不了,并报如下错误: Web应用程序项目U ...

  9. SQL学习笔记:选取第N条记录

    Northwind数据库,选取价格第二高的产品. 有两种方法,一个是用Row_Number()函数: SELECT productname FROM ( productname, Row_Number ...

  10. Nunit在VS2010加载不了程序集的解决办法

    本机环境: Win7 64位 旗舰版 VS2010 Nunit2.6.3 故障重现步骤: 1.在启动外部应用程序中增加C:\Program Files (x86)\NUnit 2.6.3\bin\nu ...