private void btnSolve_Click(object sender, EventArgs e) { txtSolutionPath.Clear(); foreach (Cell c in _model.Board) { c.RobotPath = 0; } txtSolutionPath.Font = new Font(txtSolutionPath.Font.FontFamily, (float)42.0); List <RobotMove> movesToWin = new GameSolverBreadthFirst(_model).FindSolution(); if (movesToWin == null || movesToWin.Count == 0) { txtSolvedNumberOfMoves.Text = ">12"; movesToWin = new GameSolverDepthFirst(_model).FindSolution(); if (movesToWin == null || movesToWin.Count == 0) { txtSolvedNumberOfMoves.Text = "???"; } return; } txtSolvedNumberOfMoves.Text = movesToWin.Count.ToString(); foreach (RobotMove move in movesToWin) { if (move.Direction == Direction.Up) { txtSolutionPath.AppendText("⇦", _settings.RobotColors[move.RobotId]); } if (move.Direction == Direction.Down) { txtSolutionPath.AppendText("⇨", _settings.RobotColors[move.RobotId]); } if (move.Direction == Direction.Right) { txtSolutionPath.AppendText("⇩", _settings.RobotColors[move.RobotId]); } if (move.Direction == Direction.Left) { txtSolutionPath.AppendText("⇧", _settings.RobotColors[move.RobotId]); } } _model.AutoSetRobotPath = true; foreach (RobotMove move in movesToWin) { _model.MoveRobot(move.RobotId, move.Direction); } _model.AutoSetRobotPath = false; foreach (RobotMove move in movesToWin) { _model.UndoMove(); } cbShowpath.Checked = _settings.ShowSolvedPath = true; Refresh(); }
public void AllDirections_OneRobot() { //★ /* a a|a _ a * _ b b *|b * c c c c _ * d d d d d * e e|e e R */ _model = new Engine(); _model.Board = new Cell[5, 5]; int id = 0; for (int x = 0; x <= _model.Board.GetLength(0) - 1; x++) { for (int y = 0; y <= _model.Board.GetLength(1) - 1; y++) { _model.Board[x, y] = new Cell(id++, CellWalls.None, x, y); } } _robot = _model.CreateRobot(4, 4); _model.CreateWinningDestination(1, 3, _robot.Id, true, Direction.Up, Direction.Right); _model.CreateWinningDestination(0, 0, _robot.Id, false); _model.CreateCellWall(_model.Board[0, 1], Direction.Right); _model.CreateCellWall(_model.Board[1, 0], Direction.Down); _model.CreateCellWall(_model.Board[2, 4], Direction.Down); _model.CreateCellWall(_model.Board[4, 1], Direction.Right); List <RobotMove> movesToWin = new List <RobotMove>() { new RobotMove(_robot.Id, _model.Board[4, 4], _model.Board[3, 4], Direction.Up), new RobotMove(_robot.Id, _model.Board[3, 4], _model.Board[3, 0], Direction.Left), new RobotMove(_robot.Id, _model.Board[3, 0], _model.Board[4, 0], Direction.Down), new RobotMove(_robot.Id, _model.Board[4, 0], _model.Board[4, 1], Direction.Right), new RobotMove(_robot.Id, _model.Board[4, 1], _model.Board[0, 1], Direction.Up), new RobotMove(_robot.Id, _model.Board[0, 1], _model.Board[0, 0], Direction.Left), new RobotMove(_robot.Id, _model.Board[0, 0], _model.Board[1, 0], Direction.Down), new RobotMove(_robot.Id, _model.Board[1, 0], _model.Board[1, 3], Direction.Right) }; GameSolverDepthFirst solver = new GameSolverDepthFirst(_model); List <RobotMove> moves = solver.FindSolution(); if (moves.Count < movesToWin.Count) { Assert.IsTrue(false); return; } for (int i = 0; i < movesToWin.Count; i++) { Assert.AreEqual(movesToWin[i], moves[i]); } }
public void OneMoveSolution_Right() { /* a a a * b b b * c R ★ */ _model = new Engine(); _model.Board = new Cell[3, 3]; int id = 0; for (int x = 0; x <= _model.Board.GetLength(0) - 1; x++) { for (int y = 0; y <= _model.Board.GetLength(1) - 1; y++) { _model.Board[x, y] = new Cell(id++, CellWalls.None, x, y); } } _robot = _model.CreateRobot(2, 1); _model.CreateWinningDestination(2, 2, _robot.Id, true); _model.CreateWinningDestination(0, 0, _robot.Id, false); List <RobotMove> movesToWin = new List <RobotMove>() { new RobotMove(_robot.Id, _model.Board[2, 1], _model.Board[2, 2], Direction.Right) }; GameSolverDepthFirst solver = new GameSolverDepthFirst(_model); List <RobotMove> moves = solver.FindSolution(); if (moves.Count < movesToWin.Count) { Assert.IsTrue(false); return; } for (int i = 0; i < movesToWin.Count; i++) { Assert.AreEqual(movesToWin[i], moves[i]); } }
public void MovingOneRobot_16x16Board() { //Board: MovingOneRobot_16x16.png _model = new Engine(); _model.Board = new Cell[16, 16]; int id = 0; for (int x = 0; x <= _model.Board.GetLength(0) - 1; x++) { for (int y = 0; y <= _model.Board.GetLength(1) - 1; y++) { _model.Board[x, y] = new Cell(id++, CellWalls.None, x, y); } } _robot = _model.CreateRobot(4, 2); _robotGreen = _model.CreateRobot(10, 2); _robotYellow = _model.CreateRobot(14, 1); _robotBlue = _model.CreateRobot(5, 15); //Destination Cells _model.CreateWinningDestination(1, 14, _robotGreen.Id, true, Direction.Left, Direction.Up); _model.CreateWinningDestination(2, 1, _robotGreen.Id, false, Direction.Left, Direction.Up); _model.CreateWinningDestination(2, 11, _robotGreen.Id, false, Direction.Left, Direction.Up); _model.CreateWinningDestination(4, 9, _robotGreen.Id, false, Direction.Left, Direction.Up); _model.CreateWinningDestination(5, 4, _robotGreen.Id, false, Direction.Left, Direction.Up); _model.CreateWinningDestination(5, 6, _robotGreen.Id, false, Direction.Left, Direction.Down); _model.CreateWinningDestination(6, 10, _robotGreen.Id, false, Direction.Left, Direction.Up); _model.CreateWinningDestination(6, 13, _robotGreen.Id, false, Direction.Left, Direction.Up); _model.CreateWinningDestination(7, 4, _robotGreen.Id, false, Direction.Right, Direction.Down); _model.CreateWinningDestination(9, 13, _robotGreen.Id, false, Direction.Left, Direction.Up); _model.CreateWinningDestination(10, 5, _robotGreen.Id, false, Direction.Up, Direction.Right); _model.CreateWinningDestination(10, 11, _robotGreen.Id, false, Direction.Up, Direction.Right); _model.CreateWinningDestination(11, 7, _robotGreen.Id, false, Direction.Right, Direction.Down); _model.CreateWinningDestination(11, 9, _robotGreen.Id, false, Direction.Left, Direction.Up); _model.CreateWinningDestination(12, 5, _robotGreen.Id, false, Direction.Left, Direction.Down); _model.CreateWinningDestination(13, 2, _robotGreen.Id, false, Direction.Left, Direction.Down); _model.CreateWinningDestination(14, 6, _robotGreen.Id, false, Direction.Left, Direction.Up); _model.CreateWinningDestination(14, 14, _robotGreen.Id, false, Direction.Right, Direction.Down); //Edges _model.CreateCellWall(_model.Board[0, 3], Direction.Right); _model.CreateCellWall(_model.Board[0, 11], Direction.Right); _model.CreateCellWall(_model.Board[15, 3], Direction.Right); _model.CreateCellWall(_model.Board[15, 10], Direction.Right); _model.CreateCellWall(_model.Board[3, 0], Direction.Down); _model.CreateCellWall(_model.Board[12, 0], Direction.Down); _model.CreateCellWall(_model.Board[4, 15], Direction.Down); _model.CreateCellWall(_model.Board[10, 15], Direction.Down); //Middle 2x2 _model.CreateCellWall(_model.Board[7, 7], Direction.Up, Direction.Right, Direction.Down, Direction.Left); _model.CreateCellWall(_model.Board[7, 8], Direction.Up, Direction.Right, Direction.Down, Direction.Left); _model.CreateCellWall(_model.Board[8, 7], Direction.Up, Direction.Right, Direction.Down, Direction.Left); _model.CreateCellWall(_model.Board[8, 8], Direction.Up, Direction.Right, Direction.Down, Direction.Left); List <RobotMove> movesToWin = new List <RobotMove>() { new RobotMove(_robot.Id, _model.Board[4, 2], _model.Board[0, 2], Direction.Up), new RobotMove(_robotGreen.Id, _model.Board[10, 2], _model.Board[1, 2], Direction.Up), new RobotMove(_robotGreen.Id, _model.Board[1, 2], _model.Board[1, 13], Direction.Right), new RobotMove(_robotGreen.Id, _model.Board[1, 13], _model.Board[5, 13], Direction.Down), new RobotMove(_robotGreen.Id, _model.Board[5, 13], _model.Board[5, 14], Direction.Right), new RobotMove(_robotGreen.Id, _model.Board[5, 14], _model.Board[1, 14], Direction.Up) }; GameSolverDepthFirst solver = new GameSolverDepthFirst(_model); List <RobotMove> moves = solver.FindSolution(); //System.Diagnostics.Debugger.Launch(); if (moves.Count < movesToWin.Count) { Assert.IsTrue(false); return; } for (int i = 0; i < movesToWin.Count; i++) { Assert.AreEqual(movesToWin[i], moves[i]); } }