Exemple #1
0
		public void UseBadNullEngine()
		{
			IEngine badNullEngine = this.GetEngine("Quixo.Engine.Tests.BadNullTestEngine");

			Board board = new Board();
			Assert.AreEqual(Player.X, board.CurrentPlayer, "The starting player is invalid.");
			board.MovePiece(new Point(0, 0), new Point(0, 4));
			Assert.AreEqual(Player.O, board.CurrentPlayer, "The player before the generated move is invalid.");
			Move engineMove = badNullEngine.GenerateMove(board, new ManualResetEvent(false));
			board.MovePiece(engineMove.Source, engineMove.Destination);
		}
Exemple #2
0
		public void UseGoodEngine()
		{
			IEngine goodEngine = this.GetEngine("Quixo.Engine.RandomEngine");

			Board board = new Board();
			Assert.AreEqual(Player.X, board.CurrentPlayer, "The starting player is invalid.");
			board.MovePiece(new Point(0, 0), new Point(0, 4));
			Assert.AreEqual(Player.O, board.CurrentPlayer, "The player before the generated move is invalid.");
			Move engineMove = goodEngine.GenerateMove(board, new ManualResetEvent(false));

			Assert.AreEqual(Player.O, engineMove.Player, "The player after the generated move is invalid.");
			board.MovePiece(engineMove.Source, engineMove.Destination);

			Assert.AreEqual(Player.X, board.CurrentPlayer, "The player after the generated move was performed is invalid.");
		}
Exemple #3
0
		/// <summary>
		/// Returns a <see cref="Board"/> object based on the 
		/// serialized data.
		/// </summary>
		/// <param name="serializationStream">A serialized version of a <see cref="Board"/> in the simplified format</param>
		/// <returns>A new <see cref="Board"/> object.</returns>
		/// <exception cref="ArgumentNullException">Thrown if <paramref name="serializationStream"/> is <code>null</code>.</exception>
		/// <exception cref="SerializationException">Thrown if an error occurred during deserialization.</exception>
		public object Deserialize(Stream serializationStream)
		{
			if (serializationStream == null)
			{
				throw new ArgumentNullException("serializationStream");
			}

			var board = new Board();

			string moves = null;

			using (var reader = new StreamReader(serializationStream))
			{
				moves = reader.ReadToEnd();
			}

			try
			{
				foreach (string move in moves.Split('|'))
				{
					var moveParts = move.Split(':');
					var sourceMove = moveParts[0].Split(',')[0];
					var destinationMove = moveParts[1].Split(',')[1];

					board.MovePiece(
						new Point(int.Parse(moveParts[0].Split(',')[0]),
							int.Parse(moveParts[0].Split(',')[1])),
						new Point(int.Parse(moveParts[1].Split(',')[0]),
							int.Parse(moveParts[1].Split(',')[1])));
				}
			}
			catch (FormatException formatEx)
			{
				throw new SerializationException(string.Empty, formatEx);
			}
			catch (IndexOutOfRangeException indexEx)
			{
				throw new SerializationException(string.Empty, indexEx);
			}

			return board;
		}
Exemple #4
0
        /// <summary>
        /// Returns a <see cref="Board"/> object based on the
        /// serialized data.
        /// </summary>
        /// <param name="serializationStream">A serialized version of a <see cref="Board"/> in the simplified format</param>
        /// <returns>A new <see cref="Board"/> object.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="serializationStream"/> is <code>null</code>.</exception>
        /// <exception cref="SerializationException">Thrown if an error occurred during deserialization.</exception>
        public object Deserialize(Stream serializationStream)
        {
            if (serializationStream == null)
            {
                throw new ArgumentNullException(nameof(serializationStream));
            }

            var board = new Board();

            string moves = null;

            using (var reader = new StreamReader(serializationStream))
            {
                moves = reader.ReadToEnd();
            }

            try
            {
                foreach (var move in moves.Split('|'))
                {
                    var moveParts       = move.Split(':');
                    var sourceMove      = moveParts[0].Split(',')[0];
                    var destinationMove = moveParts[1].Split(',')[1];

                    board.MovePiece(
                        new Point(int.Parse(moveParts[0].Split(',')[0]),
                                  int.Parse(moveParts[0].Split(',')[1])),
                        new Point(int.Parse(moveParts[1].Split(',')[0]),
                                  int.Parse(moveParts[1].Split(',')[1])));
                }
            }
            catch (FormatException formatEx)
            {
                throw new SerializationException(string.Empty, formatEx);
            }
            catch (IndexOutOfRangeException indexEx)
            {
                throw new SerializationException(string.Empty, indexEx);
            }

            return(board);
        }