void Turn() { bool blackWin = board.HasConsecutiveNPiece(Status.StateEnum.black, 5); bool whiteWin = board.HasConsecutiveNPiece(Status.StateEnum.white, 5); if (blackWin && whiteWin) { OnGameOver(Status.StateEnum.empty); } else if (blackWin) { OnGameOver(Status.StateEnum.black); } else if (whiteWin) { OnGameOver(Status.StateEnum.white); } else if (!board.Content.Any(e => e.State == Status.StateEnum.empty)) { OnGameOver(Status.StateEnum.empty); } else if (curPlayer == Status.StateEnum.black) { curPlayer = Status.StateEnum.white; PlayerWhite.Active(); } else { curPlayer = Status.StateEnum.black; PlayerBlack.Active(); } }
public void Next() { for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { board = new PentagoBoard(); PlayerBlack = Produce(board, i); PlayerWhite = Produce(board, j); PlayerBlack.color = Status.StateEnum.black; PlayerBlack.Place += new AbstractPlayer.PlacingDelegate(Place); PlayerBlack.RotateClockwise += new AbstractPlayer.RotateDelegate(RotateClockwise); PlayerBlack.RotateCounterclockwise += new AbstractPlayer.RotateDelegate(RotateCounterclockwise); PlayerWhite.color = Status.StateEnum.white; PlayerWhite.Place += new AbstractPlayer.PlacingDelegate(Place); PlayerWhite.RotateClockwise += new AbstractPlayer.RotateDelegate(RotateClockwise); PlayerWhite.RotateCounterclockwise += new AbstractPlayer.RotateDelegate(RotateCounterclockwise); curPlayer = Status.StateEnum.white; PlayerWhite.Active(); } } for (i = 0; i < N; i++) { string line = ""; for (j = 0; j < N; j++) { line += result[i, j]; } Debug.WriteLine(line); } }
public static Status.StateEnum GetOppoColor(Status.StateEnum color) { if (color == Status.StateEnum.black) { return(Status.StateEnum.white); } else { return(Status.StateEnum.black); } }
/// <summary> /// /// </summary> /// <param name="value"></param> /// <param name="targetType"></param> /// <param name="parameter"></param> /// <param name="culture"></param> /// <returns></returns> public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Status.StateEnum type = (Status.StateEnum)value; if (type == Status.StateEnum.black) { return(new BitmapImage(new Uri("/Pentago;component/resource/black.png", UriKind.Relative))); } else if (type == Status.StateEnum.white) { return(new BitmapImage(new Uri("/Pentago;component/resource/white.png", UriKind.Relative))); } return(null); }
public static int EvaluateNoRotation(PentagoBoard currentState, Status.StateEnum player) { int totalValue = 0; int[,] piecesInLine = new int[32, 2]; var posTable = PentagoEvaData.LineTable; var Values = PentagoEvaData.Values; for (int i = 0; i < currentState.TotalWidth; i++) { for (int j = 0; j < currentState.TotalHeight; j++) { if (currentState[i, j].State == player) { for (int k = 0; k < posTable[i, j].Length; k++) { piecesInLine[posTable[i, j][k] - 1, 0]++; } } else if (currentState[i, j].State != Status.StateEnum.empty) { for (int k = 0; k < posTable[i, j].Length; k++) { piecesInLine[posTable[i, j][k] - 1, 1]++; } } } } for (int i = 0; i < piecesInLine.GetLength(0); i++) { int ourPieces = piecesInLine[i, 0]; int theirPieces = piecesInLine[i, 1]; //if (ourPieces > 3 || theirPieces > 3) //Debug.WriteLine(i + ":" + ourPieces + "," + theirPieces); if (ourPieces == 0) { totalValue += -Values[theirPieces]; } else if (theirPieces == 0) { totalValue += Values[ourPieces]; } } return(totalValue); }
public static Action Minimax(PentagoBoard currentState, Status.StateEnum color, int depth, NodeSelecter sel, StateEvaluater eva) { Action bestAction = null; Placement curPlc = new Placement() { Player = color, }; Rotation curRtt = new Rotation(); for (int i = 0; i < currentState.TotalWidth; i++) { for (int j = 0; j < currentState.TotalHeight; j++) { if (currentState[i, j].State == Status.StateEnum.empty) { //curPlc = new Placement(); curPlc.X = i; curPlc.Y = j; curPlc.Do(currentState); for (int k = 0; k < currentState.Sections.Count; k++) { //curRtt = new Rotation(); curRtt.SectionNo = k; curRtt.Clockwise = true; curRtt.Do(currentState); if (depth <= 1 || currentState.IsFull(Status.StateEnum.empty) || currentState.HasConsecutiveNPiece(color, 5) || currentState.HasConsecutiveNPiece(GetOppoColor(color), 5)) { int value = eva(currentState, color); if (bestAction == null) { bestAction = new Action(curPlc, curRtt); bestAction.Value = value; } else if (sel(bestAction.Value, value) > 0) { bestAction.SetAction(curPlc, curRtt); bestAction.Value = value; } } else { var subAction = Minimax(currentState, GetOppoColor(color), depth - 1, sel, eva); if (bestAction == null) { bestAction = new Action(curPlc, curRtt); bestAction.Value = -subAction.Value; } else if (sel(bestAction.Value, -subAction.Value) > 0) { bestAction.SetAction(curPlc, curRtt); bestAction.Value = -subAction.Value; } } curRtt.RollBack(currentState); //if the board state remains the same after rotating clockwise, it will be the same after rotating counterclockwise //so the node can be eliminated if (!currentState.Sections[k].IsSameInBothDirection) { curRtt.Clockwise = false; curRtt.Do(currentState); if (depth == 1 || currentState.IsFull(Status.StateEnum.empty) || currentState.HasConsecutiveNPiece(color, 5) || currentState.HasConsecutiveNPiece(GetOppoColor(color), 5)) { int value = eva(currentState, color); if (bestAction == null) { bestAction = new Action(curPlc, curRtt); bestAction.Value = value; } else if (sel(bestAction.Value, value) > 0) { bestAction.SetAction(curPlc, curRtt); bestAction.Value = value; } } else { var subAction = Minimax(currentState, GetOppoColor(color), depth - 1, sel, eva); if (sel(bestAction.Value, -subAction.Value) > 0) { bestAction.SetAction(curPlc, curRtt); bestAction.Value = -subAction.Value; } } curRtt.RollBack(currentState); } } curPlc.RollBack(currentState); } } } return(bestAction); }
public static Action MinimaxLocal(PentagoBoard currentState, Status.StateEnum color, int depth, NodeSelecter sel, StateEvaluater eva) { Action bestAction = null; Placement curPlc = new Placement() { Player = color, }; Rotation curRtt = new Rotation(); var searchSpace = new bool[6, 6]; if (currentState.IsEmpty(Status.StateEnum.empty)) { for (int i = 0; i < currentState.TotalWidth; i++) { for (int j = 0; j < currentState.TotalHeight; j++) { searchSpace[i, j] = true; } } } else if (currentState.CountSteps(Status.StateEnum.empty) < 5) { for (int i = 0; i < currentState.TotalWidth; i++) { for (int j = 0; j < currentState.TotalHeight; j++) { if (currentState[i, j].State == GetOppoColor(color)) { for (int k = (i == 0) ? 0 : -1; k <= ((i == currentState.TotalWidth - 1) ? 0 : 1); k++) { for (int l = (j == 0) ? 0 : -1; l <= ((j == currentState.TotalHeight - 1) ? 0 : 1); l++) { if (!(k == 0 && l == 0) && !searchSpace[i + k, j + l] && currentState[i + k, j + l].State == Status.StateEnum.empty) { searchSpace[i + k, j + l] = true; } } } } } } } else { for (int i = 0; i < currentState.TotalWidth; i++) { for (int j = 0; j < currentState.TotalHeight; j++) { if (currentState[i, j].State != Status.StateEnum.empty) { for (int k = (i == 0) ? 0 : -1; k <= ((i == currentState.TotalWidth - 1) ? 0 : 1); k++) { for (int l = (j == 0) ? 0 : -1; l <= ((j == currentState.TotalHeight - 1) ? 0 : 1); l++) { if (!(k == 0 && l == 0) && !searchSpace[i + k, j + l] && currentState[i + k, j + l].State == Status.StateEnum.empty) { searchSpace[i + k, j + l] = true; } } } } } } } var nospace = searchSpace.Cast <bool>().All(e => !e); for (int i = 0; i < currentState.TotalWidth; i++) { for (int j = 0; j < currentState.TotalHeight; j++) { if (searchSpace[i, j]) { //curPlc = new Placement(); curPlc.X = i; curPlc.Y = j; curPlc.Do(currentState); for (int k = 0; k < currentState.Sections.Count; k++) { //curRtt = new Rotation(); curRtt.SectionNo = k; curRtt.Clockwise = true; curRtt.Do(currentState); if (depth <= 1 || currentState.IsFull(Status.StateEnum.empty) || currentState.HasConsecutiveNPiece(color, 5) || currentState.HasConsecutiveNPiece(GetOppoColor(color), 5)) { int value = eva(currentState, color); if (bestAction == null) { bestAction = new Action(curPlc, curRtt); bestAction.Value = value; } else if (sel(bestAction.Value, value) > 0) { bestAction.SetAction(curPlc, curRtt); bestAction.Value = value; } } else { var subAction = Minimax(currentState, GetOppoColor(color), depth - 1, sel, eva); if (bestAction == null) { bestAction = new Action(curPlc, curRtt); bestAction.Value = -subAction.Value; } else if (sel(bestAction.Value, -subAction.Value) > 0) { bestAction.SetAction(curPlc, curRtt); bestAction.Value = -subAction.Value; } } curRtt.RollBack(currentState); //if the board state remains the same after rotating clockwise, it will be the same after rotating counterclockwise //so the node can be eliminated if (!currentState.Sections[k].IsSameInBothDirection) { curRtt.Clockwise = false; curRtt.Do(currentState); if (depth == 1 || currentState.IsFull(Status.StateEnum.empty) || currentState.HasConsecutiveNPiece(color, 5) || currentState.HasConsecutiveNPiece(GetOppoColor(color), 5)) { int value = eva(currentState, color); if (bestAction == null) { bestAction = new Action(curPlc, curRtt); bestAction.Value = value; } else if (sel(bestAction.Value, value) > 0) { bestAction.SetAction(curPlc, curRtt); bestAction.Value = value; } } else { var subAction = Minimax(currentState, GetOppoColor(color), depth - 1, sel, eva); if (sel(bestAction.Value, -subAction.Value) > 0) { bestAction.SetAction(curPlc, curRtt); bestAction.Value = -subAction.Value; } } curRtt.RollBack(currentState); } } curPlc.RollBack(currentState); } } } return(bestAction); }
void OnGameOver(Status.StateEnum winner) { result[i, j] = winner == Status.StateEnum.empty ? "♢" : winner == Status.StateEnum.black ? "←" : "↑"; //Next(); }