# CodeChef-Ciel and A-B

```using System;

public class Program
{
public static void Main()
{
string[] splitLines = line.Split(' ');

int a = int.Parse(splitLines[0]);
int b = int.Parse(splitLines[1]);
int answerNumber = a - b;

for(int i = 0; i < length; i++)
{
}

{
}
else
{
}

{
Console.Write(num);
}
}
}```

So here’s another solution to a CodeChef problem. The problem is to take two user-generated integers on the same line(‘A’ and ‘B’), find the difference, and then make the answer incorrect but changing one of it’s integers. And finally write the incorrect answer to the console. The OCD part of my brain was not a huge fan of writing an algorithm to produce an incorrect answer to a simple math problem but I think it was overall a good exercise for the basics.

Here’s a link to the problem.

# CodeChef-Enormous Input Test

```using System;

public class Program
{
public static void Main()
{
string[] splitLines = lines.Split(' ');

int n = int.Parse(splitLines[0]);
int k = int.Parse(splitLines[1]);
int[] t = new int[n];
int output = 0;

for(int i = 0; i < n; i++)
{
}

foreach(int i in t)
{
if(i % k == 0)
{
output++;
}
}

Console.WriteLine(output);
}
}
```

I’m now officially addicted to CodeChef. These problems are so much fun. This is my solution to another beginner problem. In this one, the user inputs two integers separated by a space(hints the split with a ‘ ‘ on line 8). The first integer is ‘n’ and it indicates how many numbers are going to be tested. The second integer is ‘k’ and this is going to be used to find out if each subsequent integer is a multiple of ‘k’.

Here’s a link to the problem.

# CodeChef-ATM

```using System;

public class Test
{
public static void Main()
{
string[] splitLines = line.Split(' ');

double withdrawal = double.Parse(splitLines[0]);
double balance = double.Parse(splitLines[1]);

if(withdrawal % 5 == 0 && balance >= withdrawal + 0.5)
{
double updatedBalance = balance - withdrawal - 0.5;
Console.WriteLine(updatedBalance.ToString("F"));
}
else
{
Console.WriteLine(balance.ToString("F"));
}
}
}```

Here’s my solution to the ATM problem on CodeChef.  The user has to input a withdrawal amount and a balance amount on the same line(Why? IDK).  There is an ATM usage fee of \$0.50 that must be added to the withdrawal.  The withdrawal of course cannot exceed the balance and must be a multiple of 5.  Simple may this problem be, I actually felt like I learned few things from doing it.

1. Split() is a really nifty method for when you need to split strings.
2. Format specifiers, in this case “F”, are really nifty for when you need to specify precision.
3. Try not to get big-headed even when doing simple tasks.  Next thing you know, you’ll have a logical brain fart, get frustrated, and slip into an existential crisis.

Here’s the link to the problem.

# CodeChef

I’ve been looking for a site to practice some programming problems when I came across CodeChef.  It has tons of problems ranging from beginner to advanced.  Some of the more advanced problems are what I would imagine in a coding interview.  You have to make an account but it’s all free.  There are also contests and rankings and things.  It’s pretty convenient too because once you submit your solution, it tells you right away rather you solved it or not as the process is automated.  The following code is my solution to an easy problem I did to test it out.  All it is, is a program to print integers to the console until the number 42 is inputted.  And here’s a link to the problem.

```using System;

public class Program
{
public static void Main()
{
int input = 0;

while(input != 42)
{

if(input != 42)
{
Console.WriteLine(input);
}
}
}
}```

The IDE on the site has been super buggy for me.  It almost made me look elsewhere for some coding problems.  But I decided to try and just use another online IDE and just copy and paste my solution.  Once I did that, it worked quite nicely.  So I think I’ll stick with this site.  Other than the buggy IDE it seems really good.  The IDE I started using is .NetFiddle.

# The Fisher-Yates Shuffle

```using UnityEngine;
using System.Collections;

public class Shuffle : MonoBehaviour
{
public static void ShuffleItems<T>(T[] items)
{
for (int i = items.Length - 1; i > 0; i--)
{
int r = Random.Range (0,i);
T temp = items[i];
items[i] = items[r];
items[r] = temp;
}
}
}
```

If you’ve played Galactic Scholar you may have noticed the cards always come at the ship in a different order. I did this intentionally so that the player could not simply memorize the order thus making it more challenging. I achieved this by implementing the Fisher-Yates shuffle(above).

```using UnityEngine;
using System.Collections;

public class PlayingCards : MonoBehaviour
{
int[] deck = new int[52];

void Start ()
{
InitializeDeck();
}

void Update ()
{
if (Input.GetKeyDown (KeyCode.Return))
{
Shuffle.ShuffleItems(deck);
foreach(int card in deck)
{
print(card.ToString());
}
}
}

void InitializeDeck ()
{
for(int i = 0; i < 52; i++)
{
deck[i] = i + 1;
}
}
}
```

In this script we’ve created a standard deck of 52 cards with a numerical value assigned to each one. Then we call ShuffleItems() and print the result to the console.

### Considerations

1. ShuffleItems() is a generic function indicated by the <T> and T[] syntax.  This allows you to call this function with different types.
2. Random.Range() is a Unity function that can return either an int or a float inclusive.  So if you have a range (0,5) it could return either 0 or 5.
3. Because this is a generic function that allows you to use arrays of any length, it is very reusable thus good for modular game development.

# Obligatory Singleton Post

```using UnityEngine;
using System.Collections;

public class Singleton : Monobehaviour
{
public static Singleton instance;

void Awake()
{
if(instance == null)
{
instance = this;
}
else if(instance != this)
{
Destroy(gameObject);
}
}
}
```

This is the Singleton I often use when developing in Unity. I’ve spent the past few days doing some more in depth research for using Singletons specifically for game development in Unity. I’ve learned a few things; first, everyone and their dog has written a post on Singletons, secondly, there are an endless amount of developers arguing the merits and woes of Singletons, and lastly I’ve learned that even though there are many implementations of Singletons, this basic pattern handles most scenarios.  These things have given me inspiration as to the direction of future posts and this blog in general.  I am going to keep things concise and practical.  So without further ado, here is an example of a Singleton in use.

```using UnityEngine;
using System.Collections;

public class Player : Monobehaviour
{
public static Player instance;
public int health = 100;

void Awake()
{
if(instance == null)
{
instance = this;
}
else if(instance != this)
{
Destroy(gameObject);
}
}
}
```

In this Player class we have added health and made it Public. This is important if you want to give other scripts access to this variable.

```using UnityEngine;
using System.Collections;

public class Sword : Monobehaviour
{
void Update()
{
if(Input.GetKeyDown(KeyCode.Return))
{
Slash();
print(Player.instance.health.ToString());
}
}

void Slash()
{
Player.instance.health -= 30;
}
}
```

In this Sword class we have a Slash function that accesses the Player’s health and subtracts it by 30. In the Update function I’ve added some code to test Slash. It does indeed go down by 30. I realize the Player can now have negative health but for the sake of simplicity I’ll leave it there as it already demonstrates a Singleton.

### Considerations

1. DontDestroyOnLoad() only works with root GameObjects or components on root GameObjects.  Therefore you should leave DontDestroyOnLoad() off of child objects at least to save yourself from warnings filling up the console.