# C# TP X1

## Introductory stuff

This is an extension TP. It is entirely optional, not graded, and will not be reviewed. ACDC’s will not help you for this practical, but you are more than welcome to ping Matthieu ‘Zoroark’ Stombellini if you notice mistakes. This subject is entirely in English, as most practical work starting from S3 will only be in English (and, to be fair, everything about programming is in English in the real world, with a few exceptions here and there, so yeah, better get started fast!).

### Architecture

No specific architecture is required, nor is there a Git repository for you to clone.

• You should arrange the exercises in whichever way you find most appropriate
• You should use a Git repository, even if it is just a local one. You may use GitHub, GitLab or any other Git hosting website.
• You must test your code as thoroughly as possible. You should use a proper testsuite for this.

### Language

The recommended language is C#. You can also use any other language for these exercises.

All functions in the standard library of the chosen language are allowed, as long as you use them with your brain (i.e. use them to make your life easier, don’t use them to cheat and just let some standard function do the whole job for you).

## Threshold 1: Polynomial Showdown

### Zorostonks

The economy has crashed due to the COVID-19 crisis, and some governments have issued a challenge for young developers: write down a function that computes the rise of COVID-19 cases in the European Union.

Given an input float `day` (i.e. `x`), your function must compute the following incredibly boring formula1:

`−0.000803x^5 + 0.029503 x^4 − 0.399069 x^3 + 2.182121 x^2 − 2.133257 x + 0.6`

There are a few ways of coding this function:

• Hardcoding the formula
• Storing the coefficients in an array and iterating over each coefficient

The first one is woefully inconvenient, so try to use the second one (I think this is called foreshadowing).

``````public static float ComputeCovidCases(float day)
``````
Day Result
0 0.6
1 0.278495
2 2.31577
13 9.37841
14 4.8993

All of these numbers, and this includes yours and mine, are approximation and, no matter how good your code is, you may have noticed that there will always be a slight difference. Try to have similar results up to 0.001

Alright, we made a polynomial calculator by hand, which was tedious, boring, awful, etc.

Time to make something more useful: a polynomial calculator for anything!

``````public static float ComputePolynomial(float x, float[] coefficients)
``````

The coefficient at index `i` corresponds to the coefficient multiplied by `x^i`. For example, `-1 + x^2 -6.3x^3` would be:

``````float[] coefficients = { -1, 0, 1, -6.3F };
``````
Info

Functions like `Math.Pow` are allowed.

### Graphical Zorostonks

This exercise is quite free-form: implement a console-based user interface for your ComputePolynomial function. The user must be able to:

• Input a polynomial of any size with an easy-to-use editor.
• Preview the look of the polynomial
• Launch the computation
• Get a nicely-formatted result

You are allowed to use external libraries for this. There are a bunch of component-based Console GUIs libraries out there, feel free to have a look at them (here’s an example: https://github.com/TomaszRewak/C-sharp-console-gui-framework)

If you feel like it, you can also implement a system where the polynomials can be saved to and read from a file. A true polynomial editor!

## Threshold 2: Sorting things out

In this section, you will have to implement a few sorting algorithms.

### Bogosort

This is the simplest algorithm in existence: while the array is not sorted, shuffle its elements.

``````public static void BogoSort(int[] array)
``````

Yep, that’s it. However, for this exercise, you must implement the shuffling yourself (i.e. you must not use any predefined “Shuffle” function), to make things a bit more interesting.

### Slowsort

Slowsort is a slow algorithm – even slower than BubbleSort. Time to implement it!

The basic idea is, given indexes `i` and `j`:

• If `j <= i`, return immediately
• Find the index that corresponds to the middle of `i` and `j`
• Call yourself recursively on the left and right parts of the array (as separated by the middle you found in the first step). Note that, considering our algorithm works, the left and right parts are sorted at this point.
• Find the maximum, which is either the last value of the left part of the array or the last value of the right part of the array, and move it to `j`.
• Call yourself recursively again, this time on `i` and `j - 1`

The whole `i` and `j` story is an “implementation detail” – the user does not have to care about them. The only thing they want is to sort their array, so, on top of the recursive function, you should also provide an easier-to-use function that just takes an array and sorts it in-place2.

``````public static void SlowSort(int[] array)
``````

### Sorting Visualizer

Create a console-based sorting visualizer for the algorithm of your choice. I’d recommend going with a simple one, like Bubble sort.

The idea is to have something like this:

``````Sorting: BubbleSort | Change 21

[  4,  5,  65,  7,  8,  99 ]
^
set to
4
``````
• The visualizer shows you the name of the sorting algorithm, the number of changes so far and a preview of what is being set.
• The display is updated every time something is “set” in the array. Instead of doing a good ol’ `array[i] = x`, you should instead call a custom function that updates the display.
• The sorting action can be driven either by:
• Pausing after each update of the display (something like a fraction of a second)
• Manually requiring input from the user (wait for the user to press “Space”)

### And?…

What, you thought I’d make you implement a sorting method that’s actually useful? HAH! You FOOL!

Seriously though, feel free to implement other more complicated sorting methods, a bunch of them are described on Wikipedia with examples and everything.

## Threshold 3: Mini-Bataille

This is known as “War” or “Battle” in English.

### The game

We will implement a lighter version of the Bataille. Because implementing a full game requires features you have not seen yet (things like stacks, enums, classes, etc.), the game will be simplified here.

Instead of storing the cards each player has, we will instead only store the number of cards the players have.

• Each player reveals a card from their deck
• Because we are not storing which cards players have, just pick a random card for both players
• The player with the highest value on his card takes both cards into his own deck
• The value of each card is, in decreasing order, Ace, King, Queen, Jockey, 10, 9, 8, 7, 6, 5, 4, 3, 2. Suits are ignored in this game.
• We are not implementing decks, so just add a point to his score and deduct one from the opponent’s points.

The whole challenge of the game is when both players end up with cards that are of the same value. In this case, the game proceeds as follows:

• Both players places one card face down, one card face up
• Keep going until the face up cards are of different values
• The player who gets the better card puts all the cards that were put down as the result of the equality in their deck.
• Once again, because we are not actually storing which cards are in which deck, simply add or remove points from each player according to how many cards were (supposedly) put down in this sequence.

A player wins when he has all of the cards in the game. A player loses when he does not have any cards left.

### The implementation

You should implement this game in the following steps:

• Implement a single round
• Implement a whole game made of multiple rounds, ignoring the case where the cards are of the same value
• Implement the whole game, including the same-value case.

Of course, we are asking for a game with a console user interface, which can be as follows:

``````Zoroark                                 cobaltarrena
23                                                29

+-----+          +-----+
| ACE |          | TEN |
| HRT |          | CLB |
+-----+          +-----+

Zoroark wins this round!
``````

This is just an example, feel free to make your version as fancy as you want!

The full rules of War (aka Bataille or Battle) are available on Wikipedia.

1. which I of course didn’t do myself, I used this website: https://mycurvefit.com/ based on random data with some adjustments ↩︎

2. An algorithm that does all of its operations directly on the array you give it is called in-place. There are other methods that instead return a brand new sorted array. ↩︎

Built with Hugo
Theme Stack designed by Jimmy