Visual Studio + ReSharper private modifier

When writing in c#, I’m used to not writing the private modifier for anything.
But for some reason when using ReSharper with Unity, the default settings recommends using private. I found this annoying as microsoft suggests not writing private, due to anything being private should be the default.
I was going going through stackoverflow, and read comments about readability and clarity by adding the private, but for me I think it makes things more clutered and harder to read..
Also probably depends on what you were used to when starting off coding?

Anyways, let’s disable the private being added automatically.

1.) Extensions -> ReSharper -> Options…
2.) Category: Code Editing -> Modifiers
Prefer explicit/implicit private modifier for type members -> Implicit

ShortLink Generator (random gen)

My current coding project is a temporary file store/share web app.
Now days everyone uses free coud services such as dropbox, google drive, and many others, but I remember the days where temp upload and share sites were a thing.
I still think it’s convenient for those that don’t use any cloud storage.
SO, one of the important functions is the shortlink generator. The link you share with your friends to access the temporary file you’ve uploaded.
Here’s the simple c# code I will be using.

The short link will be generated, and from there through DbContext, the app will make sure that the short link is in fact unique. If !unique, re-generate and check again. I highly doubt the app would have to re-generate though… But you always plan for the worst right.

static void Main(string[] args)
{
    // START OF MAIN /////////////////////


    Console.WriteLine(ShortLinkGenerator(6));


    // END OF MAIN /////////////////////
}

public static string ShortLinkGenerator(int length)
{
    Random random = new Random();
    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz12345679";
    return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
}

Doing another fundamental c# run

Touching up on the fundamentals of c# again by going over a few courses that I bought on Udemy, but never got around. Quick and dirty Tic Tac Toe, bare minimum functional.

internal class Board
{
    string[,] _board;
    bool _isPlayer1 = true;
    string _playerName = "Player1";
    string _playerTic = "O";
    string _playerSymbol;
    

    public char Selection { get; set; }

    public string[,] GameBoard
    {
        get => _board;
        set => _board = value;
    }

    public Board()
    {
        _board = new string[,]        
        {
            { "1", "2", "3" },
            { "4", "5", "6" },
            { "7", "8", "9" }
        };
    }

    public void Run()
    {
        Console.WriteLine("Welcome to your tic tac toe game.");
        for (int i = 0; i < GameBoard.GetLength(0); i++)
        {
            for (int j = 0; j < GameBoard.GetLength(1); j++)
            {
                Console.Write("_" + GameBoard[i, j] + "_|");
            }
            Console.WriteLine("");
        }
    }

    public void ShowBoard()
    {
        for (int i = 0; i < GameBoard.GetLength(0); i++)
        {
            for (int j = 0; j < GameBoard.GetLength(1); j++)
            {
                Console.Write("_" + GameBoard[i, j] + "_|");
            }
            Console.WriteLine("");
        }
    }

    public void Start()
    {
        Console.WriteLine($"{_playerName}: It's your turn (enter #): ");
        Selection = Console.ReadKey().KeyChar;
        Console.WriteLine("_____________________");
        Console.WriteLine();
        Console.WriteLine();
        Select();

    }

    public void Select()
    {
        for (int i = 0; i < GameBoard.GetLength(0); i++)
        {
            for (int j = 0; j < GameBoard.GetLength(1); j++)
            {
                if (GameBoard[i, j] == Selection.ToString())
                {
                    GameBoard[i, j] = _playerTic;
                }
            }
        }

        WinCheck();
        ShowBoard();

        _isPlayer1 = !_isPlayer1;

        if (!_isPlayer1)
        {
            _playerName = "Player2";
            _playerTic = "X";
        }
        else
        {
            _playerName = "Player1";
            _playerTic = "O";
        }
        
        Start();
    }

    public void WinCheck()
    {
        _playerSymbol = _isPlayer1 ? "O" : "X";

        if (GameBoard[0, 0] == _playerSymbol && GameBoard[0, 1] == _playerSymbol &&
            GameBoard[0, 2] == _playerSymbol ||
            GameBoard[1, 0] == _playerSymbol && GameBoard[1, 1] == _playerSymbol &&
            GameBoard[1, 2] == _playerSymbol ||
            GameBoard[2, 0] == _playerSymbol && GameBoard[2, 1] == _playerSymbol &&
            GameBoard[2, 2] == _playerSymbol ||

            GameBoard[0, 0] == _playerSymbol && GameBoard[1, 0] == _playerSymbol &&
            GameBoard[2, 0] == _playerSymbol ||
            GameBoard[0, 1] == _playerSymbol && GameBoard[1, 1] == _playerSymbol &&
            GameBoard[2, 1] == _playerSymbol ||
            GameBoard[2, 0] == _playerSymbol && GameBoard[1, 2] == _playerSymbol &&
            GameBoard[2, 2] == _playerSymbol ||

            GameBoard[0, 0] == _playerSymbol && GameBoard[1, 1] == _playerSymbol &&
            GameBoard[2, 2] == _playerSymbol ||
            GameBoard[0, 2] == _playerSymbol && GameBoard[1, 1] == _playerSymbol &&
            GameBoard[2, 0] == _playerSymbol)
        {
            if (_isPlayer1)
            {
                Console.WriteLine("=============");
                Console.WriteLine("Player1 Wins!");
                Console.WriteLine("=============");
                Environment.Exit(0);
            }
            else
            {
                Console.WriteLine("=============");
                Console.WriteLine("Player2 Wins!");
                Console.WriteLine("=============");
                Environment.Exit(0);
            }
        }
    }
}

30 days of code: Day 10

https://www.hackerrank.com/challenges/30-binary-numbers/

To be honest, I looked this one up. In fact, many people had the same solution. Don’t really know who the original is, but thanks. Decided not to write the Task, head to link above for full details on the problem.

var n = int.Parse(Console.ReadLine());

var sum = 0;
var max = 0;


while (n > 0)
{
    if (n % 2 == 1)
    {
        sum++;

        if (sum > max)
            max = sum;
    }
    else sum = 0;

    n = n / 2;
}

Console.WriteLine(max);

30 days of code: Day 9

https://www.hackerrank.com/challenges/30-recursion/

Recursions were new to me. Haven’t had to use them with my previous apps. Or maybe I could have? Learned Summation, Factorial, and Exponentiation.

Sample Input
3

Sample Output
6

int n = Convert.ToInt32(Console.ReadLine());
static int Factorial(int n)
{
    if (n == 1)
    {
        return 1;
    }
    else
    {
        return n * Factorial(n - 1);
    }
}

Console.WriteLine(Factorial(n));

30 days of code: Day 8

https://www.hackerrank.com/domains/tutorials/30-days-of-code

Using a Dictionary
Sample Input

3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry
Sample Output

sam=99912222
Not found
harry=12299933
// Input number of data to retrieve
int nameCount =Convert.ToInt32(Console.ReadLine());

// Create Dictionary
Dictionary<string, string> phoneBook = new Dictionary<string, string>();

// User adds name and phonenumber to Dictionary x times
for (int i = 0; i < nameCount; i++)
{
    string inputPhoneBook = Console.ReadLine();
    string[] nameAndPhoneNumber = inputPhoneBook.Split(" ");
    string name = nameAndPhoneNumber[0];
    string phoneNumber = nameAndPhoneNumber[1];

    phoneBook.Add(name, phoneNumber);
}

// Name search and output infinite times
while (true)
{
    string inputName = Console.ReadLine();
    if (!string.IsNullOrEmpty(inputName))
    {
        if (phoneBook.ContainsKey(inputName))
        {
            Console.WriteLine(inputName + "=" + phoneBook[inputName]);
        }
        else
        {
            Console.WriteLine("Not found");
        }
    }
}

30 Days of code: Day 6

https://www.hackerrank.com/domains/tutorials/30-days-of-code
Felt like there’s a better way to do this, but this was my take.

Problem was to choose how many words to read, and then output even and odd characters in the format below.
Sample Input

2
Hacker
Rank

Sample Output

Hce akr
Rn ak

        int inputNumber = Convert.ToInt32(Console.ReadLine());

        for (int i = 0; i < inputNumber; i++)
        {
            string s = Console.ReadLine();

            for (int x = 0; x < s.Length; x += 2)
            {
                char evenChar = s[x];
                Console.Write(evenChar);
            }
            Console.Write(" ");

            for (int x = 1; x < s.Length; x += 2)
            {
                char oddChar = s[x];
                Console.Write(oddChar);
            }
            Console.WriteLine();
        }

30 Days of code: Day 4

https://www.hackerrank.com/domains/tutorials/30-days-of-code
No key notes for today’s code.

class Person {
    public int age;     
	public Person(int initialAge) {
        // Add some more code to run some checks on initialAge
        if (initialAge >= 0){
            age = initialAge;
        }
        else {
            age = 0;
            Console.WriteLine("Age is not valid, setting age to 0.");
        }
     }
     public void amIOld() {
        // Do some computations in here and print out the correct statement to the console 
        if (age < 13){
            Console.WriteLine("You are young.");
        }
        else if (age >= 13 && age < 18){
            Console.WriteLine("You are a teenager.");
        }
        else {
            Console.WriteLine("You are old.");
        }
     }

     public void yearPasses() {
        // Increment the age of the person in here
        age++;
     }
}