Reputation: 25
I've been trying to access an instance lykkesHest of my class horse from a third class game. But it tells me that the instance does not exist in the current context which makes sence because when lykkesHest hest is made it is not made as a public class. But when i try and make lykkesHest a public class instead it gives me a lot of weird error. I apologize for the text outputted to the user as it is danish.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lykkes_Hest
{
// The class with the main game hidden inside it!
class game
{
public static int money;
public static double fodder;
public game()
{
money = 1000;
fodder = 1;
}
public static void help()
{
Console.WriteLine("#######################");
Console.WriteLine("###### - HJÆLP - ######");
Console.WriteLine("#######################");
Console.WriteLine();
Console.WriteLine("Kommandoer du kan bruge:");
Console.WriteLine("Hjælp - viser dig denne skærm");
Console.WriteLine("fodre hesten - giver din hest mad hvis du har noget");
}
public static void exitSafely()
{
Console.WriteLine("Er du sikker på at du vil forlade spillet?");
Console.WriteLine("(Ja eller nej)");
if(Program.getBool())
{
Environment.Exit(0);
}
}
public void RegisterCommand()
{
string command = Console.ReadLine();
switch(command)
{
case "exit":
exitSafely();
break;
case "vent":
lykkesHest.PassDay();
break;
default:
Console.WriteLine("Ikke genkendt kommando.");
break;
}
}
}
// A class for making a horse
class horse
{
public string name;
public double age = 0;
public string color;
public double hunger = 50;
public double boredom = 50;
public horse()
{
Console.WriteLine("Hvad skal din hest hedde?");
name = Console.ReadLine();
Console.WriteLine("Hvor mange år er din hest?");
while (age == 0) {
try
{
age = Convert.ToInt32(Console.ReadLine());
} catch {
Console.WriteLine("Do skal skrive et tal.");
}
}
Console.WriteLine("Hvilken farve har din hest?");
color = Console.ReadLine();
}
public void Print()
{
Console.WriteLine("Din hest heder {0} er {1} år gammel og har farven {2}", name, age, color);
}
public static void Die(string cause)
{
Console.WriteLine("Din hest døde af {0}........", cause);
Console.WriteLine("Du burde passe bedre på den en anden gang!");
Console.WriteLine();
Console.WriteLine("Klik på en knap for at afslutte.");
Console.ReadKey();
Environment.Exit(0);
}
public void PassDay()
{
hunger =+ 10;
boredom =+ 10;
if(hunger >= 100 || boredom >= 100)
{
horse.Die("sult");
} else if(boredom >= 100)
{
horse.Die("Kedsomhed");
}
}
}
class Program
{
public static bool getBool()
{
while (true)
{
string answer = Console.ReadLine();
if (answer.ToLower() == "ja")
{
return true;
}
else if (answer.ToLower() == "nej")
{
return false;
}
Console.WriteLine("Du må kun skrive ja eller nej");
}
}
static void Main(string[] args)
{
// Get passowrd
Console.WriteLine("HVAD ER KODEORDET FOR AT MÅ SPILLE DET HER AMAZING SPIL?");
if (Console.ReadLine().ToLower() != "what is password?")
{
Environment.Exit(0);
}
// Initialize the game
game Spil = new game();
Console.WriteLine("###############################");
Console.WriteLine("######## -Lykkes Hest- ########");
Console.WriteLine("###############################");
Console.WriteLine();
Console.WriteLine("Velommen, du spiller version 001");
Console.WriteLine();
// Create a basic horse
horse lykkesHest = new horse();
lykkesHest.Print();
Console.WriteLine("Er dette korrekt?");
while(getBool() == false)
{
lykkesHest = new horse();
}
// Optional tutorial
Console.WriteLine();
Console.WriteLine("Vil du gerne lære de kommandoer du kan bruge?");
Console.WriteLine("(Ja eller nej)");
if (getBool())
{
Console.WriteLine();
Console.WriteLine("Lykkes hest er et interaktivt sandbox spil hvor du styrer EN FUCKING HEST!!");
Console.WriteLine("Du bestemmer selv om du vil have din hest til at en flotter præmie hest eller en sy' hurtig ræcer!");
Console.WriteLine();
Console.WriteLine("Du spiller ved at skrive nogle få kommandoer ind i kommando linjen (Den du læser fra lige nu).");
Console.WriteLine();
Console.WriteLine("Hvis du nogensinde glemmer en kommando eller vil lære nogle nye kan du altid skrive 'Hjælp' for at få dem vist.");
Console.WriteLine("Og hvis du nogensinde vil stoppe spillet kan du gøre det ved at skrive 'exit'");
}
Console.WriteLine();
Console.WriteLine();
// Loop the rest of the game
while (true)
{
Console.WriteLine("#####################################################");
Console.WriteLine("Penge: {0} - Foder: {1}", game.money, game.fodder);
Console.WriteLine("Hestens sult: {0} - Hestens kedsomhed: {1}", lykkesHest.hunger, lykkesHest.boredom);
Console.WriteLine("#####################################################");
game.RegisterCommand();
Console.WriteLine();
}
}
}
}
Upvotes: 0
Views: 107
Reputation: 11101
If the game is about managing a horse, then the game class (which you should be calling Game to follow usual naming conventions) should contain a field of type horse:
class Horse
{
public int Age{ get; private set };
public int Hunger { get; private set; }
public string Name { get; private set; }
public Horse(string name)
{
Name = name;
Age = 0;
Hunger = 0;
}
public bool IsDead()
{
return Hunger >= 100 || Age > 3650;
}
void PassDay()
{
Age = Age + 1;
Hunger = Hunger + 1;
}
void Feed()
{
Hunger = 0;
}
// etc.
public override string ToString()
{
return string.Format("{0} Age {1}", Name, Age);
}
}
Either the game can create it's own horse instance, or you have to pass the horse instance into the Game's constructor. If the game manages one horse, and the game always has exactly one horse, it's a good practice to assign it in the constructor so that you can't even have a game without a horse.
I translated (and changed) your game somewhat to make the example clearer.
class Game
{
private readonly Horse horse;
private int money;
// more things
public Game(Horse horse, int money)
{
this.horse = horse;
this.money = money;
}
public bool IsOver
{
get { return horse.IsDead(); }
}
}
The main program class is very different. It's a static class and contains only static methods. The other classes (horse, game) are classes of which you create objects. You have one game, which has one horse and so on.
public static class Program
{
private static Game game;
public static void Main()
{
game = CreateNewGame();
do
{
// get input
// update game state
} while (!game.IsOver);
}
private static Game CreateNewGame()
{
Horse horse;
do
{
horse = CreateRandomHorse();
Console.WriteLine("Is this ok? : " + horse); // Calls horse.ToString()
} while (!GetBoolInput());
// have a horse, can make a game
return new Game(horse, 100);
}
}
Upvotes: 3