<commit_before>﻿// Copyright (c) the authors of nanoGames. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt in the project root.

using System;

namespace NanoGames.Games.Bomberguy
{
    internal class BomberMatch : Match<BomberGuy>
    {
        public const int FIELD_MIN_SIZE = 9;
        public const double PLAYER_RATIO = .9;
        public const double BOMB_RATIO = 1;
        public const int BOMB_REACH = 2;
        public const double PLAYER_SPEED = 9;
        public const double BOMBSTACLE_PROBABILITY = 0.5;

        private int _fieldSize;
        private double _playerSpeed;
        private double _pixelsPerUnit;
        private double _widthOffset;
        private RectbombularThing[,] _field;

        public Vector CellSize
        {
            get { return new Vector(_pixelsPerUnit, _pixelsPerUnit); }
        }

        public RectbombularThing this[Vector v]
        {
            get
            {
                return this[(int)v.X, (int)v.Y];
            }

            set
            {
                this[(int)v.X, (int)v.Y] = value;
            }
        }

        public RectbombularThing this[int x, int y]
        {
            get
            {
                return _field[x, y];
            }

            set
            {
                _field[x, y] = value;
                if (value != null) value.Position = GetCoordinates(new Vector(x, y));
            }
        }

        public Vector GetCell(BomberThing thing)
        {
            var c = (thing.Center.X - _widthOffset) / _pixelsPerUnit;
            var r = thing.Center.Y / _pixelsPerUnit;
            return new Vector(Math.Floor(r), Math.Floor(c));
        }

        public Vector GetCell(Vector position)
        {
            var c = (position.X - _widthOffset) / _pixelsPerUnit;
            var r = position.Y / _pixelsPerUnit;
            return new Vector(Math.Floor(r), Math.Floor(c));
        }

        public Vector GetCoordinates(Vector cellCoordinates)
        {
            return new Vector(_widthOffset + cellCoordinates.Y * _pixelsPerUnit, cellCoordinates.X * _pixelsPerUnit);
        }

        public void CheckAllDeaths()
        {
            foreach (var p in Players)
            {
                CheckDeath(p);
            }
        }

        protected override void Initialize()
        {
            _fieldSize = FIELD_MIN_SIZE + ((int)(Players.Count / 4)) * 2;

            _field = new RectbombularThing[_fieldSize, _fieldSize];

            _playerSpeed = PLAYER_SPEED / _fieldSize;

            _pixelsPerUnit = Graphics.Height / _fieldSize;
            _widthOffset = (Graphics.Width - Graphics.Height) / 2d;

            // initialize all obstacles
            InitializeField();

            // initialize players
            InitializePlayers();
        }

        protected override void Update()
        {
            CheckCompleted();

            foreach (var p in this.Players)
            {
                DrawField(p);

                p.Draw(p.Graphics);

                /* Skip players that have already finished. */
                if (!p.Alive) continue;

                p.Score = Frame;

                MovePlayer(p);

                DropBomb(p);

                CheckDeath(p);
            }
        }

        private void InitializeField()
        {
            for (int r = 0; r < _fieldSize; r++)
            {
                for (int c = 0; c < _fieldSize; c++)
                {
                    if (r == 0 || r == _fieldSize - 1 || c == 0 || c == _fieldSize - 1 || r % 2 == 0 && c % 2 == 0)
                    {
                        //_field[r, c] = new Unbombable(this, new Vector(_widthOffset + c * _pixelsPerUnit, r * _pixelsPerUnit), new Vector(_pixelsPerUnit, _pixelsPerUnit));
                        this[r, c] = new Unbombable(this, CellSize);
                    }
                    else
                    {
                        if (this.Random.NextDouble() <= BOMBSTACLE_PROBABILITY)
                        {
                            this[r, c] = new Bombstacle(this, CellSize);
                        }
                    }
                }
            }
        }

        private void InitializePlayers()
        {
            int playersPerSide = (int)Math.Ceiling(Players.Count / 4d);
            BomberGuy[,] playerArray = new BomberGuy[4, playersPerSide];
            int side = 0;
            int playerCount = 0;
            foreach (BomberGuy p in Players)
            {
                playerArray[side, playerCount] = p;
                if (side++ > 3)
                {
                    side = 0;
                    playerCount++;
                }
            }

            Vector direction = new Vector(1, 0);
            int distance = (int)Math.Floor((double)(_fieldSize - 2) / playersPerSide);
            Vector currPos = new Vector(1, 1);
            for (int i = 0; i < 4; i++)
            {
                if (i == 1) currPos = new Vector(_fieldSize - 2, 1);
                if (i == 2) currPos = new Vector(_fieldSize - 2, _fieldSize - 2);
                if (i == 3) currPos = new Vector(1, _fieldSize - 2);<commit_msg>Bugfix: Bomberguy doesn't crash anymore when there are more than four players<commit_after>﻿// Copyright (c) the authors of nanoGames. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt in the project root.

using System;

namespace NanoGames.Games.Bomberguy
{
    internal class BomberMatch : Match<BomberGuy>
    {
        public const int FIELD_MIN_SIZE = 9;
        public const double PLAYER_RATIO = .9;
        public const double BOMB_RATIO = 1;
        public const int BOMB_REACH = 2;
        public const double PLAYER_SPEED = 9;
        public const double BOMBSTACLE_PROBABILITY = 0.5;

        private int _fieldSize;
        private double _playerSpeed;
        private double _pixelsPerUnit;
        private double _widthOffset;
        private RectbombularThing[,] _field;

        public Vector CellSize
        {
            get { return new Vector(_pixelsPerUnit, _pixelsPerUnit); }
        }

        public RectbombularThing this[Vector v]
        {
            get
            {
                return this[(int)v.X, (int)v.Y];
            }

            set
            {
                this[(int)v.X, (int)v.Y] = value;
            }
        }

        public RectbombularThing this[int x, int y]
        {
            get
            {
                return _field[x, y];
            }

            set
            {
                _field[x, y] = value;
                if (value != null) value.Position = GetCoordinates(new Vector(x, y));
            }
        }

        public Vector GetCell(BomberThing thing)
        {
            var c = (thing.Center.X - _widthOffset) / _pixelsPerUnit;
            var r = thing.Center.Y / _pixelsPerUnit;
            return new Vector(Math.Floor(r), Math.Floor(c));
        }

        public Vector GetCell(Vector position)
        {
            var c = (position.X - _widthOffset) / _pixelsPerUnit;
            var r = position.Y / _pixelsPerUnit;
            return new Vector(Math.Floor(r), Math.Floor(c));
        }

        public Vector GetCoordinates(Vector cellCoordinates)
        {
            return new Vector(_widthOffset + cellCoordinates.Y * _pixelsPerUnit, cellCoordinates.X * _pixelsPerUnit);
        }

        public void CheckAllDeaths()
        {
            foreach (var p in Players)
            {
                CheckDeath(p);
            }
        }

        protected override void Initialize()
        {
            _fieldSize = FIELD_MIN_SIZE + ((int)(Players.Count / 4)) * 2;

            _field = new RectbombularThing[_fieldSize, _fieldSize];

            _playerSpeed = PLAYER_SPEED / _fieldSize;

            _pixelsPerUnit = Graphics.Height / _fieldSize;
            _widthOffset = (Graphics.Width - Graphics.Height) / 2d;

            // initialize all obstacles
            InitializeField();

            // initialize players
            InitializePlayers();
        }

        protected override void Update()
        {
            CheckCompleted();

            foreach (var p in this.Players)
            {
                DrawField(p);

                p.Draw(p.Graphics);

                /* Skip players that have already finished. */
                if (!p.Alive) continue;

                p.Score = Frame;

                MovePlayer(p);

                DropBomb(p);

                CheckDeath(p);
            }
        }

        private void InitializeField()
        {
            for (int r = 0; r < _fieldSize; r++)
            {
                for (int c = 0; c < _fieldSize; c++)
                {
                    if (r == 0 || r == _fieldSize - 1 || c == 0 || c == _fieldSize - 1 || r % 2 == 0 && c % 2 == 0)
                    {
                        //_field[r, c] = new Unbombable(this, new Vector(_widthOffset + c * _pixelsPerUnit, r * _pixelsPerUnit), new Vector(_pixelsPerUnit, _pixelsPerUnit));
                        this[r, c] = new Unbombable(this, CellSize);
                    }
                    else
                    {
                        if (this.Random.NextDouble() <= BOMBSTACLE_PROBABILITY)
                        {
                            this[r, c] = new Bombstacle(this, CellSize);
                        }
                    }
                }
            }
        }

        private void InitializePlayers()
        {
            int playersPerSide = (int)Math.Ceiling(Players.Count / 4d);
            BomberGuy[,] playerArray = new BomberGuy[4, playersPerSide];
            int side = 0;
            int playerCount = 0;
            foreach (BomberGuy p in Players)
            {
                playerArray[side, playerCount] = p;
                if (++side > 3)
                {
                    side = 0;
                    playerCount++;
                }
            }

            Vector direction = new Vector(1, 0);
            int distance = (int)Math.Floor((double)(_fieldSize - 2) / playersPerSide);
            Vector currPos = new Vector(1, 1);
            for (int i = 0; i < 4; i++)
            {
                if (i == 1) currPos = new Vector(_fieldSize - 2, 1);
                if (i == 2) currPos = new Vector(_fieldSize - 2, _fieldSize - 2);
                if (i == 3) currPos = new Vector(1, _fieldSize - 2);
{'fraction_non_alphanumeric': 0.0935638808837656, 'fraction_numerical': 0.008069164265129682, 'mean_word_length': 1.2901451825780905, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2964969', 'n_tokens_mistral': 3053, 'n_tokens_neox': 2808, 'n_words': 740}