Ejemplo n.º 1
0
 private void NotifyBoardChanged()
 {
     if (!_suppressChangeEvents)
     {
         BoardChanged?.Invoke(this);
     }
 }
Ejemplo n.º 2
0
 public void OnBoardChanged(int i_X, int i_Y)
 {
     if (BoardChanged != null)
     {
         BoardChanged.Invoke(i_X, i_Y);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Start a new game with the two players of the session and continue until the game is over.
        /// </summary>
        /// <returns>return the chess game log with all draws etc.</returns>
        public ChessGame ExecuteGame()
        {
            // initialize new chess game
            Game = new ChessGame();

            // continue until the game is over
            while (!Game.GameStatus.IsGameOver())
            {
                // determin the drawing player
                var drawingPlayer = Game.SideToDraw == ChessColor.White ? WhitePlayer : BlackPlayer;

                // init loop variables
                bool      isDrawValid;
                ChessDraw draw;

                do
                {
                    // get the draw from the player
                    draw        = drawingPlayer.GetNextDraw(Game.Board, Game.LastDrawOrDefault);
                    isDrawValid = Game.ApplyDraw(draw, true);
                }while (!isDrawValid);

                // raise board changed event
                BoardChanged?.Invoke(Game.Board);
            }

            // return the chess game, so it can be logged, etc.
            return(Game);
        }
Ejemplo n.º 4
0
 public void place(Ship s)
 {
     if (Check_placeable(s, s.X, s.Y))
     {
         if ((int)s.orientation == 0)
         {
             for (int i = 0; i < s.Lives; i++)
             {
                 board[s.Y + i, s.X].type = s.type;
                 board[s.Y + i, s.X].ship = s;
                 s.positions.Add(board[s.Y + i, s.X]);
                 BoardChanged?.Invoke(s.X, s.Y + i);
             }
         }
         if ((int)s.orientation == 1)
         {
             for (int i = 0; i < s.Lives; i++)
             {
                 board[s.Y, s.X + i].type = s.type;
                 board[s.Y, s.X + i].ship = s;
                 s.positions.Add(board[s.Y, s.X + i]);
                 BoardChanged?.Invoke(s.X + i, s.Y);
             }
         }
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Reverts the <see cref="Game"/> back to the <see cref="LastMove"/>'s state.
        /// </summary>
        public void Undo()
        {
            if (!CanUndo)
            {
                return;
            }

            Tile removedTile = _history.Pop();

            BoardChanging?.Invoke(
                this,
                new BoardChangingEventArgs(
                    new Tile[0],
                    new Tile[] { removedTile }));

            removedTile.Piece = new Piece(Pieces.None);
            Manager.Turn.MoveBack();
            if (IsOver)
            {
                IsOver = false;
                Manager.Turn.ShiftStartBackwards();
            }

            BoardChanged?.Invoke(
                this,
                new BoardChangedEventArgs(
                    new Tile[0],
                    new Tile[] { removedTile }));
        }
 protected virtual void OnBoardChange(int i_Row, int i_Column)
 {
     if (BoardChanged != null)
     {
         BoardChanged.Invoke(i_Row, i_Column);
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// 盤上に駒を配置します。
        /// ゲームがすでに終了している場合、指定した位置にすでに駒が置かれている場合は何もしません。
        /// </summary>
        /// <param name="row">配置する行</param>
        /// <param name="column">配置する列</param>
        /// <param name="player">配置する駒</param>
        public void PutPiece(int row, int column, PlayerForm player)
        {
            if (GameStatus == GameStatus.Finished)
            {
                return;
            }

            if (this.board.BoardStatuses[row, column] != PlayerForm.None)
            {
                return;
            }

            this.board.PutPiece(row, column, player);
            BoardChanged?.Invoke(this, EventArgs.Empty);
            SwitchCurrentPleyer();
            (bool isGameEnded, PlayerForm winner) = CheckIfGameEnded(BoardSize, AlignNumber);
            if (isGameEnded)
            {
                this.GameStatus = GameStatus.Finished;
            }
            this.Winner = winner;
            if (isGameEnded)
            {
                GameEnded.Invoke(this, new GameEndedEventArgs(this.Winner));
            }
        }
Ejemplo n.º 8
0
 /// <summary>
 /// ゲームをリセットします。
 /// </summary>
 public void ResetGame()
 {
     this.board.ResetBoard(PlayerForm.None);
     CurrentPlayer = PlayerForm.Circle;
     CurrentPlayerChanged?.Invoke(this, EventArgs.Empty);
     BoardChanged?.Invoke(this, EventArgs.Empty);
     this.GameStatus = GameStatus.BeforeStart;
     Winner          = PlayerForm.None;
 }
Ejemplo n.º 9
0
 public void Go(Board board)
 {
     lock (_locker)
     {
         if (_board != board)
         {
             _board = board;
             BoardChanged?.Invoke(this, board);
         }
     }
 }
Ejemplo n.º 10
0
        public void PaintTile(float x, float y)
        {
            if (!EnablePainting)
            {
                return;
            }

            var point = new Point(
                (int)(x / Board.TileSize.Width),
                (int)(y / Board.TileSize.Height));

            Board.Tiles[point.X + point.Y * Board.Size.Width] = Tile.Block;
            BoardChanged?.Invoke(this, EventArgs.Empty);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Sets the current state of the board to the one specified by the data string
        /// </summary>
        public async void SetByDataString(string dataString, Version boardVersion = null)
        {
            // Check if the version is provided. If not, assume it is the current version.
            if (boardVersion == null)
            {
                boardVersion = BingoVersion;
            }

            await InitBoard();

            ResetBoard();
            BingoGrid.Children.Clear();

            // NOTE: When making significant changes to this algorithm,
            // don't delete the code. Create an if branch to run the
            // previous version of the algorithm.

            byte[] tileData = dataString.Replace("\r", String.Empty).Replace("\n", String.Empty).TakeEvery(2)
                              .Select(s => byte.Parse(s, System.Globalization.NumberStyles.HexNumber)).ToArray();
            for (int x = 0; x < 5; x++)
            {
                for (int y = 0; y < 5; y++)
                {
                    var data      = tileData[5 * x + y];
                    int textIndex = (data >> 1) - 1;
                    if (textIndex == -1 || (x == 2 && y == 2))
                    {
                        // Tile is free space
                        BingoGrid.Children.Add(GenerateFreeTile());
                        continue;
                    }

                    string text     = AllTiles[textIndex];
                    bool   isFilled = (data & 1) == 1;
                    AddTile(text, isFilled);
                }
            }
            BoardChanged?.Invoke(dataString);
        }
Ejemplo n.º 12
0
        public async void ResetBoard()
        {
            await InitBoard();

            // Clear the board of tiles
            var items = BingoGrid.Children.ToList();

            foreach (UIElement item in items)
            {
                if (item is ToggleButton)
                {
                    BingoGrid.Children.Remove(item);
                }
            }

            // Randomly choose 24 tiles
            var newTiles = GetRandom(AllTiles, 24);

            for (int x = 0; x < 5; x++)
            {
                for (int y = 0; y < 5; y++)
                {
                    // Check if the tile is the free space
                    if (x == 2 && y == 2)
                    {
                        continue;
                    }

                    int boardIndex = 5 * x + y;
                    if (boardIndex > 12)
                    {
                        boardIndex--;
                    }
                    SetTile(newTiles.ElementAt(boardIndex), x, y);
                }
            }
            BoardChanged?.Invoke(ToDataString());
        }
Ejemplo n.º 13
0
        public PlayResponse Play(PlayRequest request)
        {
            if (currentPlayer == null)
            {
                return(null);
            }

            PlayResponse response = currentPlayer.Play(request, victoryValidator);

            if (response.IsValidMove)
            {
                BoardChanged?.Invoke(request.Row, request.Col, currentPlayer.Mark);

                if (response.Result != Enums.GameState.Playing)
                {
                    GameOver?.Invoke(response.Result, response.Result == Enums.GameState.Draw ? Enums.MarkType.None : currentPlayer.Mark);
                }

                currentPlayer = NextPlayer;
            }

            return(response);
        }
Ejemplo n.º 14
0
        private void OnBoardChanged()
        {
            bool whiteQueenSurrounded = (CountNeighbors(PieceName.WhiteQueenBee) == 6);
            bool blackQueenSurrounded = (CountNeighbors(PieceName.BlackQueenBee) == 6);

            if (whiteQueenSurrounded && blackQueenSurrounded)
            {
                BoardState = BoardState.Draw;
            }
            else if (whiteQueenSurrounded)
            {
                BoardState = BoardState.BlackWins;
            }
            else if (blackQueenSurrounded)
            {
                BoardState = BoardState.WhiteWins;
            }
            else
            {
                BoardState = CurrentTurn == 0 ? BoardState.NotStarted : BoardState.InProgress;
            }

            BoardChanged?.Invoke(this, null);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Resets the <see cref="Game"/> to its original state.
        /// </summary>
        public void Restart()
        {
            Tile[] history = _history.ToArray();
            BoardChanging?.Invoke(
                this,
                new BoardChangingEventArgs(
                    new Tile[0],
                    history));

            foreach (Tile tile in _history)
            {
                tile.Piece = new Piece(Pieces.None);
            }

            Manager.Turn.Reset();
            _history.Clear();
            IsOver = false;

            BoardChanged?.Invoke(
                this,
                new BoardChangedEventArgs(
                    new Tile[0],
                    history));
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Raise the BoardChanged event
 /// </summary>
 /// <param name="board"></param>
 protected virtual void OnBoardChanged(TBoard board)
 {
     BoardChanged?.Invoke(this, board);
 }
Ejemplo n.º 17
0
 public PrimaryBoard()
 {
     TileChanged += (o, e) => BoardChanged?.Invoke(this, new EventArgs());
 }
Ejemplo n.º 18
0
        public void RandomFill(int seed, Difficulty diff)
        {
            if (Populated)
            {
                tiles = new PrimaryTile[10, 10];
            }
            Random   r = new Random(seed);
            CoordSet filled;
            int      i = 0;

            switch (diff)
            {
            case Difficulty.Easy:

                while (i < 5)
                {
                    Ship s = Ship.CreateRandom(r, ClassicShipLengths[i]);
                    if (TryAddShip(s))
                    {
                        i++;
                    }
                }
                break;

            case Difficulty.Medium:

                filled = new CoordSet();
                while (i < 5)
                {
                    Ship s = Ship.CreateRandom(r, ClassicShipLengths[i]);
                    if (filled.Overlaps(s.GetOccupiedArea(0)))
                    {
                        continue;
                    }

                    filled.Add(s.GetOccupiedArea(1));

                    TryAddShip(s);
                    i++;
                }
                break;

            case Difficulty.Hard:

                filled = new CoordSet();
                var border = new CoordSet();

                for (int j = 0; j < 10; j++)
                {
                    border.Add(new CoordPair(0, j));
                    border.Add(new CoordPair(j, 0));
                    border.Add(new CoordPair(9, j));
                    border.Add(new CoordPair(j, 9));
                }

                while (i < 5)
                {
                    Ship s        = Ship.CreateRandom(r, ClassicShipLengths[i]);
                    var  shipArea = s.GetOccupiedArea(0);

                    if (shipArea.Overlaps(filled))
                    {
                        continue;
                    }

                    var weakArea = new CoordSet(border);
                    weakArea.IntersectWith(shipArea);
                    if (r.NextDouble() < weakArea.Count * 0.4)
                    {
                        continue;
                    }

                    border.Add(s.GetOccupiedArea(1));
                    filled.Add(shipArea);

                    TryAddShip(s);
                    i++;
                }
                break;
            }
            Populated = true;
            BoardChanged?.Invoke(this, new EventArgs());
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Puts a <see cref="Tile"/> corresponding to <see cref="CurrentPlayer"/>
        /// and advances the <see cref="Game"/> to the next state. at position
        /// <paramref name="x"/>, <paramref name="y"/>.
        /// </summary>
        /// <param name="x">x-axis position</param>
        /// <param name="y">y-axis position</param>
        /// <exception cref="ArgumentException"></exception>
        public void Play(int x, int y)
        {
            if (x < 0 || x > Board.Width)
            {
                throw new ArgumentException("Value is out of range", nameof(x));
            }

            if (y < 0 || y > Board.Height)
            {
                throw new ArgumentException("Value is out of range", nameof(y));
            }

            // Check if game is over
            if (IsOver)
            {
                return;
            }

            Tile tile = Board[x, y];

            // Check for already placed tile
            if (tile.Piece.Type != Pieces.None)
            {
                return;
            }

            Player oldPlayer = Manager.CurrentPlayer;

            tile.Piece = oldPlayer.Piece;
            Tile previousTile = LastMove;

            _history.Push(tile);

            BoardChanging?.Invoke(
                this,
                new BoardChangingEventArgs(
                    new Tile[] { tile },
                    new Tile[0]));

            // Check for game over
            if (CheckGameOver(x, y, out IList <Tile> winningLine))
            {
                IsOver = true;

                if (ShiftPlayersOnGameOver)
                {
                    Manager.Turn.ShiftStartForwards();
                }
            }

            // Increment turn
            Manager.Turn.MoveNext();

            BoardChanged?.Invoke(
                this,
                new BoardChangedEventArgs(
                    new Tile[] { tile },
                    new Tile[0]));

            if (IsOver)
            {
                GameOver?.Invoke(
                    this,
                    new GameOverEventArgs(
                        Manager.Turn.Current,
                        oldPlayer,
                        winningLine));
            }
        }
Ejemplo n.º 20
0
 public TrackerBoard()
 {
     TileChanged += (o, e) => BoardChanged?.Invoke(this, new EventArgs());
 }
Ejemplo n.º 21
0
 private void HandleChangedBoard(string fenBoard) =>
 BoardChanged?.Invoke(this, new BoardChangedEventArgs(EditBoard(fenBoard)));