public GameState Play(FieldCoordinates action) { if (action == FieldCoordinates.Pass) { if (PreviousMove == FieldCoordinates.Pass) { return(new GameState(CurrentPlayer.Opposite, InternalState.Pass())); } else { return(new GameState(FieldCoordinates.Pass, CurrentPlayer.Opposite, InternalState.Pass())); } } else { GameStateInternal nextState = InternalState.Play(action.X, action.Y, CurrentPlayer); if (nextState == null) { throw new Exception(); } else { return(new GameState(action, CurrentPlayer.Opposite, nextState)); } } }
public GameStateInternal(GameStateInternal state) { Ko = FieldCoordinates.Pass; CapturedStones = state.CapturedStones; BoardSize = state.BoardSize; BoardFields = new FieldState[BoardSize, BoardSize]; Array.Copy(state.BoardFields, BoardFields, state.BoardFields.Length); }
public static FieldCoordinates Get(uint x, uint y) { FieldCoordinates result; uint hashCode = x * 397 + y; if (Cache.TryGetValue(hashCode, out result) == false) { result = new FieldCoordinates(x, y); Cache.Add(hashCode, result); } return(result); }
public string ToString(Stone stone) { Dictionary <FieldState, string> labels = new Dictionary <FieldState, string>(); labels[FieldState.Black] = "⬤"; labels[FieldState.White] = "⭕"; labels[FieldState.Empty] = "·"; labels[FieldState.MarkedEmpty] = "⸭"; labels[FieldState.MarkedStone] = "⊙"; labels[FieldState.Black] = "X"; labels[FieldState.White] = "O"; labels[FieldState.Empty] = "·"; labels[FieldState.MarkedEmpty] = "⸭"; labels[FieldState.MarkedStone] = "⊙"; //string illegal = "❌"; string illegal = "i"; StringBuilder builder = new StringBuilder(); Func <FieldState, string> fieldToText = state => labels[state]; for (uint y = 0; y < InternalState.BoardSize; y++) { for (uint x = 0; x < InternalState.BoardSize; x++) { if (InternalState.BoardFields[x, y] == FieldState.Empty) { var allowedActions = GetAllowedActions(); if (allowedActions.Contains(FieldCoordinates.Get(x, y))) { builder.Append(fieldToText(InternalState.BoardFields[x, y])); } else { builder.Append(illegal); } } else { builder.Append(fieldToText(InternalState.BoardFields[x, y])); } } builder.AppendLine(); } return(builder.ToString()); }
public GameStateInternal Play(uint x, uint y, Stone stone) { if (Inside(x, y)) { if (BoardFields[x, y] == FieldState.Empty) { GameStateInternal result = new GameStateInternal(this); uint capturedW = result.RemoveGroupIfHasOneBreath(x - 1, y, stone.Opposite.Color.State); uint capturedE = result.RemoveGroupIfHasOneBreath(x + 1, y, stone.Opposite.Color.State); uint capturedN = result.RemoveGroupIfHasOneBreath(x, y - 1, stone.Opposite.Color.State); uint capturedS = result.RemoveGroupIfHasOneBreath(x, y + 1, stone.Opposite.Color.State); uint capturedStones = capturedN + capturedS + capturedE + capturedW; result.BoardFields[x, y] = stone.Color.State; int breaths = result.MarkGroup(x, y, stone.Color.State); if (breaths > 0) { result.ClearMarkedFields(stone.Color.State, FieldState.Empty); result.CapturedStones.Add(stone, capturedStones); if (capturedStones == 1 && breaths == 1) { if (capturedN > 0) { result.Ko = FieldCoordinates.Get(x, y - 1); } else if (capturedS > 0) { result.Ko = FieldCoordinates.Get(x, y + 1); } else if (capturedE > 0) { result.Ko = FieldCoordinates.Get(x + 1, y); } else if (capturedW > 0) { result.Ko = FieldCoordinates.Get(x - 1, y); } } return(result); } } } return(null); }
//Dictionary<FieldCoordinates, GameState> allowedActions; public IEnumerable <FieldCoordinates> GetAllowedActions() { //if (allowedActions == null) //{ //Dictionary<FieldCoordinates, GameState> allowedActions = new Dictionary<FieldCoordinates, GameState>(); if (IsFinal == false) { for (byte y = 0; y < InternalState.BoardSize; y++) { for (byte x = 0; x < InternalState.BoardSize; x++) { FieldCoordinates field = FieldCoordinates.Get(x, y); if (field != InternalState.Ko) { GameStateInternal nextState = InternalState.Play(field.X, field.Y, CurrentPlayer); if (nextState != null) { yield return(field); //allowedActions.Add(field, new GameState(field, CurrentPlayer.Opposite, nextState)); } } } } yield return(FieldCoordinates.Pass); //if (PreviousMove == FieldCoordinates.Pass) //{ // allowedActions.Add(FieldCoordinates.Pass, new GameState(CurrentPlayer.Opposite, InternalState.Pass())); //} //else //{ // allowedActions.Add(FieldCoordinates.Pass, new GameState(FieldCoordinates.Pass, CurrentPlayer.Opposite, InternalState.Pass())); //} } //} //return allowedActions.Keys; }
private GameState(FieldCoordinates previousMove, Stone currentPlayer, GameStateInternal internalState) { PreviousMove = previousMove; CurrentPlayer = currentPlayer; InternalState = internalState; }
//public static bool operator ==(FieldCoordinates c, FieldCoordinates d) //{ // return c.X == d.X && c.Y == d.Y; //} // //public static bool operator !=(FieldCoordinates c, FieldCoordinates d) //{ // return c.X != d.X || c.Y != d.Y; //} public bool Equals(FieldCoordinates other) { return(other != null && X == other.X && Y == other.Y); }
public GameStateInternal(uint boardSize) { Ko = FieldCoordinates.Pass; BoardSize = boardSize; BoardFields = new FieldState[BoardSize, BoardSize]; }