#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 (65) - 后台任务: 音乐的后台播放和控制

    [源码下载] 重新想象 Windows 8 Store Apps (65) - 后台任务: 音乐的后台播放和控制 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台 ...

  2. 发现自己喜欢了移动端开发--Android

    喜欢.net一直到现在了,但是自己做过的项目都不是让我自己很满意,不知道为什么,可能是自己的要求比较高吧! 下面自己记录自己的学习 src专门存放我们java源代码的包 Android 4.2.2存放 ...

  3. Csharp: read excel file using Open XML SDK 2.5

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. DistributedCache小记

    一.DistributedCache简介 DistributedCache是hadoop框架提供的一种机制,可以将job指定的文件,在job执行前,先行分发到task执行的机器上,并有相关机制对cac ...

  5. 【Asphyre引擎】学习笔记(一)

    先来说说一下几个最基本的对象: TGraphicsDeviceProvider:这个对象决定我们的游戏是用什么来渲染的,比如DX或者OpenGL,DX还有多个版本可以选择. TCustomSwapCh ...

  6. PHP学习笔记:用mysqli连接数据库

    小插曲,晚上把数据的my.ini编码改为utf-8,然后数据库一直不能启动,改回gbk就可以,有知道的告知下问题所在. 因为是链接数据库,也没什么好说明的,直接上代码吧. <?php /* Co ...

  7. ArrayList实现源码分析

    本文将以以下几个问题来探讨ArrayList的源码实现 1.ArrayList的大小是如何自动增加的 2.什么情况下你会使用ArrayList?什么时候你会选择LinkedList? 3.如何复制某个 ...

  8. SharePoint 2013 删除母版页报错“This file may not be moved, deleted, renamed, or otherwise edited”

    在使用SharePoint 2013母版页的时候,我复制了一个seattle.master页面,然后想重命名一下发现报错,删除也报错,spd.页面分别试过签入签出以后均报错,错误如下: 尝试找了一下错 ...

  9. 解决SharePoint 文档库itemadded eventhandler导致的上传完成后,编辑页面保持报错的问题,错误信息为“该文档已经被编辑过 the file has been modified by...”

    在文档库中添加itemadded 后,在上传文件后,会自动打开文档属性的编辑页面,在保存的时候就会报错,说这个文档已经被编辑过了.这是应为默认itemadded实践是异步执行的,会在edit页面打开之 ...

  10. python多线程ssh爆破

    python多线程ssh爆破 Python 0x01.About 爆弱口令时候写的一个python小脚本,主要功能是实现使用字典多线程爆破ssh,支持ip表导入,字典数据导入. 主要使用到的是pyth ...