Jump to content

  • Log In with Google      Sign In   
  • Create Account

pulpfist

Member Since 01 Nov 2005
Offline Last Active Jan 29 2013 04:02 AM

Posts I've Made

In Topic: How to read file line

23 January 2013 - 06:36 AM

Maybe this is the problem

http://forums.asp.net/t/1239769.aspx/1


In Topic: The "Bounce" does not exist in current context

23 January 2013 - 04:51 AM

It seems to me you are trying to separate the games event handling from the games content, but you are doing it in a way that convolutes your code in ways you don't want.

Its hard to say though without the complete source.

 

The first thing that strikes me is, why do you have the GameItem inside the GameMechanics class? It doesn't seem right.

 

Another thing that strikes me is that the way the GameMechanics class is now, I would consider making it a static class with static variables/functions.

Then I would put the GameItem somewhere else and pass it as an argument to Bounce.

 

 
class GameItem
{
}
 
static class GameMechanics
{
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 480;        
 
    public static void Bounce(GameItem target)
    {
        // ...
    }
}
 
class Program
{
    static void Main(string[] args)
    {
        GameItem item = new GameItem();
        GameMechanics.Bounce(item);
    }
}


In Topic: How to handle this problem

22 January 2013 - 05:05 AM

I would make a loop so that the program continues. Also, if you make functions that will continue to ask for input until the input is valid, you don't really need a default

 

class Program
{
    static int GetOperand(string title)
    {
        int operand;
        while (true)
        {                
            Console.Write(title + "\n> ");
            if (int.TryParse(Console.ReadLine(), out operand))                
                break;                
            else Console.WriteLine("Invalid input. Try again");
        }
        return operand;
    }

    static string GetOperator(string title)
    {
        string op;
        while (true)
        {
            Console.Write(title + "\n> ");
            op = Console.ReadLine();
            if (op == "+" || op == "-" || op == "*" || op == "/")
                break;
            else Console.WriteLine("Invalid input. Try again");
        }
        return op;
    }

    static void Main(string[] args)
    {
        int num1, num2, answer=0;
        string op;
        while (true)
        {
            num1 = GetOperand("Write Your First Number");                                
            op = GetOperator("Write Your Operand");
            num2 = GetOperand("Write Your Second Number");                                
            switch (op)
            {
                case "+":
                    answer = num1 + num2;
                    break;
                case "-":
                    answer = num1 - num2;
                    break;
                case "*":
                    answer = num1 * num2;
                    break;
                case "/":
                    answer = num1 / num2;
                    break;                    
            }
            Console.WriteLine("Answer is " + answer.ToString());
        }
    }
}

 

 

edit: Don't forget to check for division by zero. If the denominator is zero, your program will crash

edit2: For a more flexible design, search for "reverse polish notation", as Mercile55 mentioned


In Topic: How to create c# text based game like this

21 January 2013 - 05:15 PM

deleted


In Topic: How to create c# text based game like this

21 January 2013 - 08:38 AM

I suggest you read up on classes, loops and lists (including the Dictionary)

 

If you do, you should be able to understand how this skeleton works

 

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace RPG
{
    // A class representing a location in the game world
    class Location
    {
        public Location(string description) // Class constructor
        {
            Description = description;
            Exits = new Dictionary<string, Location>();
        }
 
        // Description of this location
        public string Description;
 
        // Available exits for this location (Could use a List instead, but I prefer a Dictionary)
        public Dictionary<string, Location> Exits;
    }
 
    class Program
    {        
        static void Main(string[] args)
        {
            // A Dictionary of locations, representing the game world
            Dictionary<string, Location> Locations = new Dictionary<string, Location>();                    
 
            // Create the locations and add them to the "world"
            Locations["Island"] = new Location("An island surrounded by turquoise water");
            Locations["Town"] = new Location("A town with shops and houses");
            Locations["Farm"] = new Location("A farm in the forrest");
            Locations["Tavern"] = new Location("A tavern selling food and drinks");
 
            // Connect the locations together
            Locations["Island"].Exits["Town"] = Locations["Town"];
            Locations["Town"].Exits["Island"] = Locations["Island"];
 
            Locations["Town"].Exits["Farm"] = Locations["Farm"];
            Locations["Farm"].Exits["Town"] = Locations["Town"];
 
            Locations["Town"].Exits["Tavern"] = Locations["Tavern"];
            Locations["Tavern"].Exits["Town"] = Locations["Town"];
 
            // A reference to the "current" location
            // This reference always points to the current location
            Location currentLocation = Locations["Island"];
            
            // Game loop
            while (true)
            {
                // Print information
                Console.Clear();
                Console.WriteLine("You are here: " + currentLocation.Description);                
                Console.Write("You can go: ");
                foreach (string exit in currentLocation.Exits.Keys)                
                    Console.Write(exit + " ");                
                Console.WriteLine();
 
                // Take input from player
                string input = Console.ReadLine();
                if (input == "Quit" || input == "quit") // Exit if the player types Quit
                    break;
 
                // If the player typed the name of one of the current locations exits, update the current location
                if (currentLocation.Exits.Keys.Contains(input))
                {
                    currentLocation = currentLocation.Exits[input];
                }
                else
                {
                    Console.WriteLine("Can not go to " + input);
                }
            }
        }
    }
}


PARTNERS