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); } }
void NextGame() { NextGameButtonVisibility = Visibility.Collapsed; GameMessage = ""; Title = "Diff " + Difficulty; WholeBoard = new PentagoBoard(); base.RaisePropertyChanged("WholeBoard"); var AI = AIFactory.NextAI(WholeBoard, Difficulty / 2); if (Difficulty % 2 == 1) { Control = new GameControl(WholeBoard, AI, null); Control.GameOver = postGameProcess; Control.RotatabilityChange = new GameControl.RotatabilityChangeHandler(RotateAction); Control.Start(); } else { Title += " - You go first"; Control = new GameControl(WholeBoard, null, AI); Control.GameOver = postGameProcess; Control.RotatabilityChange = new GameControl.RotatabilityChangeHandler(RotateAction); Control.Start(); } }
public GameControl(PentagoBoard board, AbstractPlayer player1, AbstractPlayer player2) { this.board = board; PlayerBlack = player2; PlayerWhite = player1; if (PlayerBlack != null) { PlayerBlack.color = Status.StateEnum.black; PlayerBlack.Place += new AbstractPlayer.PlacingDelegate(Place); PlayerBlack.RotateClockwise += new AbstractPlayer.RotateDelegate(RotateClockwise); PlayerBlack.RotateCounterclockwise += new AbstractPlayer.RotateDelegate(RotateCounterclockwise); } if (PlayerWhite != null) { PlayerWhite.color = Status.StateEnum.white; PlayerWhite.Place += new AbstractPlayer.PlacingDelegate(Place); PlayerWhite.RotateClockwise += new AbstractPlayer.RotateDelegate(RotateClockwise); PlayerWhite.RotateCounterclockwise += new AbstractPlayer.RotateDelegate(RotateCounterclockwise); } foreach (var section in board.Sections) { section.StartRotating += new EventHandler(section_StartRotating); section.BoardRotate += new RotatableBoard <Status> .RotateEvent(HumanRotate); } Placeable = false; }
public static AIPlayerEx NextAI(PentagoBoard board, int para, bool localSearch, int rndSel) { var result = new AIPlayerEx(board, para) { Evaluater = EvaluateNoRotation, }; if (localSearch) { result.Decide = MinimaxLocal; } else { result.Decide = Minimax; } switch (rndSel) { case 0: result.Selecter = NormallySelect; break; case 1: result.Selecter = StochasticallySelect; break; case 2: result.Selecter = StochasticallySelect2; break; } return(result); }
public virtual PentagoNode FindOptimalNode2(PentagoBoard board, int depth) { var clone = board.Clone() as PentagoBoard; var root = new PentagoNode(clone, base.color, depth); root.Evaluate(this.color); return(root.BestChild); }
void HumanVsRandomPlayer() { WholeBoard = new PentagoBoard(); base.RaisePropertyChanged("WholeBoard"); Control = new GameControl(this.WholeBoard, null, new RandomAIPlayer(WholeBoard)); Control.GameOver = new GameControl.GameOverHandler(GameOver); Control.RotatabilityChange = new GameControl.RotatabilityChangeHandler(RotateAction); Control.Start(); }
void ComputerVsHuman() { WholeBoard = new PentagoBoard(); base.RaisePropertyChanged("WholeBoard"); Control = new GameControl(this.WholeBoard, null, AIFactory.NextAI(WholeBoard, 2, false, 1)); Control.GameOver = new GameControl.GameOverHandler(GameOver); Control.RotatabilityChange = new GameControl.RotatabilityChangeHandler(RotateAction); Control.Start(); }
public PentagoNode(PentagoBoard currentState, PentagoNode parent) : base(parent.Depth + 1) { this.currentState = currentState; this.currentTurn = parent.NextTurn(); this.Parent = parent; this.maxDepth = parent.maxDepth; this.nodeType = (parent.nodeType == NodeTypeEnum.MAX) ? NodeTypeEnum.MIN : NodeTypeEnum.MAX; //System.Diagnostics.Debug.WriteLine(Depth+" "+currentTurn.State); }
/// <summary> /// constructor for root node /// </summary> /// <param name="currentState">current board</param> /// <param name="currentTurn">current turn(who is moving now)</param> public PentagoNodeEx(PentagoBoard currentState, Status currentTurn, int maxDepth) : base(0) { this.CurrentState = currentState; this.currentTurn = currentTurn; this.currentTurn = this.NextTurn(); this.Parent = null; this.maxDepth = maxDepth; this.nodeType = NodeTypeEnum.MAX; System.Diagnostics.Debug.WriteLine(Depth); }
public void RollBack(PentagoBoard board) { if (!Clockwise) { board.Sections[SectionNo].StartRotatingClockwise(); } else { board.Sections[SectionNo].StartRotatingCounterclockwise(); } }
void ComputerVsComputer() { WholeBoard = new PentagoBoard(); //WholeBoard[1, 2].State = Status.StateEnum.black; //WholeBoard[1, 1].State = Status.StateEnum.black; //WholeBoard[1, 4].State = Status.StateEnum.black; //WholeBoard[4, 1].State = Status.StateEnum.white; //WholeBoard[4, 4].State = Status.StateEnum.white; base.RaisePropertyChanged("WholeBoard"); Control = new GameControl(this.WholeBoard, AIFactory.NextAI(WholeBoard, 2, false, 0), new AIPlayer(WholeBoard, 2)); Control.GameOver = new GameControl.GameOverHandler(GameOver); Control.RotatabilityChange = new GameControl.RotatabilityChangeHandler(RotateAction); Control.Start(); }
public PentagoBoard(PentagoBoard orig) : base(6, 6) { for (int i = 0; i < _height; i++) { for (int j = 0; j < _width; j++) { board [i, j] = orig.board[i, j]; } } activeplayer = orig.activeplayer; turn = orig.turn; terminal = orig.terminal; winchecked = orig.winchecked; movesmade = orig.movesmade; }
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); }
void HumanVsHuman() { WholeBoard = new PentagoBoard(); //WholeBoard[1, 1].State = Status.StateEnum.black; //WholeBoard[1, 2].State = Status.StateEnum.black; //WholeBoard[1, 4].State = Status.StateEnum.black; //WholeBoard[4, 1].State = Status.StateEnum.white; //WholeBoard[4, 3].State = Status.StateEnum.white; //WholeBoard[4, 4].State = Status.StateEnum.white; base.RaisePropertyChanged("WholeBoard"); Control = new GameControl(this.WholeBoard, null, null); //Control = new GameControl(this.WholeBoard, null, new AIPlayerCut(WholeBoard, 1)); Control.GameOver = new GameControl.GameOverHandler(GameOver); Control.RotatabilityChange = new GameControl.RotatabilityChangeHandler(RotateAction); Control.Start(); }
public static AIPlayerEx NextAI(PentagoBoard board, int difficulty) { switch (difficulty) { case 0: return(AIFactory.NextAI(board, 2, true, 2)); case 1: return(AIFactory.NextAI(board, 2, true, 1)); case 2: return(AIFactory.NextAI(board, 2, false, 1)); case 3: return(AIFactory.NextAI(board, 2, true, 0)); default: return(AIFactory.NextAI(board, 2, false, 0)); } }
public AIPlayerCut(PentagoBoard board, int depth) : base(board, depth) { }
public AbstractPlayer(PentagoBoard board) { this.board = board; }
public AIPlayerEx(PentagoBoard board, int depth) : base(board) { this.depth = depth; }
public PentagoView(PentagoBoard myboard, int centerx = 300, int centery = 300, int targetwidth = 480) { _myboard = myboard; // prepare graphics: cell = new AnimationSprite[myboard._height, myboard._width]; for (int row = 0; row < _myboard._height; row++) { for (int col = 0; col < _myboard._width; col++) { AnimationSprite newcell = new AnimationSprite("../../assets/tileset.png", 3, 1); newcell.x = col * newcell.width; newcell.y = row * newcell.width; AddChild(newcell); cell [row, col] = newcell; } } float stdwidth = cell[0, 0].width; arrow = new AnimationSprite[8]; for (int i = 0; i < 8; i++) { arrow [i] = new AnimationSprite("../../assets/arrow-sprite.png", 2, 2); AddChild(arrow [i]); arrow [i].SetScaleXY(stdwidth / arrow[i].width, stdwidth / arrow[i].width); arrow [i].color = 0xff000000; } /* * 0 TL CW * 1 BL CW * 2 TR CW * 3 BR CW * 4 TL ACW * 5 BL ACW * 6 TR ACW * 7 BR ACW */ arrow [0].x = stdwidth * 1; arrow [0].y = stdwidth * -1; arrow [0].SetFrame(1); arrow [1].x = stdwidth * -1; arrow [1].y = stdwidth * 4; arrow [1].SetFrame(2); arrow [2].x = stdwidth * 6; arrow [2].y = stdwidth * 1; arrow [2].SetFrame(3); arrow [3].x = stdwidth * 4; arrow [3].y = stdwidth * 6; arrow [3].SetFrame(0); arrow [4].x = stdwidth * -1; arrow [4].y = stdwidth * 1; arrow [4].SetFrame(3); arrow [5].x = stdwidth * 1; arrow [5].y = stdwidth * 6; arrow [5].SetFrame(1); arrow [6].x = stdwidth * 4; arrow [6].y = stdwidth * -1; arrow [6].SetFrame(0); arrow [7].x = stdwidth * 6; arrow [7].y = stdwidth * 4; arrow [7].SetFrame(2); float realwidth = stdwidth * 8; scaleX = targetwidth / realwidth; scaleY = targetwidth / realwidth; x = centerx - targetwidth / 2 + scaleX * stdwidth; y = centery - targetwidth / 2 + scaleX * stdwidth; // set callbacks: _myboard.OnCellChange += CellChangeHandler; _myboard.OnWin += WinHandler; wincells = new List <AnimationSprite> (); }
public RandomAIPlayer(PentagoBoard board) : base(board) { }
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); }
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 void RollBack(PentagoBoard board) { board[X, Y].State = Status.StateEnum.empty; }
public void Do(PentagoBoard board) { board[X, Y].State = Player; }
public void Do(PentagoBoard board) { Plc.Do(board); Rtt.Do(board); }
public void RollBack(PentagoBoard board) { Rtt.RollBack(board); Plc.RollBack(board); }
/// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel() { if (IsInDesignMode) { // Code runs in Blend --> create design time data. } else { // Code runs "for real" } RotateButtonVisibility = Visibility.Collapsed; WholeBoard = new PentagoBoard(); //WholeBoard[0, 0] = new Status(Status.StateEnum.black); //WholeBoard[1, 3] = new Status(Status.StateEnum.black); //WholeBoard[4, 5] = new Status(Status.StateEnum.black); //WholeBoard[5, 2] = new Status(Status.StateEnum.white); //WholeBoard[3, 0] = new Status(Status.StateEnum.white); //control = new GameControl(this.WholeBoard, null ,new AIPlayer(WholeBoard,Status.StateEnum.black)); //control = new GameControl(this.WholeBoard, null, null); CmdStartHumanVsHuman = new RelayCommand(HumanVsHuman); CmdStartComputerVsHuman = new RelayCommand(ComputerVsHuman); CmdStartComputerVsComputer = new RelayCommand(ComputerVsComputer); CmdStartChallenge = new RelayCommand(StartChallenge); CmdStartTestLevel = new RelayCommand(this.StartTestLevels); CmdNextGame = new RelayCommand(NextGame); CmdVsRandom = new RelayCommand(HumanVsRandomPlayer); CmdCloseMessage = new RelayCommand(OnClosingMessage); HumanVsHuman(); if (IsInDesignMode) { // Code runs in Blend --> create design time data. WholeBoard[1, 0].State = Status.StateEnum.white; WholeBoard[2, 0].State = Status.StateEnum.white; WholeBoard[0, 2].State = Status.StateEnum.white; WholeBoard[5, 1].State = Status.StateEnum.white; WholeBoard[4, 2].State = Status.StateEnum.white; WholeBoard[1, 3].State = Status.StateEnum.white; WholeBoard[0, 4].State = Status.StateEnum.white; WholeBoard[2, 4].State = Status.StateEnum.white; WholeBoard[3, 5].State = Status.StateEnum.white; WholeBoard[0, 0].State = Status.StateEnum.black; WholeBoard[1, 1].State = Status.StateEnum.black; WholeBoard[2, 1].State = Status.StateEnum.black; WholeBoard[2, 2].State = Status.StateEnum.black; WholeBoard[3, 1].State = Status.StateEnum.black; WholeBoard[4, 1].State = Status.StateEnum.black; WholeBoard[0, 5].State = Status.StateEnum.black; WholeBoard[1, 4].State = Status.StateEnum.black; WholeBoard[2, 3].State = Status.StateEnum.black; WholeBoard[4, 4].State = Status.StateEnum.black; } }
public AbstractPlayer Produce(PentagoBoard board, int level) { return(AIFactory.NextAI(board, level)); }