beginner – C# Console, Connect 4 Game

this is my second post here, and the second project I made. I wanted to know, how to improve my code of connect-4 made in c# for console.

using System;

namespace Connect_4
{
    class Program
    {
        // computer's board, that computer checks if someone wins
        static string() BoardColumn1 = { "a", "b", "c", "d", "e", "f" };
        static string() BoardColumn2 = { "g", "h", "i", "j", "k", "l" };
        static string() BoardColumn3 = { "m", "n", "p", "p", "q", "r" };
        static string() BoardColumn4 = { "t", "u", "v", "w", "i", "y" };
        static string() BoardColumn5 = { "z", "1", "2", "3", "4", "5" };
        static string() BoardColumn6 = { "6", "7", "8", "9", "0", "(" };
        static string() BoardColumn7 = { ")", "-", "_ ", "+", "=", "}" };

        // what the user sees
        static string() UserBoardColumn1 = { " ", " ", " ", " ", " ", " " };
        static string() UserBoardColumn2 = { " ", " ", " ", " ", " ", " " };
        static string() UserBoardColumn3 = { " ", " ", " ", " ", " ", " " };
        static string() UserBoardColumn4 = { " ", " ", " ", " ", " ", " " };
        static string() UserBoardColumn5 = { " ", " ", " ", " ", " ", " " };
        static string() UserBoardColumn6 = { " ", " ", " ", " ", " ", " " };
        static string() UserBoardColumn7 = { " ", " ", " ", " ", " ", " " };

        static int turn = 0;
        static bool playing = true;

        static void Main(string() args)
        {
            Intro();

            while (playing) //stops the game once someone wins
            {
                Board(); 

                if (turn % 2 == 0) // tells whose turn it is
                {
                    Console.WriteLine("Player 1's turn");
                }
                else 
                {
                    Console.WriteLine("Player 2's turn");
                }

                int UserChoice;
                Console.WriteLine("Type the number you want your token to go in or type restart to restart"); //takes user input
                bool IsChoiceInt = int.TryParse(Console.ReadLine(), out UserChoice);

                if (IsChoiceInt) //checks if is correct input
                {
                    if (UserChoice == 1) // checks if someone wants to put the token in 1 and puts it there
                    {
                        int EmptyLocation = Array.IndexOf(UserBoardColumn1, " ");
                        if (EmptyLocation == -1)
                        {
                            Console.WriteLine("Stop trying to steal other people's places");
                            System.Threading.Thread.Sleep(2000);
                        }
                        else if (turn % 2 == 0)
                        {
                            turn++;
                            BoardColumn1(EmptyLocation) = "o";
                            UserBoardColumn1(EmptyLocation) = "o";
                        }
                        else
                        {
                            turn++;
                            BoardColumn1(EmptyLocation) = "x";
                            UserBoardColumn1(EmptyLocation) = "x";
                        }
                        Board();
                        VerticalWinCondition();
                        HorizontalWinCondition();
                        DiagonalWin();
                        IsTieGame();
                    }
                    else if (UserChoice == 2) // checks if someone wants to put the token in 2 and puts it there
                    {
                        int EmptyLocation = Array.IndexOf(UserBoardColumn2, " ");
                        if (EmptyLocation == -1)
                        {
                            Console.WriteLine("Stop trying to steal other people's places");
                            System.Threading.Thread.Sleep(2000);
                        }
                        else if (turn % 2 == 0)
                        {
                            turn++;
                            BoardColumn2(EmptyLocation) = "o";
                            UserBoardColumn2(EmptyLocation) = "o";
                        }
                        else
                        {
                            turn++;
                            BoardColumn2(EmptyLocation) = "x";
                            UserBoardColumn2(EmptyLocation) = "x";
                        }
                        Board();
                        VerticalWinCondition();
                        HorizontalWinCondition();
                        DiagonalWin();
                        IsTieGame();
                    }
                    else if (UserChoice == 3) // checks if someone wants to put the token in 3 and puts it there
                    {
                        int EmptyLocation = Array.IndexOf(UserBoardColumn3, " ");
                        if (EmptyLocation == -1)
                        {
                            Console.WriteLine("Stop trying to steal other people's places");
                            System.Threading.Thread.Sleep(2000);
                        }
                        else if (turn % 2 == 0)
                        {
                            turn++;
                            BoardColumn3(EmptyLocation) = "o";
                            UserBoardColumn3(EmptyLocation) = "o";
                        }
                        else
                        {
                            turn++;
                            BoardColumn3(EmptyLocation) = "x";
                            UserBoardColumn3(EmptyLocation) = "x";
                        }
                        Board();
                        VerticalWinCondition();
                        HorizontalWinCondition();
                        DiagonalWin();
                        IsTieGame();
                    }
                    else if (UserChoice == 4) // checks if someone wants to put the token in 4 and puts it there
                    {
                        int EmptyLocation = Array.IndexOf(UserBoardColumn4, " ");
                        if (EmptyLocation == -1)
                        {
                            Console.WriteLine("Stop trying to steal other people's places");
                            System.Threading.Thread.Sleep(2000);
                        }
                        else if (turn % 2 == 0)
                        {
                            turn++;
                            BoardColumn4(EmptyLocation) = "o";
                            UserBoardColumn4(EmptyLocation) = "o";
                        }
                        else
                        {
                            turn++;
                            BoardColumn4(EmptyLocation) = "x";
                            UserBoardColumn4(EmptyLocation) = "x";
                        }
                        Board();
                        VerticalWinCondition();
                        HorizontalWinCondition();
                        DiagonalWin();
                        IsTieGame();
                    }
                    else if (UserChoice == 5) // checks if someone wants to put the token in 5 and puts it there
                    {
                        int EmptyLocation = Array.IndexOf(UserBoardColumn5, " ");
                        if (EmptyLocation == -1)
                        {
                            Console.WriteLine("Stop trying to steal other people's places");
                            System.Threading.Thread.Sleep(2000);
                        }
                        else if (turn % 2 == 0)
                        {
                            turn++;
                            BoardColumn5(EmptyLocation) = "o";
                            UserBoardColumn5(EmptyLocation) = "o";
                        }
                        else
                        {
                            turn++;
                            BoardColumn5(EmptyLocation) = "x";
                            UserBoardColumn5(EmptyLocation) = "x";
                        }
                        Board();
                        VerticalWinCondition();
                        HorizontalWinCondition();
                        DiagonalWin();
                        IsTieGame();
                    }
                    else if (UserChoice == 6) // checks if someone wants to put the token in 6 and puts it there
                    {
                        int EmptyLocation = Array.IndexOf(UserBoardColumn6, " ");
                        if (EmptyLocation == -1)
                        {
                            Console.WriteLine("Stop trying to steal other people's places");
                            System.Threading.Thread.Sleep(2000);
                        }
                        else if (turn % 2 == 0)
                        {
                            turn++;
                            BoardColumn6(EmptyLocation) = "o";
                            UserBoardColumn6(EmptyLocation) = "o";
                        }
                        else
                        {
                            turn++;
                            BoardColumn6(EmptyLocation) = "x";
                            UserBoardColumn6(EmptyLocation) = "x";
                        }
                        Board();
                        VerticalWinCondition();
                        HorizontalWinCondition();
                        DiagonalWin();
                        IsTieGame();
                    }
                    else if (UserChoice == 7)// checks if someone wants to put the token in 7 and put it there
                    {
                        int EmptyLocation = Array.IndexOf(UserBoardColumn7, " ");
                        if (EmptyLocation == -1)
                        {
                            Console.WriteLine("Stop trying to steal other people's places");
                            System.Threading.Thread.Sleep(2000);
                        }
                        else if (turn % 2 == 0)
                        {
                            turn++;
                            BoardColumn7(EmptyLocation) = "o";
                            UserBoardColumn7(EmptyLocation) = "o";
                        }
                        else
                        {
                            turn++;
                            BoardColumn7(EmptyLocation) = "x";
                            UserBoardColumn7(EmptyLocation) = "x";
                        }
                        Board();
                        VerticalWinCondition();
                        HorizontalWinCondition();
                        DiagonalWin();
                        IsTieGame();
                    }
                    else if (UserChoice > 7 || UserChoice < 0) //error check
                    {
                        Console.WriteLine("Please input a valid number, the valid numbers are 1, 2, 3, 4, 5, 6, 7");
                        System.Threading.Thread.Sleep(2000);
                    }
                }
                else //error check
                {
                    Console.WriteLine("Please input a valid number, the valid numbers are 1, 2, 3, 4, 5, 6, 7");
                    System.Threading.Thread.Sleep(2000);
                }

            }
        }

        static void Board() //makes the board
        {
            Console.Clear();
            Console.WriteLine("  1   2   3   4   5   6   7");
            Console.WriteLine($"| {UserBoardColumn1(5)} | {UserBoardColumn2(5)} | {UserBoardColumn3(5)} | {UserBoardColumn4(5)} | {UserBoardColumn5(5)} | {UserBoardColumn6(5)} | {UserBoardColumn7(5)} |");
            Console.WriteLine($"| {UserBoardColumn1(4)} | {UserBoardColumn2(4)} | {UserBoardColumn3(4)} | {UserBoardColumn4(4)} | {UserBoardColumn5(4)} | {UserBoardColumn6(4)} | {UserBoardColumn7(4)} |");
            Console.WriteLine($"| {UserBoardColumn1(3)} | {UserBoardColumn2(3)} | {UserBoardColumn3(3)} | {UserBoardColumn4(3)} | {UserBoardColumn5(3)} | {UserBoardColumn6(3)} | {UserBoardColumn7(3)} |");
            Console.WriteLine($"| {UserBoardColumn1(2)} | {UserBoardColumn2(2)} | {UserBoardColumn3(2)} | {UserBoardColumn4(2)} | {UserBoardColumn5(2)} | {UserBoardColumn6(2)} | {UserBoardColumn7(2)} |");
            Console.WriteLine($"| {UserBoardColumn1(1)} | {UserBoardColumn2(1)} | {UserBoardColumn3(1)} | {UserBoardColumn4(1)} | {UserBoardColumn5(1)} | {UserBoardColumn6(1)} | {UserBoardColumn7(1)} |");
            Console.WriteLine($"| {UserBoardColumn1(0)} | {UserBoardColumn2(0)} | {UserBoardColumn3(0)} | {UserBoardColumn4(0)} | {UserBoardColumn5(0)} | {UserBoardColumn6(0)} | {UserBoardColumn7(0)} |");
            Console.WriteLine("-----------------------------");
        }
        static void VerticalWinCondition() //checks if someone won vertially
        {
            if (BoardColumn1(0) == BoardColumn1(1) && BoardColumn1(0) == BoardColumn1(2) && BoardColumn1(0) == BoardColumn1(3) || BoardColumn1(1) == BoardColumn1(2) && BoardColumn1(1) == BoardColumn1(3) && BoardColumn1(1) == BoardColumn1(4) || BoardColumn1(2) == BoardColumn1(3) && BoardColumn1(2) == BoardColumn1(4) && BoardColumn1(2) == BoardColumn1(5))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn2(0) == BoardColumn2(1) && BoardColumn2(0) == BoardColumn2(2) && BoardColumn2(0) == BoardColumn2(3) || BoardColumn2(1) == BoardColumn2(2) && BoardColumn2(1) == BoardColumn2(3) && BoardColumn2(1) == BoardColumn2(4) || BoardColumn2(2) == BoardColumn2(3) && BoardColumn2(2) == BoardColumn2(4) && BoardColumn2(2) == BoardColumn2(5))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn3(0) == BoardColumn3(1) && BoardColumn3(1) == BoardColumn3(2) && BoardColumn3(2) == BoardColumn3(3) || BoardColumn3(1) == BoardColumn3(2) && BoardColumn3(1) == BoardColumn3(3) && BoardColumn3(1) == BoardColumn3(4) || BoardColumn3(2) == BoardColumn3(3) && BoardColumn3(2) == BoardColumn3(4) && BoardColumn3(2) == BoardColumn3(5))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn4(0) == BoardColumn4(1) && BoardColumn4(1) == BoardColumn4(2) && BoardColumn4(0) == BoardColumn4(3) || BoardColumn4(1) == BoardColumn4(2) && BoardColumn4(1) == BoardColumn4(3) && BoardColumn4(1) == BoardColumn4(4) || BoardColumn4(2) == BoardColumn4(3) && BoardColumn4(2) == BoardColumn4(4) && BoardColumn4(2) == BoardColumn4(5))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn5(0) == BoardColumn5(1) && BoardColumn5(1) == BoardColumn5(2) && BoardColumn5(2) == BoardColumn5(3) || BoardColumn5(1) == BoardColumn5(2) && BoardColumn5(1) == BoardColumn5(3) && BoardColumn5(1) == BoardColumn5(4) || BoardColumn5(2) == BoardColumn5(3) && BoardColumn5(2) == BoardColumn5(4) && BoardColumn5(2) == BoardColumn5(5))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn6(0) == BoardColumn6(1) && BoardColumn6(1) == BoardColumn6(2) && BoardColumn6(2) == BoardColumn6(3) || BoardColumn6(1) == BoardColumn6(2) && BoardColumn6(1) == BoardColumn6(3) && BoardColumn6(1) == BoardColumn6(4) || BoardColumn6(2) == BoardColumn6(3) && BoardColumn6(2) == BoardColumn6(4) && BoardColumn6(2) == BoardColumn6(5))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn7(0) == BoardColumn7(1) && BoardColumn7(1) == BoardColumn7(2) && BoardColumn7(2) == BoardColumn7(3) || BoardColumn7(1) == BoardColumn7(2) && BoardColumn7(1) == BoardColumn7(3) && BoardColumn7(1) == BoardColumn7(4) || BoardColumn7(2) == BoardColumn7(3) && BoardColumn7(2) == BoardColumn7(4) && BoardColumn7(2) == BoardColumn7(5))
            {
                playing = false;
                Winner();
            }
        }
        static void HorizontalWinCondition() // checks if soemone won horizontally
        {
            if (BoardColumn1(0) == BoardColumn2(0) && BoardColumn1(0) == BoardColumn3(0) && BoardColumn1(0) == BoardColumn4(0))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn1(1) == BoardColumn2(1) && BoardColumn1(1) == BoardColumn3(1) && BoardColumn1(1) == BoardColumn4(1))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn1(2) == BoardColumn2(2) && BoardColumn1(2) == BoardColumn3(2) && BoardColumn1(2) == BoardColumn4(2))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn1(3) == BoardColumn2(3) && BoardColumn1(3) == BoardColumn3(3) && BoardColumn1(3) == BoardColumn4(3))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn1(4) == BoardColumn2(4) && BoardColumn1(4) == BoardColumn3(4) && BoardColumn1(4) == BoardColumn4(4))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn1(5) == BoardColumn2(5) && BoardColumn1(5) == BoardColumn3(5) && BoardColumn1(5) == BoardColumn4(5))
            {
                playing = false;
                Winner();
            }
        }
        static void DiagonalWin() //checks if a player won diagonally
        {
            if (BoardColumn1(0) == BoardColumn2(1) && BoardColumn1(0) == BoardColumn3(2) && BoardColumn1(0) == BoardColumn4(3) || BoardColumn1(1) == BoardColumn2(2) && BoardColumn1(1) == BoardColumn3(3) && BoardColumn1(1) == BoardColumn4(4) || BoardColumn1(2) == BoardColumn2(3) && BoardColumn1(2) == BoardColumn3(4) && BoardColumn1(2) == BoardColumn4(5))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn1(5) == BoardColumn2(4) && BoardColumn1(5) == BoardColumn3(3) && BoardColumn1(5) == BoardColumn4(2) || BoardColumn1(4) == BoardColumn2(3) && BoardColumn1(4) == BoardColumn3(2) && BoardColumn1(4) == BoardColumn4(1) || BoardColumn1(3) == BoardColumn2(2) && BoardColumn1(3) == BoardColumn3(1) && BoardColumn1(3) == BoardColumn4(0))
            {
                playing = false;
                Winner();
            }

            else if (BoardColumn2(0) == BoardColumn3(1) && BoardColumn2(0) == BoardColumn4(2) && BoardColumn2(0) == BoardColumn5(3) || BoardColumn2(1) == BoardColumn3(2) && BoardColumn2(1) == BoardColumn4(3) && BoardColumn2(1) == BoardColumn5(4) || BoardColumn2(2) == BoardColumn3(3) && BoardColumn4(2) == BoardColumn2(4) && BoardColumn2(2) == BoardColumn5(5))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn2(5) == BoardColumn3(4) && BoardColumn2(5) == BoardColumn4(3) && BoardColumn2(5) == BoardColumn5(2) || BoardColumn2(4) == BoardColumn3(3) && BoardColumn2(4) == BoardColumn4(2) && BoardColumn2(4) == BoardColumn5(1) || BoardColumn2(3) == BoardColumn3(2) && BoardColumn4(3) == BoardColumn2(1) && BoardColumn2(3) == BoardColumn5(0))
            {
                playing = false;
                Winner();
            }

            else if (BoardColumn3(0) == BoardColumn4(1) && BoardColumn3(0) == BoardColumn5(2) && BoardColumn3(0) == BoardColumn6(3) || BoardColumn3(1) == BoardColumn4(2) && BoardColumn3(1) == BoardColumn5(3) && BoardColumn3(1) == BoardColumn6(4) || BoardColumn3(2) == BoardColumn4(3) && BoardColumn3(2) == BoardColumn5(4) && BoardColumn3(2) == BoardColumn6(5))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn3(5) == BoardColumn4(4) && BoardColumn3(5) == BoardColumn5(3) && BoardColumn3(5) == BoardColumn6(2) || BoardColumn3(4) == BoardColumn4(3) && BoardColumn3(4) == BoardColumn5(2) && BoardColumn3(4) == BoardColumn6(1) || BoardColumn3(3) == BoardColumn4(2) && BoardColumn3(3) == BoardColumn5(1) && BoardColumn3(3) == BoardColumn6(0))
            {
                playing = false;
                Winner();
            }

            else if (BoardColumn4(0) == BoardColumn5(1) && BoardColumn4(0) == BoardColumn6(2) && BoardColumn4(0) == BoardColumn7(3) || BoardColumn4(1) == BoardColumn5(2) && BoardColumn4(1) == BoardColumn6(3) && BoardColumn4(1) == BoardColumn7(4) || BoardColumn4(2) == BoardColumn5(3) && BoardColumn4(2) == BoardColumn6(4) && BoardColumn4(2) == BoardColumn7(5))
            {
                playing = false;
                Winner();
            }
            else if (BoardColumn4(5) == BoardColumn5(4) && BoardColumn4(5) == BoardColumn6(3) && BoardColumn4(5) == BoardColumn7(2) || BoardColumn4(4) == BoardColumn5(3) && BoardColumn4(4) == BoardColumn6(2) && BoardColumn4(4) == BoardColumn7(1) || BoardColumn4(3) == BoardColumn5(2) && BoardColumn4(3) == BoardColumn6(1) && BoardColumn4(3) == BoardColumn7(0))
            {
                playing = false;
                Winner();
            }
        }

        static void IsTieGame() //checks if game is tie
        {
            int TieCheker1 = Array.IndexOf(UserBoardColumn1, " ");
            int TieCheker2 = Array.IndexOf(UserBoardColumn2, " ");
            int TieCheker3 = Array.IndexOf(UserBoardColumn3, " ");
            int TieCheker4 = Array.IndexOf(UserBoardColumn4, " ");
            int TieCheker5 = Array.IndexOf(UserBoardColumn5, " ");
            int TieCheker6 = Array.IndexOf(UserBoardColumn6, " ");
            int TieCheker7 = Array.IndexOf(UserBoardColumn7, " ");

            if (TieCheker1 == -1 && TieCheker2 == -1 && TieCheker3 == -1 && TieCheker4 == -1 && TieCheker5 == -1 && TieCheker6 == -1 && TieCheker7 == -1)
            {
                playing = false;
                Tie();
            }
        }

        static void Intro() //Intro
        {
            Console.WriteLine("Welcome to");
            Console.WriteLine(@"  ______   ______   .__   __. .__   __.  _______   ______ .___________.    _  _    ");
            Console.WriteLine(@" /      | /  __    |   |  | |   |  | |   ____| /      ||           |   | || |   ");
            Console.WriteLine(@"|  ,----'|  |  |  | |   |  | |   |  | |  |__   |  ,----'`---|  |----`   | || |_  ");
            Console.WriteLine(@"|  |     |  |  |  | |  . `  | |  . `  | |   __|  |  |         |  |        |__   _| ");
            Console.WriteLine(@"|  `----.|  `--'  | |  |   | |  |   | |  |____ |  `----.    |  |           | |   ");
            Console.WriteLine(@" ______| ______/  |__| __| |__| __| |_______| ______|    |__|           |_|   ");
            Console.WriteLine("nPress a Key to begin");
            Console.ReadKey();
            Console.Clear();
            Rules();
        }
        static void Rules() //Rules
        {
            Console.WriteLine("How to Play:");
            Console.WriteLine("1. Choose who plays first. Player 1 will be 'o' and Player 2 will be 'x'");
            Console.WriteLine("2. Player's will alternate taking turns on putting their token in the board. You will put your token in the board by typing in the number at the top of the column you wish to put it in.  ");
            Console.WriteLine("3. The first player to get 4 tokens in a row wins. The four in a row can be horizontal, vertical or diagonal");
            Console.WriteLine("nPress a Key to begin");
            Console.ReadKey();
        }
        static void Winner() //message when player wins game
        {
            if (turn % 2 == 0)
            {
                Console.WriteLine("Congrats on winning player 2");
            }
            else
            {
                Console.WriteLine("Congrats on winning player 1");
            }
        }
        static void Tie() //message when tie game
        {
            Console.WriteLine("The game is a tie");
        }
    }
}

publishing – I want to make a video game based off of Dungeons and Dragons. How much am I legally permitted to use?

I want to make a video game based off of Dungeons and Dragons. It’ll be a top-down RPG with similar battle and class mechanics. But I’ve been planning out more, and seeing how much I’m copying. How much am I legally permitted to use? I am awayer of the SRD, but don’t understand it fully. Is it like a book of what I’m allowed to use?

javascript – How can I publish a Python game on itch.io as html?

A game made with Pygame is not playable in the browser. Games made with that tool can be put on itch, but only as a downloadable desktop package, not as a web game.

For completeness, I should mention there are a few projects that aim to get Python/Pygame working in a browser, but I would not call any of these a great option. The most prominent of these projects is Skulpt, and here are some examples of using it with Pygame.

Refer to this existing closely related question.

c# – Final Testing Of Snake Game

Hello!

I’ve been posting here as of recently because i need help with my final update of my code!

I know that everything is kinda of a mess but i’ll try to explain as best i can.

I have three different classes: Starter, GameWorld, TestClass (And an interface).
The GameWorld has two methods, one for the boarder of the game and one to clear the console.

The Starter Class has a method called “GameSense” which bascially has everything about the food and the behavior of my snake.

Now i’ve been trying to make a tests using a constructor in a Class within my Starter Class named “Pixel”. Pixel has the X/Y values and background color i need. PS: Pixel is at the bottom of the Starter Class.

All i want is to be able to test different things such as “Will (2,4)+(1,3) = (3,7)?”.

Any kinda of help or anything you would like me to fix and ill be right on it!
I am trying to learn as much as possible about testing right now!
Thanks in advance!

StarterClass:

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

namespace Snake
{
    /// <summary>
    /// Start class, everything goes from here.
    /// Made by Karwan!
    /// </summary>
    public class Starter : InterF
    {
        private static Direction movement;

        public enum Direction { Up, Down, Left, Right }
        public static object Key { get; private set; }

        /// <summary>
        /// Main method.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string() args)
        {
            //Making the text green and also given a player 
            //options to chose from. 
            Console.ForegroundColor = ConsoleColor.Green;
            bool loopC = true;
            while (loopC)
            {
                string mystring = null; Console.WriteLine("Welcome to Snake.nPress S+Enter to start or press Q+Enter to exit.");
                mystring = Console.ReadLine();
                switch (mystring)
                {
                    case "Q":
                        Environment.Exit(0);
                        break;
                    case "S":
                        Console.WriteLine("Starting game!");
                        System.Threading.Thread.Sleep(4000);
                        loopC = false;
                        break;
                    default:
                        Console.WriteLine("Invalid Entry.. Try again.");
                        break;
                }

            }

            //Call for GameSense if
            //they want to play.
            GameSense();

        }



        /// <summary>
        /// Game object!
        /// </summary>
        public static void GameSense()

        {
            //Game console height/width.
            Console.WindowHeight = 30;
            Console.WindowWidth = 70;
            int screenwidth = Console.WindowWidth;
            int screenheight = Console.WindowHeight;
            Random randomnummer = new Random();
            //Lenght of tail == current score.
            int score = 2;
            int gameover = 0;
            //Gather positions from pixel class.
            pixel positions = new pixel();
            positions.xpos = screenwidth / 2;
            positions.ypos = screenheight / 2;
            positions.Black = ConsoleColor.Red;
         
            //Position manegment.
            List<int> xpos = new List<int>();
            List<int> ypos = new List<int>();
            int berryx = randomnummer.Next(0, screenwidth);
            int berryy = randomnummer.Next(0, screenheight);
            //Time manegment.
            DateTime time1 = DateTime.Now;
            DateTime time2 = DateTime.Now;
       


            //Draw world from GameWorld.cs.
            GameWorld.DrawBorder(screenwidth, screenheight);

            while (true)
            {
                GameWorld.ClearConsole(screenwidth, screenheight);
                if (positions.xpos == screenwidth - 1 || positions.xpos == 0 || positions.ypos == screenheight - 1 || positions.ypos == 0)
                {
                    gameover = 1;
                }

                Console.ForegroundColor = ConsoleColor.Green;
                if (berryx == positions.xpos && berryy == positions.ypos)
                {
                    score++;
                    berryx = randomnummer.Next(1, screenwidth - 2);
                    berryy = randomnummer.Next(1, screenheight - 2);
                }
                for (int i = 0; i < xpos.Count(); i++)
                {
                    Console.SetCursorPosition(xpos(i), ypos(i));
                    Console.Write("*");
                    if (xpos(i) == positions.xpos && ypos(i) == positions.ypos)
                    {
                        gameover = 1;
                    }
                }
                if (gameover == 1)
                {
                    break;
                }
                Console.SetCursorPosition(positions.xpos, positions.ypos);
                Console.ForegroundColor = positions.Black;
                Console.Write("*");
                //Food color & position.
                Console.SetCursorPosition(berryx, berryy);
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write("*");
                Console.CursorVisible = false;
                time1 = DateTime.Now;
     
                while (true)
                {
                    time2 = DateTime.Now;
                    if (time2.Subtract(time1).TotalMilliseconds > 500) { break; }
                    if (Console.KeyAvailable)
                    {
                        ConsoleKeyInfo info = Console.ReadKey(true);
                        if (info.Key.Equals(ConsoleKey.UpArrow) && movement != Direction.Down)
                        {
                            movement = Direction.Up;
                        }
                        else if (info.Key.Equals(ConsoleKey.DownArrow) && movement != Direction.Up)
                        {
                            movement = Direction.Down;
                        }
                        else if (info.Key.Equals(ConsoleKey.LeftArrow) && movement != Direction.Right)
                        {
                            movement = Direction.Left;
                        }
                        else if (info.Key.Equals(ConsoleKey.RightArrow) && movement != Direction.Left)
                        {
                            movement = Direction.Right;
                        }
                    }
                }
                //Giving the connections value
                //to change x/y to make the movment happen.
                xpos.Add(positions.xpos);
                ypos.Add(positions.ypos);
                switch (movement)
                {
                    case Direction.Up:
                        positions.ypos--;
                        break;
                    case Direction.Down:
                        positions.ypos++;
                        break;
                    case Direction.Left:
                        positions.xpos--;
                        break;
                    case Direction.Right:
                        positions.xpos++;
                        break;
                }
                if (xpos.Count() > score)
                {
                    xpos.RemoveAt(0);
                    ypos.RemoveAt(0);
                }
            }
            Console.SetCursorPosition(20, screenheight / 2);
            Console.WriteLine("Game over, Score: " + score);
            Console.SetCursorPosition(20, screenheight / 2 );
            System.Threading.Thread.Sleep(1000);
            restart();

        }

        /// <summary>
        /// Restarter.
        /// </summary>
        public static void restart()
        {
            Console.WindowHeight = 30;
            Console.WindowWidth = 70;
            int screenwidth = Console.WindowWidth;
            int screenheight = Console.WindowHeight;
 
            string Over = null; Console.WriteLine("n             Would you like to start over? Y/N");
            Console.SetCursorPosition(20, screenheight/2 +2 );
            bool O = true;

            while (O)
            {
                Over = Console.ReadLine();
                switch (Over)
                {
                    case "Y":
      
                        Console.WriteLine("n              Restarting!");
                        System.Threading.Thread.Sleep(2000);
                        
            
                        GameSense(); 
                        break;
                    case "N":
                        Console.WriteLine("n                Thank you for playing!");
                        Environment.Exit(0);
                        break;
                    default:
                        Console.WriteLine("Invalid Entry.. Try again.");
                        break;
                }
            }

        }
        /// <summary>
        /// Set/get pixel position.
        /// </summary>
        public class pixel
        {

            public int xpos { get; set; }
            public int ypos { get; set; }
            public ConsoleColor Black { get; set; }
        }
    }
}

GameWorld Class:

using System;
using System.Linq;
using static Snake.Starter;

public class GameWorld 
{

    /// <summary>
    /// Clear console for snake.
    /// </summary>
    /// <param name="screenwidth"></param>
    /// <param name="screenheight"></param>
    public static void ClearConsole(int screenwidth, int screenheight)
    {
        var blackLine = string.Join("", new byte(screenwidth - 2).Select(b => " ").ToArray());
        Console.ForegroundColor = ConsoleColor.Black;
        for (int i = 1; i < screenheight - 1; i++)
        {
            Console.SetCursorPosition(1, i);
            Console.Write(blackLine);
        }
    }

    /// <summary>
    /// Draw boared.
    /// </summary>
    /// <param name="screenwidth"></param>
    /// <param name="screenheight"></param>
    public static void DrawBorder(int screenwidth, int screenheight)
    {
        var horizontalBar = string.Join("", new byte(screenwidth).Select(b => "■").ToArray());

        Console.SetCursorPosition(0, 0);
        Console.Write(horizontalBar);
        Console.SetCursorPosition(0, screenheight - 1);
        Console.Write(horizontalBar);

        for (int i = 0; i < screenheight; i++)
        {
            Console.SetCursorPosition(0, i);
            Console.Write("■");
            Console.SetCursorPosition(screenwidth - 1, i);
            Console.Write("■");
        }
    }

}

Interface:

using System;

public class InterF
{

    /// <summary>
    /// Interface for Starter.
    /// </summary>
    interface INterF
    {

        void GameSense();
        void DrawBoarder();
        void ClearConsole(); 

    }
}

And the test class, the class i need to update.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using Snake;
using System;
using System.Collections.Generic;
using System.Text;
using static Snake.Starter;

namespace Snake.Tests
{
    (TestClass())
    public class StarterTests
    {
        (TestMethod())

        public void Test()
        {
            // pixel a = new pixel(1, 2);
            // pixel b = new pixel(3, 4);
            /// pixel c = a + b;

        //Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(c.xpos, 4);
        //Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(c.ypos, 6);
    }
    }
}

c# – Position Testing, a Snake game

Right now i am trying to make as many different tests as possible to learn as much as possible about testing. I have a game that i made, a snake game, but i need to test it.

Right now i am using a class called “Pixel” that has my x/y cordinates. I am trying to add them together as my first test but i’m stuck.

What am i doing wrong here and what can i add on, what more tests can i do?

Test Code:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using Snake;
using System;
using System.Collections.Generic;
using System.Text;
using static Snake.Starter;

namespace Snake.Tests
{
    (TestClass())
    public class StarterTests
    {
        (TestMethod())
        public void Test()
        {
            pixel a = new pixel(1, 2);
            pixel b = new pixel(3, 4);
            pixel c = a + b; //This doesn't work...
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(c.xpos, 4);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(c.ypos, 6);
        }
    }
}


Pixel code:

   public class pixel
        {
            public pixel(int v1, int v2)
            {
                this.xpos = v1;
                this.ypos = v2; 
            }

            public int xpos { get; set; }
            public int ypos { get; set; }
            public ConsoleColor Black { get; set; }


        }
    }

C++ guess number game – Stack Overflow

This game is supposed to give a number between 0 – 100
What’s wrong with this code (I’m a beginner and switched from python)
I’m trying hard and to have a new line is it “new” or “newl”???

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
   int randomNumber = (rand() % 101);
   int guess;
   cout << "guess my number! (between 0 and 100 and in whole numbers)" << new;
   cin >> guess;

   while(1 == 1){
       switch(guess){
           case guess == randomNumber:
               cout << "Well done! You have guessed my number" << new;
               break;
           case guess > randomNumber:
               cout << "Too High!" << new;
               cin >> randomNumber;
           case guess < randomNumber:
               cout << "Too Low!" << new;
               cin >>  randomNumber;
           default:
               cout << "Invalid Guess..." << new;
            cin >> randomNumber;
        }

       int randomNumber = (rand() % 101);

    }
    
}

}

combinatorics – A particular two-player game starts with a pile of diamonds and a pile of rubies.

On your turn, you can take any number of diamonds, or any number of rubies, or an equal number of each. You must take at least one gem on each of your turns. Whoever takes the last gem wins the game. You get to choose the starting number of diamonds and rubies, and whether you go
first or second. Find all starting configurations (including who goes first) with 8 gems
where you are guaranteed to win. If you have to let your opponent go first, what are
the starting configurations of gems where you are guaranteed to win?

Is there a book on the algorithms and code needed to build most popular board and card game?

Is there a book on the algorithms and code needed to build most popular board and card game? I am trying to find some useful resources to build a bunch of mini games. I am trying to build every game in Clubhouse Games: 51 Worldwide Classics in less than a year, but to do that I need to have the algorithms handed out to me given I am working full time. Is there any good reference book that would help me do this?

Python beginner looking for feedback on simple dice game

Dice “Game”

So, just started python a weeks time ago, so I’m looking for feedback/critique on things i can do to improve my code. I wrote a little user/pc dice rolling game. Any feedback at all is appreciated!



def dice(n: int) -> tuple:
    """
    Simple function to roll 'n' number of dices.
    Returns numbers of each roll and a total.

    :param n: number of dices to roll
    :return: dices_numbers, total
    """
    numbers = ()
    total = 0
    for dices in range(n):
        number = random.randint(1, 6)
        numbers.append(number)
        total += number
    dices_numbers = ", ".join(map(str, numbers))
    return dices_numbers, total


def dice_check(user_total: int = 0, pc_total: int = 0) -> None:
    """
    Function to check whether user or pc won dice roll.

    :param user_total: user's total roll
    :param pc_total:  pc's total roll
    :return: None
    """
    if user_total > pc_total:
        print("Congratulations! You won")
    elif user_total < pc_total:
        print("Sorry, BOT wins!")
    else:
        print("Tie!")


number_of_dices = ""
user_roll = ""
pc_roll = ""
while True:
    number_of_dices = int(input("How many dices would you like to roll?"
                                " (0 to quit) >"))
    if number_of_dices == 0:
        break
    else:
        user_roll = dice(number_of_dices)
        pc_roll = dice(number_of_dices)
        print(f"You: Roll: {user_roll(0)}, a total of: {user_roll(1)}")
        print(f"BOT: Roll: {pc_roll(0)}, a total of: {pc_roll(1)}")
        dice_check(user_roll(1), pc_roll(1))

c# – Object orianted programing in a game

Right now I have a snake game that is working but I need a way to make my code look better and work better.

A method I named “GameSense” is very long and I need a way to make it more object oriented. I want to use Enums.

This is what my teacher sent back to me:

The game is very nice and nicely working tail! The test you have also follows the right idea but uses an UpdatePosition method that you have not implemented. I’m also thinking about your class pixel – you do not use it for anything in the rest of the game. You should use it instead of e.g. berryx, berryy. What I am trying to access is more use of object orientation and division into methods, and not a single long method like GameSense right now. Also want to see you use enum / bool instead of strings for e.g. the variables movement and buttonpressed. However, I’m glad you got an interface on a corner

As you can see… I feel pretty suck right now.
What do I need to do here in order to make the program be more object oriented?

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

namespace Snake
{
    /// <summary>
    /// Start class, everything goes from here.
    /// Made by Karwan!
    /// </summary>
   public class Starter : InterF
    { 
        public static object Key { get; private set; }

        /// <summary>
        /// Main method.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string() args)
        {
            //Making the text green and also given a player 
            //options to chose from. 
            Console.ForegroundColor = ConsoleColor.Green;
            bool loopC = true;
            while (loopC)
            {
                string mystring = null; Console.WriteLine("Welcome to Snake.nPress S+Enter to start or press Q+Enter to exit.");
                mystring = Console.ReadLine();
                switch (mystring)
                {
                    case "Q":
                        Environment.Exit(0);
                        break;
                    case "S":
                        Console.WriteLine("Starting game!");
                        System.Threading.Thread.Sleep(4000);
                        loopC = false;
                        break;
                    default:
                        Console.WriteLine("Invalid Entry.. Try again.");
                        break;
                }

            }

            //Call for GameSense if
            //they want to play.
            GameSense(); 

        }


      
        /// <summary>
        /// Game object!
        /// </summary>
        public static void GameSense()

        {
            //Game console height/width.
            Console.WindowHeight = 30;
            Console.WindowWidth = 70;
            int screenwidth = Console.WindowWidth;
            int screenheight = Console.WindowHeight;
            Random randomnummer = new Random();
            //Lenght of tail == current score.
            int score = 2;
            int gameover = 0;
            //Gather positions from pixel class.
            pixel positions = new pixel();
            positions.xpos = screenwidth / 2;
            positions.ypos = screenheight / 2;
            positions.Black = ConsoleColor.Red;
            string movement = "RIGHT";
            //Position manegment.
            List<int> xpos = new List<int>();
            List<int> ypos = new List<int>();
            int berryx = randomnummer.Next(0, screenwidth);
            int berryy = randomnummer.Next(0, screenheight);
            //Time manegment.
            DateTime time1 = DateTime.Now;
            DateTime time2 = DateTime.Now;
            string buttonpressed = "no";

            
            //Draw world from GameWorld.cs.
            GameWorld.DrawBorder(screenwidth, screenheight);

            while (true)
            {
                GameWorld.ClearConsole(screenwidth, screenheight);
                if (positions.xpos == screenwidth - 1 || positions.xpos == 0 || positions.ypos == screenheight - 1 || positions.ypos == 0)
                {
                    gameover = 1;
                }

                Console.ForegroundColor = ConsoleColor.Green;
                if (berryx == positions.xpos && berryy == positions.ypos)
                {
                    score++;
                    berryx = randomnummer.Next(1, screenwidth - 2);
                    berryy = randomnummer.Next(1, screenheight - 2);
                }
                for (int i = 0; i < xpos.Count(); i++)
                {
                    Console.SetCursorPosition(xpos(i), ypos(i));
                    Console.Write("*");
                    if (xpos(i) == positions.xpos && ypos(i) == positions.ypos)
                    {
                        gameover = 1;
                    }
                }
                if (gameover == 1)
                {
                    break;
                }
                Console.SetCursorPosition(positions.xpos, positions.ypos);
                Console.ForegroundColor = positions.Black;
                Console.Write("*");
                //Food color & position.
                Console.SetCursorPosition(berryx, berryy);
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write("*");
                Console.CursorVisible = false;
                time1 = DateTime.Now;
                buttonpressed = "no";
                while (true)
                {
                    time2 = DateTime.Now;
                    if (time2.Subtract(time1).TotalMilliseconds > 500) { break; }
                    if (Console.KeyAvailable)
                    {
                        ConsoleKeyInfo info = Console.ReadKey(true);
                        //Connecting the buttons to the x/y movments. 
                        if (info.Key.Equals(ConsoleKey.UpArrow) && movement != "DOWN" && buttonpressed == "no")
                        {
                            movement = "UP";
                            buttonpressed = "yes";
                        }
                        if (info.Key.Equals(ConsoleKey.DownArrow) && movement != "UP" && buttonpressed == "no")
                        {
                            movement = "DOWN";
                            buttonpressed = "yes";
                        }
                        if (info.Key.Equals(ConsoleKey.LeftArrow) && movement != "RIGHT" && buttonpressed == "no")
                        {
                            movement = "LEFT";
                            buttonpressed = "yes";
                        }
                        if (info.Key.Equals(ConsoleKey.RightArrow) && movement != "LEFT" && buttonpressed == "no")
                        {
                            movement = "RIGHT";
                            buttonpressed = "yes";
                        }
                    }
                }
                //Giving the connections value
                //to change x/y to make the movment happen.
                xpos.Add(positions.xpos);
                ypos.Add(positions.ypos);
                switch (movement)
                {
                    case "UP":
                        positions.ypos--;
                        break;
                    case "DOWN":
                        positions.ypos++;
                        break;
                    case "LEFT":
                        positions.xpos--;
                        break;
                    case "RIGHT":
                        positions.xpos++;
                        break;
                }
                if (xpos.Count() > score)
                {
                    xpos.RemoveAt(0);
                    ypos.RemoveAt(0);
                }
            }
            Console.SetCursorPosition(screenwidth / 5, screenheight / 2);
            Console.WriteLine("Game over, Score: " + score);
            Console.SetCursorPosition(screenwidth / 5, screenheight / 2 + 1);
            System.Threading.Thread.Sleep(1000);
            restart();
            
        }

        /// <summary>
        /// Restarter.
        /// </summary>
        public static void restart()
        {
        
            string Over = null; Console.WriteLine("nWould you like to start over? Y/N");
            bool O = true;

            while (O)
            {
                Over = Console.ReadLine();
                switch (Over)
                {
                    case "Y":
                        Console.WriteLine("nRestarting!");
                        System.Threading.Thread.Sleep(2000);
                        break;
                    case "N":
                        Console.WriteLine("nThank you for playing!");
                        Environment.Exit(0);
                        break;
                    default:
                        Console.WriteLine("Invalid Entry.. Try again.");
                        break;
                }
            }

        }
        /// <summary>
        /// Set/get pixel position.
        /// </summary>
       public class pixel
        {
            public int xpos { get; set; }
            public int ypos { get; set; }
            public ConsoleColor Black { get; set; }
        }
    }
}

My test class

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using Snake;
using System;
using System.Collections.Generic;
using System.Text;
using static Snake.Starter;

namespace Snake.Tests
{
    (TestClass())
    public class StarterTests
    {
        /// <summary>
        /// Testing the movment.
        /// </summary>
        (TestMethod())
        public void test()
        {
           
           var pos = new pixel();
            pos.xpos = 10;
            pos.ypos = 10;

            // this is the operation:
            UpdatePosition(pos, "UP");

            // check the results
            NUnit.Framework.Assert.That(pos.xpos, Is.EqualTo(10));
            NUnit.Framework.Assert.That(pos.ypos, Is.EqualTo(9));
        }

        /// <summary>
        /// Updating pixel class.
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="v"></param>
        private void UpdatePosition(pixel pos, string v)
        {
            throw new NotImplementedException();
        }
    }
}

This is just the gameworld:

using System;
using System.Linq;

public class GameWorld 
{


    /// <summary>
    /// Clear console for snake.
    /// </summary>
    /// <param name="screenwidth"></param>
    /// <param name="screenheight"></param>
    public static void ClearConsole(int screenwidth, int screenheight)
    {
        var blackLine = string.Join("", new byte(screenwidth - 2).Select(b => " ").ToArray());
        Console.ForegroundColor = ConsoleColor.Black;
        for (int i = 1; i < screenheight - 1; i++)
        {
            Console.SetCursorPosition(1, i);
            Console.Write(blackLine);
        }
    }

    /// <summary>
    /// Draw boared.
    /// </summary>
    /// <param name="screenwidth"></param>
    /// <param name="screenheight"></param>
    public static void DrawBorder(int screenwidth, int screenheight)
    {
        var horizontalBar = string.Join("", new byte(screenwidth).Select(b => "■").ToArray());

        Console.SetCursorPosition(0, 0);
        Console.Write(horizontalBar);
        Console.SetCursorPosition(0, screenheight - 1);
        Console.Write(horizontalBar);

        for (int i = 0; i < screenheight; i++)
        {
            Console.SetCursorPosition(0, i);
            Console.Write("■");
            Console.SetCursorPosition(screenwidth - 1, i);
            Console.Write("■");
        }
    }

}