Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Game {
- private class Measurement {
- public Measurement(string name, int minVal, int maxVal, int val) {
- Name = name;
- MinVal = minVal;
- MaxVal = maxVal;
- Val = val;
- }
- public string Name { get; }
- public int MinVal { get; }
- public int MaxVal { get; }
- public int Val { get; private set; }
- public void SetValue(int newValue) {
- if( Val < MinVal ) {
- Val = MinVal;
- }
- else if( Val > MaxVal ) {
- Val = MaxVal;
- }
- else {
- Val = newValue;
- }
- }
- public override string ToString() => $"{Name} = {Val}";
- }
- private State _currentState = new StartGame();
- public void Run() {
- while( true ) {
- _currentState.Process(this);
- }
- }
- private class StartGame : State {
- public override void Process(Game game) {
- begin:
- Console.Clear();
- Console.WriteLine("Добро пожаловать в игру Virtual pet");
- Console.WriteLine("Введите ваше имя");
- name_input:
- // select an player name
- string playerName = Console.ReadLine();
- if( string.IsNullOrEmpty(playerName) )
- goto name_input;
- pet_input:
- // select an pet
- Console.WriteLine("Выберите питомца");
- Console.WriteLine("1. Кот");
- Console.WriteLine("2. Собака");
- Console.WriteLine("3. Рыбка");
- if( int.TryParse(Console.ReadLine(), out var pet) is false || pet is < 1 or > 3 )
- goto pet_input;
- pet_name_input:
- Console.WriteLine("Введите имя питомца");
- var petName = Console.ReadLine();
- if( string.IsNullOrEmpty(petName) )
- goto pet_name_input;
- Console.WriteLine("Настройка закончена");
- Console.WriteLine("Вас зовут : {0}", playerName);
- Console.WriteLine("Петомец : {0}", GetPetType());
- Console.WriteLine("Петомца зовут : {0}", petName);
- Console.WriteLine("Нажимте [Enter] чтобы начать игру");
- Console.ReadLine();
- game._currentState = new VirtualPetGame(
- new Pet() {
- PetType = GetPetType(),
- Name = petName
- },
- new Player(playerName)
- );
- string GetPetType() => pet switch {
- 1 => "Кот",
- 2 => "Собака",
- 3 => "Рыбка",
- };
- }
- }
- private class Store {
- }
- private class Goods {
- public Goods(string name, string description, int buyCost, int sellCost) {
- Name = name;
- Description = description;
- BuyCost = buyCost;
- SellCost = sellCost;
- }
- public string Name { get; }
- public string Description { get; }
- public int BuyCost { get; }
- public int SellCost { get; }
- }
- private class Player {
- private Measurement _money = new Measurement("Money", 0, 9_999_999, 1000);
- private string _playerName;
- public Player(String playerName) {
- _playerName = playerName;
- }
- public string Name => _playerName;
- public int Moneys => _money.Val;
- public void AddMoney(int count) => _money.SetValue(_money.Val + count);
- public bool TryTakeMoney(int count) {
- if( _money.Val < count ) {
- return false;
- }
- _money.SetValue(_money.Val - count);
- return true;
- }
- }
- private class Pet {
- private Measurement _hunger = new("Hunger", 0, 100, 50);
- private Measurement _joy = new("Joy", 0, 100, 50);
- private Measurement _energy = new("Energy", 0, 100, 50);
- public string Name { get; set; }
- public string PetType { get; set; }
- public Int32 Hunger => _hunger.Val;
- public Int32 Energy => _energy.Val;
- public Int32 Joy => _joy.Val;
- public void SeeStats() {
- Console.WriteLine($"Вы осматриваете {Name}");
- Console.WriteLine("Вам кажется что:");
- Console.WriteLine($"Он голоден на {Hunger}%");
- Console.WriteLine($"Весел : {Joy}%");
- Console.WriteLine($"Имеет энергии : {Energy}%");
- }
- public void Play() {
- if( _energy.Val < 5 ) {
- Console.WriteLine($"{Name} выглядит вялым и не хочет играть...");
- return;
- }
- if( _hunger.Val < 25 ) {
- Console.WriteLine($"{Name} голоден и не хочет играть...");
- return;
- }
- _joy.SetValue(_joy.Val + 15);
- _energy.SetValue(_energy.Val - 5);
- }
- public void Feed() {
- _energy.SetValue(_energy.Val + 10);
- _hunger.SetValue(_hunger.Val - 50);
- }
- internal void Sleep() {
- _energy.SetValue(_energy.Val + 50);
- }
- internal void LooseEnergy(Int32 value) => _energy.SetValue(_energy.Val - value);
- internal void LooseJoy(Int32 value) => _joy.SetValue(_joy.Val - value);
- internal void IncreaseHunger(Int32 value) => _hunger.SetValue(_hunger.Val + value);
- }
- private class VirtualPetGame : State {
- private readonly Pet _pet;
- private readonly Player _player;
- int _dayCount = 0;
- int _actionsCount;
- public VirtualPetGame(Pet pet, Player player) {
- _pet = pet;
- _player = player;
- }
- public override void Process(Game game) {
- _dayCount++;
- _actionsCount = 3;
- if( _actionsCount < 1 )
- return;
- _pet.LooseEnergy(5);
- _pet.LooseJoy(10);
- _pet.IncreaseHunger(10);
- if( _pet.Hunger >= 100 ) {
- Console.WriteLine($"{_pet.Name} умер от голода... Игра окончена");
- }
- Console.WriteLine($"День : {_dayCount}");
- begin:
- Console.WriteLine($"Осталось действий: {_actionsCount}");
- Console.WriteLine("Выберите действие");
- Console.WriteLine("1. Поиграть");
- Console.WriteLine("2. Покормить");
- Console.WriteLine("3. Пойти спать");
- Console.WriteLine("4. Пойти в магазин");
- Console.WriteLine("5. Пойти работать");
- Console.WriteLine("6. Проверить питомца");
- var action = Console.ReadLine();
- if( int.TryParse(action, out var val) is false || val is < 1 or > 6 )
- goto begin;
- if( val < 5 ) {
- _actionsCount--;
- }
- switch( val ) {
- case 1:
- _pet.Play();
- break;
- case 2:
- _pet.Feed();
- break;
- case 3:
- _pet.Sleep();
- return;
- case 4:
- Console.WriteLine("Вы отправились в магазин");
- // надо придумать интерфейс и товары
- break;
- case 5:
- _player.AddMoney(Random.Shared.Next(100, 500));
- break;
- case 6:
- _pet.SeeStats();
- break;
- }
- if( _actionsCount > 0 )
- goto begin;
- Console.WriteLine($"День закончился... Вы и {_pet.Name} отправились спать");
- _pet.Sleep();
- }
- }
- private abstract class State {
- public abstract void Process(Game game);
- }
Advertisement
Add Comment
Please, Sign In to add comment