C# and WPF tutorial files
This commit is contained in:
6
C# Beginners/ConsoleApplication1/App.config
Normal file
6
C# Beginners/ConsoleApplication1/App.config
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
|
||||
</startup>
|
||||
</configuration>
|
||||
61
C# Beginners/ConsoleApplication1/ConsoleApplication1.csproj
Normal file
61
C# Beginners/ConsoleApplication1/ConsoleApplication1.csproj
Normal file
@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{E7E6E236-2FD2-4D03-AED0-B15841A080B5}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>ConsoleApplication1</RootNamespace>
|
||||
<AssemblyName>ConsoleApplication1</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="NumberGuesser.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
22
C# Beginners/ConsoleApplication1/ConsoleApplication1.sln
Normal file
22
C# Beginners/ConsoleApplication1/ConsoleApplication1.sln
Normal file
@ -0,0 +1,22 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 14
|
||||
VisualStudioVersion = 14.0.25420.1
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApplication1", "ConsoleApplication1.csproj", "{E7E6E236-2FD2-4D03-AED0-B15841A080B5}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{E7E6E236-2FD2-4D03-AED0-B15841A080B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E7E6E236-2FD2-4D03-AED0-B15841A080B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E7E6E236-2FD2-4D03-AED0-B15841A080B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E7E6E236-2FD2-4D03-AED0-B15841A080B5}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
148
C# Beginners/ConsoleApplication1/NumberGuesser.cs
Normal file
148
C# Beginners/ConsoleApplication1/NumberGuesser.cs
Normal file
@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ConsoleApplication1
|
||||
{
|
||||
/// <summary>
|
||||
/// Asks the user to guess a number between a certain range and then guesses that number in the fewest possible guesses
|
||||
/// </summary>
|
||||
public class NumberGuesser
|
||||
{
|
||||
#region Public Properties
|
||||
|
||||
/// <summary>
|
||||
/// The largest number we ask the user to guess, between 0 and this number
|
||||
/// </summary>
|
||||
public int MaximumNumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The current number of guesses the computer has had
|
||||
/// </summary>
|
||||
public int CurrentNumberOfGuesses { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The current known minimum number the user is thinking of
|
||||
/// </summary>
|
||||
public int CurrentGuessMinimum { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The current known maximum number the user is thinking of
|
||||
/// </summary>
|
||||
public int CurrentGuessMaximum { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region .ctor
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor
|
||||
/// </summary>
|
||||
public NumberGuesser()
|
||||
{
|
||||
// Set default maximum number
|
||||
this.MaximumNumber = 100;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Asks the user to think of a number between 0 and the maximum number
|
||||
/// </summary>
|
||||
public void InformUser()
|
||||
{
|
||||
// Ask user to think of a number between 0 and MaximumNumber
|
||||
Console.WriteLine($"I want you to think of a number between 0 and { this.MaximumNumber }. Ok?");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asks the user a series of questions to discover the number they are thinking of
|
||||
/// </summary>
|
||||
public void DiscoverNumber()
|
||||
{
|
||||
// Clear variables to their initial values before a discovery
|
||||
this.CurrentNumberOfGuesses = 0;
|
||||
this.CurrentGuessMinimum = 0;
|
||||
this.CurrentGuessMaximum = this.MaximumNumber / 2;
|
||||
|
||||
// While the guess isn't the same as the known maximum value
|
||||
while (this.CurrentGuessMinimum != this.CurrentGuessMaximum)
|
||||
{
|
||||
// Increase guess amount (by 1)
|
||||
this.CurrentNumberOfGuesses++;
|
||||
|
||||
// Ask the user if their number is between the guess range
|
||||
Console.WriteLine($"Is your number between { this.CurrentGuessMinimum } and { this.CurrentGuessMaximum}?");
|
||||
string response = Console.ReadLine();
|
||||
|
||||
// If the user confirmed their number is within the current range...
|
||||
if (response?.ToLower().FirstOrDefault() == 'y')
|
||||
{
|
||||
// We know the number is between guessFrom and guessTo
|
||||
// So set the new maximum number
|
||||
this.MaximumNumber = this.CurrentGuessMaximum;
|
||||
|
||||
// Change the next guess range to be half of the new maximum range
|
||||
this.CurrentGuessMaximum = this.CurrentGuessMaximum - (this.CurrentGuessMaximum - this.CurrentGuessMinimum) / 2;
|
||||
}
|
||||
// The number is greater than guessMax and less than or equal to max
|
||||
else
|
||||
{
|
||||
// The new minimum is one above the old maximum
|
||||
this.CurrentGuessMinimum = this.CurrentGuessMaximum + 1;
|
||||
|
||||
// Guess the bottom half of the new range
|
||||
int remainingDifference = this.MaximumNumber - this.CurrentGuessMaximum;
|
||||
|
||||
// Set the guess max to half way between the guessMin and max
|
||||
// NOTE: Math.Ceiling will round up the remaining difference to 2, if the difference is 3
|
||||
this.CurrentGuessMaximum += (int)Math.Ceiling(remainingDifference / 2f);
|
||||
}
|
||||
|
||||
// If we only have 2 numbers left, guess one of them
|
||||
if (this.CurrentGuessMinimum + 1 == this.MaximumNumber)
|
||||
{
|
||||
// Increase guess amount (by 1)
|
||||
this.CurrentNumberOfGuesses++;
|
||||
|
||||
// Ask the user if their number is the lower number
|
||||
Console.WriteLine($"Is your number { this.CurrentGuessMinimum }?");
|
||||
response = Console.ReadLine();
|
||||
|
||||
// If the user confirmed their number is the lower number...
|
||||
if (response?.ToLower().FirstOrDefault() == 'y')
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
// That means the number must be the higher of the two
|
||||
this.CurrentGuessMinimum = this.MaximumNumber;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Annouces the number the user was thinking of and the number of guesses it took
|
||||
/// </summary>
|
||||
public void AnnounceResults()
|
||||
{
|
||||
// Tell the user their number
|
||||
Console.WriteLine($"** Your number is { this.CurrentGuessMinimum } **");
|
||||
|
||||
// Let the user know how many guesses it took
|
||||
Console.WriteLine($"Guessed in { this.CurrentNumberOfGuesses } guesses");
|
||||
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
29
C# Beginners/ConsoleApplication1/Program.cs
Normal file
29
C# Beginners/ConsoleApplication1/Program.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ConsoleApplication1
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// Create a new instance of our number guesser class
|
||||
var numberGuesser = new NumberGuesser();
|
||||
|
||||
// Change the default maximum number to 200
|
||||
numberGuesser.MaximumNumber = 200;
|
||||
|
||||
// Ask the user to think of a number
|
||||
numberGuesser.InformUser();
|
||||
|
||||
// Discover the number the user is thinking of
|
||||
numberGuesser.DiscoverNumber();
|
||||
|
||||
// Announce the results
|
||||
numberGuesser.AnnounceResults();
|
||||
}
|
||||
}
|
||||
}
|
||||
36
C# Beginners/ConsoleApplication1/Properties/AssemblyInfo.cs
Normal file
36
C# Beginners/ConsoleApplication1/Properties/AssemblyInfo.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("ConsoleApplication1")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("ConsoleApplication1")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2016")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("e7e6e236-2fd2-4d03-aed0-b15841a080b5")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
Reference in New Issue
Block a user