/// <summary> /// Adds all the tokens on the board. /// This method clears any previously drawn tokens from the canvas. /// </summary> private void AddTokens() { foreach (Token token in _tokens) { BoardCanvas.RemoveToken(token); } _tokens.Clear(); Point p1 = new Point(0, BoardCanvas.ActualHeight); Point p2 = new Point(0, BoardCanvas.ActualHeight - 15); Point p3 = new Point(0, BoardCanvas.ActualHeight - 30); Point p4 = new Point(0, BoardCanvas.ActualHeight - 45); Point[] pts = new Point[] { p1, p2, p3, p4 }; enGameToken gametoken = enGameToken.Green; for (int i = 0; i < (int)_enGameType; i++) { Token token = new Token(this, gametoken, pts[i], BoardCanvas.ActualWidth, BoardCanvas.ActualHeight); gametoken = gametoken.Next(_enGameType); _tokens.Add(token); BoardCanvas.AddToken(token); } }
/// <summary> /// Adds all the snakes on the board. This method should be called when creating a new board. /// This method clears any previously drawn snakes from the canvas. /// </summary> private void AddSnakesOnStartGame() { _snakeNumbers.Clear(); if (_snakes.Count > 0) { foreach (Snake snake in _snakes) { BoardCanvas.RemoveSnake(snake); } _snakes.Clear(); } Random _LadderSnakeRandom = new Random(); int numberofSnakes = _intNumberOfSnakes; for (int i = 0; i < numberofSnakes; i++) { int startNumber, endNumber; startNumber = _LadderSnakeRandom.Next(1, 100); endNumber = _LadderSnakeRandom.Next(1, 100); if (endNumber > startNumber) { int x = startNumber; startNumber = endNumber; endNumber = x; } Point start = GameBoard.GetCenterCoordinates(startNumber); Point end = GameBoard.GetCenterCoordinates(endNumber); if (_ladderNumbers.ContainsKey(startNumber) || _ladderNumbers.Values.Contains(startNumber) || _ladderNumbers.ContainsKey(endNumber) || _ladderNumbers.Values.Contains(endNumber) || _snakeNumbers.ContainsKey(startNumber) || _snakeNumbers.Values.Contains(startNumber) || _snakeNumbers.ContainsKey(endNumber) || _snakeNumbers.Values.Contains(endNumber) || startNumber - endNumber > _intMaxSnakeLength || start.X - end.X == 0 || start.Y - end.Y == 0) { i--; continue; } if (!AddSnake(start, end)) { i--; continue; } _snakeNumbers.Add(startNumber, endNumber); } GameBoard.SetBoxColor(_snakeNumbers.Keys, _SnakeHeadBoxColor); GameBoard.SetBoxColor(_snakeNumbers.Values, _SnakeTailBoxColor); }
/// <summary> /// This method is used to add the snake on the board. This method doesn't check the start and end point rules. /// It will assume that the start and end points are already checked for possible errors and rules. /// This method will check only the intersection of new snake with other already drawn snakes. /// </summary> /// <param name="start">Start point from which the snake will start.</param> /// <param name="end">End point upto which the snake will go.</param> /// <param name="checkIsIntersecting">If this argument is true, then before adding the snake, it will be checked for intersection with other snakes. /// Else, it will be directly added to the board.</param> /// <returns>True if the snake is added to the board, else false.</returns> public bool AddSnake(Point start, Point end, bool checkIsIntersecting = true) { Snake snake = new Snake(); snake.SnakeColor = _SnakeColor; snake.SnakeWidth = Math.Sqrt(GameBoard.Boxes[1].BoxTextBlock.ActualWidth * GameBoard.Boxes[1].BoxTextBlock.ActualWidth + GameBoard.Boxes[1].BoxTextBlock.ActualHeight * GameBoard.Boxes[1].BoxTextBlock.ActualHeight); snake.LineStrokeThickness = GameBoard.ActualWidth / _dbSnakeThicknessFactor; snake.CanvasHeight = GameBoard.ActualHeight; snake.CanvasWidth = GameBoard.ActualWidth; snake.StartPoint = start; snake.EndPoint = end; if (!checkIsIntersecting || !IsIntersecting(snake)) { snake.DrawCurve(); _snakes.Add(snake); BoardCanvas.AddSnake(snake); return(true); } return(false); }