C# Text Based Game Error

Started by
4 comments, last by ApochPiQ 12 years, 9 months ago
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:

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


Stats.cs
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();
}
}
}
Advertisement
You skipped {3} in your Console.Write call that prints out the stats sheet.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Your "stats sheet" is missing it's fourth parameter.
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
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);
}
}
}


Stats.cs - the {0} displays a blank instead of hero.Id
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);


Hero.cs
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;
What's your intention behind the static stuff in your Hero class? I don't think it does what you think it does.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I was just tinkering with it to find a way to get it to work.

Hero.cs
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();
}
}


Options.cs - the loading part of it
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();


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?
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 constructors, as they are the correct solution for what you are trying to do.


Make sure you don't skip Step One.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement