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) }; GameSolverBreadthFirst solver = new GameSolverBreadthFirst(_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]); } }
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 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) }; GameSolverBreadthFirst solver = new GameSolverBreadthFirst(_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 MovingTwoRobotsEightDeflectors_16x16Board() { //Board: MovingTwoRobotsEightDeflectors_16x16Board.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, 9); _robotGreen = _model.CreateRobot(14, 10); _robotYellow = _model.CreateRobot(7, 2); _robotBlue = _model.CreateRobot(15, 15); //Destination Cells _model.CreateWinningDestination(1, 4, _robotGreen.Id, false, Direction.Down, Direction.Right); _model.CreateWinningDestination(1, 14, _robotBlue.Id, false, Direction.Down, Direction.Right); _model.CreateWinningDestination(2, 6, _robotGreen.Id, false, Direction.Left, Direction.Down); _model.CreateWinningDestination(2, 9, _robotGreen.Id, false, Direction.Up, Direction.Left); _model.CreateWinningDestination(4, 9, _robotGreen.Id, false, Direction.Up, Direction.Right); _model.CreateWinningDestination(5, 6, _robotGreen.Id, false, Direction.Left, Direction.Up); _model.CreateWinningDestination(5, 11, _robotGreen.Id, false, Direction.Left, Direction.Down); _model.CreateWinningDestination(7, 2, _robotGreen.Id, false, Direction.Up, Direction.Right); _model.CreateWinningDestination(9, 1, _robotGreen.Id, false, Direction.Down, Direction.Right); _model.CreateWinningDestination(9, 11, _robotGreen.Id, false, Direction.Up, Direction.Right); _model.CreateWinningDestination(9, 13, _robotYellow.Id, true, Direction.Down, Direction.Right); _model.CreateWinningDestination(10, 4, _robotGreen.Id, false, Direction.Up, Direction.Left); _model.CreateWinningDestination(11, 1, _robotGreen.Id, false, Direction.Down, Direction.Left); _model.CreateWinningDestination(12, 6, _robotGreen.Id, false, Direction.Up, Direction.Right); _model.CreateWinningDestination(14, 8, _robotGreen.Id, false, Direction.Left, Direction.Down); _model.CreateWinningDestination(14, 10, _robotGreen.Id, false, Direction.Up, Direction.Left); //Edges _model.CreateCellWall(_model.Board[0, 1], Direction.Right); _model.CreateCellWall(_model.Board[0, 9], Direction.Right); _model.CreateCellWall(_model.Board[15, 3], Direction.Right); _model.CreateCellWall(_model.Board[15, 11], 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[12, 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); _model.Board[4, 3].Deflector = new Deflector(_robot.Id, DeflectorType.Backward); _model.Board[4, 13].Deflector = new Deflector(_robot.Id, DeflectorType.Backward); _model.Board[6, 5].Deflector = new Deflector(_robotYellow.Id, DeflectorType.Forward); _model.Board[6, 10].Deflector = new Deflector(_robotGreen.Id, DeflectorType.Forward); _model.Board[8, 12].Deflector = new Deflector(_robotGreen.Id, DeflectorType.Backward); _model.Board[10, 6].Deflector = new Deflector(_robotBlue.Id, DeflectorType.Backward); _model.Board[10, 10].Deflector = new Deflector(_robotYellow.Id, DeflectorType.Forward); _model.Board[13, 3].Deflector = new Deflector(_robotBlue.Id, DeflectorType.Forward); List <RobotMove> movesToWin = new List <RobotMove>() { new RobotMove(_robotBlue.Id, _model.Board[15, 15], _model.Board[15, 12], Direction.Left), new RobotMove(_robotBlue.Id, _model.Board[15, 12], _model.Board[8, 9], Direction.Up), new RobotMove(_robotBlue.Id, _model.Board[8, 9], _model.Board[5, 9], Direction.Up), new RobotMove(_robotYellow.Id, _model.Board[7, 2], _model.Board[15, 2], Direction.Down), new RobotMove(_robotYellow.Id, _model.Board[15, 2], _model.Board[15, 3], Direction.Right), new RobotMove(_robotYellow.Id, _model.Board[15, 3], _model.Board[13, 15], Direction.Up), new RobotMove(_robotYellow.Id, _model.Board[13, 15], _model.Board[15, 15], Direction.Down), new RobotMove(_robotYellow.Id, _model.Board[15, 15], _model.Board[15, 12], Direction.Left), new RobotMove(_robotYellow.Id, _model.Board[15, 12], _model.Board[8, 9], Direction.Up), new RobotMove(_robotYellow.Id, _model.Board[8, 9], _model.Board[6, 9], Direction.Up), new RobotMove(_robotYellow.Id, _model.Board[6, 9], _model.Board[10, 0], Direction.Right), new RobotMove(_robotBlue.Id, _model.Board[5, 9], _model.Board[5, 10], Direction.Right), new RobotMove(_robotYellow.Id, _model.Board[10, 0], _model.Board[4, 10], Direction.Down), new RobotMove(_robotYellow.Id, _model.Board[4, 10], _model.Board[9, 13], Direction.Right) }; System.Diagnostics.Debugger.Launch(); GameSolverBreadthFirst solver = new GameSolverBreadthFirst(_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 MovingThreeRobots_16x16Board() { //Board: MovingThreeRobots_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(13, 1); _robotGreen = _model.CreateRobot(12, 2); _robotYellow = _model.CreateRobot(4, 2); _robotBlue = _model.CreateRobot(14, 2); //Destination Cells _model.CreateWinningDestination(1, 9, _robotGreen.Id, false, Direction.Up, Direction.Right); _model.CreateWinningDestination(2, 3, _robotBlue.Id, true, Direction.Right, Direction.Down); _model.CreateWinningDestination(2, 13, _robotGreen.Id, false, Direction.Left, Direction.Up); _model.CreateWinningDestination(3, 9, _robotGreen.Id, false, Direction.Right, Direction.Down); _model.CreateWinningDestination(4, 4, _robotGreen.Id, false, Direction.Right, Direction.Down); _model.CreateWinningDestination(4, 11, _robotGreen.Id, false, Direction.Left, Direction.Down); _model.CreateWinningDestination(6, 3, _robotGreen.Id, false, Direction.Left, Direction.Up); _model.CreateWinningDestination(6, 14, _robotGreen.Id, false, Direction.Up, Direction.Right); _model.CreateWinningDestination(8, 12, _robotGreen.Id, false, Direction.Up, Direction.Right); _model.CreateWinningDestination(9, 1, _robotGreen.Id, false, Direction.Left, Direction.Up); _model.CreateWinningDestination(9, 14, _robotGreen.Id, false, Direction.Left, Direction.Up); _model.CreateWinningDestination(10, 11, _robotGreen.Id, false, Direction.Down, Direction.Right); _model.CreateWinningDestination(11, 4, _robotGreen.Id, false, Direction.Up, Direction.Right); _model.CreateWinningDestination(11, 6, _robotGreen.Id, false, Direction.Left, Direction.Down); _model.CreateWinningDestination(12, 10, _robotGreen.Id, false, Direction.Right, Direction.Down); _model.CreateWinningDestination(13, 13, _robotGreen.Id, false, Direction.Right, Direction.Down); _model.CreateWinningDestination(14, 2, _robotGreen.Id, false, Direction.Left, Direction.Down); _model.CreateWinningDestination(14, 8, _robotGreen.Id, false, Direction.Up, Direction.Right); //Edges _model.CreateCellWall(_model.Board[0, 2], Direction.Right); _model.CreateCellWall(_model.Board[0, 11], Direction.Right); _model.CreateCellWall(_model.Board[15, 3], Direction.Right); _model.CreateCellWall(_model.Board[15, 11], Direction.Right); _model.CreateCellWall(_model.Board[3, 0], Direction.Down); _model.CreateCellWall(_model.Board[12, 0], Direction.Down); _model.CreateCellWall(_model.Board[2, 15], Direction.Down); _model.CreateCellWall(_model.Board[12, 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(_robotYellow.Id, _model.Board[4, 2], _model.Board[0, 2], Direction.Up), new RobotMove(_robotGreen.Id, _model.Board[12, 2], _model.Board[1, 2], Direction.Up), new RobotMove(_robotBlue.Id, _model.Board[14, 2], _model.Board[2, 2], Direction.Up), new RobotMove(_robotBlue.Id, _model.Board[2, 2], _model.Board[2, 3], Direction.Right) }; GameSolverBreadthFirst solver = new GameSolverBreadthFirst(_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]); } }