Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace ConsoleApp19
- {
- //курсор
- public struct CursorPosition
- {
- public int X {get; set;}
- public int Y { get; set; }
- public CursorPosition(int x, int y) => (X, Y) = (x, y);
- }
- public enum ShipType
- {
- Small = 1,
- Medium,
- Large,
- XLarge
- }
- public enum ShootResult
- {
- Miss,
- Success
- }
- public class Field
- {
- private readonly int _width;
- private readonly int _height;
- //поле
- private int[,] _feild;
- //выстрелы
- private Dictionary<(int, int), ShootResult> _results = new();
- public Field(int width, int heigth)
- {
- _width = width;
- _height = heigth;
- _feild = new int[_width, _height];
- }
- public bool PlaceShip(CursorPosition position, ShipType type, bool rotateHorizontal)
- {
- //проверяем можно ли поставить корабль
- //если можно - ставим
- //я правила уже не помню, как там ставится
- return true;
- }
- public bool IsAllShipDestroyed()
- {
- //проверяешь что все корабли убиты
- return false;
- }
- public ShootResult Shot(CursorPosition position)
- {
- _results[(position.X, position.Y)] = _feild[position.X, position.Y] > 0 ? ShootResult.Success : ShootResult.Miss;
- return _results[(position.X, position.Y)];
- }
- public void DrawField(CursorPosition position)
- {
- //рисуем поле полностью, ничего не скрывая
- for(int y = position.Y; y < position.Y + _height; y++)
- {
- for (int x = position.X; x < position.X + _width; x++)
- {
- Console.SetCursorPosition(x, y);
- int posX = x - position.X;
- int posY = y - position.Y;
- var cell = _feild[posX, posY];
- //логика рисования того что на поле
- }
- }
- }
- //поле пока игра не закончена
- public void DrawFogOfWarField(CursorPosition position)
- {
- //рисуем поле полностью, ничего не скрывая
- for (int y = position.Y; y < position.Y + _height; y++)
- {
- for (int x = position.X; x < position.X + _width; x++)
- {
- Console.SetCursorPosition(x, y);
- int posX = x - position.X;
- int posY = y - position.Y;
- if(_results.ContainsKey((posX, posY)))
- {
- var cell = _results[(posX, posY)];
- //логика рисования результатов
- }
- }
- }
- }
- }
- public class UserInterface
- {
- public void DrawUserInterface(CursorPosition position, string message)
- {
- Console.SetCursorPosition(position.X, position.Y);
- Console.Write(message);
- }
- }
- public class Game
- {
- public string GameName = "Морской бой";
- //поле для первого игрока
- private Field _player1Field = new(10, 10);
- //поле для второго игрока
- private Field _player2Field = new(10, 10);
- //поле сейчас ходит первый игрок
- private bool IsFirstPlayerTurn = true;
- UserInterface ui = new();
- public void Run()
- {
- //реализуешь инциирующую логику по заполнению кораблей
- while(true)
- {
- var field = IsFirstPlayerTurn ? _player2Field : _player1Field;
- ui.DrawUserInterface(new CursorPosition(0, 0), $"Player {(IsFirstPlayerTurn ? 1 : 2)} turn");
- field.DrawFogOfWarField(new CursorPosition(0,1));
- MakeTurn(field);
- if (field.IsAllShipDestroyed())
- {
- break;
- }
- IsFirstPlayerTurn = !IsFirstPlayerTurn;
- }
- Console.Clear();
- Console.WriteLine($"Player {(IsFirstPlayerTurn ? 1 : 2)} win!!!");
- }
- private void MakeTurn(Field field)
- {
- while (true)
- {
- //логику по управлению курсором
- //промазали - выходим из цикла
- //if(field.Shot(position) == ShootResult.Miss)
- //{
- //break
- //}
- if (field.IsAllShipDestroyed())
- {
- break;
- }
- }
- }
- private void GetMessage(CursorPosition position, int xOffset = 1, int yOffset = 0)
- {
- Console.WriteLine($"Position X = {position.X - 1} Y = {TranslatePosition(position.Y)}");
- }
- private char TranslatePosition(int position)
- {
- //смещение по таблице ASCII
- return (char)( 41 + position );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement