Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Hey, I am trying to make a backgammon game, My teacher asked me to do somestuff in my project and i dont really know how to do it, Can someone help me with that please?
I am trying to do it and its not working for me, Please can someone help me with that?

My Teacher Says "There is a place where we wrote to check if he was allowed to move
Then write a function that checks the can move at all: and returns can or can not. Once you find one place that can move, then you will return the truth and should not continue to check
If there is a player out there, then check for each cube if allowed to put it in place that it brings it.
If all its parts are in place of the end (and then can pull out) then check that can move (because the legality there is a little different)
If there is no player out there, then for any place that has players check for each cube if you can move"


My C# Code
C#
using System;

namespace ConsoleApp43
{
    class Program
    {
        static string playerName1, playerName2;
        static int whatToMove;
        static int countToMove;
        static int turn;
        static int[] bord = new int[26];  // מערך בשביל הלוח
        static int[] dice = new int[4];  // מערך בשביל הקוביות
        static Random rnd = new Random(); // בשביל מספרים אקראיים בשביל הקוביות
        static int outRed;  // משתנה עבור כמה אדומים בחוץ
        static int outBlue; // משתנה עבור כמה כחולים בחוץ

        static void PrintPlayers()  // מדפיס על הלוח את מיקום השחקנים
        {
            ///משתנה עזר לקפוץ מעל 99
            int jump = 0;
            /// לולאה עבור כתיבת השחקנים
            for (int i = 0; i < 13; i++)
            {
                // מדפיס את השורה העליונה
                Console.SetCursorPosition(i * 5 + jump + 3, 2);
                if (bord[i] != 0)
                {
                    if (bord[i] > 0)
                        Console.ForegroundColor = ConsoleColor.Blue;
                    else
                        Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write(Math.Abs(bord[i]) + " ");
                }


                // מדפיס את השורה התחתונה 
                Console.SetCursorPosition(i * 5 + jump + 3, 12);
                if (bord[25 - i] != 0)
                {
                    if (bord[25 - i] > 0)
                        Console.ForegroundColor = ConsoleColor.Blue;
                    else
                        Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write(Math.Abs(bord[25 - i]) + " ");
                }

                if (i == 6) // קפיצה על המקום האמצעי
                    jump = 5;
            }

            if (outBlue > 0) // מדפיס כמה כחולים בחוץ אם יש
            {
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.SetCursorPosition(38, 2);
                Console.Write(outBlue);
            }
            if (outRed > 0) // מדפיס כמה אדומים בחוץ אם יש
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.SetCursorPosition(38, 12);
                Console.Write(outRed);
            }

            Console.ForegroundColor = ConsoleColor.White;  // מחזיר את הצבע ללבן

        }


        public static void PrintScreen() // מדפיס את המסגרת ודברים שלא משתנים כלל
        {
            /// שורה ראשונה של מינוסים
            Console.SetCursorPosition(1, 1);
            for (int i = 1; i <= 71; i++)
                Console.Write("═");

            // שורה אחרונה של מינוסים
            Console.SetCursorPosition(1, 13);
            for (int i = 1; i <= 71; i++)
                Console.Write("═");

            ///מדפיס 5 עמודות של קוים עומדים
            for (int i = 2; i <= 12; i++)
            {
                Console.SetCursorPosition(1, i);
                Console.Write("║");
                Console.SetCursorPosition(6, i);
                Console.Write("║");
                Console.SetCursorPosition(36, i);
                Console.Write("║");
                Console.SetCursorPosition(41, i);
                Console.Write("║");
                Console.SetCursorPosition(71, i);
                Console.Write("║");
            }
            /// מדפיס קוים עומדים להפריד בין המספרים
            for (int i = 0; i < 14; i++)
            {
                Console.SetCursorPosition(1 + i * 5, 2);
                Console.Write("║");
                Console.SetCursorPosition(1 + i * 5, 12);
                Console.Write("║");

            }
            /// מדפיס את המספרים
            int jump = 0;
            for (int i = 1; i < 13; i++)
            {
                // מדפיס שורה עליונה
                Console.SetCursorPosition(jump + 3 + i * 5, 0);
                Console.Write(i);

                // מדפיס שורה תחתונה
                Console.SetCursorPosition(jump + 3 + i * 5, 14);
                Console.Write(25 - i);
                if (i == 6)
                    jump = 5;
            }
            // מדפיס את ה99
            Console.SetCursorPosition(38, 0);
            Console.Write(99);
            Console.SetCursorPosition(38, 14);
            Console.Write(99);
        }

        static void PrintDice(int x, int y, int dice) // מדפיס קוביה לפי מיקום שנותנים לו וערך הקוביה
        {
            Console.SetCursorPosition(x, y);
            Console.Write("╔═══╗");
            Console.SetCursorPosition(x, y + 1);
            Console.Write("║");
            Console.SetCursorPosition(x + 2, y + 1);
            Console.Write(dice);
            Console.SetCursorPosition(x + 4, y + 1);
            Console.Write("║");
            Console.SetCursorPosition(x, y + 2);
            Console.Write("╚═══╝");
        }
        static void DeleteDice() /// מוחק את הקוביה
        {
            for (int i = 0; i < 3; i++)
            {
                Console.SetCursorPosition(14, 6 + i);
                Console.Write("                     ");
                Console.SetCursorPosition(49, 6 + i);
                Console.Write("                     ");

            }
        }
        /// מגריל ערך לקוביות ומכניס את הערך למערך ומדפיס את הקוביות
        public static void PrintDice()
        {
            dice[0] = rnd.Next(1, 7);
            dice[1] = rnd.Next(1, 7);

            //  קודם למחוק את הקוביות הקודמות
            DeleteDice();

            /// בודק אם יש דאבל ואם יש זה מדפיס ארבע קוביות
            if (dice[0] == dice[1])
            {
                ///קוביה מספר 1
                PrintDice(14, 6, dice[0]);
                ///קוביה מספר 2
                PrintDice(24, 6, dice[0]);
                ///קוביה מספר 3
                PrintDice(49, 6, dice[0]);
                ///קוביה מספר 4
                PrintDice(59, 6, dice[0]);

                dice[2] = dice[0];
                dice[3] = dice[0];
            }
            /// אם אין דאבל זה מדפיס שתי קוביות
            else
            {
                ///קוביה מספר 1
                PrintDice(17, 6, dice[0]);
                ///קוביה מספר 2
                PrintDice(55, 6, dice[1]);

                dice[2] = 0;
                dice[3] = 0;
            }
        }

        // roie
        // לבנות פונקציה שבודקת אם בכלל יכול לזוז         
        // roie
        // צריך להוסיף בדיקות האם לא יצאנו מהלוח
        // לבדוק אם מותר להוציא חלקים
        // לבדוק אם בחר אפס
        public static bool checkDice(int countToMove, bool toPrint = false)
        {
            for (int i = 0; i < dice.Length; i++)
                if (dice[i] == countToMove)
                {
                    int whereToMove = bord[whatToMove + (countToMove * turn)];
                    if (whereToMove == 0 || whereToMove == Math.Abs(whereToMove) * turn || whereToMove == turn * -1)
                        return true;
                    else
                    {
                        if (toPrint == true)
                            Console.WriteLine("You cannot move there");
                        return false;
                    }
                }
            if (toPrint == true)
                Console.WriteLine("No dice like that");
            return false;
        }
        public static bool checkMove(int whatToMove)
        {
            if (whatToMove == 99)
            {
                if (turn == 1 && outBlue > 0)
                    return true;
                else
                {
                    if (turn == -1 && outRed > 0)
                        return true;
                    else
                        return false;
                }
            }

            if (turn == 1 && bord[whatToMove] > 0)
                return true;
            else
                if (turn == -1 && bord[whatToMove] < 0)
                return true;

            return false;
        }

        // פונקציה שמדפיסה את התור של השחקן
        public static void PrintPlayerTurn()
        {
            if (turn == 1)
            {
                Console.SetCursorPosition(0, 15);
                Console.WriteLine("Its your turn: " + playerName1);
            }
            else
            {
                Console.WriteLine("Its your turn: " + playerName2);
            }
        }

        // roie
        public static void MoveMe(int whatToMove, int countToMove)
        {

        }


        static void Main(string[] args)  // פונקציה ראשית - תחילת התכנית
        {
            //Console.WriteLine("Please enter first player name");
            //playerName1 = Console.ReadLine();
            playerName1 = "Roie";
            // Console.WriteLine("Please enter second player name");
            // playerName2 = Console.ReadLine();
            playerName2 = "Avichay";


            // איתחול הלוח למצב ראשוני
            for (int i = 0; i < 26; i++)
                bord[i] = 0;
            bord[1] = 2;
            bord[12] = 5;
            bord[17] = 3;
            bord[19] = 5;
            bord[24] = -2;
            bord[13] = -5;
            bord[8] = -3;
            bord[6] = -5;

            outBlue = 0;
            outRed = 0;
            turn = 1;


            PrintScreen(); // נדפיס רק פעם אחת

            //while ()
            // {
            PrintPlayers(); // זה יהיה בתוך לולאה של המשחק
            PrintDice(); // מדפיס את הקוביות
            PrintPlayerTurn(); // להדפיס תור מי עכשיו

            Console.SetCursorPosition(0, 16);

            do
            {
                Console.Write("What do you want to move? ");

                try
                {
                    whatToMove = int.Parse(Console.ReadLine());
                }
                catch (Exception e)
                {
                    whatToMove = 0;
                }
            } while (((whatToMove > 0 && whatToMove < 26) || whatToMove == 99) && checkMove(whatToMove) == false);

            do
            {
                Console.Write("how much do you want to move? ");

                // roie
                // לעשות בדיקה שלא הכניס אותיות בטעות
                countToMove = int.Parse(Console.ReadLine());
            } while (checkDice(countToMove, true) == false);



            //}
            Console.SetCursorPosition(0, 20); // להחזיר את הסמן לסוף הלוח
        }
    }
}


What I have tried:

I have tried to make it and its not worked for me...
The project looks like that right now :
Screenshot 1 — imgbb.com[^]
Posted
Updated 26-Sep-18 1:50am
v2

1 solution

Just dumping your whole code on us and saying "it doesn't work" isn't going to get you a solution: We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900