📄 main.cpp
字号:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#include "gobangPlayer.cpp"
#include"chessboard.cpp"
// Function Declaration Beginning
void displayInformation(void);
// 函数名: displayInformation
// 功能: 打印信息
// 参数: void
// 返回值: void
bool isPlayAgain(void);
// 函数名: isPlayAgain
// 功能: 询问是否继续游戏
// 参数: void
// 返回值: bool 如果继续游戏, 返回true, 否则返回flase
char getChoice(void);
// 函数名: getChoice
// 功能: 决定先手还是后手
// 参数: void
// 返回值: char 先下, 返回'1'; 后下, 返回'2'
void input(char* A, char* B, int max);
// 函数名: input
// 功能: 输入字符串
// 参数: char* A, 用来储存输入的字符串
// char* B, 将输入的字符串去掉空格,换行,存储在B中
// int max, 输入字符的最大长度
// 返回值: void
void pause(void);
// 函数名: pause
// 功能: 程序暂停, 按回车继续执行
// 参数: void
// 返回值: void
void newline(int n=1);
// 函数名: newline
// 功能: 输出换行
// 参数: int n, 为换行的数目, 默认为1
// 返回值: void
// Function Declaration End
// Main Beginning
int main()
{
enum player{ computer, human }; // 玩家分人或者电脑
gobangPlayer Human; // 人
gobangPlayer Computer; // 电脑
char choice;
char x, y;
player Flag; //Flag为computer时轮到电脑下
// Flag为human时到人来下
char inputWithSpace[50+1]; // 存储输入的字符串
char inputWithoutSpace[50+1]; //去掉空格后的字符串
do
{
displayInformation();
choice=getChoice();
newline();
if( choice=='1' )
{
Flag=human;
Human.setChessmanColor(black);
Computer.setChessmanColor(white);
puts(" 玩家先下. ");
}
else
{
Flag=computer;
Computer.setChessmanColor(black);
Human.setChessmanColor(white);
puts(" 电脑先下. ");
}
printf(" 祝你好运! ");
pause();
newline(1);
puts("游戏开始.\n");
gobangPlayer::m_chessboard.renew();
gobangPlayer::m_chessboard.outputStates();
gobangPlayer::count=0; // gobangPlayer::count指出走了多少步
// 初始游戏, 先下执黑子, 黑子用#表示,白子用O表示
while(1)
{
if( Flag==human )
{
printf("请输入坐标(中间以空格隔开): ");
while(1)
{
input(inputWithSpace,inputWithoutSpace,50);
if( strlen(inputWithoutSpace)==0 )
continue;
if( strlen(inputWithoutSpace)==2 )
{
Human.setMove(inputWithoutSpace[0],inputWithoutSpace[1]);
if( Human.checkMove() )
break;
}
printf("你输入的坐标为 \"%s\". \n",inputWithSpace);
puts(" 错误,已有棋子在那!");
printf(" 请重新输入 ");
}
// 输入坐标点, 并检查输入错误
newline();
cout<<" 第 "<<gobangPlayer::count+1<<" 步:玩家"<<endl;
newline();
Human.m_chessboard.outputStates();
Human.getMove(x,y);
printf("你输入的坐标为:(%c,%c).\n",x,y);
if( Human.isWin() )
{
newline();
puts(" 你赢了 ");
break;
}
//人赢了之后的输出信息, 跳出循环
pause();
}
else
{
Computer.autoMove();
newline();
cout<<" 第 "<<gobangPlayer::count+1<<" 步 :电脑"<<endl;
// printf(" 第 \n",gobangPlayer::count+1);
newline();
Computer.m_chessboard.outputStates();
Computer.getMove(x,y);
printf("电脑输入的坐标为: (%c,%c).\n",x,y);
if( Computer.isWin() )
{
newline();
puts(" 你输了 ");
break;
}
// 电脑赢了之后的输出信息, 跳出循环
}
Flag=( Flag==human ? computer: human);
gobangPlayer::count++;
// 每走一步,Flag要改变,gobangPlayer::count++
if( gobangPlayer::count>=size*size)
{
newline();
puts(" 和棋 ");
break;
}
//用gobangPlayer::count来判断上是否和棋。
// 和棋的输出信息,跳出循环
}
} while( isPlayAgain() );
printf("按 'Enter' 键退出. ");
getchar();
return 0;
}
void displayInformation(void)
{
puts("黑子(先下, 以'*'为黑子) 1 ");
puts("白子(后下, 以'O'为白子) 2 ");
printf("请选择 (1 or 2): ");
}
bool isPlayAgain(void)
{
char A[50+1];
char B[50+1];
bool result;
printf(" 继续游戏?(Y/N): ");
while(1)
{
input(A,B,50);
B[0]=toupper(B[0]);
if( strlen(B)==1 && (B[0]=='Y' || B[0]=='N') )
break;
if( strlen(B)==0 )
continue;
printf("输入错误! 请重新输入(Y/N): ");
}
// 检查输入错误
B[0]=='Y' ? result=true : result=false;
newline(6);
return result;
}
char getChoice(void)
{
char A[50+1];
char B[50+1];
while(1)
{
input(A,B,50);
if( strlen(B)==1 && (B[0]=='1' || B[0]=='2') )
break;
if( strlen(B)==0 )
continue;
printf("输入错误!");
printf("请重新输入,输入内容必须为1或2: ");
}
// 检查输入错误
return B[0];
}
void input(char *A, char *B, int max)
{
int countA=0;
int countB=0;
char temp;
while( (temp=getchar())!='\n' && countA<max )
{
A[countA++]=temp;
if( !isspace(temp)) // 去掉空格,回车等字符
B[countB++]=temp;
}
if( countA>=max)
while( getchar()!='\n' ); // C常用的语句, 清空缓冲区
A[countA]='\0';
B[countB]='\0';
}
void pause(void)
{
newline(1);
printf("请按 'Enter' 以继续. ");
while( getchar()!='\n') ;
}
void newline(int n)
{
int i;
for( i=1; i<=n; i++)
putchar('\n');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -