Old School C++ guess the number game

It's been a while since I blogged but I've got some old fashioned code I've updated using the latest versions. This is some C++ code for a simple guessing game. You get 10 tries to guess a number between 1 and 100. The game picks the number randomly and tells you if you've guessed to low, to high, or correctly. This is written using the newer C++ 11 standard so you'll notice some weird stuff with the random number generating. I will post some links to this below. #include <iostream> #include <random> using namespace std; int main() { random_device randomDevice; mt19937 mt(randomDevice()); uniform_real_distribution<double> dist(1.0, 100.0); int numberOfTries = 0; int numberToGuess = dist(mt); int maxNumberOfTries = 10; int currentGuess = 0; bool wonGame = false; cout << "Welcome to guess the number" << endl << end…

Raspberry PI 3+ python blinking LED

I've recently been getting into the raspberry PI world. I have a model 3+ that one of my first projects was to get an LED to blink. The following below will make an LED blink for a random amount of time for a random amount of times. import RPi.GPIO as GPIO import time import random from random import * # set the output mode of the GPIO pins GPIO.setmode(GPIO.BCM) # don't show warnings in the console GPIO.setwarnings(False) # this sets the output to pin 18 (positive) GPIO.setup(18,GPIO.OUT) # we randomly pick some numbers numberOfBlinks = randint(10,30) blinkLength = randint(1,5) print(numberOfBlinks,blinkLength) print("LEDs on") counter = 0 # loop through and turn the LED on and off while counter <= numberOfBlinks: # set the output to high (ON) GPIO.output(18,GPIO.HIGH) # wait for X seconds time.sleep(blinkLength) # set the output to low (OFF) GPIO.output(18,GPIO.LOW) counter =…

WPF create a grid splitter

You will often find yourself needing to have two panels on a screen with a slider between them. WPF gives you a nice way to handle this with the GridSplitter control. You will need to create a column for the actual splitter and it will use the width you give it in the column definition.

<Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="5" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <TextBlock FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Left side</TextBlock> <GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch" /> <TextBlock Grid.Column="2" FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextW…

WPF Bind a combobox to an enumeration

I've often found myself needing to bind a combo box to an enumeration I've created. You can do this in WPF but it's a bit complicated. You end up needing to create a static resource. I had mine in a user control. First the enumeration I was using:

namespace XmlCoderBI.Enums { /// <summary> /// An enumeration that represents the type of conversion we're doing /// </summary> public enum ConversionType { /// <summary> /// The binary /// </summary> [Description( "Binary" )] Binary = 0, /// <summary> /// The base64 /// </summary> [Description( "Binary" )] Base64 = 1, /// <summary> /// The hexadecimal /// </summary> [Description( "Hexadecimal" )] Hexadecimal = 2, /// <summary> /// The UTF-8 /// </summary> [Description( "…

C# async and await

The async and await keywords in C# are the heart of async programming. By using those two keywords, you can use resources in the .NET Framework or the Windows Runtime to create an asynchronous method almost as easily as you create a synchronous method. Asynchronous methods that you define by using async and await are referred to as async methods. The following example shows an async method. Almost everything in the code should look completely familiar to you. The comments call out the features that you add to create the asynchrony.

// Three things to note in the signature: // - The method has an async modifier. // - The return type is Task or Task<T>. (See "Return Types" section.) // Here, it is Task<int> because the return statement returns an integer. // - The method name ends in "Async." async AccessTheWebAsync() { // You need to add a reference to System.Net.Http to declare client. HttpClient client = new Ht…

C# Updated on Threading with .net 4

With .NET 4 we now have better ways of handling threads. So in this post I'm going to demonstrate several ways of creating and waiting for threads to finish.

First example is creating 10 threads with the new Task.Factory.StartNew() method then we call Task.WaitAll and the framework handles the rest. This call will however block until all the tasks are finished.

// Wait for all tasks to complete. Task[] tasks = new Task[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.Factory.StartNew(() => DoSomeWork(10000000)); } Task.WaitAll(tasks);
The second way allows us to no block.

var task1 = DoWorkAsync(); var task2 = DoMoreWorkAsync(); await Task.WhenAll(task1, task2);

C# parse invalid XML characters

I've been dealing with XML a bit lately and have found that when you don't control the data you get all sorts of weird stuff. XML 1.0 doesn't allow certain characters or the XML is invalid. I tested a variety of ways using streams and string builders but I found a bit of LINQ and using a .NET function and in two lines you get a string of XML that only has valid characters.

var validXmlChars = val.Where( ch => XmlConvert.IsXmlChar( ch ) ).ToArray(); return new string( validXmlChars );