Hello world S.B.S.
#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!";
}
随机推荐
- 创建WCF服务自我寄宿
WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务.Windows服务.Winform程序.控制台程序中进行寄宿,从而实现WCF服务的运行,为 ...
- linux下的inode记录
我们经常在Linux下可以看到inode,都不知道是什么东东,那么我们现在来慢慢了解下. 一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做&q ...
- .NET Framework 4.0之Tuple(元组)
Tuple,是函数式编程的概念之一,早见于Elang.F#等动态语言.Tuple类型像一个口袋,在出门前可以把所需的任何东西一股脑地放在里面.您可以将钥匙.驾驶证.便笺簿和钢笔放在口袋里,您的口袋是存 ...
- java线程控制、状态同步、volatile、Thread.interupt以及ConcurrentLinkedQueue
在有些严格的系统中,我们需要做到干净的停止线程并清理相关状态.涉及到这个主题会带出很多的相关点,简单的总结如下: 我们知道,在java中,有一个volatile关键字,其官方说明(https://do ...
- centos/rhel 6.5下rabbitmq安装(最简单方便的方式)
wget -c http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.x86_64.r ...
- 六个创建模式之单例模式(Singleton Pattern)
定义: 确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.三个特点:一个类只有一个实例:必需自己创建这个实例:必需自行向整个系统提供这个实例. 结构图: Singleton:单例类,提 ...
- SAPScript、Smartforms动态打印图像或者背景图片
在利用 SMARTFORMS 进行打印的时候有时候要求输出的图片可能是随着打印内容的不同而不同了,也就是动态输出图片,SMARTFORMS的提供了相关的支持技术,下面是实现截图 1.创建要显示的图片 ...
- Android DiskLruCache 硬盘缓存
概述 记得在很早之前,我有写过一篇文章Android高效加载大图.多图解决方案,有效避免程序OOM,这篇文章是翻译自Android Doc的,其中防止多图OOM的核心解决思路就是使用LruCache技 ...
- IOS字典NSDictionary与NSMutableDictionary知识点
字典中的元素是以键值对的形式存储的,键值对的键和值,都是任意的对象,但是键往往使用字符串,字典存储对象的地址没有顺序,字典的遍历分为:键的遍历和值的遍历,字典与数组的区别:数组讲究顺序,而字典可以快速 ...
- App开发流程之源代码Git管理
base项目已经可以编译运行了,只不过没有实际功能而已.继续完善base项目前,我们需要考虑一下代码管理的事情了. 不管是一个人开发还是一个团队开发,对源代码进行版本管理是必须的,任何人对代码的的任何 ...