Sign in to follow this  
raidzero

C# Text Based Game Error

Recommended Posts

raidzero    112
I just finished up some last parts to the game. I run it and one of the options on the main menu is to view your stats.
So you type stats in and I end up with this error:
[code]
Unhandled Exception: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at System.IO.TextWriter.Write(String format, Object[] arg)
at System.IO.TextWriter.SyncTextWriter.Write(String format, Object[] arg)
at System.Console.Write(String format, Object[] arg)
at TextBasedGame.Stats.PrintStats(Hero hero) in C:\Users\Key\Documents\Visual Studio 2010\Projects\TextBasedGame\TextBasedGame\Stats.cs:line 11
at TextBasedGame.GameState.GameLoop() in C:\Users\Key\Documents\Visual Studio 2010\Projects\TextBasedGame\TextBasedGame\GameState.cs:line 42
at TextBasedGame.GameState..ctor() in C:\Users\Key\Documents\Visual Studio 2010\Projects\TextBasedGame\TextBasedGame\GameState.cs:line 20
at TextBasedGame.Program.Main() in C:\Users\Key\Documents\Visual Studio 2010\Projects\TextBasedGame\TextBasedGame\Program.cs:line 10[/code]

Stats.cs
[code]using System;
using System.Collections.Generic;

namespace TextBasedGame
{
public class Stats
{
public static void PrintStats(Hero hero)
{
Console.Clear();
Console.Write(@"
Stats Sheet
_____________
Name: {0}
Hp: {1}/{2}
Attack: {4}
Defense: {5}
Exp: {6}
Gold: {7}", hero.Id, hero.CurrentHealth, hero.MaxHealth, hero.Strength, hero.Defense, hero.Exp, hero.Gold);
Console.WriteLine();
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
Console.Clear();
}
}
}
[/code]

Share this post


Link to post
Share on other sites
raidzero    112
So I'm trying to develop a Main Menu for my game...trouble is that the hero's name doesn't display correctly. Can't figure this one out..
I created a save with all the info in it including the name of the hero. Choosing either Create or Load will display a blank for the name either way. So I'm assuming that my Hero class is incorrect in some way.

MainMenu.cs
[code]using System;
using System.Collections.Generic;

namespace TextBasedGame
{
public class MainMenu
{
Options data = new Options();
Hero myhero;
string answer;
public static string myheroName;
bool heroName = false;

public MainMenu()
{
Console.Title = "[Title]";
myhero = new Hero();
Hero.Initialize(myhero);

do
{
Console.Clear();
Console.WriteLine();
Console.Write(@"
_____________________________
Create
Load
Quit
_____________________________");
Console.WriteLine();
answer = Console.ReadLine();
Console.WriteLine();
switch (answer)
{
case "Quit":
case "quit":
Console.Clear();
Console.WriteLine("Goodbye");
Environment.Exit(0);
break;
case "Load":
case "load":
data.Load(myhero);
break;
case "Create":
case "create":
Console.WriteLine("Choose a name for your hero.");
Console.WriteLine();
myheroName = Console.ReadLine();
heroName = true;
if (heroName == true)
{
GameState gamestate = new GameState();
}
break;
}
}
while (heroName == false);
}
}
}[/code]

Stats.cs - the {0} displays a blank instead of hero.Id
[code] public class Stats
{
public static void PrintStats(Hero hero)
{
Console.Clear();
Console.Write(@"
_____________
Stats Sheet
_____________
Name: {0}
Hp: {1}/{2}
Attack: {3}
Defense: {4}
Exp: {5}
Gold: {6}
_____________
Equipment
_____________
Sword:
Helmet:
Armor:
_____________", hero.Id, hero.CurrentHealth, hero.MaxHealth, hero.Strength, hero.Defense, hero.Exp, hero.Gold);[/code]

Hero.cs
[code] public class Hero : Character
{
public List<string> items;
public static string myheroName = MainMenu.myheroName;

public Hero()
{
items = new List<string>();
}
public static void Initialize(Hero hero)
{
hero.Id = myheroName;[/code]

Share this post


Link to post
Share on other sites
raidzero    112
I was just tinkering with it to find a way to get it to work.

Hero.cs
[code] public static void Initialize(Hero hero)
{
hero.CurrentHealth = 100;
hero.MaxHealth = 100;
hero.CurrentMagic = 20;
hero.MaxMagic = 20;
hero.Strength = 10;
hero.Defense = 5;
hero.Exp = 0;
hero.Gold = 0;
hero.isAlive = true;
hero.PhysicalDamage = hero.Strength;
while (hero.Id == null || hero.Id == "" || hero.Id == " ")
{
Console.WriteLine("Choose a name for your hero.");
hero.Id = Console.ReadLine();
}
}[/code]

Options.cs - the loading part of it
[code] hero.CurrentHealth = int.Parse(file.ReadLine());
hero.MaxHealth = int.Parse(file.ReadLine());
hero.CurrentMagic = int.Parse(file.ReadLine());
hero.MaxMagic = int.Parse(file.ReadLine());
hero.Strength = int.Parse(file.ReadLine());
hero.Defense = int.Parse(file.ReadLine());
hero.Exp = int.Parse(file.ReadLine());
hero.Gold = int.Parse(file.ReadLine());
hero.PhysicalDamage = int.Parse(file.ReadLine());
hero.Id = file.ReadLine();[/code]

When I choose to load a save it ask me to choose a name, but isn't it already defined when I chose to load my game? If I do choose a new name, that name will be displayed instead of whats in my save file. How do I fix this?

Share this post


Link to post
Share on other sites
ApochPiQ    23005
First, you need to look up what "static" actually means and what it does. This should help you understand why your code is broken.

Second, you need to look up [i]constructors[/i], as they are the correct solution for what you are trying to do.


Make sure you don't skip Step One.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this