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

📄 honey_hole.hpp

📁 本程序是主要是扫雷
💻 HPP
字号:
/*     Copyright(c) Ben Bear 2003-2004  */

//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License as
//  published by the Free Software Foundation; either version 2 of the
//  License, or (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
//  02111-1307, USA.

#ifndef  __honey_hole_h
#define  __honey_hole_h

#include <cassert>

// unable hole number, an ERROR number
const int HONEY_HOLE_ERROR = -1;

class honey_hole
{
protected:
  int  _No;    //  the No. coordiates, also the hole'_s value
  
  static void No_to_FSO (int  N, int& F, int& S, int& O );
  static void FSO_to_XY (int  F, int  S, int  o, int& X, int& Y );
  static void XY_to_FSO (int  X, int  Y, int& F, int& S, int& O );
  static void FSO_to_No (int  F, int  S, int  O, int& N );
  //  4 methods of cast,  No <--> FSO <--> XY 
public:
  honey_hole ()                    { set ();        }
  honey_hole (const honey_hole& H) { set (H);       }
  honey_hole (int N)               { set (N);       }
  honey_hole (int X, int Y)        { set (X, Y);    }
  honey_hole (int F, int S, int O) { set (F, S, O); }

  ~honey_hole () {}
  
  operator int () const { return _No; }
  
  //  set the hole'_s value
  //  return the No coordinates value
  int set (const honey_hole& H);   //  by other hole
  int set (int N = 0);             //  by the No. coordinates
  int set (int X, int Y);          //  by the XY  coordinates
  int set (int F, int S, int O);   //  by the FSO coordinates
  
  //  get the hole'_s coordinates
  //  return the No coordinates value
  int get () const;                      //  by the No. coordinates
  int get (int& N) const;                //  by the No. coordinates
  int get (int& X, int& Y) const;        //  by the XY  coordinates
  int get (int& F, int& S, int& O) const;//  by the FSO coordinates
  
  //  return the distance between *this and H (hole by hole)
  //  default to Origin
  int distance (honey_hole H = honey_hole ()) const;
  static int distance (honey_hole h1, honey_hole h2);
  
  //  walk step holes by direction dir
  int move (int dir, int step = 1);
  
  //  turn 60*a degree ( turn left )
  int turn (int a = 1);
};

int
honey_hole::set (const honey_hole& h)
{
  assert(h._No != HONEY_HOLE_ERROR);

  return _No = h._No;
}

int
honey_hole::set (int N)
{
  assert(N >= 0);

  return _No = N;
}

int
honey_hole::set (int X, int Y)
{
  int F, S, O, N;
  XY_to_FSO (X, Y, F, S, O);  //  cast XY to FSO
  FSO_to_No (F, S, O, N);     //  cast FSO to No
  return _No = N;
}

int
honey_hole::set (int F, int S, int O)
{
  assert((F >= 0) && (S >= 0) && (S < 6) && (O >= 0) && (O <= F));

  int N;
  FSO_to_No (F, S, O, N);     //  cast FSO to No
  return _No = N;
}

int
honey_hole::get () const
{
  assert(_No != HONEY_HOLE_ERROR);

  return _No;
}

int
honey_hole::get (int& N) const
{
  assert(_No != HONEY_HOLE_ERROR);

  return N = _No;
}

int
honey_hole::get (int& X, int& Y) const
{
  assert(_No != HONEY_HOLE_ERROR);

  int F, S, O;
  No_to_FSO (_No, F, S, O);    //  cast No to FSO
  FSO_to_XY (F, S, O, X, Y);   //  cast FSO to XY
  return _No;
}

int
honey_hole::get (int& F, int& S, int& O) const
{
  assert(_No != HONEY_HOLE_ERROR);
  
  No_to_FSO (_No, F, S, O);
  return _No;
}

int
honey_hole::distance (honey_hole h) const
{
  assert((_No != HONEY_HOLE_ERROR) && (h._No != HONEY_HOLE_ERROR));

  int _x, _y, x1, y1;
  get (_x, _y);
  h.get (x1, y1);
  _x -= x1;    //  sub this->_x with h._x
  _y -= y1;    //  sub this->_y with h._y
               //  look h as origin
  honey_hole d(_x, _y);   //  get new temp coordinates system 
  int _f, _s, _o;
  d.get (_f, _s, _o);
  return  _f;      //  dis to origin is just the F (of FSO)
}

int
honey_hole::distance (honey_hole h1, honey_hole h2)
{
  assert((h1._No != HONEY_HOLE_ERROR) && (h2._No != HONEY_HOLE_ERROR));

  int x1, y1, x2, y2;
  h1.get (x1, y1);
  h2.get (x2, y2);
  x1 -= x2;    //  sub this->_x with h._x
  y1 -= y2;    //  sub this->_y with h._y
               //  look h as origin
  honey_hole d(x1, y1);   //  get new temp coordinates system 
  int _f, _s, _o;
  d.get (_f, _s, _o);
  return  _f;      //  dis to origin is just the F (of FSO)
}

int
honey_hole::move (int dir, int step)  // move in directin dir by step
{
  assert(_No != HONEY_HOLE_ERROR);

  int _x, _y;
  get (_x, _y);   //  get old XY
  switch( dir )
    {
    case 0: _x += step;             break;
    case 1: _x += step; _y += step; break;
    case 2: _y += step;             break;
    case 3: _x -= step;             break;
    case 4: _x -= step; _y -= step; break;
    case 5: _y -= step;             break;
    default:
      return  -1;  //  wrong direction, only return -1
    }
  return  set (_x, _y);   //  set New XY 
}

int
honey_hole::turn (int a)
{
  assert(_No != HONEY_HOLE_ERROR);

  int _f, _s, _o;
  get (_f, _s, _o);
  _s += a % 6 + 6;  //  ++_s equal turn left 60 degree
  _s %= 6;
  return set (_f, _s, _o);
}

void
honey_hole::No_to_FSO (int  N , int& F , int& S , int& O)
{
  assert(N >= 0);

  if (N == 0)  // if No. equal to 0
    {
      F = 0;
      S = 0;
      O = 0;
      
      return ;
    }
  
  int  _f, _s, _o;
  
  _f = 0;
  while ((_f + 1) * _f * 3 < N)  //  get the F 
    _f++;
  
  _s = ((_f + 1) * _f * 3 - N) / _f;  //  get the S 

  _o = ((_f + 1) * _f * 3 - N - _s * _f) % _f;  //  get the O 
  
  F = _f;
  S = _s;
  O = _o;
}

void
honey_hole::FSO_to_No (int  F , int  S , int  O , int& N)
{
  assert((F >= 0) && (S >= 0) && (S < 6) && (O >= 0) && (O <= F));

  N = (F + 1) * F * 3 - S * F - O;
}

void
honey_hole::FSO_to_XY (int  F , int  S , int  O , int& X , int& Y)
{
  assert((F >= 0) && (S >= 0) && (S < 6) && (O >= 0) && (O <= F));
  
  if (F == 0)         // if No. equal to 0
    {
      X = 0;
      Y = 0;
      
      return ;
    }
  
  int _x, _y;
  
  switch (S)
    {
    case 0:  _x = F;        _y = O;        break;
    case 1:  _x = F - O;    _y = F;        break;
    case 2:  _x = - O;      _y = F - O;    break;
    case 3:  _x = - F;      _y = - O;      break;
    case 4:  _x = - F + O;  _y = - F;      break;
    case 5:  _x = O;        _y = - F + O;  break;
    }

  X = _x;
  Y = _y;
}

void
honey_hole::XY_to_FSO (int  X , int  Y , int& F , int& S , int& O)
{
  if (!X && !Y)       // if X==0 and Y==0
    {
      F = 0;
      S = 0;
      O = 0;
      
      return ;
    }

  int  _f, _s, _o;

  if ((Y >= 0) && (X > Y ))   // get the value of S
    {
      _s = 0;
      _f = X;
      _o = Y;
    }
  else if ((X > 0) && (Y >= X))
    {
      _s = 1;
      _f = Y;
      _o = Y - X;
    }
  else if((Y > 0) && (X <= 0 ))
    {
      _s = 2;
      _f = Y - X;
      _o = -X;
    }
  else if((Y <= 0) && (Y > X ))
    {
      _s = 3;
      _f = -X;
      _o = -Y;
    }
  else if((X < 0) && (X >= Y ))
    {
      _s = 4;
      _f = -Y;
      _o = X - Y;
    }
  else if((Y < 0) && (X >= 0 ))
    {
      _s = 5;
      _f = X - Y;
      _o = X;
    }

  F = _f;
  S = _s;
  O = _o;
}

#endif

⌨️ 快捷键说明

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