Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_01
{
#region INCLUDE
public class Stack
{
public virtual object Pop()
{
// ...
#region EXCLUDE
return new object();
#endregion EXCLUDE
}

public virtual void Push(object obj)
{
// ...
}

// ...
}

#endregion INCLUDE
}
33 changes: 25 additions & 8 deletions src/Chapter12/Listing12.02.SupportingUndoInEtchASketchGame.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
// TODO: Update listing in Manuscript
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_02
{
using System;
#region INCLUDE
using System.Collections.Generic;

public class Program
{
#region EXCLUDE
public static void Main()
{
Sketch();
}
#endregion EXCLUDE

public static void Sketch()
{

Stack<Cell> path = new Stack<Cell>();
Cell currentPosition;
ConsoleKeyInfo key; // Added in C# 2.0

#region EXCLUDE
Console.WriteLine("Use arrow keys to draw. X to exit.");
for(int i = 2; i < Console.WindowHeight; i++)
{
Expand All @@ -27,6 +28,7 @@ public static void Sketch()
currentPosition = new Cell(Console.WindowWidth / 2, Console.WindowHeight / 2);
path.Push(currentPosition);
FillCell(currentPosition);
#endregion EXCLUDE

do
{
Expand All @@ -40,60 +42,73 @@ public static void Sketch()
// Undo the previous Move
if(path.Count >= 1)
{
#region HIGHLIGHT
currentPosition = (Cell)path.Pop();
#endregion HIGHLIGHT
Console.SetCursorPosition(
currentPosition.X, currentPosition.Y);
#region EXCLUDE
FillCell(currentPosition, ConsoleColor.Black);
#endregion EXCLUDE
Undo();
}
break;
case ConsoleKey.DownArrow:
if(Console.CursorTop < Console.WindowHeight - 2)
#region EXCLUDE
if (Console.CursorTop < Console.WindowHeight - 2)
{
currentPosition = new Cell(
Console.CursorLeft, Console.CursorTop + 1);
}
path.Push(currentPosition);
FillCell(currentPosition);
break;
#endregion EXCLUDE
case ConsoleKey.UpArrow:
if(Console.CursorTop > 1)
#region EXCLUDE
if (Console.CursorTop > 1)
{
currentPosition = new Cell(
Console.CursorLeft, Console.CursorTop - 1);
}
path.Push(currentPosition);
FillCell(currentPosition);
break;
#endregion EXCLUDE
case ConsoleKey.LeftArrow:
if(Console.CursorLeft > 1)
#region EXCLUDE
if (Console.CursorLeft > 1)
{
currentPosition = new Cell(
Console.CursorLeft - 1, Console.CursorTop);
}
path.Push(currentPosition);
FillCell(currentPosition);
break;
#endregion EXCLUDE
case ConsoleKey.RightArrow:
// SaveState()
if(Console.CursorLeft < Console.WindowWidth - 2)
{
currentPosition = new Cell(
Console.CursorLeft + 1, Console.CursorTop);
}
#region HIGHLIGHT
path.Push(currentPosition);
#endregion HIGHLIGHT
#region EXCLUDE
FillCell(currentPosition);
#endregion EXCLUDE
break;

default:
Console.Beep(); // Added in C# 2.0
break;
}

}
while(key.Key != ConsoleKey.X); // Use X to quit.
}

#region EXCLUDE
private static ConsoleKeyInfo Move()
{
return Console.ReadKey(true);
Expand All @@ -117,6 +132,7 @@ private static void FillCell(Cell cell, ConsoleColor color)
Console.SetCursorPosition(cell.X, cell.Y);
Console.BackgroundColor = ConsoleColor.Black;
}
#endregion EXCLUDE
}

public struct Cell
Expand All @@ -131,4 +147,5 @@ public Cell(int x, int y)
Y = y;
}
}
#endregion INCLUDE
}
8 changes: 5 additions & 3 deletions src/Chapter12/Listing12.03.DefiningASpecializedStackClass.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_03
{
using Listing12_02;

#region INCLUDE
public class CellStack
{
public virtual object Pop() { return new Cell(); } // would return that last cell added and remove it from the list
public virtual void Push(Cell obj) { }
public virtual Cell Pop() { return new Cell(); } // would return that last cell added and remove it from the list
public virtual void Push(Cell cell) { }
// ...
}
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_04
{
using System;

#region INCLUDE
struct NullableInt
{
/// <summary>
Expand Down Expand Up @@ -35,4 +36,5 @@ struct NullableGuid
}

// ...
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_05
{
#region INCLUDE
struct Nullable
{
/// <summary>
Expand All @@ -15,4 +16,5 @@ struct Nullable

// ...
}
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
// TODO: Update listing in Manuscript
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_06
{
#region INCLUDE
using System;
using System.Collections.Generic;

public class Program
{
#region EXCLUDE
public static void Main()
{
Sketch();
}
#endregion EXCLUDE

public static void Sketch()
{
#region HIGHLIGHT
Stack<Cell> path = new Stack<Cell>();
#endregion HIGHLIGHT
Cell currentPosition;
ConsoleKeyInfo key; // Added in C# 2.0

#region EXCLUDE
Console.WriteLine("Use arrow keys to draw. X to exit.");
for(int i = 2; i < Console.WindowHeight; i++)
{
Expand All @@ -26,6 +30,7 @@ public static void Sketch()
currentPosition = new Cell(Console.WindowWidth / 2, Console.WindowHeight / 2);
path.Push(currentPosition);
FillCell(currentPosition);
#endregion EXCLUDE

do
{
Expand All @@ -39,16 +44,19 @@ public static void Sketch()
// Undo the previous Move
if(path.Count >= 1)
{
#region HIGHLIGHT
// No cast required
currentPosition = path.Pop();
#endregion HIGHLIGHT
Console.SetCursorPosition(
currentPosition.X, currentPosition.Y);
FillCell(currentPosition, ConsoleColor.Black);
Undo();
}
break;
case ConsoleKey.DownArrow:
if(Console.CursorTop < Console.WindowHeight - 2)
#region EXCLUDE
if (Console.CursorTop < Console.WindowHeight - 2)
{
currentPosition = new Cell(
Console.CursorLeft, Console.CursorTop + 1);
Expand All @@ -57,8 +65,10 @@ public static void Sketch()
path.Push(currentPosition);
FillCell(currentPosition);
break;
#endregion EXCLUDE
case ConsoleKey.UpArrow:
if(Console.CursorTop > 1)
#region EXCLUDE
if (Console.CursorTop > 1)
{
currentPosition = new Cell(
Console.CursorLeft, Console.CursorTop - 1);
Expand All @@ -67,8 +77,10 @@ public static void Sketch()
path.Push(currentPosition);
FillCell(currentPosition);
break;
#endregion EXCLUDE
case ConsoleKey.LeftArrow:
if(Console.CursorLeft > 1)
#region EXCLUDE
if (Console.CursorLeft > 1)
{
currentPosition = new Cell(
Console.CursorLeft - 1, Console.CursorTop);
Expand All @@ -77,15 +89,18 @@ public static void Sketch()
path.Push(currentPosition);
FillCell(currentPosition);
break;
#endregion EXCLUDE
case ConsoleKey.RightArrow:
// SaveState()
if(Console.CursorLeft < Console.WindowWidth - 2)
{
currentPosition = new Cell(
Console.CursorLeft + 1, Console.CursorTop);
}
#region HIGHLIGHT
// Only type Cell allowed in call to Push()
path.Push(currentPosition);
#endregion HIGHLIGHT
FillCell(currentPosition);
break;

Expand All @@ -97,7 +112,7 @@ public static void Sketch()
}
while(key.Key != ConsoleKey.X); // Use X to quit
}

#region EXCLUDE
private static ConsoleKeyInfo Move()
{
return Console.ReadKey(true);
Expand All @@ -121,8 +136,9 @@ private static void FillCell(Cell cell, ConsoleColor color)
Console.SetCursorPosition(cell.X, cell.Y);
Console.BackgroundColor = ConsoleColor.Black;
}
#endregion EXCLUDE
}

#endregion INCLUDE
public struct Cell
{
readonly public int X;
Expand Down
5 changes: 4 additions & 1 deletion src/Chapter12/Listing12.07.DeclaringAGenericClassStack.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_07
{
#region INCLUDE
public class Stack<T>
{
public Stack(int maxSize)
Expand All @@ -17,8 +18,10 @@ public void Push(T data)

public T Pop()
{
//...
#region EXCLUDE
return InternalItems[0]; //just for the example
#endregion EXCLUDE
}
}
#endregion INCLUDE
}
2 changes: 2 additions & 0 deletions src/Chapter12/Listing12.08.DeclaringAGenericInterface.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_08
{
#region INCLUDE
interface IPair<T>
{
T First { get; set; }
T Second { get; set; }
}
#endregion INCLUDE
}
4 changes: 2 additions & 2 deletions src/Chapter12/Listing12.09.ImplementingAGenericInterface.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_09
{
using Listing12_08;

#region INCLUDE
public struct Pair<T> : IPair<T>
{
public T First { get; set; }
public T Second { get; set; }
}

#endregion INCLUDE
}
Loading