Advertisement
Guest User

Untitled

a guest
Dec 19th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp19
  5. {
  6.     //курсор
  7.     public struct CursorPosition
  8.     {
  9.         public int X {get; set;}
  10.         public int Y { get; set; }
  11.         public CursorPosition(int x, int y) => (X, Y) = (x, y);
  12.     }
  13.  
  14.     public enum ShipType
  15.     {
  16.         Small = 1,
  17.         Medium,
  18.         Large,
  19.         XLarge
  20.     }
  21.     public enum ShootResult
  22.     {
  23.         Miss,
  24.         Success
  25.     }
  26.  
  27.     public class Field
  28.     {
  29.         private readonly int _width;
  30.         private readonly int _height;
  31.         //поле
  32.         private int[,] _feild;
  33.         //выстрелы
  34.         private Dictionary<(int, int), ShootResult> _results = new();
  35.         public Field(int width, int heigth)
  36.         {
  37.             _width = width;
  38.             _height = heigth;
  39.             _feild = new int[_width, _height];
  40.         }
  41.        
  42.         public bool PlaceShip(CursorPosition position, ShipType type, bool rotateHorizontal)
  43.         {
  44.             //проверяем можно ли поставить корабль
  45.             //если можно - ставим
  46.             //я правила уже не помню, как там ставится
  47.             return true;
  48.         }
  49.  
  50.         public bool IsAllShipDestroyed()
  51.         {
  52.             //проверяешь что все корабли убиты
  53.             return false;
  54.         }
  55.  
  56.         public ShootResult Shot(CursorPosition position)
  57.         {
  58.             _results[(position.X, position.Y)] = _feild[position.X, position.Y] > 0 ? ShootResult.Success : ShootResult.Miss;
  59.             return _results[(position.X, position.Y)];
  60.         }
  61.  
  62.         public void DrawField(CursorPosition position)
  63.         {
  64.             //рисуем поле полностью, ничего не скрывая
  65.             for(int y = position.Y; y < position.Y + _height; y++)
  66.             {
  67.                 for (int x = position.X; x < position.X + _width; x++)
  68.                 {
  69.                     Console.SetCursorPosition(x, y);
  70.                     int posX = x - position.X;
  71.                     int posY = y - position.Y;
  72.                     var cell = _feild[posX, posY];
  73.  
  74.                     //логика рисования того что на поле
  75.                 }
  76.             }
  77.         }
  78.  
  79.         //поле пока игра не закончена
  80.         public void DrawFogOfWarField(CursorPosition position)
  81.         {
  82.  
  83.             //рисуем поле полностью, ничего не скрывая
  84.             for (int y = position.Y; y < position.Y + _height; y++)
  85.             {
  86.                 for (int x = position.X; x < position.X + _width; x++)
  87.                 {
  88.                     Console.SetCursorPosition(x, y);
  89.                     int posX = x - position.X;
  90.                     int posY = y - position.Y;
  91.                    
  92.                     if(_results.ContainsKey((posX, posY)))
  93.                     {
  94.                         var cell = _results[(posX, posY)];
  95.                         //логика рисования результатов
  96.                     }
  97.  
  98.                 }
  99.             }
  100.         }
  101.     }
  102.  
  103.     public class UserInterface
  104.     {
  105.         public void DrawUserInterface(CursorPosition position, string message)
  106.         {
  107.             Console.SetCursorPosition(position.X, position.Y);
  108.             Console.Write(message);
  109.         }
  110.     }
  111.  
  112.     public class Game
  113.     {
  114.         public string GameName = "Морской бой";
  115.  
  116.         //поле для первого игрока
  117.         private Field _player1Field = new(10, 10);
  118.  
  119.         //поле для второго игрока
  120.         private Field _player2Field = new(10, 10);
  121.  
  122.         //поле сейчас ходит первый игрок
  123.         private bool IsFirstPlayerTurn = true;
  124.         UserInterface ui = new();
  125.         public void Run()
  126.         {
  127.             //реализуешь инциирующую логику по заполнению кораблей
  128.             while(true)
  129.             {
  130.                 var field = IsFirstPlayerTurn ? _player2Field : _player1Field;
  131.                 ui.DrawUserInterface(new CursorPosition(0, 0), $"Player {(IsFirstPlayerTurn ? 1 : 2)} turn");
  132.                 field.DrawFogOfWarField(new CursorPosition(0,1));
  133.                 MakeTurn(field);
  134.                 if (field.IsAllShipDestroyed())
  135.                 {
  136.                     break;
  137.                 }
  138.                 IsFirstPlayerTurn = !IsFirstPlayerTurn;
  139.             }
  140.             Console.Clear();
  141.             Console.WriteLine($"Player {(IsFirstPlayerTurn ? 1 : 2)} win!!!");
  142.         }
  143.  
  144.         private void MakeTurn(Field field)
  145.         {
  146.             while (true)
  147.             {
  148.                 //логику по управлению курсором
  149.                 //промазали - выходим из цикла
  150.                 //if(field.Shot(position) == ShootResult.Miss)
  151.                 //{
  152.                 //break
  153.                 //}
  154.                 if (field.IsAllShipDestroyed())
  155.                 {
  156.                     break;
  157.                 }
  158.             }            
  159.         }
  160.  
  161.         private void GetMessage(CursorPosition position, int xOffset = 1, int yOffset = 0)
  162.         {
  163.             Console.WriteLine($"Position X = {position.X - 1} Y = {TranslatePosition(position.Y)}");
  164.         }
  165.  
  166.         private char TranslatePosition(int position)
  167.         {
  168.             //смещение по таблице ASCII
  169.             return (char)( 41 + position );
  170.         }
  171.     }
  172. }
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement