public void SolveKnightTourProblem(int[][] canvas, KnightMove currentPosition)
        {
            canvas[currentPosition.Y][currentPosition.X] = _tryNumber;
            if (AllCellsAreVisited(canvas))
            {
                // SOLUTION FOUNDED
                // WILL START TO GO OUT FROM RECURSION
                return;
            }

            for (var i = 0; i < _knightMoves.Count; i++)
            {
                if (MoveIsValid(canvas, currentPosition, _knightMoves[i]))
                {
                    int newX = currentPosition.X + _knightMoves[i].X;
                    int newY = currentPosition.Y + _knightMoves[i].Y;
                    _tryNumber += 1;

                    SolveKnightTourProblem(canvas, new KnightMove {
                        X = newX, Y = newY
                    });
                }

                if (i == _knightMoves.Count - 1 && !AllCellsAreVisited(canvas))
                {
                    // backtracking
                    canvas[currentPosition.Y][currentPosition.X] = -1;
                    _tryNumber -= 1;
                }
            }
        }
        public void TestKnightMoveFromInsideBoard()
        {
            var pos    = new Position(3, 3);
            var knight = new KnightMove();

            var moves = knight.ValidMovesFor(pos).ToArray();

            Assert.IsNotNull(moves);
            Assert.AreEqual(8, moves.Length);

            foreach (var move in moves)
            {
                switch (Math.Abs(move.X - pos.X))
                {
                case 1:
                    Assert.AreEqual(2, Math.Abs(move.Y - pos.Y));
                    break;

                case 2:
                    Assert.AreEqual(1, Math.Abs(move.Y - pos.Y));
                    break;

                default:
                    Assert.Fail();
                    break;
                }
            }
        }
Beispiel #3
0
        public void Play(int moves)
        {
            // TODO: Play the game moves here
            var Queen      = new QueenMove();
            var Knight     = new KnightMove();
            var Bishop     = new BishopMove();
            var Queen_pos  = _startPosition;
            var Knight_pos = _startPosition;
            var Bishop_pos = _startPosition;

            Console.WriteLine("0: My Inint position is {0}", _startPosition);


            for (var move = 1; move <= moves; move++)
            {
                int i = _rnd.Next(1, 4); //Randomly obtained 1 to 3, corresponding to three pieces of random movement.
                if (i == 1)
                {
                    var possibleMoves = Queen.ValidMovesFor(Queen_pos).ToList();
                    if (possibleMoves.Contains(Knight_pos))
                    {
                        possibleMoves.Remove(Knight_pos);
                    }
                    if (possibleMoves.Contains(Bishop_pos))
                    {
                        possibleMoves.Remove(Bishop_pos);
                    }
                    Queen_pos = possibleMoves[_rnd.Next(possibleMoves.Count)];
                    Console.WriteLine("{1}: My Queen position is {0}", Queen_pos, move);
                }
                if (i == 2)
                {
                    var possibleMoves = Knight.ValidMovesFor(Knight_pos).ToList();
                    if (possibleMoves.Contains(Queen_pos))
                    {
                        possibleMoves.Remove(Queen_pos);
                    }
                    if (possibleMoves.Contains(Bishop_pos))
                    {
                        possibleMoves.Remove(Bishop_pos);
                    }
                    Knight_pos = possibleMoves[_rnd.Next(possibleMoves.Count)];
                    Console.WriteLine("{1}: My Knight position is {0}", Knight_pos, move);
                }
                if (i == 3)
                {
                    var possibleMoves = Bishop.ValidMovesFor(Bishop_pos).ToList();
                    if (possibleMoves.Contains(Queen_pos))
                    {
                        possibleMoves.Remove(Queen_pos);
                    }
                    if (possibleMoves.Contains(Knight_pos))
                    {
                        possibleMoves.Remove(Knight_pos);
                    }
                    Bishop_pos = possibleMoves[_rnd.Next(possibleMoves.Count)];
                    Console.WriteLine("{1}: My Bishop_pos position is {0}", Bishop_pos, move);
                }
            }
        }
Beispiel #4
0
        public void Play(int moves)
        {
            var knight    = new KnightMove();
            var knightPos = knightStartPosition;

            var bishop    = new BishopMove();
            var bishopPos = bishopStartPosition;

            var queen    = new QueenMove();
            var queenPos = queenStartPosition;

            Console.WriteLine("0: Knight position is {0}, Bishop position is {1}, Queen position is {2}", knightPos, bishopPos, queenPos);

            for (var move = 1; move <= moves; move++)
            {
                var knightPossibleMoves = knight.ValidMovesFor(knightPos).ToArray();
                knightPos = knightPossibleMoves[_rnd.Next(knightPossibleMoves.Length)];

                var bishopPossibleMoves = bishop.ValidMovesFor(bishopPos).ToArray();
                do
                {
                    bishopPos = bishopPossibleMoves[_rnd.Next(bishopPossibleMoves.Length)];
                } while (bishopPos.Equals(knightPos));

                var queenPossibleMoves = queen.ValidMovesFor(queenPos).ToArray();
                do
                {
                    queenPos = queenPossibleMoves[_rnd.Next(queenPossibleMoves.Length)];
                } while (queenPos.Equals(knightPos) || queenPos.Equals(bishopPos));

                Console.WriteLine("{0}: Knight position is {1}, Bishop position is {2}, Queen position is {3}", move, knightPos, bishopPos, queenPos);
            }
        }
        public void Play(int moves)
        {
            var knight = new KnightMove();
            var pos    = _startPosition;

            Console.WriteLine("0: My position is {0}", pos);

            for (var move = 1; move <= moves; move++)
            {
                var possibleMoves = knight.ValidMovesFor(pos).ToArray();
                pos = possibleMoves[_rnd.Next(possibleMoves.Length)];
                Console.WriteLine("{1}: My position is {0}", pos, move);
            }
        }
        public void TestKnightMoveFromCorner()
        {
            var pos    = new Position(1, 1);
            var knight = new KnightMove();

            var moves = new HashSet <Position>(knight.ValidMovesFor(pos));

            Assert.IsNotNull(moves);
            Assert.AreEqual(2, moves.Count);

            var possibles = new[] { new Position(2, 3), new Position(3, 2) };

            foreach (var possible in possibles)
            {
                Assert.IsTrue(moves.Contains(possible));
            }
        }
Beispiel #7
0
        public int MinKnightCount(CoordinatePoint coordinatPoint, CoordinatePoint coordinatPoint1)
        {
            var knightMoves = KnightMove.Crosswise(coordinatPoint1);
            var endMoves    = KnightMove.Crosswise(coordinatPoint);

            if (knightMoves.Contains((coordinatPoint)))
            {
                return(count);
            }
            else
            {
                count++;
                if (KnightMove.Equals(coordinatPoint1, coordinatPoint))
                {
                    return(count);
                }
                else
                {
                    count++;
                    if (KnightMove.Equals(knightMoves, coordinatPoint))
                    {
                        return(count);
                    }
                    else
                    {
                        count++;
                        if (KnightMove.Equals(knightMoves, endMoves))
                        {
                            return(count);
                        }
                        else
                        {
                            count++;
                            if (KnightMove.EqualsEnd(knightMoves, endMoves))
                            {
                                return(count);
                            }
                            else
                            {
                                return(6);
                            }
                        }
                    }
                }
            }
        }
        public bool MoveIsValid(int[][] canvas, KnightMove currentPos, KnightMove move)
        {
            if (currentPos.X + move.X >= 0 && currentPos.X + move.X <= canvas[0].Length - 1)
            {
                if (currentPos.Y + move.Y >= 0 && currentPos.Y + move.Y <= canvas.Length - 1)
                {
                    var nextY = currentPos.Y + move.Y;
                    var nextX = currentPos.X + move.X;
                    if (canvas[nextY][nextX] == -1)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        public override Position Play(Dictionary <string, Position> currPosDict)
        {
            //Play the game moves
            var knight = new KnightMove();
            var pos    = _startPosition;

            Console.WriteLine("(Knight)0 My old position is {0}", pos);

            for (var move = 1; move <= _moves; move++)
            {
                var possibleMovesList = knight.ValidMovesFor(pos).ToList();
                possibleMovesList.RemoveAll(x => currPosDict.Any(y => y.Value.X == x.X && y.Value.Y == x.Y));

                var possibleMoves = possibleMovesList.ToArray();
                pos             = possibleMoves[_rnd.Next(possibleMoves.Length)];
                CurrentPosition = pos;
                Console.WriteLine("(Knight){1}: My new position is {0}", pos, move);
            }

            return(CurrentPosition);
        }
Beispiel #10
0
 public Knight()
 {
     _knight = new KnightMove();
 }