Guest User

Untitled

a guest
Jul 23rd, 2023
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.37 KB | Source Code | 0 0
  1. public class Game {
  2.     private class Measurement {
  3.         public Measurement(string name, int minVal, int maxVal, int val) {
  4.             Name = name;
  5.             MinVal = minVal;
  6.             MaxVal = maxVal;
  7.             Val = val;
  8.         }
  9.  
  10.         public string Name { get; }
  11.         public int MinVal { get; }
  12.         public int MaxVal { get; }
  13.         public int Val { get; private set; }
  14.         public void SetValue(int newValue) {
  15.             if( Val < MinVal ) {
  16.                 Val = MinVal;
  17.             }
  18.             else if( Val > MaxVal ) {
  19.                 Val = MaxVal;
  20.             }
  21.             else {
  22.                 Val = newValue;
  23.             }
  24.         }
  25.         public override string ToString() => $"{Name} = {Val}";
  26.     }
  27.  
  28.     private State _currentState = new StartGame();
  29.  
  30.     public void Run() {
  31.         while( true ) {
  32.             _currentState.Process(this);
  33.         }
  34.  
  35.     }
  36.  
  37.  
  38.     private class StartGame : State {
  39.  
  40.         public override void Process(Game game) {
  41.         begin:
  42.  
  43.             Console.Clear();
  44.  
  45.             Console.WriteLine("Добро пожаловать в игру Virtual pet");
  46.             Console.WriteLine("Введите ваше имя");
  47.         name_input:
  48.             // select an player name
  49.             string playerName = Console.ReadLine();
  50.             if( string.IsNullOrEmpty(playerName) )
  51.                 goto name_input;
  52.  
  53.             pet_input:
  54.             // select an pet
  55.             Console.WriteLine("Выберите питомца");
  56.             Console.WriteLine("1. Кот");
  57.             Console.WriteLine("2. Собака");
  58.             Console.WriteLine("3. Рыбка");
  59.             if( int.TryParse(Console.ReadLine(), out var pet) is false || pet is < 1 or > 3 )
  60.                 goto pet_input;
  61.  
  62.             pet_name_input:
  63.             Console.WriteLine("Введите имя питомца");
  64.             var petName = Console.ReadLine();
  65.  
  66.             if( string.IsNullOrEmpty(petName) )
  67.                 goto pet_name_input;
  68.  
  69.             Console.WriteLine("Настройка закончена");
  70.  
  71.             Console.WriteLine("Вас зовут     : {0}", playerName);
  72.             Console.WriteLine("Петомец       : {0}", GetPetType());
  73.             Console.WriteLine("Петомца зовут : {0}", petName);
  74.  
  75.             Console.WriteLine("Нажимте [Enter] чтобы начать игру");
  76.             Console.ReadLine();
  77.  
  78.             game._currentState = new VirtualPetGame(
  79.                 new Pet() {
  80.                     PetType = GetPetType(),
  81.                     Name = petName
  82.                 },
  83.                 new Player(playerName)
  84.             );
  85.  
  86.             string GetPetType() => pet switch {
  87.                 1 => "Кот",
  88.                 2 => "Собака",
  89.                 3 => "Рыбка",
  90.             };
  91.         }
  92.     }
  93.  
  94.     private class Store {
  95.  
  96.  
  97.     }
  98.  
  99.     private class Goods {
  100.         public Goods(string name, string description, int buyCost, int sellCost) {
  101.             Name = name;
  102.             Description = description;
  103.             BuyCost = buyCost;
  104.             SellCost = sellCost;
  105.         }
  106.  
  107.         public string Name { get; }
  108.         public string Description { get; }
  109.         public int BuyCost { get; }
  110.         public int SellCost { get; }
  111.     }
  112.  
  113.     private class Player {
  114.         private Measurement _money = new Measurement("Money", 0, 9_999_999, 1000);
  115.         private string _playerName;
  116.  
  117.         public Player(String playerName) {
  118.             _playerName = playerName;
  119.         }
  120.  
  121.         public string Name => _playerName;
  122.         public int Moneys => _money.Val;
  123.  
  124.         public void AddMoney(int count) => _money.SetValue(_money.Val + count);
  125.  
  126.         public bool TryTakeMoney(int count) {
  127.             if( _money.Val < count ) {
  128.                 return false;
  129.             }
  130.  
  131.             _money.SetValue(_money.Val - count);
  132.             return true;
  133.         }
  134.     }
  135.  
  136.     private class Pet {
  137.         private Measurement _hunger = new("Hunger", 0, 100, 50);
  138.         private Measurement _joy = new("Joy", 0, 100, 50);
  139.         private Measurement _energy = new("Energy", 0, 100, 50);
  140.         public string Name { get; set; }
  141.         public string PetType { get; set; }
  142.         public Int32 Hunger => _hunger.Val;
  143.         public Int32 Energy => _energy.Val;
  144.         public Int32 Joy => _joy.Val;
  145.  
  146.         public void SeeStats() {
  147.             Console.WriteLine($"Вы осматриваете {Name}");
  148.             Console.WriteLine("Вам кажется что:");
  149.             Console.WriteLine($"Он голоден на {Hunger}%");
  150.             Console.WriteLine($"Весел : {Joy}%");
  151.             Console.WriteLine($"Имеет энергии : {Energy}%");
  152.         }
  153.  
  154.         public void Play() {
  155.  
  156.             if( _energy.Val < 5 ) {
  157.                 Console.WriteLine($"{Name} выглядит вялым и не хочет играть...");
  158.                 return;
  159.             }
  160.  
  161.             if( _hunger.Val < 25 ) {
  162.                 Console.WriteLine($"{Name} голоден и не хочет играть...");
  163.                 return;
  164.             }
  165.  
  166.             _joy.SetValue(_joy.Val + 15);
  167.             _energy.SetValue(_energy.Val - 5);
  168.         }
  169.  
  170.  
  171.         public void Feed() {
  172.             _energy.SetValue(_energy.Val + 10);
  173.             _hunger.SetValue(_hunger.Val - 50);
  174.         }
  175.  
  176.         internal void Sleep() {
  177.             _energy.SetValue(_energy.Val + 50);
  178.         }
  179.  
  180.         internal void LooseEnergy(Int32 value) => _energy.SetValue(_energy.Val - value);
  181.         internal void LooseJoy(Int32 value) => _joy.SetValue(_joy.Val - value);
  182.         internal void IncreaseHunger(Int32 value) => _hunger.SetValue(_hunger.Val + value);
  183.     }
  184.     private class VirtualPetGame : State {
  185.         private readonly Pet _pet;
  186.         private readonly Player _player;
  187.  
  188.         int _dayCount = 0;
  189.         int _actionsCount;
  190.  
  191.         public VirtualPetGame(Pet pet, Player player) {
  192.             _pet = pet;
  193.             _player = player;
  194.         }
  195.  
  196.         public override void Process(Game game) {
  197.             _dayCount++;
  198.             _actionsCount = 3;
  199.             if( _actionsCount < 1 )
  200.                 return;
  201.  
  202.             _pet.LooseEnergy(5);
  203.             _pet.LooseJoy(10);
  204.             _pet.IncreaseHunger(10);
  205.  
  206.             if( _pet.Hunger >= 100 ) {
  207.                 Console.WriteLine($"{_pet.Name} умер от голода... Игра окончена");
  208.             }
  209.  
  210.             Console.WriteLine($"День : {_dayCount}");
  211.         begin:
  212.             Console.WriteLine($"Осталось действий: {_actionsCount}");
  213.             Console.WriteLine("Выберите действие");
  214.             Console.WriteLine("1. Поиграть");
  215.             Console.WriteLine("2. Покормить");
  216.             Console.WriteLine("3. Пойти спать");
  217.             Console.WriteLine("4. Пойти в магазин");
  218.             Console.WriteLine("5. Пойти работать");
  219.             Console.WriteLine("6. Проверить питомца");
  220.  
  221.             var action = Console.ReadLine();
  222.  
  223.             if( int.TryParse(action, out var val) is false || val is < 1 or > 6 )
  224.                 goto begin;
  225.  
  226.             if( val < 5 ) {
  227.                 _actionsCount--;
  228.             }
  229.  
  230.             switch( val ) {
  231.                 case 1:
  232.                     _pet.Play();
  233.                     break;
  234.                 case 2:
  235.                     _pet.Feed();
  236.                     break;
  237.                 case 3:
  238.                     _pet.Sleep();
  239.                     return;
  240.  
  241.                 case 4:
  242.                     Console.WriteLine("Вы отправились в магазин");
  243.                     // надо придумать интерфейс и товары
  244.                     break;
  245.                 case 5:
  246.                     _player.AddMoney(Random.Shared.Next(100, 500));
  247.                     break;
  248.                 case 6:
  249.                     _pet.SeeStats();
  250.                     break;
  251.  
  252.             }
  253.             if( _actionsCount > 0 )
  254.                 goto begin;
  255.  
  256.             Console.WriteLine($"День закончился... Вы и {_pet.Name} отправились спать");
  257.             _pet.Sleep();
  258.         }
  259.     }
  260.  
  261.     private abstract class State {
  262.         public abstract void Process(Game game);
  263.     }
Advertisement
Add Comment
Please, Sign In to add comment