/
C#_dotnet_tutorials.cs
548 lines (479 loc) · 14.7 KB
/
C#_dotnet_tutorials.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// How to write to the console
Console.WriteLine("Hello World!");
// How to write a variable inside a string to the console
// Use the '$' to allow variables inside of {} in a string
string aFriend = "Dudebro"
Console.WriteLine($"Hello {aFriend}");
// Trim white space
string greeting = " Hello World! ";
trimmedGreeting = greeting.Trim();
Console.WriteLine($"{trimmedGreeting}");
// Replace word in string
string sayHello = "Hello World!";
sayHello = sayHello.Replace("Hello", "Greetings");
Console.WriteLine(sayHello);
// Uppercase/lowercase strings
Console.WriteLine(sayHello.ToUpper());
Console.WriteLine(sayHello.ToLower());
// Search for substring in string
string songLyrics = "You say goodbye, and I say hello";
Console.WriteLine(songLyrics.Contains("goodbye")); // true
Console.WriteLine(songLyrics.Contains("greetings")); // false
// Check if string start/ends with substring
Console.WriteLine(songLyrics.StartsWith("You")); // true
Console.WriteLine(songLyrics.StartsWith("goodbye")); // false
Console.WriteLine(songLyrics.EndsWith("hello")); // true
Console.WriteLine(songLyrics.EndsWith("goodbye")); // false
// Parse different number forms based on culture
// (currency symbols, thousands separators, decimal separators)
// https://docs.microsoft.com/en-us/dotnet/standard/base-types/parsing-numeric#parsing-and-format-providers
//
// This example displays the following output:
// en-US: 1,304.16 --> 1304.16
//
// en-US: Unable to parse '$1,456.78'.
// fr-FR: Unable to parse '$1,456.78'.
//
// en-US: 1,094 --> 1094
//
// en-US: 152 --> 152
//
// en-US: Unable to parse '123,45 €'.
// fr-FR: Unable to parse '123,45 €'.
//
// en-US: Unable to parse '1 304,16'.
// fr-FR: 1 304,16 --> 1304.16
//
// en-US: Unable to parse 'Ae9f'.
// fr-FR: Unable to parse 'Ae9f'.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] values = { "1,304.16", "$1,456.78", "1,094", "152",
"123,45 €", "1 304,16", "Ae9f" };
double number;
CultureInfo culture = null;
foreach (string value in values) {
try {
culture = CultureInfo.CreateSpecificCulture("en-US");
number = Double.Parse(value, culture);
Console.WriteLine("{0}: {1} --> {2}", culture.Name, value, number);
}
catch (FormatException) {
Console.WriteLine("{0}: Unable to parse '{1}'.",
culture.Name, value);
culture = CultureInfo.CreateSpecificCulture("fr-FR");
try {
number = Double.Parse(value, culture);
Console.WriteLine("{0}: {1} --> {2}", culture.Name, value, number);
}
catch (FormatException) {
Console.WriteLine("{0}: Unable to parse '{1}'.",
culture.Name, value);
}
}
Console.WriteLine();
}
}
}
// Parse number styles from strings into numbers
// ("1,304" -> 1304)
// https://docs.microsoft.com/en-us/dotnet/standard/base-types/parsing-numeric#parsing-and-numberstyles-values
//
// This example displays the following output:
// Unable to convert '1,304'
// 1,304 --> 1304
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string value = "1,304";
int number;
IFormatProvider provider = CultureInfo.CreateSpecificCulture("en-US");
if (Int32.TryParse(value, out number))
Console.WriteLine("{0} --> {1}", value, number);
else
Console.WriteLine("Unable to convert '{0}'", value);
if (Int32.TryParse(value, NumberStyles.Integer | NumberStyles.AllowThousands,
provider, out number))
Console.WriteLine("{0} --> {1}", value, number);
else
Console.WriteLine("Unable to convert '{0}'", value);
}
}
// Parse unicode
// https://docs.microsoft.com/en-us/dotnet/standard/base-types/parsing-numeric#parsing-and-unicode-digits
//
// The example displays the following output:
// '12345' --> 12345
// Unable to parse '12345'.
// Unable to parse '١٢٣٤٥'.
// Unable to parse '১২৩৪৫'.
using System;
public class Example
{
public static void Main()
{
string value;
// Define a string of basic Latin digits 1-5.
value = "\u0031\u0032\u0033\u0034\u0035";
ParseDigits(value);
// Define a string of Fullwidth digits 1-5.
value = "\uFF11\uFF12\uFF13\uFF14\uFF15";
ParseDigits(value);
// Define a string of Arabic-Indic digits 1-5.
value = "\u0661\u0662\u0663\u0664\u0665";
ParseDigits(value);
// Define a string of Bangla digits 1-5.
value = "\u09e7\u09e8\u09e9\u09ea\u09eb";
ParseDigits(value);
}
static void ParseDigits(string value)
{
try {
int number = Int32.Parse(value);
Console.WriteLine("'{0}' --> {1}", value, number);
}
catch (FormatException) {
Console.WriteLine("Unable to parse '{0}'.", value);
}
}
}
// Convert string to number / number to string
// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number#calling-the-parse-and-tryparse-methods
//
using System;
public class StringConversion
{
public static void Main()
{
string input = String.Empty;
try
{
int result = Int32.Parse(input);
Console.WriteLine(result);
}
catch (FormatException)
{
Console.WriteLine($"Unable to parse '{input}'");
}
// Output: Unable to parse ''
try
{
int numVal = Int32.Parse("-105");
Console.WriteLine(numVal);
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
}
// Output: -105
if (Int32.TryParse("-105", out int j))
Console.WriteLine(j);
else
Console.WriteLine("String could not be parsed.");
// Output: -105
try
{
int m = Int32.Parse("abc");
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
}
// Output: Input string was not in a correct format.
string inputString = "abc";
if (Int32.TryParse(inputString, out int numValue))
Console.WriteLine(inputString);
else
Console.WriteLine($"Int32.TryParse could not parse '{inputString}' to an int.");
// Output: Int32.TryParse could not parse 'abc' to an int.
}
}
// Parse strings using split
// .Split can also take an array of characters
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
System.Console.WriteLine($"<{word}>");
}
// Navigate hero through grid to find princess
// m = hero; p = princess;
// where grid like:
// [---]
// [-m-]
// [--p]
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void displayPathtoPrincess(int n, String [] grid){
int princessX = 0;
int princessY = 0;
int heroX = 0;
int heroY = 0;
int xDif = 0;
int yDif = 0;
// Loop through each row
for (int i = 0; i < n; i++) {
// Loop through each column
for (int j = 0; j < n; j++) {
if (grid[i][j].Equals('p')) {
// Find princess location
princessX = i;
princessY = j;
} else if (grid[i][j].Equals('m')) {
// Find hero location
heroX = i;
heroY = j;
}
}
}
// Get X and Y difference between hero and princess
xDif = heroX - princessX;
yDif = heroY - princessY;
// Navigate hero LEFT OR RIGHT
if (xDif > 0) {
for (int i = 0; i < xDif; i++) {
Console.WriteLine("LEFT");
}
} else if (xDif < 0) {
int xDifPos = Math.Abs(xDif);
for (int i = 0; i < xDifPos; i++) {
Console.WriteLine("RIGHT");
}
}
// Navigate hero UP OR DOWN
if (yDif > 0) {
for (int i = 0; i < yDif; i++) {
Console.WriteLine("UP");
}
} else if (yDif < 0) {
int yDifPos = Math.Abs(yDif);
for (int i = 0; i < yDifPos; i++) {
Console.WriteLine("DOWN");
}
}
}
static void Main(String[] args) {
int m;
m = int.Parse(Console.ReadLine());
String[] grid = new String[m];
for(int i=0; i < m; i++) {
grid[i] = Console.ReadLine();
}
displayPathtoPrincess(m,grid);
}
}
// Log hero's next move to navigate through grid to princess
// m = hero; p = princess;
// where grid like:
// [-----]
// [-m---]
// [-----]
// [-----]
// [----p]
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void nextMove(int gSize,int heroY, int heroX, String [] grid){
int princessX = 0;
int princessY = 0;
int xDif = 0;
int yDif = 0;
string nextMove = "";
for (int i = 0; i < gSize; i++) {
for (int j = 0; j < gSize; j++) {
if (grid[i][j].Equals('p')) {
princessX = j;
princessY = i;
}
}
}
xDif = heroX - princessX;
yDif = heroY - princessY;
if (xDif != 0) {
if (xDif > 0) {
nextMove = "LEFT";
} else if (xDif < 0) {
nextMove = "RIGHT";
}
}
if (yDif != 0) {
if (yDif > 0) {
nextMove = "UP";
} else if (yDif < 0) {
nextMove = "DOWN";
}
}
Console.WriteLine(nextMove);
}
static void Main(String[] args) {
int n;
n = int.Parse(Console.ReadLine());
String pos;
pos = Console.ReadLine();
String[] position = pos.Split(' ');
int [] int_pos = new int[2];
int_pos[0] = Convert.ToInt32(position[0]);
int_pos[1] = Convert.ToInt32(position[1]);
String[] grid = new String[n];
for(int i=0; i < n; i++) {
grid[i] = Console.ReadLine();
}
nextMove(n, int_pos[0], int_pos[1], grid);
}
}
// Network custom menu
public class NetworkCustomMenu : NetworkBehaviour {
public Text ipTextBox;
public Text portTextBox;
public void StartServer()
{
//note that you can do this directly from hooking the button to the network manager if you want
NetworkManager.singleton.StartHost();
Debug.Log(NetworkManager.singleton.networkAddress);
}
public void JoinAsClient()
{
//You'll probably want to do a more robust check that the ip is either localhost or an ip format here
if (ipTextBox.text != null && ipTextBox.text.Length > 0)
{
NetworkManager.singleton.networkAddress = ipTextBox.text;
//again, we need a more careful check that we have a valid value for port here ideally
int x;
int.TryParse(portTextBox.text, out x); //usually the port will just be 7777 so this part isn't really nescessary unless you're specifying the port on a server
NetworkManager.singleton.networkPort = x;
//this is the actual code to start the client
NetworkManager.singleton.StartClient();
}
}
}
// Animate object
public class RisingGameObject : MonoBehaviour {
Animator anim;
void Start () {
anim = GetComponent<Animator>();
DelegaterSubject.methodDefinedElsewhere += makePlanetRise;
DelegaterSubject.methodDefinedElsewhere += logAnimationHappened;
}
public void makePlanetRise()
{
anim.SetBool("isRising", true);
DelegaterSubject.methodDefinedElsewhere -= makePlanetRise;
DelegaterSubject.methodDefinedElsewhere += stopPlanetRise;
}
public void stopPlanetRise()
{
anim.SetBool("isRising", false);
DelegaterSubject.methodDefinedElsewhere -= stopPlanetRise;
DelegaterSubject.methodDefinedElsewhere += makePlanetRise;
}
public void logAnimationHappened()
{
Debug.Log("Hey, it worked");
}
}
// Weighted sum
public static class InputHandler
{
public static string processText(string inS)
{
return inS;
}
public static double getWeightedSum(double[] gradesArr, double weight)
{
double sum = 0;
foreach (double grade in gradesArr)
{
sum += grade;
}
return (sum / gradesArr.Length) * weight;
}
public static double[] parseAsArray(string doubleString)
{
string[] numAsString = doubleString.Split(' ');
double[] arr = new double[numAsString.Length];
for (int i = 0; i < numAsString.Length; i++)
{
arr[i] = getNum(numAsString[i]);
}
return arr;
}
public static double getNum(string inS)
{
try
{
double num = Double.Parse(inS);
return num;
}
catch (FormatException e)
{
Debug.Log(e);
return -1;
}
}
public static bool isNum(string inS)
{
try
{
double num = Double.Parse(inS);
return true;
}
catch (FormatException e)
{
Debug.Log(e);
return false;
}
}
public static bool betterIsNum(string inS)
{
if (Double.TryParse(inS, out double j))
return true;
else
return false;
}
}
// Generate objects dynamically
public class Utilities : MonoBehaviour {
public GameObject prefab;
public Instantiate instScript;
public float spacing = 2f;
public void instantiatePrefab()
{
Instantiate(prefab, Vector3.zero, Quaternion.identity);
}
public void instantiatePrefabAtPosition(Vector3 pos)
{
Instantiate(prefab, pos, Quaternion.identity);
}
public void instantiatPrefabsEqualSpacing()
{
int i = 0;
int r = instScript.r;
Vector3 pos;
while (i < r) //i=0, r = whatever is input
{
pos = new Vector3(i * spacing, 2, i * spacing);
instantiatePrefabAtPosition(pos);
for (int j = 0; j < r; j++)
{
pos = new Vector3(i * spacing, 2 + j * spacing, i * spacing);
instantiatePrefabAtPosition(pos);
for (int k = 0; k < r; k++)
{
pos = new Vector3(i * spacing + k * spacing, 2 + j * spacing, i * spacing);
instantiatePrefabAtPosition(pos);
}
}
i++;
}
}
}