Пример #1
0
        public ShipCell[,] GetPersonalBoard()
        {
            var board = (Turn ? personalBoard2 : personalBoard1);
            var res   = new ShipCell[BoardSize, BoardSize];

            Array.Copy(board, res, board.Length);
            return(res);
        }
Пример #2
0
        public Battleships(GameSave save)
        {
            Player_1       = save.Player1;
            Player_2       = save.Player2;
            Touch          = save.Touch_;
            BoardSize      = save.BoardSize_;
            GameId         = save.GameSaveId;
            personalBoard1 = new ShipCell[BoardSize, BoardSize];
            battleBoard1   = new CellState[BoardSize, BoardSize];
            personalBoard2 = new ShipCell[BoardSize, BoardSize];
            battleBoard2   = new CellState[BoardSize, BoardSize];
            for (int col = 0; col < BoardSize; col++)
            {
                for (int row = 0; row < BoardSize; row++)
                {
                    personalBoard1[row, col] = new ShipCell();
                    personalBoard2[row, col] = new ShipCell();
                }
            }
            var tShipList1 = JsonSerializer.Deserialize <List <Ship> >(save.ShipList1_, jsonOptions) ?? new List <Ship>();

            try
            {
                foreach (var ship in tShipList1)
                {
                    PlaceShip(ship.SX, ship.SY, ship.EX, ship.EY);
                }
            }
            catch (Exception e)
            {
                Console.Clear();
                Console.WriteLine(e.Message);
            }
            Turn = !Turn;
            var tShipList2 = JsonSerializer.Deserialize <List <Ship> >(save.ShipList2_, jsonOptions) ?? new List <Ship>();

            foreach (var ship in tShipList2)
            {
                PlaceShip(ship.SX, ship.SY, ship.EX, ship.EY);
            }
            Turn = !Turn;
            List <GameMove> tLog = JsonSerializer.Deserialize <List <GameMove> >(save.MoveLog_, jsonOptions) ?? new List <GameMove>();

            foreach (var move in tLog)
            {
                MakeMove(move.X, move.Y);
            }
            ShipsReq = JsonSerializer.Deserialize <LinkedList <Boat> >(save.ShipsReq_, jsonOptions) ?? new LinkedList <Boat>();
        }
Пример #3
0
        public GameMove?MakeMove(int x, int y)
        {
            var target         = Turn ? battleBoard2 : battleBoard1;
            var player         = Turn ? personalBoard1 : personalBoard2;
            int destroyedCount = Turn ? shipsDestroyed2 : shipsDestroyed1;

            if (target[x, y] != CellState.EMPTY)
            {
                return(null);
            }
            ShipCell ship = player[x, y];

            if (ship.Ship == null)
            {
                target[x, y]       = CellState.SHOT_EMPTY;
                player[x, y].State = CellState.SHOT_EMPTY;
                Turn = !Turn;
            }
            else
            {
                target[x, y]       = CellState.SHOT_DAMAGED;
                player[x, y].State = CellState.SHOT_DAMAGED;
                ship.Ship.Shot();
                if (ship.Ship.status == ShipStatus.DESTROYED)
                {
                    destroyedCount = (int)destroyedCount + 1;
                    for (int row = ship.Ship.SX; row <= ship.Ship.EX; row++)
                    {
                        for (int col = ship.Ship.SY; col <= ship.Ship.EY; col++)
                        {
                            target[row, col]       = CellState.SHOT_DESTROYED;
                            player[row, col].State = CellState.SHOT_DESTROYED;
                        }
                    }
                }
            }
            if (Turn)
            {
                shipsDestroyed2 = destroyedCount;
            }
            else
            {
                shipsDestroyed1 = destroyedCount;
            }
            MoveLog.Add(new GameMove(x, y, (target[x, y] == CellState.SHOT_EMPTY ? !Turn : Turn), target[x, y]));
            CheckWin();
            return(MoveLog[MoveCount - 1].CopyOf());
        }
Пример #4
0
 public bool PlaceShip(int sX, int sY, int eX, int eY)
 {
     if (sX > eX)
     {
         int t = eX;
         eX = sX;
         sX = t;
         t  = eY;
         eY = sY;
         sY = t;
     }
     if (sX == eX && sY > eY)
     {
         int t = eY;
         eY = sY;
         sY = t;
     }
     if (CheckFree(sX, sY, eX, eY) == false)
     {
         return(false);
     }
     if (!Turn)
     {
         ShipList1.Add(new Ship(sX, sY, eX, eY));
     }
     else
     {
         ShipList2.Add(new Ship(sX, sY, eX, eY));
     }
     for (int indexRow = sX; indexRow <= eX; indexRow++)
     {
         for (int indexCol = sY; indexCol <= eY; indexCol++)
         {
             if (!Turn)
             {
                 personalBoard1[indexRow, indexCol] = new ShipCell(ShipList1.Last());
             }
             else
             {
                 personalBoard2[indexRow, indexCol] = new ShipCell(ShipList2.Last());
             }
         }
     }
     return(true);
 }
Пример #5
0
 public void Init()
 {
     GameId          = 0;
     Turn            = false;
     Check           = false;
     shipsDestroyed1 = 0;
     shipsDestroyed2 = 0;
     MoveLog.Clear();
     ShipList1.Clear();
     ShipList2.Clear();
     for (int rowIndex = 0; rowIndex < BoardSize; rowIndex++)
     {
         for (int colIndex = 0; colIndex < BoardSize; colIndex++)
         {
             personalBoard1[rowIndex, colIndex] = new ShipCell();
             personalBoard2[rowIndex, colIndex] = new ShipCell();
             battleBoard1[rowIndex, colIndex]   = CellState.EMPTY;
             battleBoard2[rowIndex, colIndex]   = CellState.EMPTY;
         }
     }
 }