0

I am trying to code a card game, and I'm making a shuffling system using a function, however it's telling me I need a closing bracket while all my brackets are closed. It's also asking me for an end-of-file or namespace definition. I'm using an online editor (dotnetfiddle.net) to edit this code, if that changes anything.

Here's my current code-

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        List<string> shuffle(List<string> l) { //ERROR 1: } expected
            int count = l.Count-1;
            List<string> ret = new List<string>();
            int ind = 0;
            Random rng = new Random();
            string card = null;
            while (count > -1) {
                ind = rng.Next(0, count);
                card = l[ind];
                l.RemoveAt(ind);
                ret.Add(card);
                card = null;
                count--;
            }
            return ret;
        }
        List<List<string>> playerHands = new List<List<string>>();
        // (copypaste symbols)
        List<string> deck = new List<string>  {"1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        List<string> sDeck = new List<string> {"R", "S❌", "D", "X⛈", "+✨", "A", "A"};
        List<string> vDeck = new List<string> {"V◆", "V◇", "V◈"};
    }
}//ERROR 2: Namespace, type, or end-of-file expected
Razzon22
  • 17
  • 7
  • 1
    The code you are showing doesn't produce that error. Is there more code? Does the error message give you a line number? – Gabriel Luci Nov 28 '22 at 18:10
  • The Visual Studio editor (and VS Code) has a parens and brace matching feature https://stackoverflow.com/questions/30678299/highlight-matching-brace-in-visual-studio and https://stackoverflow.com/questions/1501921/go-to-matching-brace-in-visual-studio – Flydog57 Nov 28 '22 at 18:13
  • Did you not close your namespace parens? Or did you switch to file scoped namespace and forgot to remove one? – Fildor Nov 28 '22 at 18:15
  • 1
    Side note: in the dotnet world most people follow the [Allman style](https://en.wikipedia.org/wiki/Indentation_style#Allman_style) and to be very honest this helps quite a bit with solving missing brace errors. K&R does look nicer but after some time I've returned to allman's, its just more useful – Narish Nov 28 '22 at 18:24
  • Maybe it is related to the C# version you used. Local functions are only allowed when using C# 7. When I pasted your code in VS 2019, the compilation succeeded. The only warning I have, is related to the function `shuffle()` that is declared but never used. Also, always put your class in a namespace. – Rivo R. Nov 28 '22 at 20:39
  • @RivoR. Can you put that up as an answer? I just had to switch to .NET 7 -_- – Razzon22 Nov 28 '22 at 22:43

1 Answers1

0

Maybe it is related to the C# version you used.

Local functions are only allowed when using C# 7.

When I pasted your code in VS 2019, the compilation succeeded.

The only warning I have, is related to the function shuffle() that is declared but never used.

Also, always put your class in a namespace.

Rivo R.
  • 351
  • 2
  • 8