help me with coding

Posts 18 of 8 · Page 1 of 1
help me with coding
i am really bad at coding i dont know anything and my assignment is due next week could someone do this or help thanks in advance
any help in ethier of the games/tasks would be helpful
also its in visual studio
ppppppppppp.PNG78 KB · 43 downloads ppppppppppp2.PNG75 KB · 23 downloads
Can't see the attachments, what are you currently learning in your C# class? I may be able to help you. Let me know! You could also try SO, but they are full of angry people who will flame if you ask a question in a dumb way.
thanks we are learning to make a nots and crosses gaming using console.write,readline,readkey, the attachments display the task i have to finish i would be grateful for any help given!
Wow I wish my school did this, this shit looks so fun

- - - Updated - - -

I mean I could easily do this in c++ but I'm just now getting into c# sorry lol

I recommend you to not just copy and paste this since I don't know the exact output the task wanted. Also I don't know if the lecturer will ask you about certain code-style and flow decisions I made.

 
tic-tac-toe
Code:
namespace Nac
{
    using System;
    using System.Collections.Generic;

    internal static class Program
    {
        private class Position
        {
            public int X { get; private set; }

            public int Y { get; private set; }

            public Position(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }

            public void Right()
            {
                this.Y++;
            }

            public void Left()
            {
                this.Y--;
            }

            public void Up()
            {
                this.X--;
            }

            public void Down()
            {
                this.X++;
            }
        }

        private const int Height = 3;
        private const int Width = 3;

        private const char Blank = ' ';
        private const char HumanPlayerSymbol = 'X';
        private const char AiPlayerSymbol = 'O';

        private static readonly Random Random = new Random();
        private static Position _cursorPosition = new Position(0, 0);
        private static readonly char[,] Field = new char[Program.Height, Program.Width];

        private static void Main()
        {
            Console.Title = "Tic-tac-toe";
            Console.CursorVisible = false;

            do
            {
                Program.ResetField();
                while (Program.HandleInput())
                {
                    Program.Render();
                }
            } while (Console.ReadKey(false).Key == ConsoleKey.R);
        }

        private static void ResetField()
        {
            for (int x = 0; x < Program.Height; x++)
            {
                for (int y = 0; y < Program.Width; y++)
                {
                    Program.Field[x, y] = Program.Blank;
                }
            }

            Program.Render();
        }

        private static bool HandleInput()
        {
            switch (Console.ReadKey(true).Key)
            {
                case ConsoleKey.Escape:
                case ConsoleKey.Q:
                    return false;

                case ConsoleKey.RightArrow:
                    if (Program._cursorPosition.Y < Program.Width - 1)
                    {
                        Program._cursorPosition.Right();
                    }

                    break;

                case ConsoleKey.LeftArrow:
                    if (Program._cursorPosition.Y > 0)
                    {
                        Program._cursorPosition.Left();
                    }

                    break;

                case ConsoleKey.DownArrow:
                    if (Program._cursorPosition.X < Program.Height - 1)
                    {
                        Program._cursorPosition.Down();
                    }

                    break;

                case ConsoleKey.UpArrow:
                    if (Program._cursorPosition.X > 0)
                    {
                        Program._cursorPosition.Up();
                    }

                    break;

                case ConsoleKey.Spacebar:
                    if (Program.SetCell(Program._cursorPosition.X, Program._cursorPosition.Y, Program.HumanPlayerSymbol) == false)
                    {
                        break;
                    }

                    if (Program.PrintWinner())
                    {
                        return false;
                    }

                    if (Program.MakeAiMove() == false)
                    {
                        Program.PrintMessage("Uh oh.. nobody wins. Press 'R' to play again.", ConsoleColor.Red);
                        return false;
                    }

                    if (Program.PrintWinner())
                    {
                        return false;
                    }
                    
                    break;

                case ConsoleKey.R:
                    Program.ResetField();
                    break;
            }

            return true;
        }

        private static bool PrintWinner()
        {
            char winner = Program.DetermineWinner();
            if (winner != Program.Blank)
            {
                Program.Render();
                Program.PrintMessage($"Player {winner} won. Press 'R' to play again.", ConsoleColor.Green);
                return true;
            }

            return false;
        }

        private static void PrintMessage(string message, ConsoleColor color)
        {
            Console.ForegroundColor = color;
            Console.SetCursorPosition(0, Program.Height * 2);
            Console.WriteLine(message);
            Console.ResetColor();

            Program.RenderCursor();
        }

        private static IList<Position> GetAvailableCells()
        {
            List<Position> available = new List<Position>();
            for (int x = 0; x < Program.Height; x++)
            {
                for (int y = 0; y < Program.Width; y++)
                {
                    if (Program.Field[x, y] == Program.Blank)
                    {
                        available.Add(new Position(x, y));
                    }
                }
            }

            return available;
        }

        private static char DetermineWinner()
        {
            // Horizontally
            for (int x = 0; x < Program.Height; x++)
            {
                bool won = true;
                for (int y = 0; y < Program.Width - 1; y++)
                {
                    won = won && Program.Field[x, y] != Program.Blank && Program.Field[x, y] == Program.Field[x, y + 1];
                }

                if (won)
                {
                    return Program.Field[x, 0];
                }
            }

            // Vertically
            for (int y = 0; y < Program.Width; y++)
            {
                bool won = true;
                for (int x = 0; x < Program.Height - 1; x++)
                {
                    won = won && Program.Field[x, y] != Program.Blank && Program.Field[x, y] == Program.Field[x + 1, y];
                }

                if (won)
                {
                    return Program.Field[0, y];
                }
            }

            // Diagonal left to right
            {
                bool won = true;
                for (int i = 0; i < Program.Height - 1; i++)
                {
                    won = won && Program.Field[i, i] != Program.Blank &&
                          Program.Field[i, i] == Program.Field[i + 1, i + 1];
                }

                if (won)
                {
                    return Program.Field[0, 0];
                }
            }

            // Diagonal right to left
            {
                bool won = true;
                for (int i = 0; i < Program.Height - 1; i++)
                {
                    won = won && Program.Field[i, Program.Width - i - 1] != Program.Blank &&
                          Program.Field[i, Program.Width - i - 1] == Program.Field[i + 1, Program.Width - i - 2];
                }

                if (won)
                {
                    return Program.Field[0, Program.Width - 1];
                }
            }

            return Program.Blank;
        }

        private static bool MakeAiMove()
        {
            IList<Position> availableCells = Program.GetAvailableCells();
            if (availableCells.Count == 0)
            {
                return false;
            }

            int pick = Program.Random.Next(0, availableCells.Count);
            Program.SetCell(availableCells[pick].X, availableCells[pick].Y, Program.AiPlayerSymbol);
            return true;
        }

        private static bool SetCell(int x, int y, char symbol)
        {
            if (Program.Field[x, y] != Program.Blank)
            {
                return false;
            }

            if (x > Program.Height || x < 0 || y > Program.Width || y < 0)
            {
                return false;
            }

            Program.Field[x, y] = symbol;
            return true;
        }

        private static void Render()
        {
            Console.SetCursorPosition(0, 0);

            for (int x = 0; x < Program.Height; x++)
            {
                for (int y = 0; y < Program.Width - 1; y++)
                {
                    Console.Write($" {Program.Field[x, y]} |");
                }

                Console.Write($" {Program.Field[x, Program.Width - 1]} ");
                Console.WriteLine();

                if (x != Program.Height - 1)
                {
                    const int numberOfDashes = (Program.Width - 1) * 4 + 3;
                    Console.WriteLine(new string('-', numberOfDashes));
                }
            }

            // Clear printed message
            Console.WriteLine();
            Console.WriteLine(new string('\0', Console.BufferWidth));

            Program.RenderCursor();
        }

        private static void RenderCursor()
        {
            Console.SetCursorPosition(Program._cursorPosition.Y * 4 + 1, Program._cursorPosition.X * 2);

            Console.BackgroundColor = ConsoleColor.Cyan;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.Write(Program.Field[Program._cursorPosition.X, Program._cursorPosition.Y]);
            Console.ResetColor();
        }
    }
}


 
Default output


 
Output after setting the Height and Width to 7



Use the arrow keys to navigate within the grid, spacebar to set the cell to 'X' and R to reset the game.

 
Golf
Code:
namespace Golf
{
    using System;
    using System.Collections.Generic;

    internal static class Program
    {
        private class GolfClub
        {
            public string Name { get; private set; }

            public int MinDistance { get; private set; }

            public int MaxDistance { get; private set; }

            public GolfClub(string name, int min, int max)
            {
                this.Name = name;
                this.MinDistance = min;
                this.MaxDistance = max;
            }

            public int Hit(int distanceToHole)
            {
                if (distanceToHole <= this.MaxDistance && distanceToHole >= this.MinDistance)
                {
                    return Program.Random.Next(this.MinDistance, distanceToHole);
                }

                return Program.Random.Next(this.MinDistance, this.MaxDistance + 1);
            }
        }

        private static readonly Random Random = new Random();
        private static int _remainingDistance;
        private static GolfClub _selectedClub;
        private static int _hits;

        private static readonly List<GolfClub> Clubs = new List<GolfClub>
        {
            new GolfClub("Wood", 150, 500),
            new GolfClub("Iron", 100, 300),
            new GolfClub("Putter", 1, 50)
        };

        private static void Main()
        {
            Console.Title = "Golf";
            Console.CursorVisible = false;
            
            do
            {
                Program.Reset();
                while (Program._remainingDistance != 0)
                {
                    Console.Clear();

                    Program.PrintFieldInfo();
                    Program.PrintClubInfos();
                    Program.SelectClub();

                    Program.PerformHit();
                }

                Console.Clear();
                Console.WriteLine($"Took you {Program._hits} damn hits.");

            } while (Console.ReadKey(true).Key == ConsoleKey.R);
        }

        private static void PrintClubInfos()
        {
            for (int i = 0; i < Program.Clubs.Count; i++)
            {
                Console.WriteLine($"{i + 1}) {Program.Clubs[i].Name}");
                Console.WriteLine($"   Distance: {Program.Clubs[i].MinDistance}m - {Program.Clubs[i].MaxDistance}m");
                Console.WriteLine();
            }
        }

        private static void PrintFieldInfo()
        {
            Console.WriteLine($"Distance to hole: {Program._remainingDistance}m");
            Console.WriteLine($"Tries: {Program._hits}");
            Console.WriteLine();
        }

        private static int ConsoleKeyToInt()
        {
            ConsoleKeyInfo key;
            do
            {
                key = Console.ReadKey(true);
            } while (key.Key > ConsoleKey.D9 || key.Key < ConsoleKey.D0);

            return (int)key.Key - (int)ConsoleKey.D0;
        }

        private static void SelectClub()
        {
            Console.WriteLine("Please select your club.");
            Console.WriteLine();

            int selection = Program.ConsoleKeyToInt();
            Program._selectedClub = Program.Clubs[selection - 1];
        }

        private static void PerformHit()
        {
            int distance = Program._selectedClub.Hit(Program._remainingDistance);
            Program._remainingDistance -= distance;
            if (Program._remainingDistance < 0)
            {
                Program._remainingDistance *= -1;
            }

            Program._hits++;
        }

        private static void Reset()
        {
            Program._remainingDistance = Program.Random.Next(300, 500);
            Program._hits = 0;
            Program._selectedClub = Program.Clubs[0];
        }
    }
}


 
Output


Press 1-3 to select a club for the next hit. When the game has finished press R for another round. I didn't spend much time trying to create a decent calculation tho.

There's a dude in the marketplace that'll help ya for some small cash
This app helped me really much! It's 100% free, but I THINK only on android.
https://play.google.com/store/apps/d...lolearn.csharp
500 000 downloads with a rating of 4.8 out of 5.
It's more like a quiz with description. Great interface and a growing community, they also have a free IDE which you can create your own console application through your phone though you cannot install your own made app, but you can run it on their servers.
Good luck!
-Supermarre1
Posts 18 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?