Пример #1
0
        /// <summary>
        /// Method to score Boxes that a Turn may have created.
        /// </summary>
        /// <param name="Turn">The Turn completed</param>
        /// <returns>Returns the number of boxes created.</returns>
        private int ScoreBoxes(Turn Turn)
        {
            int score = 0;
            var x = 0;
            var y = 0;
            var b1 = 0;
            var b2 = 0;
            //Check if Line is Verticle or Horizontal
            if(Turn.Line.Direction == "Verticle")
            {
                //If Line is Verticle, the X value is static.
                x = Turn.Line.To.X;
                //Find the lowest Y value
                if(Turn.Line.To.Y > Turn.Line.From.Y)
                {
                    y = Turn.Line.From.Y;
                }
                else
                {
                    y = Turn.Line.To.Y;
                }
                //Calcaluate the Position for the boxes on either side of the line.
                b1 = ( y * height ) + x;
                b2 = ( y * height ) + (x - 1);

                //Retreive box1 from list.
                var box1 = this.Boxes.ElementAtOrDefault( b1 );
                //If box is out of bounds, box1 will be null
                if ( box1 != null )
                {
                    //A verticle line will be the west line of the first box.
                    if(box1.West.Created)
                    {
                        //Line was already created. Throw an exception.
                        throw new InvalidTurnException( "Line Has already been created." );
                    }
                    else
                    {
                        //Assign the Line to the Player
                        box1.West.Created = true;
                        box1.West.Player = Turn.Player;
                        //Check to see if the new line creates a box
                        if ( box1.Completed() )
                        {
                            //If so, set the owner to the current Player and increment score.
                            box1.Owner = Turn.Player;
                            score++;
                        }
                        //Otherwise, do nothing.
                        //Move on to box 2.
                    }
                }
                //Retreive box2 from list.
                var box2 = this.Boxes.ElementAtOrDefault( b2 );
                //If box is out of bounds, box2 will be null
                if ( box2 != null )
                {
                    //A verticle line will be the east line of the first box.
                    if ( box2.East.Created )
                    {
                        //Line was already created. Throw an exception.
                        throw new InvalidTurnException( "Line Has already been created." );
                    }
                    else
                    {
                        //Assign the Line to the Player
                        box2.East.Created = true;
                        box2.East.Player = Turn.Player;
                        //Check to see if the new line creates a box
                        if ( box2.Completed() )
                        {
                            //If so, set the owner to the current Player and increment score.
                            box2.Owner = Turn.Player;
                            score++;
                        }
                        //Otherwise, do nothing.
                        //Both boxes have been examined. The turn has been successfully added.
                    }
                }

            }
            else
            {
                //If Line is Horizontal, the Y value is static.
                y = Turn.Line.To.Y;
                //Find the lowest X value.
                if(Turn.Line.To.X > Turn.Line.From.X)
                {
                    x = Turn.Line.From.X;
                }
                else
                {
                    x = Turn.Line.To.X;
                }
                //Calcaluate the Position for the boxes on either side of the line.
                b1 = ( y * height ) + x;
                b2 = ( (y - 1) * height ) + x;

                //Retreive box1 from list.
                var box1 = this.Boxes.ElementAtOrDefault( b1 );
                //If box is out of bounds, box1 will be null
                if ( box1 != null )
                {
                    //A horizontal line will be the south line of the first box.
                    if ( box1.South.Created )
                    {
                        //Line was already created. Throw an exception.
                        throw new InvalidTurnException( "Line Has already been created." );
                    }
                    else
                    {
                        //Assign the Line to the Player
                        box1.South.Created = true;
                        box1.South.Player = Turn.Player;
                        //Check to see if the new line creates a box
                        if ( box1.Completed() )
                        {
                            //If so, set the owner to the current Player and increment score.
                            box1.Owner = Turn.Player;
                            score++;
                        }
                        //Otherwise, do nothing.
                        //Move on to box 2.
                    }
                }
                //Retreive box2 from list.
                var box2 = this.Boxes.ElementAtOrDefault( b2 );
                //If box is out of bounds, box2 will be null
                if ( box2 != null )
                {
                    //A horizontal line will be the north line of the first box.
                    if ( box2.North.Created )
                    {
                        //Line was already created. Throw an exception.
                        throw new InvalidTurnException( "Line Has already been created." );
                    }
                    else
                    {
                        //Assign the Line to the Player
                        box2.North.Created = true;
                        box2.North.Player = Turn.Player;
                        //Check to see if the new line creates a box
                        if ( box2.Completed() )
                        {
                            //If so, set the owner to the current Player and increment score.
                            box2.Owner = Turn.Player;
                            score++;
                        }
                        //Otherwise, do nothing.
                        //Both boxes have been examined. The turn has been successfully added.
                    }
                }
            }
            return score;
            /*
            //Go through each box on the grid.
            foreach(var box in Boxes)
            {
                //If Current Box is not completed already, check to see if this turn completed it.
                if(!box.Completed())
                {
                    //Check if the line from the Turn matches the Northern Line for the box.
                    if(box.North.Equals(Turn.Line))
                    {
                        //Check if Line has Already Been Created (A line touches up to two boxes directly)
                        if(!box.North.Created)
                        {
                            //Line has not been created. Create line, attach it to the player, and check if the line completes the box.
                            box.North.Created = true;
                            box.North.Player = Turn.Player;
                            if(box.Completed())
                            {
                                //Line completes
                                box.Owner = Turn.Player;
                                score++;
                            }
                        }
                        else
                        {
                            throw new InvalidTurnException("Line Has already been created.");
                        }
                    }
                    else if(box.South.Equals(Turn.Line))
                    {
                        if ( !box.South.Created )
                        {
                            box.South.Created = true;
                            box.South.Player = Turn.Player;
                            if ( box.Completed() )
                            {
                                box.Owner = Turn.Player;
                                score++;
                            }
                        }
                        else
                        {
                            throw new InvalidTurnException( "Line Has already been created." );
                        }
                    }
                    else if(box.West.Equals(Turn.Line))
                    {
                        if ( !box.West.Created )
                        {
                            box.West.Created = true;
                            box.West.Player = Turn.Player;
                            if ( box.Completed() )
                            {
                                box.Owner = Turn.Player;
                                score++;
                            }
                        }
                        else
                        {
                            throw new InvalidTurnException( "Line Has already been created." );
                        }
                    }
                    else if(box.East.Equals(Turn.Line))
                    {
                        if ( !box.East.Created )
                        {
                            box.East.Created = true;
                            box.East.Player = Turn.Player;
                            if ( box.Completed() )
                            {
                                box.Owner = Turn.Player;
                                score++;
                            }
                        }
                        else
                        {
                            throw new InvalidTurnException( "Line Has already been created." );
                        }
                    }
                }
                if ( score == 2 )
                {
                    break;
                }
            }
            return score;
             */
        }
Пример #2
0
 /// <summary>
 /// Method to Add a Turn to the Game.
 /// </summary>
 /// <param name="Turn">The Turn to Play</param>
 public void AddTurn(Turn Turn)
 {
     //Grab who the current player ought to be from the Dictionary.
     var currentPlayer = Players.FirstOrDefault( c => c.Key == this.CurrentPlayer );
     //Verify the game is started/startable, and the gmae is not finished. Also, verify the player submitting the turn is correct.
     if(!GameFinished && (GameStarted || GameStartable) && currentPlayer.Value.Id.Equals(Turn.Player.Id)  )
     {
         //Add the Turn to the list.
         this.Turns.Add( Turn );
         //Draw the lines and score the boxes.
         var score = this.ScoreBoxes( Turn );
         //If the player did not score, then the turn moves to the next player.
         if ( score == 0 )
         {
             //If the current player is the last player in the list, return to the first player
             if(this.CurrentPlayer == this.Players.Count - 1 )
             {
                 this.CurrentPlayer = this.Players.First().Key;
             }
             //Else, proceed to the next Player in the list.
             else
             {
                 this.CurrentPlayer++;
             }
         }
         //Otherwise, the player goes again, and we save his score to his player account.
         else
         {
             currentPlayer.Value.Score += score;
         }
     }
     else
         throw new InvalidTurnException("It is not your turn, or the game has not yet started, or the game is finished!");
 }