#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. 重新想象 Windows 8 Store Apps (47) - 多线程之线程同步: Semaphore, CountdownEvent, Barrier, ManualResetEvent, AutoResetEvent

    [源码下载] 重新想象 Windows 8 Store Apps (47) - 多线程之线程同步: Semaphore, CountdownEvent, Barrier, ManualResetEve ...

  2. 与众不同 windows phone (44) - 8.0 位置和地图

    [源码下载] 与众不同 windows phone (44) - 8.0 位置和地图 作者:webabcd 介绍与众不同 windows phone 8.0 之 位置和地图 位置(GPS) - Loc ...

  3. javascript: detect mobile devices or browser

    http://detectmobilebrowsers.com/ http://hgoebl.github.io/mobile-detect.js/ http://www.hand-interacti ...

  4. <s:iterator>各种遍历用法

    struts2<S:iterator>遍历map小结 1.MapAction.java import java.util.ArrayList;   import java.util.Has ...

  5. 浅谈一下缓存策略以及memcached 、redis区别

    缓存策略三要素:缓存命中率   缓存更新策略  最大缓存容量.衡量一个缓存方案的好坏标准是:缓存命中率.缓存命中率越高,缓存方法设计的越好. 三者之间的关系为:当缓存到达最大的缓存容量时,会触发缓存更 ...

  6. 设置让ASP.NET管道接收所有类型的请求

    在web.config文件添加如下一段配置: <configuration> <system.webServer> <modules runAllManagedModul ...

  7. javascript --- 设计模式之单体模式(一)

    单体是一个用来划分命名空间并将一些相关的属性与方法组织在一起的对象,如果她可以被实例化的话,那她只能被实例化一次(她只能嫁一次,不能二婚). 单体模式是javascript里面最基本但也是最有用的模式 ...

  8. 2015年第6本(英文第5本):Harry Potter 1 哈利波特与魔法石

    书名: Harry Potter 1 – Harry Potter and the Sorcerer’s Stone 作者:J.K. Rowling 单词数:7.8万 不重复单词数:6000(我怎么感 ...

  9. Servlet开发配置

    本文主要简单实践一下servlet开发相关开发 1.Servlet的创建 两种方法: 创建普通的java类,继承自HttpServlet类,在通过手动配置web.xml文件注册Servlet对象,比较 ...

  10. UIStoryBoard 中修改控件borderColor

    storyBoard中直接修改UI控件的边框颜色,可以避免先关联再从控制器中修改属性等繁琐操作 因为layer.borderColor隶属于CALayer对象,且CGColor无法直接在sb中直接使用 ...