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

📄 weiqiply.cpp

📁 此代码是用BCB做的围棋代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#if !defined( globalH )
#include "global.h"
#endif

#include <stdlib.h>
#include "weiqiply.h"
#if !defined( weiqibrdH )
#include "weiqibrd.h"
#endif

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainFormWeiqi *MainFormWeiqi;
extern TWeiqiBoard *WeiqiBoard;

//---------------------------------------------------------------------------
__fastcall TMainFormWeiqi::TMainFormWeiqi(TComponent* Owner)
    : TForm(Owner)
{
  bTurn = true;
  GameType = gtLearn;
  WhichSide = wqSideBlack;
  BitmapNormalCross = new( Graphics::TBitmap );
  BitmapNormalCross->LoadFromFile( "nrmlcrs.bmp" );
  BitmapStarCross = new( Graphics::TBitmap );
  BitmapStarCross->LoadFromFile( "starcrs.bmp" );
  BitmapTopCross = new( Graphics::TBitmap );
  BitmapTopCross->LoadFromFile( "topcrs.bmp" );
  BitmapBottomCross = new( Graphics::TBitmap );
  BitmapBottomCross->LoadFromFile( "btmcrs.bmp" );
  BitmapLeftCross = new( Graphics::TBitmap );
  BitmapLeftCross->LoadFromFile( "leftcrs.bmp" );
  BitmapRightCross = new( Graphics::TBitmap );
  BitmapRightCross->LoadFromFile( "rightcrs.bmp" );
  BitmapLeftTopCross = new( Graphics::TBitmap );
  BitmapLeftTopCross->LoadFromFile( "lefttop.bmp" );
  BitmapRightTopCross = new( Graphics::TBitmap );
  BitmapRightTopCross->LoadFromFile( "righttop.bmp" );
  BitmapLeftBottomCross = new( Graphics::TBitmap );
  BitmapLeftBottomCross->LoadFromFile( "leftbtm.bmp" );
  BitmapRightBottomCross = new( Graphics::TBitmap );
  BitmapRightBottomCross->LoadFromFile( "rightbtm.bmp" );
  BitmapStoneBlack = new( Graphics::TBitmap );
  BitmapStoneBlack->LoadFromFile( "blkstone.bmp" );
  BitmapStoneWhite = new( Graphics::TBitmap );
  BitmapStoneWhite->LoadFromFile( "whtstone.bmp" );
  BitmapStoneNewBlack = new( Graphics::TBitmap );
  BitmapStoneNewBlack->LoadFromFile( "nwblkstn.bmp" );
  BitmapStoneNewWhite = new( Graphics::TBitmap );
  BitmapStoneNewWhite->LoadFromFile( "nwwhtstn.bmp" );

  WeiqiBoard->Order = ImageListStones->Add( BitmapNormalCross, NULL );
  ImageListStonesLived->Add( BitmapNormalCross, NULL );
}
//---------------------------------------------------------------------------

void __fastcall TMainFormWeiqi::MenuConnectionExitClick(TObject *Sender)
{
  Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TMainFormWeiqi::BitBtnExitClick(TObject *Sender)
{
  Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TMainFormWeiqi::FormCreate(TObject *Sender)
{
  RectFrontier = ImageFrontier->ClientRect;
  AnsiString asStartTime;
  asStartTime = AnsiString( "Start Time: " )
    + AnsiString( WeiqiBoard->StartTime.wHour ) + AnsiString( ":" )
    + AnsiString( WeiqiBoard->StartTime.wMinute ) + AnsiString( "'" )
    + AnsiString( WeiqiBoard->StartTime.wSecond ) + AnsiString( "''" );
  StatusBarWeiqi->Panels->Items[1]->Text = asStartTime;
  StatusBarWeiqi->Panels->Items[3]->Text = "Dead Stones: Black   0, White   0";
  StatusBarWeiqi->Panels->Items[4]->Text = "New game...";

#if defined( __DEBUG__ )
//  ShowMessage(CurrRect.Left);
//  ShowMessage(CurrRect.Right);
//  ShowMessage(CurrRect.Top);
//  ShowMessage(CurrRect.Bottom);
#endif
  if( EditGo->Focused() )
    EditGo->SelectAll();

}
//---------------------------------------------------------------------------
bool __fastcall TMainFormWeiqi::GetPlaceToGoTo()
{
  div_t nDiv1, nDiv2;
//  int nX, nY;
//  bool bX = false, bY = false;

  // Determine whether the mouse is near a point.
  if( ( MousePoint.x <= 500 ) && ( MousePoint.x >= 0 ) &
      ( MousePoint.y <= 500 ) && ( MousePoint.y >= 0 ) )
  {
    nDiv1 = div( MousePoint.x, 25 );
    if( nDiv1.rem <= 5 )
    {
      StonePlaceToGoTo.nX = nDiv1.quot;
      StonePlaceToGoTo.bX = true;
    }
    else if( nDiv1.rem >= 20 )
    {
      StonePlaceToGoTo.nX = nDiv1.quot + 1;
      StonePlaceToGoTo.bX = true;
    }
    nDiv2 = div( MousePoint.y, 25 );
    if( nDiv2.rem <= 5 )
    {
      StonePlaceToGoTo.nY = nDiv2.quot;
      StonePlaceToGoTo.bY = true;
    }
    else if( nDiv2.rem >= 20 )
    {
      StonePlaceToGoTo.nY = nDiv2.quot + 1;
      StonePlaceToGoTo.bY = true;
    }
  }
}
//---------------------------------------------------------------------------
bool __fastcall TMainFormWeiqi::ReceivePlaceToGoTo()
{
  div_t nDiv1, nDiv2;
//  int nX, nY;
//  bool bX = false, bY = false;

  // Determine whether the mouse is near a point.
  if( ( MousePoint.x <= 500 ) && ( MousePoint.x >= 0 ) &&
      ( MousePoint.y <= 500 ) && ( MousePoint.y >= 0 ) )
  {
    nDiv1 = div( MousePoint.x, 25 );
    if( nDiv1.rem <= 5 )
    {
      StonePlaceToGoTo.nX = nDiv1.quot;
      StonePlaceToGoTo.bX = true;
    }
    else if( nDiv1.rem >= 20 )
    {
      StonePlaceToGoTo.nX = nDiv1.quot + 1;
      StonePlaceToGoTo.bX = true;
    }
    nDiv2 = div( MousePoint.y, 25 );
    if( nDiv2.rem <= 5 )
    {
      StonePlaceToGoTo.nY = nDiv2.quot;
      StonePlaceToGoTo.bY = true;
    }
    else if( nDiv2.rem >= 20 )
    {
      StonePlaceToGoTo.nY = nDiv2.quot + 1;
      StonePlaceToGoTo.bY = true;
    }
  }
}
//---------------------------------------------------------------------------
bool __fastcall TMainFormWeiqi::DrawStone( TWhichSide WhichSide, int X, int Y )
{
  POINT t;
  t.x = 25 * X - 12;
  t.y = 25 * Y - 12;
  int nIndex = WeiqiBoard->Order;
  ImageListStones->Draw( MainFormWeiqi->Canvas, t.x, t.y, nIndex );
/*  if( GameType == gtPlay )
  {
    if( bTurn )
    {
      if( WhichSide == wqSideBlack )
      {
        ImageListStones->Draw( MainFormWeiqi->Canvas, t.x, t.y, nIndex );
      }
      else if( WhichSide = wqSideWhite )
      {
        ImageListStones->Draw( ImageFrontier->Canvas, t.x, t.y, nIndex );
      }
    }
    else  // The oppenent has just done.
    {
      if( WhichSide == wqSideBlack )
      {
        ImageListStones->Draw( MainFormWeiqi->Canvas, t.x, t.y, nIndex );
      }
      else if( WhichSide = wqSideWhite )
      {
        ImageListStones->Draw( ImageFrontier->Canvas, t.x, t.y, nIndex );
      }
    }
    return( true );
  }
  else if( GameType == gtLearn )
  {
    if( WhichSide == wqSideBlack )
    {
      ImageListStones->Draw( MainFormWeiqi->Canvas, t.x, t.y, nIndex );
    }
    else if( WhichSide = wqSideWhite )
    {
      ImageListStones->Draw( ImageFrontier->Canvas, t.x, t.y, nIndex );
    }
    return( true );
  }
  else
  {
//    Application->MessageBox("观棋不语!", "严正警告", MB_OK );
    return( false );
  }
*/
}
//---------------------------------------------------------------------------
bool __fastcall TMainFormWeiqi::SetStoneCoordination( TWhichSide WhichSide, int X, int Y )
{
  AnsiString as;
  switch( Y )
  {
    case  1: as = AnsiString("A"); break;
    case  2: as = AnsiString("B"); break;
    case  3: as = AnsiString("C"); break;
    case  4: as = AnsiString("D"); break;
    case  5: as = AnsiString("E"); break;
    case  6: as = AnsiString("F"); break;
    case  7: as = AnsiString("G"); break;
    case  8: as = AnsiString("H"); break;
    case  9: as = AnsiString("I"); break;
    case 10: as = AnsiString("J"); break;
    case 11: as = AnsiString("K"); break;
    case 12: as = AnsiString("L"); break;
    case 13: as = AnsiString("M"); break;
    case 14: as = AnsiString("N"); break;
    case 15: as = AnsiString("O"); break;
    case 16: as = AnsiString("P"); break;
    case 17: as = AnsiString("Q"); break;
    case 18: as = AnsiString("R"); break;
    case 19: as = AnsiString("S"); break;
    default: as = AnsiString("");
  }
  as += AnsiString( X );
  EditGo->Text = as;
  return( true );
}
//---------------------------------------------------------------------------
bool __fastcall TMainFormWeiqi::GetStoneCoordination()
{
  int X, Y;
  AnsiString as = EditGo->Text.UpperCase();
/*#if defined( __DEBUG__ )
  ShowMessage( as );
  ShowMessage( as.SubString( 1, 1 ) );
  int i = ( as.SubString( 1, 1 ) == AnsiString( "A" ) );
  ShowMessage( i );
#endif*/
  as.Trim(); // Remove blank spaces after and before the first printing character.
  if( as.SubString( 1, 1 ) == AnsiString( "A" ) )
    Y = 1;
  else if( as.SubString( 1, 1 ) == AnsiString( "B" ) )
    Y = 2;
  else if( as.SubString( 1, 1 ) == AnsiString( "C" ) )
    Y = 3;
  else if( as.SubString( 1, 1 ) == AnsiString( "D" ) )
    Y = 4;
  else if( as.SubString( 1, 1 ) == AnsiString( "E" ) )
    Y = 5;
  else if( as.SubString( 1, 1 ) == AnsiString( "F" ) )
    Y = 6;
  else if( as.SubString( 1, 1 ) == AnsiString( "G" ) )
    Y = 7;
  else if( as.SubString( 1, 1 ) == AnsiString( "H" ) )
    Y = 8;
  else if( as.SubString( 1, 1 ) == AnsiString( "I" ) )
    Y = 9;
  else if( as.SubString( 1, 1 ) == AnsiString( "J" ) )
    Y = 10;
  else if( as.SubString( 1, 1 ) == AnsiString( "K" ) )
    Y = 11;
  else if( as.SubString( 1, 1 ) == AnsiString( "L" ) )
    Y = 12;
  else if( as.SubString( 1, 1 ) == AnsiString( "M" ) )
    Y = 13;
  else if( as.SubString( 1, 1 ) == AnsiString( "N" ) )
    Y = 14;
  else if( as.SubString( 1, 1 ) == AnsiString( "O" ) )
    Y = 15;
  else if( as.SubString( 1, 1 ) == AnsiString( "P" ) )
    Y = 16;
  else if( as.SubString( 1, 1 ) == AnsiString( "Q" ) )
    Y = 17;
  else if( as.SubString( 1, 1 ) == AnsiString( "R" ) )
    Y = 18;
  else if( as.SubString( 1, 1 ) == AnsiString( "S" ) )
    Y = 19;
  else
    Y = 0;
  as.Delete( 1, 1 );
  X = as.ToIntDef( 0 );
  if( ( X > 0 ) && ( X < 20 ) && ( Y > 0 ) && ( Y < 20 ) )
  {
    StonePlaceToGoTo.nX = X;
    StonePlaceToGoTo.nY = Y;
    StonePlaceToGoTo.bX = true;
    StonePlaceToGoTo.bX = true;
//#if defined( __DEBUG__ )
//  ShowMessage( StonePlaceToGoTo.nX );
//  ShowMessage( StonePlaceToGoTo.nY );
//#endif
    return( true );
  }
  else
    return( false );
}
//---------------------------------------------------------------------------
void __fastcall TMainFormWeiqi::ImageFrontierClick(TObject *Sender)
{
// Receive or get place to go to.
  if( GameType == gtPlay )
  {
    if( bTurn )
    {
      GetPlaceToGoTo();
      if( StonePlaceToGoTo.bX && StonePlaceToGoTo.bY )
      {
//      ShowMessage( WeiqiBoard->Order );
        if( WeiqiBoard->Phase[ StonePlaceToGoTo.nX ][ StonePlaceToGoTo.nY ] == wqSideNone )
        {
          SetStoneCoordination( WhichSide, StonePlaceToGoTo.nX, StonePlaceToGoTo.nY );
          if( WhichSide == wqSideBlack )
          {
            ImageListStones->Add( BitmapStoneBlack, NULL );
            ImageListStonesLived->Add( BitmapStoneBlack, NULL );
          }
          else if( WhichSide == wqSideWhite )
          {
            ImageListStones->Add( BitmapStoneWhite, NULL );
            ImageListStonesLived->Add( BitmapStoneWhite, NULL );
          }
          WeiqiBoard->InsertStone( WhichSide, StonePlaceToGoTo.nX, StonePlaceToGoTo.nY );
          StatusBarWeiqi->Panels->Items[0]->Text = "Order: " + IntToStr( WeiqiBoard->Order );
          StatusBarWeiqi->Panels->Items[4]->Text = "Black to move...";

⌨️ 快捷键说明

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