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

📄 checkers_client.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.

/*  This file definition the Chinese Checkers' client kernel.
 */

#ifndef __checkers_client_hpp
#define __checkers_client_hpp

#include <cassert>
#include <vector>
#include <string>

#include "info.hpp"
#include "honey_hole.hpp"
#include "checkers_hole.hpp"
#include "checkers.hpp"

class checkers_client: public checkers
{
protected:
  // my direction
  int dir;

  std::string name;

  // the floor[f] of the checkers and direction[d] of the gamer
   void init (const setting& set, int d, const char* n);

  // join a player[p, name]
  void join_player (int p, const char* name);

  // player[p] quit this game
  void quit_player (int p);

protected:
  enum
    {
      NONE_MODE,
      STEP_MODE,
      DIRECT_MODE
    };

  // can move 
  bool movable;

  // move mode: step or direct
  int move_mode;

  // the chess been moved [0, num_chess)
  int moved_chess;
  // the chess' position now
  int pos_chess;

  // the length of the help chess
  int help_len;
  // all the help chess
  std::vector<int> help_chess;

  // start to move a chess
  void order_move ();
  void finish_move ();

  // hide the moved_chess if hide==true
  void hide_moved_chess (bool hide);

  // choose a chess[ch] to move
  void choose_chess (int ch);
  
  // select a move mode[m]
  void set_mode (int m);

  // move a step to hole[h]
  bool move_step (int h);

  // move to next or prev help chess' hole
  bool move_help (bool next);
};

void
checkers_client::init(const setting& set, int d, const char* n)
{
  assert ((set.floor >= 6) && (set.floor % 2 == 0)
	  && (d >= 0) && (d < 6));
  
  checkers::init (set);
  
  dir = d;

  name = n;

  for (int i = 0; i < 6; ++i)
    {
      player[i].start = -1;
      player[i].end = -1;
      player[i].chess.resize (num_chess, -1);
    }

  player[d].name = n;
  player[d].start = d;
  player[d].end = (d + 3) % 6;

  lay_chess (dir);
  
  help_len = -1;
  help_chess.clear ();
  help_chess.resize (num_hole, -1);

  movable = false;
}

void
checkers_client::join_player (int p, const char* name)
{
  assert ((p >= 0) && (p < 6) && (name != 0));
  assert (player[p].end == -1);

  if (player[p].start == -1)
    {
      player[p].start = p;
      player[p].end = (p + 3) % 6;
      lay_chess (p);
    }

  player[p].name = name;
}

void
checkers_client::quit_player (int p)
{
  assert ((p >= 0) && (p < 6) 
	  && (player[p].end != -1));

  player[p].name = ""; // .clear();
  player[p].end = -1;
}

void
checkers_client::order_move ()
{
  assert ((floor != -1) && (movable == false));

  movable = true;
  moved_chess = -1;
  pos_chess = -1;
  move_mode = NONE_MODE;
  help_len = -1;
}

void
checkers_client::finish_move ()
{
  assert ((floor != -1) && (movable == true));

  if (moved_chess != -1)
    hide_moved_chess (false);
  movable = false;
  moved_chess = -1;
  pos_chess = -1;
  move_mode = NONE_MODE;
  help_len = -1;
}

void
checkers_client::hide_moved_chess (bool hide)
{
  if (hide)
    holes[player[dir].chess[moved_chess]] = CHESS_NONE;
  else
    holes[player[dir].chess[moved_chess]] = dir;
}

void
checkers_client::choose_chess (int ch)
{
  assert ((ch >= 0) && (ch < num_chess)
	  && movable);

  int tmp = player[dir].chess[ch];
  if (moved_chess != -1)
    hide_moved_chess (false);
  if (move_mode == STEP_MODE)
    {
      pos_chess = player[dir].chess[ch];
      help_len = search_step (tmp, help_chess.begin());
    }
  else if (move_mode == DIRECT_MODE)
    {
      pos_chess = 0;
      help_len = search (dir, tmp, help_chess.begin());
    }
  else
    {
      pos_chess = -1;
    }
  moved_chess = ch;
  hide_moved_chess (true);
}

void
checkers_client::set_mode (int m)
{
  assert (((m == STEP_MODE) || (m == DIRECT_MODE))
	  && movable);

  if (move_mode == m)
    return;

  move_mode = m;
  if (move_mode == STEP_MODE)
    {
      if (moved_chess != -1)
	{
	  pos_chess = player[dir].chess[moved_chess];
	  help_len = search_step (pos_chess, help_chess.begin());
	}
    }
  else if (move_mode == DIRECT_MODE)
    {
      if (moved_chess != -1)
	{
	  pos_chess = 0;
	  help_len = search (dir, player[dir].chess[moved_chess], 
			     help_chess.begin());
	}
    }
}

bool
checkers_client::move_step (int h)
{
  assert (movable && (move_mode == STEP_MODE));
  assert ((moved_chess != -1) && (pos_chess != -1));
  assert (checkers_hole::in(floor, h));

  pos_chess = h;
  help_len = search_step (pos_chess, help_chess.begin());
  return true;
}

bool
checkers_client::move_help (bool next)
{
  assert (movable && (move_mode == DIRECT_MODE)
	  && (moved_chess != -1) && (pos_chess != -1));

  pos_chess = (pos_chess + (next ? 1 : (help_len - 1))) % help_len;
  return true;
}

#endif

⌨️ 快捷键说明

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