4

I created this Ping Pong game against a computer but I have a problem. Everything works fine but whenever the ball touches one of the walls it shows me out of range error, even though I have set it so it can't exit the console. The problem only happens on two of the walls and the others work fine! I also have a score system, and I can't check if it works if I don't fix that problem.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Ping_Pong
{
    class Program
    {
        static int firstplayerpadsize = 4;
        static int secondplayerpadsize = 4;
        static int ballpositionx = 0;
        static int ballpositiony = 0;
        static bool balldirectionup = true;
        static bool balldirectionright = false;
        static int firstplayerposition = 0;
        static int secondplayerposition = 0;
        static int firstplayerresult = 0;
        static int secondplayerresult = 0;
        static Random randomGenerator = new Random();

        static void RemoveScrollbars()
        {
             Console.ForegroundColor = ConsoleColor.Yellow;
             Console.BufferHeight = Console.WindowHeight;
             Console.BufferWidth = Console.WindowWidth;
        }

        static void DrawFirstPlayer()
        {
            for (int y = firstplayerposition; y < firstplayerposition + firstplayerpadsize; y++)
            {
                PrintAtPosition(0, y, '|');
                PrintAtPosition(1, y, '|');
            }
        }

        static void PrintAtPosition(int x, int y, char symbol)
        {
            Console.SetCursorPosition(x, y);
            Console.Write(symbol);
        }

        static void DrawSecondPlayer()
        {
            for (int y = secondplayerposition; y < secondplayerposition + secondplayerpadsize; y++)
            {
                PrintAtPosition(Console.WindowWidth - 1, y, '|');
                PrintAtPosition(Console.WindowWidth - 2, y, '|');
            }
        }

        static void SetInitialsPoints()
        {
            firstplayerposition = Console.WindowHeight / 2 - firstplayerpadsize / 2;
            secondplayerposition = Console.WindowHeight / 2 - secondplayerpadsize / 2;
            SetBall();
        }

        static void SetBall()
        {
            ballpositionx = Console.WindowWidth / 2;
            ballpositiony = Console.WindowHeight / 2;
        }

        static void DrawBall()
        {
            PrintAtPosition(ballpositionx, ballpositiony, '@');
        }

        static void PrintResult()
        {
            Console.SetCursorPosition(Console.WindowWidth / 2 - 1, 0);
            Console.WriteLine("{0}-{1}", firstplayerresult, secondplayerresult);
        }

        static void MoveFirstPlayerDown()
        {
            if (firstplayerposition < Console.WindowHeight - firstplayerpadsize)
            {
                firstplayerposition++;
            }
        }

        static void MoveFirstPlayerUp()
        {
            if (firstplayerposition > 0)
            {
                firstplayerposition--;
            }
        }

        static void MoveSecondPlayerDown()
        {
            if (secondplayerposition < Console.WindowHeight - secondplayerpadsize)
            {
                secondplayerposition++;
            }
        }

        static void MoveSecondPlayerUp()
        {
            if (secondplayerposition > 0)
            {
                secondplayerposition--;
            }
        }

        static void SecondPlayerAIMove()
    {
        //int randomNumber = randomGenerator.Next(0, 2);
        //    if (randomNumber == 0)
        //    {
        //        MoveSecondPlayerUp();
        //    }
        //    if (randomNumber == 1)
        //    {
        //        MoveSecondPlayerDown();
        //    }

       // if (randomNumber == 0)
       // {
            if (balldirectionup == true)
            {
                MoveSecondPlayerUp();
            }
            else
            {
                MoveSecondPlayerDown();
            }
        }
   // }

        static void MoveBall()
        {
            if (ballpositiony == 0)
            {
                balldirectionup = false;
            }
            if (ballpositiony == Console.WindowHeight - 1)
            {
                balldirectionup = true;
            }
            if (ballpositionx == Console.WindowWidth - 1)
            {
                SetBall();
                balldirectionright = false;
                balldirectionup = true;
                firstplayerresult++;
                Console.SetCursorPosition(Console.WindowHeight / 2, Console.WindowWidth / 2);
                Console.WriteLine("Score for player 1.");
                Console.ReadKey();
            }
            if (ballpositionx == 0)
            {
                SetBall();
                balldirectionright = true;
                balldirectionup = true;
                secondplayerresult++;
                Console.SetCursorPosition(Console.WindowHeight / 2, Console.WindowWidth / 2);
                Console.WriteLine("Score for player 2.");
                Console.ReadKey();
            }

            if (ballpositionx < 3)
            {
                if (ballpositiony >= firstplayerposition && ballpositiony < firstplayerposition 
                   + firstplayerpadsize)
                {
                    balldirectionright = true;
                }
            }


            if (ballpositionx >= Console.WindowWidth - 3 - 1)
            {
                if (ballpositiony >= secondplayerposition && ballpositiony < secondplayerposition + secondplayerpadsize)
                {
                    balldirectionright = false;
                }
            }
            if (balldirectionup)
            {
                ballpositiony--;
            }
            else
            {
                ballpositiony++;
            }

            if (balldirectionright)
            {
                ballpositionx++;
            }
            else
            {
                ballpositionx--;
            }
        }

        static void Main(string[] args)
        {
            RemoveScrollbars();
            SetInitialsPoints();
            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo keyInfo = Console.ReadKey();
                    if (keyInfo.Key == ConsoleKey.UpArrow)
                    {
                        MoveFirstPlayerUp();
                    }
                    if (keyInfo.Key == ConsoleKey.DownArrow)
                    {
                        MoveFirstPlayerDown();
                    }
                }
                SecondPlayerAIMove();
                MoveBall();
                Console.Clear();
                DrawFirstPlayer();
                DrawSecondPlayer();
                DrawBall();
                PrintResult();
                Thread.Sleep(60);
            }
        }
    }
}
3
  • What's the exact error and which method does it occur in? Mar 16, 2014 at 8:14
  • Unhandled Exception: System.ArgumentOutOfRangeException: The value must be great er than or equal to zero and less than the console's buffer size in that dimensi on. Parameter name: top Actual value was 40. at System.Console.SetCursorPosition(Int32 left, Int32 top) at Ping_Pong.Program.MoveBall() in c:\Users\PC\Documents\Visual Studio 2013\P rojects\Ping Pong\Ping Pong\Program.cs:line 162 at Ping_Pong.Program.Main(String[] args) in c:\Users\PC\Documents\Visual Stud io 2013\Projects\Ping Pong\Ping Pong\Program.cs:line 223 ^ Exact error, copied after it occured. Thank you Nate W.
    – developi
    Mar 16, 2014 at 8:20
  • Copied the code, executed it, and started playing :). Forgot all about the exception. Mar 16, 2014 at 8:36

1 Answer 1

3

Inside the method MoveBall, you have the SetCursorPosition arguments inverted.
The first one is the distance from the left border and the second one the distance from the top border.

Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2);

This causes the error because the max value for the y value is 24 and you pass 40.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.