⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform1.cpp

📁 《C++Builder程序设计范例--中国象棋》配书盘自述文件
💻 CPP
📖 第 1 页 / 共 3 页
字号:
                      RBishopPosDone = false;

         if (ChessBoard[sq].color == black)
          if (ChessBoard[sq].piece == bishop)
                 if ((sq != 0x52) && (sq != 0x56) && (sq != 0x70) && (sq != 0x74)
                     && (sq != 0x78) && (sq != 0x92) && (sq != 0x96))
                 BBishopPosDone = false;

          if (ChessBoard[sq].color == red)
             if (ChessBoard[sq].piece == pawn)
               {
               if ((sq < 0x50) && (sq  >= 0x30 ))
                 if ((sq == 0x31) || (sq == 0x33) || (sq ==0x35) || (sq == 0x37)
                   || (sq == 0x41) || (sq == 0x43) || (sq == 0x45) || (sq == 0x47))
                      RPawnPosDone = false;
                   if ((sq >> 4) < 3)
                     RPawnPosDone = false;
                }

          if (ChessBoard[sq].color == black)
            if (ChessBoard[sq].piece == pawn)
              {
                if ((sq  >= 0x50) && (sq < 0x70 ))
                 if ((sq == 0x51) || (sq == 0x53) || (sq == 0x55) || (sq == 0x57)
                    || (sq == 0x61) || (sq == 0x63) || (sq == 0x65) || (sq == 0x67))
                  BPawnPosDone = false;
                  if (( sq >> 4) >= 7)
                     BPawnPosDone = false;
              }
           }

             if (RKingPosDone == false)
                 ErrorPosText = ErrorPosText+ "帅";
             if (BKingPosDone == false)
                ErrorPosText = ErrorPosText + "将";
             if (RAssistPosDone == false)
              ErrorPosText = ErrorPosText + "仕";
             if (BAssistPosDone == false)
              ErrorPosText = ErrorPosText + "士";
             if (RBishopPosDone == false)
              ErrorPosText = ErrorPosText + "相";
             if (BBishopPosDone == false)
              ErrorPosText = ErrorPosText + "象";
             if (RPawnPosDone == false)
             ErrorPosText = ErrorPosText + "兵";
             if (BPawnPosDone == false)
             ErrorPosText = ErrorPosText + "卒";

             ErrorPosText = ErrorPosText + "的位置错误";

      if ((TotalCount[red] <= 16) && (TotalCount[black] <= 16) &&
          RKingNumDone && BKingNumDone && RAssistNumDone && BAssistNumDone
           && RBishopNumDone && BBishopNumDone && RKnightNumDone && BKnightNumDone
           && RRookNumDone && BRookNumDone  && RGunnerNumDone && BGunnerNumDone
           && RPawnNumDone && BPawnNumDone)
         {
         ResetNewPos();
         if (Attacks(Player, PieceTable[Opponent][0].isquare))
               ErrorPosText = "王" + ErrorPosText; 

	 if (!Attacks(Player, PieceTable[Opponent][0].isquare)&&
           RKingPosDone && BKingPosDone && RAssistPosDone && BAssistPosDone
           && RBishopPosDone && BBishopPosDone && RPawnPosDone && BPawnPosDone)
            Done = true;
         else
            Error(ErrorPosText.c_str());
          }
      else
        Error(ErrorNumText.c_str());

      }
   else
      NormalSetup();

   if (Done)
      NormalSetup();

}
//---------------------------------------------------------------------------
/*
主菜单项“提示”事件处理函数
 把程序计算得出的最好走法在信息框中显示出来。
*/
void __fastcall TMainForm::Hint1Click(TObject *Sender)
{
   SearchHintMove();
   DisplayHint();
}
//---------------------------------------------------------------------------
/*
主菜单项“对弈”事件处理函数
 每点击一次,程序走一步,每走完一次,程序轮流改变对手。
*/
void __fastcall TMainForm::PassClick(TObject *Sender)
{
 if (Level == easygame)
      HideAttacks();
   CurPlayer = Opponent;
   ComputerColor = Player;
   ComputersTurn();
        
}
//---------------------------------------------------------------------------
/*
主菜单项“轮流”子菜单项”红方“事件处理函数
 选中子菜单项”红方“时,在该项前打勾。同时设置为当前玩家。
*/
void __fastcall TMainForm::RedTurnClick(TObject *Sender)
{
   CheckMenuItem(TChessEditMenu->Handle, BlackTurn->Command, MF_UNCHECKED);
   CheckMenuItem(TChessEditMenu->Handle, RedTurn->Command, MF_CHECKED);
   CurPlayer = red;
   ComputerColor = black;
   if (CurPlayer != Player)
      {
      Opponent = ComputerColor;
      Player = ProgramColor = CurPlayer;
      }
        
}
//---------------------------------------------------------------------------
/*
主菜单项“轮流”子菜单项”黑方“事件处理函数
 选中子菜单项”黑方“时,在该项前打勾。同时设置为当前玩家。
*/
void __fastcall TMainForm::BlackTurnClick(TObject *Sender)
{
   CheckMenuItem(TChessEditMenu->Handle, BlackTurn->Command, MF_CHECKED);
   CheckMenuItem(TChessEditMenu->Handle, RedTurn->Command, MF_UNCHECKED);
   CurPlayer = black;
   ComputerColor = red;
   if (CurPlayer != Player)
      {
      Opponent = ComputerColor;
      Player = ProgramColor = CurPlayer;
      }
        
}
//---------------------------------------------------------------------------
/*
  主菜单项“颜色”事件处理函数
  弹出颜色对话框,设置棋盘背景颜色。
  Invalidate函数请求重画
*/
void __fastcall TMainForm::ColorsClick(TObject *Sender)
{
  ColorDlg->ShowModal();
  hColorBrush = CreateSolidBrush(
      RGB(ColorSquareColors[0], ColorSquareColors[1], ColorSquareColors[2]));
  Invalidate();
}
//---------------------------------------------------------------------------
/*
  主菜单项“棋子值”事件处理函数
  模态弹出设置棋子值对话框,设置每个棋子(士、象、马、车、炮、兵)的估价值。
*/
void __fastcall TMainForm::PieceValuesClick(TObject *Sender)
{
 PieceValueDlg->ShowModal();
}
//---------------------------------------------------------------------------
/*
  主菜单项“最好路线”事件处理函数
   翻转子菜单项”最好路线“时,改变该项前的勾号。
   勾住 ”最好路线“时,显示对弈时的最好路线。
   去掉勾时,则不显示对弈时的最好路线 。
  */
void __fastcall TMainForm::BestPathClick(TObject *Sender)
{
ShowBestPath = !ShowBestPath;

   if (ShowBestPath)
      CheckMenuItem(TChessMenu->Handle, BestPath->Command, MF_CHECKED);
   else
      {
      CheckMenuItem(TChessMenu->Handle, BestPath->Command, MF_UNCHECKED);
      InfoForm->BestPath->Caption="";
      }
        
}
//---------------------------------------------------------------------------
/*
  主菜单项“拖动”事件处理函数
   选中该子菜单项时,在该项前打勾号。
  当移动棋子时,采用动画方式。
  */
void __fastcall TMainForm::MouseDragClick(TObject *Sender)
{
 POINT Point;

   if (!Dragging && GotStartSquare)
      {
         DrawNormalBitmap(MoveStartSquare);
         GetCursorPos(&Point);
         DragStart(MoveStartSquare, Point);
      }

   Dragging = true;
   CheckMenuItem(TChessMenu->Handle, MouseDrag->Command, MF_CHECKED);
   CheckMenuItem(TChessMenu->Handle, MouseSelect->Command, MF_UNCHECKED);
        
}
//---------------------------------------------------------------------------
/*
  主菜单项“选择”事件处理函数
   选中该子菜单项时,在该项前打勾号。
  移动棋子时,采用棋子反相方式。
  */
void __fastcall TMainForm::MouseSelectClick(TObject *Sender)
{
 DragEnd(true);
   Dragging = false;
   CheckMenuItem(TChessMenu->Handle, MouseDrag->Command, MF_UNCHECKED);
   CheckMenuItem(TChessMenu->Handle, MouseSelect->Command, MF_CHECKED);
   if (GotStartSquare)
         DrawInvertedBitmap(MoveStartSquare);
         
}
//---------------------------------------------------------------------------
/*
  主窗体鼠标移动事件处理函数
   选中棋子时,在当前鼠标位置显示棋子(动画)。
  */
void __fastcall TMainForm::FormMouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
 if (!GotStartSquare || !Dragging)
      return;
   Drag(Point(X,Y));
       
}
//---------------------------------------------------------------------------
 /*
  主菜单项“声音”事件处理函数
   翻转子菜单项”声音“时,改变该项前的勾号。
 只有系统中配有多媒体喇叭时,才有可能发出声音。
  */
void __fastcall TMainForm::SoundClick(TObject *Sender)
{
 SoundOn = !SoundOn;
   if (SoundOn)
    Sound->Checked = true;
   else
    Sound->Checked = false;

}
//---------------------------------------------------------------------------
 /*
  主窗体定时器事件处理函数
   每当程序发出WM_TIMER消息时,在信息框体中显示下棋时间。
  */
void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
  DisplayTime();
}
//---------------------------------------------------------------------------
/*
  主窗体窗体关闭事件处理函数
   释放系统资源。
   对于非可视化指针,用new创建,还需用delete撤消。
  */
void __fastcall TMainForm::FormDestroy(TObject *Sender)
{
    delete FileName;
    delete InfoForm;
    delete EditForm;
   DeleteObject(hColorBrush);
   for (int i = 0; i < 7; i++)
      {
      DeleteObject(MaskArray[i]);
      for (int j = 0; j < 2; j++)
         DeleteObject(PieceBmpArray[i][j]);
      }
   Timer1->Enabled=false;

}
//---------------------------------------------------------------------------
  /*
  主菜单项“每次移动时间”事件处理函数
   选中该子菜单项时,在该项前打勾号。
  模态弹出“设置每次移动秒数”对话框,设置每次移动程序“想问题”的时间。如果
  输入没有两位小数的浮点数值,弹出错误对话框。
  */
void __fastcall TMainForm::MoveTimeClick(TObject *Sender)
{
 char *SecsPerMove = new char[40];
       COLORTYPE color;
       int temp;
  sprintf(SecsPerMove, "%.2f", AverageTime);

  if (InputSecPerMoveDlg->ShowModal()==mrOk)
      {
   SecsPerMove = InputSecPerMoveDlg->Edit1->Text.c_str();
      double NewMoveTime = atof(SecsPerMove);

      if (NewMoveTime > 0.)
         {
         if (Level != normal)
            {
            UnCheckLevelMenu(Level);
            CheckMenuItem(TChessMenu->Handle, MoveTime->Command, MF_CHECKED);
            }
         Level = normal;
         AverageTime = NewMoveTime;
         PrintCurLevel();
         /*
    在C++ Builder中枚举常量加一个常数,不能成为后一个常量。
     只得采用整数间接来达到目的。
    */
        for (temp=0;temp<=1;temp++)
          {
            { if (temp==0)
              color = red;
                 if (temp==1)
                   color= black;
            ChessTime[color].totaltime = (MoveNumber / 2) * AverageTime;
            }
           } 
         MaxLevel = MAXPLY;
         }
      else
	 Error("时间不正确。请重输。");
      }
   delete SecsPerMove;

}
//---------------------------------------------------------------------------
  /*
  主菜单项“总时间”事件处理函数
   选中该子菜单项时,在该项前打勾号。
  模态弹出“设置每次游戏分钟数”对话框,设置每次游戏程序的时间。如果
  输入没有两位小数的浮点数值,弹出错误对话框。
  */
void __fastcall TMainForm::TotalTimeClick(TObject *Sender)
{
  char *TotalMoveTime = new char[40];
  int temp;
  COLORTYPE color;
   sprintf(TotalMoveTime, "%.2f", AverageTime);

    if (InputMinPerGameDlg->ShowModal()==mrOk)
      {
      TotalMoveTime = InputMinPerGameDlg->Edit1->Text.c_str();
      double NewMoveTime = atof(TotalMoveTime);
      if (NewMoveTime > 0.)
         {
         if (Level != fullgametime)
            {
            UnCheckLevelMenu(Level);
            CheckMenuItem(TChessMenu->Handle, TotalTime->Command, MF_CHECKED);
            }
         Level = fullgametime;
         AverageTime = NewMoveTime;
         PrintCurLevel();
         /*
    在C++ Builder中枚举常量加一个常数,不能成为后一个常量。
    只得采用整数间接来达到目的。
    */
      for (temp=0;temp<=1;temp++)
         {   { if (temp==0)
              color = red;
                 if (temp==1)
                   color= black;
            ChessTime[color].totaltime = (MoveNumber / 2) * AverageTime;
             }
          }
         MaxLevel = MAXPLY;
         }
      else
	 Error("时间不正确。请重输。");
      }
   delete TotalMoveTime;

}
//---------------------------------------------------------------------------
 /*
  主菜单项“匹配”事件处理函数
   选中该子菜单项时,在该项前打勾号。同时设置信息窗体中的级别文本。
  */
void __fastcall TMainForm::MatchingClick(TObject *Sender)
{
 if (Level != matching)
      {
      UnCheckLevelMenu(Level);
      CheckMenuItem(TChessMenu->Handle, Matching->Command, MF_CHECKED);
      }
   Level = matching;
   PrintCurLevel();

}
//---------------------------------------------------------------------------
  /*
  主菜单项“退出”事件处理函数
  */
void __fastcall TMainForm::Exit1Click(TObject *Sender)
{
 MainForm->Close();
}
  /*
  自定义消息处理函数
  响应IDM_EXIT消息请求关闭窗口。
  */
void  TMainForm::Exit(TMessage &Msg )
 {
    MainForm->Exit1Click(Exit1);
  }
//---------------------------------------------------------------------------
  /*
  自定义结束游戏函数
  动态创建对话框。注意,别忘记撤消创建的资源。
  */
void TMainForm::EndGame()
{
    TEndChessGameDlg *EndChessGameDlg = new TEndChessGameDlg(Application,(AnsiString) EndGameMessage);
    EndChessGameDlg->ShowModal();
   delete EndChessGameDlg;
   return;

}
//---------------------------------------------------------------------------
/*
    关闭应用程序时,为避免数据丢失,应对一些重要的数据进行保存。
    FormCloseQuery是解决问题的关键。当窗口关闭时,
    调用VCL函数TForm::Close。
    Close并不是自己关闭窗体。首先它申请FormCloseQuery句柄允许。
    程序退出前,必须经过FormCloseQuery句柄函数允许。
    当选择“游戏->退出”或揿击程序标题栏的关闭图标时,FormCloseQuery
    将执行。如果FormCloseQuery设置CanClose为false,窗体将不关闭。
    设置CanClose为true则允许程序关闭。在FormCloseQuery返回前,调用
    QuitProgram()保存chess.ini文件。
*/
void __fastcall TMainForm::FormCloseQuery(TObject *Sender, bool &CanClose)
{
if (ComputerThinking || AutoPlay)
            {
            ::PostMessage(MainForm->Handle,WM_COMMAND,Stop->Command,0);
            ::PostMessage(MainForm->Handle,WM_COMMAND,IDM_EXIT,0);
            CanClose=false;
            }
         QuitProgram();
         CanClose = true;
}
//---------------------------------------------------------------------------








⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -