📄 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("You chose one. You go first. ");
}
else
{
Flag=computer;
Computer.setChessmanColor(black);
Human.setChessmanColor(white);
puts("You chose two. The computer go first. ");
}
printf("Good luck to you! ");
pause();
newline(1);
puts(" Game begin.\n");
gobangPlayer::m_chessboard.renew();
gobangPlayer::m_chessboard.outputStates();
gobangPlayer::count=0; // gobangPlayer::count指出走了多少步
// 初始游戏, 先下执黑子, 黑子用#表示,白子用O表示
while(1)
{
if( Flag==human )
{
printf("Please input: ");
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("Your input are \"%s\". \n",inputWithSpace);
puts("Wrong input!");
printf("Please input again: ");
}
// 输入坐标点, 并检查输入错误
newline(5);
printf(" Good Luck to you! %-4d\n",gobangPlayer::count+1);
newline();
Human.m_chessboard.outputStates();
Human.getMove(x,y);
printf("Your input is (%c,%c).\n",x,y);
if( Human.isWin() )
{
newline();
puts(" ");
puts(" Congratulate ");
puts(" You are so clever. You have won ");
puts("***********************************");
break;
}
//人赢了之后的输出信息, 跳出循环
pause();
}
else
{
Computer.autoMove();
newline(5);
printf(" Good Luck to you! %-4d\n",gobangPlayer::count+1);
newline();
Computer.m_chessboard.outputStates();
Computer.getMove(x,y);
printf("The computer's input is (%c,%c).\n",x,y);
if( Computer.isWin() )
{
newline();
puts("*****************************************");
puts("* I'm sorry to say that you are lost. *");
puts("* Don't disappointed. *");
puts("*****************************************");
break;
}
// 电脑赢了之后的输出信息, 跳出循环
}
Flag=( Flag==human ? computer: human);
gobangPlayer::count++;
// 每走一步,Flag要改变,gobangPlayer::count++
if( gobangPlayer::count>=size*size)
{
newline();
puts("*****************************************");
puts("* Drew with computer. Not bad. *");
puts("* Go on and you can win the computer *");
puts("*****************************************");
break;
}
//用gobangPlayer::count来判断上是否和棋。
// 和棋的输出信息,跳出循环
}
} while( isPlayAgain() );
printf("Good bye! Press 'Enter' to exit. ");
getchar();
return 0;
}
void displayInformation(void)
{
puts("========================================================");
puts("人工只能五子棋实验项目 计算机C班 第六组");
newline();
puts("== ==");
puts("========================================================");
newline();
puts("黑子(First, symbol'#') 1 ");
puts("白子(Second, symbol'O') 2 ");
printf("选择黑子还是白子(黑先) (1 or 2): ");
}
bool isPlayAgain(void)
{
char A[50+1];
char B[50+1];
bool result;
printf("Do you want to play again? Please choose(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("\nYour input are \"%s\".\nWrong input!\n",A);
printf("Please choose again(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("\nYour input are \"%s\".\nWrong input!\n",A);
printf("Please choose again, the choice must be 1 or 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)
{
printf("Please press 'Enter' to continue. ");
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 + -