/// <summary> /// Deserialize method for the class /// </summary> /// <param name="array">Serialized object</param> /// <returns></returns> public static Message Deserialize(string array) { Message msg = new Message(); using (Stream stream = new MemoryStream()) { byte[] data = System.Text.Encoding.UTF8.GetBytes(array); stream.Write(data, 0, data.Length); stream.Position = 0; DataContractSerializer deserializer = new DataContractSerializer(typeof(Message)); msg = (Message)deserializer.ReadObject(stream); } return msg; }
/// <summary> /// Serializing method for the class /// </summary> /// <param name="msg">object of the class</param> /// <returns></returns> public static string Serialize(Message msg) { string array; using (MemoryStream stream = new MemoryStream()) { using (StreamReader reader = new StreamReader(stream)) { DataContractSerializer serializer = new DataContractSerializer(typeof(Message)); serializer.WriteObject(stream, msg); stream.Position = 0; //array = stream.ToArray(); array = reader.ReadToEnd(); } } return array; }
//CHECK FOR TURN /// <summary> /// Method called to enter move, the user will decide on a first coordinate and an end coordinate to move the checker piece /// across the board /// </summary> private void enterMove() { bool moveAcceptable = false; try { if (isFirstClick) { isFirstClick = false; //Select another coordinate to determine movement prevColumn = (int)currSquareSelected.GetValue(Grid.ColumnProperty); //Prev Column prevRow = (int)currSquareSelected.GetValue(Grid.RowProperty); //Prev Row //Get the colour of the piece myPieceColor = (SolidColorBrush)currSquareSelected.Children.ElementAt(0).GetValue(Ellipse.FillProperty); //Check if the brush resembles king myBorderColor = (SolidColorBrush)currSquareSelected.Children.ElementAt(0).GetValue(Ellipse.StrokeProperty); if (myPieceColor.Color == Colors.Red && playerColor == "red") { //Piece that was selected boardSquares[prevRow - 1, prevColumn].Background = new SolidColorBrush(Colors.Yellow); colorOfPiece = true; //COLOR OF PIECE IS RED if (myBorderColor.Color == Colors.White) { moveKing = true; } else { moveKing = false; } } else if (myPieceColor.Color == Colors.Orange && playerColor == "orange") { //Piece that was selected boardSquares[prevRow - 1, prevColumn].Background = new SolidColorBrush(Colors.Yellow); colorOfPiece = false; //COLOR OF PIECE IS ORANGE if (myBorderColor.Color == Colors.White) { moveKing = true; } else { moveKing = false; } } else { //Wrong color choice isFirstClick = true; //Redo the click move } } else { boardSquares[prevRow - 1, prevColumn].Background = new SolidColorBrush(Colors.Black); isFirstClick = true; //Allow to select new coordinates to click on for movement desColumnn = (int)currSquareSelected.GetValue(Grid.ColumnProperty); //Current Column desRow = (int)currSquareSelected.GetValue(Grid.RowProperty); //Current Row //Now send the information off to the server! moveAcceptable = CheckMove(colorOfPiece, moveKing, prevColumn + 1, prevRow, desColumnn + 1, desRow); //Send this info the game logic controller if (moveAcceptable) //Returns true, change checker position on board { Message msg = new Message(); msg.color = colorOfPiece; msg.king = moveKing; msg.currentX = prevColumn; msg.currentY = prevRow; msg.newX = desColumnn; msg.newY = desRow; this.connectionStream.Send("!send"); this.connectionStream.Read(); this.connectionStream.Send(Message.Serialize(msg)); this.connectionStream.Read(); if (myBorderColor.Color == Colors.White) //Check if there is a border color of white (meaining king piece) { MoveCheckerPiece(true); //Move the KING checker piece } else { //No stroke present, thus error, so imply that it's a regulat checker piece MoveCheckerPiece(false); //Move the REGULAR checker piece if (myPieceColor.Color == Colors.Orange) //Change into dark orange if required { CheckIfKing(false); } else //Change into dark red if required { CheckIfKing(true); } } bool win = false; if (playerColor == "orange") { if (this.RedPiece.Count == 0) { this.connectionStream.Send("!win"); win = true; } } else { if (this.OrgPiece.Count == 0) { this.connectionStream.Send("!win"); win = true; } } if (!win) { this.connectionStream.Send("!end"); turnStatus.Text = "NOT YOUR TURN"; } else { turnStatus.Text = "YOU WON"; } this.isAbleToMove = false; this.middleGame.Start(); } } } catch (Exception exp) { //Post the error gameOutcomeMsg.Text = "Error with the move entered!"; } }