📄 checkers_server.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' server kernel.
*/
#ifndef __checkers_server_hpp
#define __checkers_server_hpp
#include "info.hpp"
#include "honey_hole.hpp"
#include "checkers_hole.hpp"
#include "checkers.hpp"
class checkers_server: public checkers
{
protected:
// number of players who had joined this game
int join_player;
// order array of the players
int order[6];
// current player
int cur_player;
// true for the game had started
bool started;
// if player[p] had win, return true
bool win (int p);
// if player[p]'s group win, return true
bool group_win (int p);
// restart game
bool restart ();
// player[name] join this game
// return the direction, -1 for failed
int join (const char* name);
// player[p] quit this game
bool quit (int p);
// if the players are full, return true
bool full () { return join_player == num_player; }
// move a chess from hole[s] to hole[e]
bool move (int p, int s, int e);
public:
// build a checkers with floor[f] and a number[n] of players
checkers_server (const setting& set);
};
checkers_server::checkers_server (const setting& set)
: checkers (set)
{
assert((set.number >= 2) && (set.number <= 6)
&& (set.number != 5));
int dir[6][6] = {{0, 0, 0, 0, 0, 0},
{1, 0, 0, 1, 0, 0},
{1, 0, 1, 0, 1, 0},
{1, 1, 0, 1, 1, 0},
{0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1}};
for (int i = 0; i < 6; ++i)
{
//player[i].name = "";
player[i].start = dir[set.number-1][i] ? i : -1;
player[i].end = -1;
player[i].chess.resize (num_chess, -1);
order[i] = -1;
}
if (set.number != 4)
{
for (int i = 0; i < 6; i += 6 / set.number)
order[i] = (i + 6 / set.number) % 6;
}
else
{
order[0] = 1;
order[1] = 3;
order[3] = 4;
order[4] = 0;
}
join_player = 0;
cur_player = -1;
}
/******************** win() *****************/
bool
checkers_server::win (int p)
{
assert (player[p].end != -1);
int i;
checkers_hole h(floor);
int f, s, o;
for( i = 0; i < num_chess; i++ )
{
h = player[p].chess[i];
h.get (f, s, o);
if ((s != player[p].end) || (f <= floor/2))
break;
}
if (i < num_chess)
return false;
else
return true;
}
bool
checkers_server::group_win (int p)
{
assert ((p >= 0) && (p < 6));
// player[p] should have win
bool all_win = true;
for (int i = 0; i < 6; ++i)
if ((i != p) && is_group(p, i))
{
if (!win(i))
{
all_win = false;
break;
}
}
return all_win;
}
bool
checkers_server::restart ()
{
assert (join_player == num_player);
for (int i = 0; i < 6; ++i)
{
if (player[i].start == -1)
continue;
lay_chess (i);
}
cur_player = 0;
return true;
}
int
checkers_server::join (const char* name)
{
if (join_player >= num_player)
return -1;
for (int i = 0; i < 6; ++i)
{
if ((player[i].start == -1) || (player[i].end != -1))
continue;
player[i].name = name;
player[i].end = (i + 3) % 6;
++join_player;
return i;
}
return -1;
}
bool
checkers_server::quit (int p)
{
assert ((p >= 0) && (p < 6));
if (player[p].start == -1)
return false;
//player[p].name.clear();
player[p].name = "";
player[p].end = -1;
--join_player;
return true;
}
bool
checkers_server::move (int p, int s, int e)
{
assert (checkers_hole::in(floor, s) && checkers_hole::in(floor, e));
//assert ((p >= 0) && (p < 6));
if ((s == e) || (holes[s] != p) || (holes[e] != CHESS_NONE))
return false;
if (!in_stay(p, e))
return false;
checkers::move (s, e);
return true;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -