📄 rules.java
字号:
// Copyright (c) 2005 Sony Ericsson Mobile Communications AB
//
// This software is provided "AS IS," without a warranty of any kind.
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
//
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se)
package bluegammon.logic;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
/**
* Contains state of rules and logic of deciding valid moves.
* @author Peter Andersson
*/
public class Rules
{
/** Flag denoting that there may be maximum five pieces on a row */
public static final int MAX_FIVE = 1;
/** Flag denoting that a piece must go out on an even dice */
public static final int EVEN_OUT = 2;
/** Current rule flag */
protected static int RULEFLAGS = 0;
private static final int FULLMASK = MAX_FIVE | EVEN_OUT;
/**
* Returns true if any special rule has been enabled. Returns false if no
* special rules are enabled.
* @return true if any rule is enabled, false otherwise.
*/
public static boolean isAnyRuleSet()
{
return RULEFLAGS != 0;
}
/**
* Enables/disables a rule.
* @param rule The rule.
* @param set true to enable, false to disable.
*/
public static void set(int rule, boolean set)
{
if (set)
{
RULEFLAGS |= rule;
}
else
{
RULEFLAGS &= (FULLMASK ^ rule);
}
}
/**
* Queries if a rule is enabled or not.
* @param rule The rule.
* @return true if enabled, false otherwise.
*/
public static boolean isSet(int rule)
{
return (RULEFLAGS & rule) != 0;
}
/**
* Returns if a move is valid from guardposition to specified position.
* @param state The board state.
* @param to The destination index to move piece to.
* @param player Player indices.
* @param opponent Opponend indices.
* @return True if valid, false otherwise.
*/
public static boolean isValidFromGuard(BoardState state, int to,
int[] player, int[] opponent)
{
boolean ok = opponent[to] < 2;
if (isSet(MAX_FIVE))
{
ok &= player[to] < 5;
}
return ok;
}
/**
* Returns if a move is valid from specified position to specified position.
* @param state The board state.
* @param from The source index to move piece from.
* @param to The destination index to move piece to.
* @param white The color of the piece.
* @param player Player indices.
* @param opponent Opponend indices.
* @return True if valid, false otherwise.
*/
public static boolean isValidMove(BoardState state, int from, int to,
boolean white, int[] player, int[] opponent)
{
if (to <= Board.POS_BOARD)
{
boolean ok = opponent[to] < 2;
if (isSet(MAX_FIVE))
{
ok &= player[to] < 5;
}
return ok;
}
else if (to > Board.POS_BOARD)
{
boolean ok = state.areAllPiecesHome(white) &&
(to == Board.POS_OUT || state.isNoneBefore(white, from));
if (isSet(EVEN_OUT))
{
ok &= to == Board.POS_OUT;
}
return ok;
}
else
{
return false;
}
}
/**
* Returns current rule settings as an integer.
* @return Current rule settings.
*/
public static int getRuleFlags()
{
return RULEFLAGS;
}
/**
* Sets current rules as an integer.
* @param flags Current rule settings.
*/
public static void setRuleFlags(int flags)
{
RULEFLAGS = flags;
}
// I/O
/**
* Loads rules.
* @param dis The stream to read from.
* @return number of bytes read.
* @throws IOException if loading fails.
*/
public static int loadRules(DataInputStream dis) throws IOException
{
RULEFLAGS = dis.readInt();
return 4;
}
/**
* Saves rules.
* @param dos The stream to write to.
* @return Number of bytes written.
* @throws IOException if save failed.
*/
public static int saveRules(DataOutputStream dos) throws IOException
{
dos.writeInt(RULEFLAGS);
return 4;
}
/** Prevent instantiation */
private Rules()
{}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -