c# Console app error [SOLVED]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

c# Console app error [SOLVED]

Post by mattykins »

So i'm writing a quick little console game for practice and basically what i want is:
when the player presses "d" they move right, "a" they move left, etc etc. here is the code i have right now

Code: Select all

cki = Console.ReadKey();
                if (cki.Key == ConsoleKey.D)
                {
                    
                    for (int x = 0; x < 10; x++)
                    {
                        for (int y = 0; y < 10; y++)
                        {
                           if(Tiles.gsTiles[x , y] == "P" & Tiles.gsTiles[x + 1, y] == ".")
                           {
                               Tiles.gsTiles[x, y] = ".";
                               Tiles.gsTiles[x + 1, y] = "P";
                               Tiles.boardWrite();
                           }
                        }
                    }
                }
The two for loops are checking for the players position, and once they have the players position they are checking if they tile next to it is a "." or a blank tile. Once all of those things are true it writes the current player position to a "." and the tile next to it as a "P" for Player.

My only problem as of now is that once i press D it moves the player all the way to the edge of the screen and tries to write a "P" in a slot on the array that does not exist, thus giving me an error.

Also fyi if it helps the Tiles.gsTiles is the get/set method for a tile array i wrote in the tile class which is basically a 2 dimensional string array that holds the tile info that looks like:

Code: Select all

static private string[,] tiles = new string[ 10, 10];
        
            static public string[,] gsTiles
        {
            get { return tiles; }
            set { tiles = value; }
        }
Last edited by mattykins on Wed Mar 14, 2012 11:21 pm, edited 1 time in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: c# Console app error

Post by Jackolantern »

Have you tried debugging in VS? One error I see right off the bat is you are using the logical & operator instead of the conditional && operator. & does not do anything remotely like the && operator. Change that, and if you are still getting the error, place some break points and step through it with the debugger. You can mouse over any variable to see what it contains at the point you are stopped, or you can also watch them in the Locals window.
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: c# Console app error

Post by mattykins »

I have tried running through it with the vs debugger and thanks for the tip about the &&, it explains why the player was going through the wall haha. yet the problem remains that with one press of the "d" key the player shoots as far right as possible.

EDIT: I found out what the problem was after more debugging. the for loop that was searching for the players position continued to search even after the player moved, so it found its position and moved it multiple times in one keypress. I fixed this with a simple Boolean value and the revised code looked like:

Code: Select all

cki = Console.ReadKey();
                if (cki.Key == ConsoleKey.D)
                {
                    bool playermoved = false;
                    for (int x = 0; x < 11; x++)
                    {
                        for (int y = 0;  y < 11; y++)
                         {
                            if (Tiles.gsTiles[x, y] == "P" && Tiles.gsTiles[x + 1, y] == "." && playermoved == false)
                           {
                               Tiles.gsTiles[x, y] = ".";
                               Tiles.gsTiles[x + 1, y] = "P";
                               Tiles.boardWrite();
                               playermoved = true;
                               


                           }
                        }
                    }
                }
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: c# Console app error [SOLVED]

Post by Jackolantern »

Glad you got it fixed! 8-)
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”