#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. PIC32MZ tutorial -- UART Communication
  2. 求出数组前面k个元素或数组中元素大于一半的元素(快速排序与堆排序的灵活运用)
  3. Axure母版
  4. 【面试题003】c数组做为参数退化的问题,二维数组中的查找
  5. poj3259
  6. 关于C++需要加强学习的几点内容
  7. A WCF-WPF Chat Application
  8. (转)Asp.net的HttpCookie写入汉字读取时为乱...
  9. PC--CSS维护
  10. Android studio Debug效率提升
  11. Philosophy is systematic reflective thinking on life.
  12. FusionCharts重写单系列图
  13. git只追踪特定类型的文件
  14. Django框架详细介绍---cookie、session、自定义分页
  15. nginx问题集锦
  16. 部署vmware-vcsa 6.5
  17. [Oracle,2018-02-07] Oracle 报错:“ORA-02292:违反完整约束条件(XXX.FKXXX)
  18. vue开发移动端项目 过渡动画问题
  19. poj1062(分区间迪杰斯特拉,内含测试数据,一直wa的同学可以进来看看)
  20. Django(app的概念、ORM介绍及编码错误问题)

热门文章

  1. Win7配置Go环境
  2. scons学习
  3. 自定义HttpMessageHandler实现HTTP方法的重写
  4. 我的一个javascript项目的重构历程
  5. CSS 去掉inline-block元素间隙的几种方法
  6. javascript-this,call,apply,bind简述3
  7. Sharepoint 2013 创建TimeJob 自动发送邮件
  8. FIJ Jobs – 2013/8/12
  9. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q125-Q126)
  10. 安卓开发_浅谈ListView(SimpleAdapter数组适配器)