Console Game

This would have been a really good learning exorcise except for the fact that it doesn’t work. I understand what it’s supposed to be doing, but I don’t understand why it’s not. Normally I’d ask what I’m doing wrong, but I ran it straight from the project solution file and it doesn’t work there either. It compiles with no errors, but doesn’t function how it’s supposed to.

https://www.codecademy.com/practice/projects/console-game-challenge-project

I passed it and it worked. What is your issue?

The game just doesn’t really function at all. I have absolutely no idea why, because it’s doing the same thing when I run it straight from the solution code. It doesn’t work in the codecademy browswer window either. I didn’t change anything at all in the solution code. The console comes up and the fruit appears, but there’s no cursor to move. The only thing I see that could potentially be an issue is the “unused parameter” hints in SuperGame.cs. I understand the lesson and the code, which I suppose it’s what’s ultimately important, but it still irks me that it’s not working and I don’t know why.

I think the issue would be in the Game.cs file if there is one. This was my code.

EDIT. I can’t seem to get the code posted and put a spoiler on it. Sorry.

using System;

namespace ConsoleGame
{
  class Game : SuperGame
  {
  	public new static void UpdatePosition(string Key, out int X, out int Y)
  	{
  		switch (Key)
    	{
      	case "LeftArrow":
        	X = -1;
        	Y = 0;
        	break;         
      	case "RightArrow":
        	X = 1;
        	Y = 0;
        	break;
        case "UpArrow":
        	X = 0;
        	Y = -1;
        	break;        
      	case "DownArrow": 
        	X = 0;
        	Y = 1;
        	break;
      	default:
        	X = 0;
        	Y = 1;
        	break;
      }
  	}
    public new static char UpdateCursor(string Key)
    {
   		switch (Key)
    	{
      	case "LeftArrow":
					return '<';
        	break;         
      	case "RightArrow":
        	return '>';
        	break;
        case "UpArrow":
        	return '^';
        	break;        
      	case "DownArrow": 
        	return 'v';
        	break;
      	default:
					return '<';
        	break;
      }   
    }
    public new static int KeepInBounds(int Coordinate, int MaxValue)
    {
    	return Coordinate < 0 ? 0 : Coordinate >= MaxValue ? MaxValue - 1 : Coordinate;
    }
    public new static bool DidScore(int x1, int x2, int y1, int y2)
    {
    	return x1 == x2 && y1 == y2;
    }
  }
}

So I figured out part of it, the y coordinate is like a million rows long instead of being constrained to the console window. I had to scroll all the way up to find the cursor, but even still whenever I touch any arrows to move it, it just jumps to the right of the fruit and blinks. Notice the size of the scroll bar in the screen shot. I made the changes from the code you posted as well, same issue.

I struggle to understand the KeepInBounds() method. According to the project it asks for, Every time a key is pressed, it will be called twice: once for x and once for y. The method will constrain a coordinate between 0 and its maximum value to keep the character from going off-screen.

In that method, the exercise suggests us to use two arguments, one for the coordinate value and one for the maximum value.

Suppose we have, int KeepInBounds(int pos, int maxValue). The pos should be the coordinate in the x Axis and y Axis? I know that coordinates in a Cartesian coordinate system follow a principle like (2,9), where X and Y values accordingly. How can we find the MaxValue of the screen or can we declare it at our own preference, for example, to take the value of 20?

I can see that maxValue is an integer, but what does that integer tell the program to do? What that value represents? 20 pixels, or something else?

I also noticed that Game.cs does not have a static void Main(string args) like Program.cs, so I suppose that any method that we need to call should be done inside the Program.cs file.

Hi @psmilliorn,

Could you please explain the code inside KeepInBounds(int Coordinate, int MaxValue) method?

Hello, @madaskalas.

If you look in the Program.cs file at lines 12-14:

// Determine bounds and set starting positions
int rows = Console.BufferHeight;
int cols = Console.BufferWidth;

Then at the KeepInBounds method calls on line 61-64:

// Keep character in bounds
// Uses KeepInBounds()
characterCol = Game.KeepInBounds(characterCol, cols);
characterRow = Game.KeepInBounds(characterRow, rows);

You can see that the maximum value for the X axis (cols) is determined by the Console.BufferWidth and the maximum value for the Y axis (rows) is determined by the Console.BufferHeight. Each time a key that moves the ‘character’ is pressed, the KeepInBounds() method is called to assign the characterCol (X coordinate) and the characterRow (Y coordinate).

I haven’t seen your particular KeepInBounds() method, so I can’t say for sure how it behaves, but it should either prevent the X and Y coordinates from exceeding the maximum value for each by returning the maximum value when either is greater than the maximum value and returning 0 if either is less than 0, or by allowing the character to ‘loop around’ to the opposite side of the screen.

You are correct. The functions in your Game.cs file are all called by code in the Program.cs file. That doesn’t mean that we couldn’t include a call to another method inside a method in the Game.cs file, but in this project, there is no need to do so.