From 6e0c4b85c9b955baa92cc6f464224dbec809bfa4 Mon Sep 17 00:00:00 2001 From: "James.Woodard" Date: Mon, 13 Aug 2018 20:24:47 -0500 Subject: [PATCH 1/5] started the animal classification project --- .../AnimalClassification.csproj | 8 + AnimalClassification/Program.cs | 56 +++++++ Mastermind/Mastermind.cs | 151 ++++++++++-------- 3 files changed, 144 insertions(+), 71 deletions(-) create mode 100644 AnimalClassification/AnimalClassification.csproj create mode 100644 AnimalClassification/Program.cs diff --git a/AnimalClassification/AnimalClassification.csproj b/AnimalClassification/AnimalClassification.csproj new file mode 100644 index 00000000..23df6047 --- /dev/null +++ b/AnimalClassification/AnimalClassification.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.1 + + + diff --git a/AnimalClassification/Program.cs b/AnimalClassification/Program.cs new file mode 100644 index 00000000..1234df8f --- /dev/null +++ b/AnimalClassification/Program.cs @@ -0,0 +1,56 @@ +using System; + +namespace AnimalClassification +{ + + public class Animal { + + } + + class Vertebrate : Animal { + + } + + class Invertebrate : Animal { + + } + + class WarmBlooded : Vertebrate { + + } + + class ColdBlooded : Vertebrate { + + } + + class WithLegs : Invertebrate { + + } + + class WithoutLegs : Invertebrate { + + } + + class Mammals : WarmBlooded { + bool livesOnLand; + + } + class Birds : WarmBlooded {} + class Fish : ColdBlooded {} + class Reptiles : ColdBlooded {} + class Amphibians : ColdBlooded {} + class ThreePairsOfLegs : WithLegs {} + class MoreThanThreePairsOfLegs : WithLegs {} + class WormLike : WithoutLegs {} + class NotWormLike : WithoutLegs {} + + + + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/Mastermind/Mastermind.cs b/Mastermind/Mastermind.cs index 365fb84c..9d0ac1cb 100644 --- a/Mastermind/Mastermind.cs +++ b/Mastermind/Mastermind.cs @@ -1,86 +1,95 @@ using System; +using System.Collections.Generic; -namespace Mastermind -{ - class Program - { - // possible letters in code - public static char[] letters = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }; - - // size of code - public static int codeSize = 4; - - // number of allowed attempts to crack the code - public static int allowedAttempts = 10; - - // number of tried guesses - public static int numTry = 0; - - // test solution - public static char[] solution = new char[] {'a', 'b', 'c', 'd'}; - - // game board - public static string[][] board = new string[allowedAttempts][]; - - - public static void Main() - { - char[] guess = new char[4]; +namespace Mastermind { + class Program { + static void Main (string[] args) { + Game game = new Game (new string[] { "a", "b", "c", "d" }); + for (int turns = 10; turns > 0; turns--) { + Console.WriteLine($"You have {turns} tries left"); + Console.WriteLine ("Choose four letters: "); + string letters = Console.ReadLine (); + Ball[] balls = new Ball[4]; + for (int i = 0; i < 4; i++) { + balls[i] = new Ball (letters[i].ToString()); + } + Row row = new Row (balls); + game.AddRow (row); + Console.WriteLine (game.Rows); + } + Console.WriteLine ("Out Of Turns"); + } + } - CreateBoard(); - DrawBoard(); - Console.WriteLine("Enter Guess:"); - guess = Console.ReadLine().ToCharArray(); + class Game { + private List rows = new List (); + private string[] answer = new string[4]; - // leave this command at the end so your program does not close automatically - Console.ReadLine(); + public Game (string[] answer) { + this.answer = answer; } - - public static bool CheckSolution(char[] guess) - { - // Your code here - return false; - } - - public static string GenerateHint(char[] guess) - { - // Your code here - return " "; + private string Score (Row row) { + string[] answerClone = (string[]) this.answer.Clone (); + // red is correct letter and correct position + // white is correct letters minus red + // this.answer => ["a", "b", "c", "d"] + // row.balls => [{ Letter: "c" }, { Letter: "b" }, { Letter: "d" }, { Letter: "a" }] + int red = 0; + for (int i = 0; i < 4; i++) { + if (answerClone[i] == row.balls[i].Letter) { + red++; + } + } + + int white = 0; + for (int i = 0; i < 4; i++) { + int foundIndex = Array.IndexOf (answerClone, row.balls[i].Letter); + if (foundIndex > -1) { + white++; + answerClone[foundIndex] = null; + } + } + return $" {red} - {white - red}"; } - - public static void InsertCode(char[] guess) - { - // Your code here + + public void AddRow (Row row) { + this.rows.Add (row); } - - public static void CreateBoard() - { - for (var i = 0; i < allowedAttempts; i++) - { - board[i] = new string[codeSize + 1]; - for (var j = 0; j < codeSize + 1; j++) - { - board[i][j] = " "; + + public string Rows { + get { + foreach (var row in this.rows) { + Console.Write (row.Balls); + Console.WriteLine (Score (row)); } + } } - - public static void DrawBoard() - { - for (var i = 0; i < board.Length; i++) - { - Console.WriteLine("|" + String.Join("|", board[i])); - } - + } + + class Ball { + public string Letter { get; private set; } + + public Ball (string letter) { + this.Letter = letter; } - - public static void GenerateRandomCode() { - Random rnd = new Random(); - for(var i = 0; i < codeSize; i++) - { - solution[i] = letters[rnd.Next(0, letters.Length)]; + } + + class Row { + public Ball[] balls = new Ball[4]; + + public Row (Ball[] balls) { + this.balls = balls; + } + + public string Balls { + get { + foreach (var ball in this.balls) { + Console.Write (ball.Letter); + } + return ""; } } } -} +} \ No newline at end of file From 2dfcd11f88f8c49d22e66385306b5b0f7b9a78ab Mon Sep 17 00:00:00 2001 From: "James.Woodard" Date: Wed, 15 Aug 2018 17:47:16 -0500 Subject: [PATCH 2/5] continuing work on projects --- AnimalClassification/.vscode/launch.json | 28 ++++ AnimalClassification/.vscode/tasks.json | 15 +++ CarInheritance/.vscode/launch.json | 28 ++++ CarInheritance/.vscode/tasks.json | 15 +++ CarInheritance/CarInheritance.csproj | 8 ++ CarInheritance/Program.cs | 76 +++++++++++ Mastermind/.vscode/launch.json | 28 ++++ Mastermind/.vscode/tasks.json | 15 +++ Mastermind/Mastermind.cs | 24 +++- TowersOfHanoi/.vscode/launch.json | 28 ++++ TowersOfHanoi/.vscode/tasks.json | 15 +++ TowersOfHanoi/TowersOfHanoi.cs | 159 ++++++++++++++++++++++- 12 files changed, 434 insertions(+), 5 deletions(-) create mode 100644 AnimalClassification/.vscode/launch.json create mode 100644 AnimalClassification/.vscode/tasks.json create mode 100644 CarInheritance/.vscode/launch.json create mode 100644 CarInheritance/.vscode/tasks.json create mode 100644 CarInheritance/CarInheritance.csproj create mode 100644 CarInheritance/Program.cs create mode 100644 Mastermind/.vscode/launch.json create mode 100644 Mastermind/.vscode/tasks.json create mode 100644 TowersOfHanoi/.vscode/launch.json create mode 100644 TowersOfHanoi/.vscode/tasks.json diff --git a/AnimalClassification/.vscode/launch.json b/AnimalClassification/.vscode/launch.json new file mode 100644 index 00000000..7bc7220c --- /dev/null +++ b/AnimalClassification/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/AnimalClassification.dll", + "args": [], + "cwd": "${workspaceFolder}", + // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window + "console": "internalConsole", + "stopAtEntry": false, + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ,] +} \ No newline at end of file diff --git a/AnimalClassification/.vscode/tasks.json b/AnimalClassification/.vscode/tasks.json new file mode 100644 index 00000000..de0b881e --- /dev/null +++ b/AnimalClassification/.vscode/tasks.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/AnimalClassification.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/CarInheritance/.vscode/launch.json b/CarInheritance/.vscode/launch.json new file mode 100644 index 00000000..8f5202d3 --- /dev/null +++ b/CarInheritance/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/CarInheritance.dll", + "args": [], + "cwd": "${workspaceFolder}", + // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window + "console": "internalConsole", + "stopAtEntry": false, + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ,] +} \ No newline at end of file diff --git a/CarInheritance/.vscode/tasks.json b/CarInheritance/.vscode/tasks.json new file mode 100644 index 00000000..ae52861d --- /dev/null +++ b/CarInheritance/.vscode/tasks.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/CarInheritance.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/CarInheritance/CarInheritance.csproj b/CarInheritance/CarInheritance.csproj new file mode 100644 index 00000000..23df6047 --- /dev/null +++ b/CarInheritance/CarInheritance.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.1 + + + diff --git a/CarInheritance/Program.cs b/CarInheritance/Program.cs new file mode 100644 index 00000000..0e33f846 --- /dev/null +++ b/CarInheritance/Program.cs @@ -0,0 +1,76 @@ +using System; + +namespace CarInheritance +{ + class Program + { + + + + + + + static void Main(string[] args) + { + Truck aTruck = new Truck(1,4,"white",4,4,true,6); + Truck gravedigga = new Truck(1,4,"green and black like a can of Monster",4,4,true,6); + Motorcycle aMotorcycle = new Motorcycle(true, true, "red", 3, 2, true, 4); + + + Console.WriteLine("Inheritance practice!"); + + Console.WriteLine("aTruck's color is " + aTruck.color); + Console.WriteLine("aMotorcycle's color is " + aMotorcycle.color); + Console.WriteLine("Gravedigga's color is " + gravedigga.color); + } + } + + public class Vehicle{ + public string color {get;set;} + public int numWheels {get;set;} + public int numPassengers {get;set;} + public bool gasPowered {get;set;} + public int engineSize {get;set;} + + + public Vehicle(string color, int numWheels, int numPassengers, bool gasPowered, int engineSize){ + this.color = color; + this.numWheels = numWheels; + this.numPassengers = numPassengers; + this.gasPowered = gasPowered; + this.engineSize = engineSize; + } + } + + public class Truck : Vehicle { + public int cabSize {get;set;} + public int towingCapacity {get;set;} + + public Truck(int cabSize, int towingCapacity, string color, int numWheels, int numPassengers, bool gasPowered, int engineSize) : base(color, numWheels, numPassengers, gasPowered, engineSize){ + this.numWheels = numWheels; + this.cabSize = cabSize; + this.towingCapacity = towingCapacity; + } + } + + public class Motorcycle : Vehicle { + public bool isCruiser {get;set;} + public bool hasSidecar {get;set;} + public Motorcycle(bool isCruiser, bool hasSidecar, string color, int numWheels, int numPassengers, bool gasPowered, int engineSize) : base(color, numWheels, numPassengers, gasPowered, engineSize){ + this.isCruiser = isCruiser; + this.hasSidecar = hasSidecar; + } + } + + public class Van : Vehicle { + public bool hasSlidingDoor {get;set;} + public bool isCargoVan {get;set;} + + public Van(bool hasSlidingDoor, bool isCargoVan, string color, int numWheels, int numPassengers, bool gasPowered, int engineSize) : base(color, numWheels, numPassengers, gasPowered, engineSize){ + this.hasSlidingDoor = hasSlidingDoor; + this.isCargoVan = isCargoVan; + } + } + + +} diff --git a/Mastermind/.vscode/launch.json b/Mastermind/.vscode/launch.json new file mode 100644 index 00000000..60c9449e --- /dev/null +++ b/Mastermind/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/Mastermind.dll", + "args": [], + "cwd": "${workspaceFolder}", + // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window + "console": "internalConsole", + "stopAtEntry": false, + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ,] +} \ No newline at end of file diff --git a/Mastermind/.vscode/tasks.json b/Mastermind/.vscode/tasks.json new file mode 100644 index 00000000..ccfc68f7 --- /dev/null +++ b/Mastermind/.vscode/tasks.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/Mastermind.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Mastermind/Mastermind.cs b/Mastermind/Mastermind.cs index 9d0ac1cb..ea91d3b5 100644 --- a/Mastermind/Mastermind.cs +++ b/Mastermind/Mastermind.cs @@ -1,14 +1,20 @@ using System; using System.Collections.Generic; +using System.Threading; + namespace Mastermind { class Program { static void Main (string[] args) { - Game game = new Game (new string[] { "a", "b", "c", "d" }); + + string[] randomLetters = new string[] {"a", "b", "c", "d", "e", "f"}; + Random rando = new Random(); + Game game = new Game (new string[] { randomLetters[rando.Next(0,6)], randomLetters[rando.Next(0,6)], randomLetters[rando.Next(0,6)], randomLetters[rando.Next(0,6)] }); for (int turns = 10; turns > 0; turns--) { Console.WriteLine($"You have {turns} tries left"); Console.WriteLine ("Choose four letters: "); string letters = Console.ReadLine (); + string[] lettersArray = letters.Split(""); Ball[] balls = new Ball[4]; for (int i = 0; i < 4; i++) { balls[i] = new Ball (letters[i].ToString()); @@ -16,6 +22,8 @@ static void Main (string[] args) { Row row = new Row (balls); game.AddRow (row); Console.WriteLine (game.Rows); + + } Console.WriteLine ("Out Of Turns"); } @@ -29,7 +37,7 @@ public Game (string[] answer) { this.answer = answer; } - private string Score (Row row) { + public string Score (Row row) { string[] answerClone = (string[]) this.answer.Clone (); // red is correct letter and correct position // white is correct letters minus red @@ -50,7 +58,15 @@ private string Score (Row row) { answerClone[foundIndex] = null; } } + if (white - red == 0) + { + Console.WriteLine("You've won!"); + // System.Environment.Exit(1); + } + + return $" {red} - {white - red}"; + } public void AddRow (Row row) { @@ -60,10 +76,10 @@ public void AddRow (Row row) { public string Rows { get { foreach (var row in this.rows) { - Console.Write (row.Balls); + Console.WriteLine (row.Balls); Console.WriteLine (Score (row)); } - + return null; } } } diff --git a/TowersOfHanoi/.vscode/launch.json b/TowersOfHanoi/.vscode/launch.json new file mode 100644 index 00000000..4ceac24d --- /dev/null +++ b/TowersOfHanoi/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/TowersOfHanoi.dll", + "args": [], + "cwd": "${workspaceFolder}", + // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window + "console": "internalConsole", + "stopAtEntry": false, + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ,] +} \ No newline at end of file diff --git a/TowersOfHanoi/.vscode/tasks.json b/TowersOfHanoi/.vscode/tasks.json new file mode 100644 index 00000000..bca810e7 --- /dev/null +++ b/TowersOfHanoi/.vscode/tasks.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/TowersOfHanoi.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/TowersOfHanoi/TowersOfHanoi.cs b/TowersOfHanoi/TowersOfHanoi.cs index 10475b4a..27fcb02f 100644 --- a/TowersOfHanoi/TowersOfHanoi.cs +++ b/TowersOfHanoi/TowersOfHanoi.cs @@ -1,12 +1,169 @@ using System; +using System.Collections; +using System.Collections.Generic; namespace TowersOfHanoi { class Program { + static void Main(string[] args) { - Console.WriteLine("Hello World!"); + Console.WriteLine("Towers of Hanoi"); + Game myGame = new Game(); + + + } + + } + + + public class Game{ + Dictionary towers = new Dictionary(); + + public Game(){ + + towers["A"] = new Tower(); + towers["B"] = new Tower(); + towers["C"] = new Tower(); + + Block block1 = new Block(1); + Block block2 = new Block(2); + Block block3 = new Block(3); + Block block4 = new Block(4); + + towers["A"].blockStack.Push(block4); + towers["A"].blockStack.Push(block3); + towers["A"].blockStack.Push(block2); + towers["A"].blockStack.Push(block1); + + bool hasWon = false; + string location = ""; + string destination = ""; + + while (!hasWon) + { + PrintBoard(); + PlayerMove(); + CheckForWin(); + + } + + void PrintBoard(){ + // foreach (var tower in towers) + // { + // Console.Write(tower.Key + ": "); + + // foreach (Block block in towers["A"].blockStack) + // { + // Console.Write(block.weight); + // } + // Console.WriteLine(); + // } + Console.Write("A: "); + foreach (Block block in towers["A"].blockStack) + { + Console.Write(block.weight); + } + Console.WriteLine(); + Console.Write("B: "); + foreach (Block block in towers["B"].blockStack) + { + Console.Write(block.weight); + } + Console.WriteLine(); + Console.Write("C: "); + foreach (Block block in towers["C"].blockStack) + { + Console.Write(block.weight); + } + Console.WriteLine(); + } + + void CheckForWin(){ + if (towers["C"].blockStack.Count >= 4){ + Console.WriteLine("You Win!"); + hasWon = true; + } + } + + void PlayerMove(){ + // string[] acceptableLetters = new string[] {"a", "A", "b", "B", "c", "C"}; + Console.WriteLine("What stack would you like to move the block from?"); + string userInput = Console.ReadLine(); + + if ((userInput == "a") ||(userInput == "A") || (userInput == "b") ||(userInput == "B") || (userInput == "c") ||(userInput == "C")) + { + location = userInput.ToUpper(); + } + else + { + Console.WriteLine("Sorry, your input is invalid."); + userInput = ""; + location = ""; + destination = ""; + PlayerMove(); + } + + Console.WriteLine("What stack would you like to move the block to?"); + userInput = Console.ReadLine(); + + + if ((userInput == "a") ||(userInput == "A") || (userInput == "b") ||(userInput == "B") || (userInput == "c") ||(userInput == "C")) + { + destination = userInput.ToUpper(); + } + else + { + Console.WriteLine("Sorry, your input is invalid."); + userInput = ""; + location = ""; + destination = ""; + PlayerMove(); + } + + + + + var pieceToMove = towers[location].blockStack.Peek(); + if (towers[destination].blockStack.Count == 0) + { + towers[location].blockStack.Pop(); + towers[destination].blockStack.Push(pieceToMove); + } + else if (towers[destination].blockStack.Peek().weight > pieceToMove.weight) + { + towers[location].blockStack.Pop(); + towers[destination].blockStack.Push(pieceToMove); + } + else + { + Console.WriteLine("Sorry, that move is invalid"); + } + } + + // void MovePiece(){ + // var pieceToMove = towers[location.ToUpper()].blockStack.Pop(); + // towers[destination.ToUpper()].blockStack.Push(pieceToMove); + // } + + } } + public class Tower{ + public Stack blockStack {get;set;} + + public Tower(){ + + this.blockStack = new Stack(); + } + + } + public class Block{ + public int weight {get; set;} + public Block(int weight) { + this.weight = weight; + } + } + } From d774092d7c371f97e83c1266dda62c8c4742f22c Mon Sep 17 00:00:00 2001 From: "James.Woodard" Date: Wed, 15 Aug 2018 18:42:04 -0500 Subject: [PATCH 3/5] cleaning up code --- TowersOfHanoi/TowersOfHanoi.cs | 75 +++++++++++++++++----------------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/TowersOfHanoi/TowersOfHanoi.cs b/TowersOfHanoi/TowersOfHanoi.cs index 27fcb02f..6305b7ec 100644 --- a/TowersOfHanoi/TowersOfHanoi.cs +++ b/TowersOfHanoi/TowersOfHanoi.cs @@ -11,7 +11,8 @@ static void Main(string[] args) { Console.WriteLine("Towers of Hanoi"); Game myGame = new Game(); - + //add myGame.run() + //playagain } @@ -19,7 +20,7 @@ static void Main(string[] args) public class Game{ - Dictionary towers = new Dictionary(); + Dictionary towers = new Dictionary(); public Game(){ @@ -36,7 +37,7 @@ public Game(){ towers["A"].blockStack.Push(block3); towers["A"].blockStack.Push(block2); towers["A"].blockStack.Push(block1); - + //this is where constructor should end bool hasWon = false; string location = ""; string destination = ""; @@ -50,34 +51,32 @@ public Game(){ } void PrintBoard(){ - // foreach (var tower in towers) - // { - // Console.Write(tower.Key + ": "); + foreach (string name in towers.Keys) + { + Console.Write(name + ": "); + Tower tower = towers[name]; + Stack stackOfBlocks = tower.blockStack; - // foreach (Block block in towers["A"].blockStack) + //FINISH THIS + } + // Console.Write("A: "); + // foreach (Block block in towers["A"].blockStack) + // { + // Console.Write(block.weight); + // } + // Console.WriteLine(); + // Console.Write("B: "); + // foreach (Block block in towers["B"].blockStack) + // { + // Console.Write(block.weight); + // } + // Console.WriteLine(); + // Console.Write("C: "); + // foreach (Block block in towers["C"].blockStack) // { // Console.Write(block.weight); // } // Console.WriteLine(); - // } - Console.Write("A: "); - foreach (Block block in towers["A"].blockStack) - { - Console.Write(block.weight); - } - Console.WriteLine(); - Console.Write("B: "); - foreach (Block block in towers["B"].blockStack) - { - Console.Write(block.weight); - } - Console.WriteLine(); - Console.Write("C: "); - foreach (Block block in towers["C"].blockStack) - { - Console.Write(block.weight); - } - Console.WriteLine(); } void CheckForWin(){ @@ -88,6 +87,8 @@ void CheckForWin(){ } void PlayerMove(){ + + // this gets player input // string[] acceptableLetters = new string[] {"a", "A", "b", "B", "c", "C"}; Console.WriteLine("What stack would you like to move the block from?"); string userInput = Console.ReadLine(); @@ -99,10 +100,10 @@ void PlayerMove(){ else { Console.WriteLine("Sorry, your input is invalid."); - userInput = ""; - location = ""; - destination = ""; - PlayerMove(); + userInput = null; + location = null; + destination = null; + return; } Console.WriteLine("What stack would you like to move the block to?"); @@ -116,15 +117,15 @@ void PlayerMove(){ else { Console.WriteLine("Sorry, your input is invalid."); - userInput = ""; - location = ""; - destination = ""; - PlayerMove(); + userInput = null; + location = null; + destination = null; + return; } - + //this moves the pieces var pieceToMove = towers[location].blockStack.Peek(); if (towers[destination].blockStack.Count == 0) { @@ -151,11 +152,11 @@ void PlayerMove(){ } } public class Tower{ - public Stack blockStack {get;set;} + public Stack blockStack {get;set;} public Tower(){ - this.blockStack = new Stack(); + this.blockStack = new Stack(); } } From 2c4731db5421c2d2878af0a2d36ffa458b864b54 Mon Sep 17 00:00:00 2001 From: "James.Woodard" Date: Sun, 19 Aug 2018 11:24:15 -0500 Subject: [PATCH 4/5] added comments and finished hanoi --- Mastermind/Mastermind.cs | 63 ++++++---- TowersOfHanoi/TowersOfHanoi.cs | 205 +++++++++++++++------------------ 2 files changed, 134 insertions(+), 134 deletions(-) diff --git a/Mastermind/Mastermind.cs b/Mastermind/Mastermind.cs index ea91d3b5..2654112b 100644 --- a/Mastermind/Mastermind.cs +++ b/Mastermind/Mastermind.cs @@ -3,14 +3,17 @@ using System.Threading; -namespace Mastermind { - class Program { +namespace Mastermind +{ + class Program + { static void Main (string[] args) { string[] randomLetters = new string[] {"a", "b", "c", "d", "e", "f"}; Random rando = new Random(); - Game game = new Game (new string[] { randomLetters[rando.Next(0,6)], randomLetters[rando.Next(0,6)], randomLetters[rando.Next(0,6)], randomLetters[rando.Next(0,6)] }); - for (int turns = 10; turns > 0; turns--) { + Game game = new Game (new string[] { randomLetters[rando.Next(0,6)], randomLetters[rando.Next(0,6)], randomLetters[rando.Next(0,6)], randomLetters[rando.Next(0,6)] }, 10); + // Game game = new Game (new string[] { "a", "a", "a", "a" }); + for (int turns = game.numTries; turns > 0; turns--) { Console.WriteLine($"You have {turns} tries left"); Console.WriteLine ("Choose four letters: "); string letters = Console.ReadLine (); @@ -33,7 +36,10 @@ class Game { private List rows = new List (); private string[] answer = new string[4]; - public Game (string[] answer) { + public int numTries {get;set;} + + public Game (string[] answer, int numTries) + { this.answer = answer; } @@ -44,24 +50,28 @@ public string Score (Row row) { // this.answer => ["a", "b", "c", "d"] // row.balls => [{ Letter: "c" }, { Letter: "b" }, { Letter: "d" }, { Letter: "a" }] int red = 0; - for (int i = 0; i < 4; i++) { - if (answerClone[i] == row.balls[i].Letter) { + for (int i = 0; i < 4; i++) + { + if (answerClone[i] == row.balls[i].Letter) + { red++; } } int white = 0; - for (int i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) + { int foundIndex = Array.IndexOf (answerClone, row.balls[i].Letter); if (foundIndex > -1) { white++; answerClone[foundIndex] = null; } } - if (white - red == 0) + //win conditions + if ((white - red == 0) && (red == 4)) { Console.WriteLine("You've won!"); - // System.Environment.Exit(1); + System.Environment.Exit(1); } @@ -69,13 +79,17 @@ public string Score (Row row) { } - public void AddRow (Row row) { + public void AddRow (Row row) + { this.rows.Add (row); } - public string Rows { - get { - foreach (var row in this.rows) { + public string Rows + { + get + { + foreach (var row in this.rows) + { Console.WriteLine (row.Balls); Console.WriteLine (Score (row)); } @@ -84,24 +98,31 @@ public string Rows { } } - class Ball { + class Ball + { public string Letter { get; private set; } - public Ball (string letter) { + public Ball (string letter) + { this.Letter = letter; } } - class Row { + class Row + { public Ball[] balls = new Ball[4]; - public Row (Ball[] balls) { + public Row (Ball[] balls) + { this.balls = balls; } - public string Balls { - get { - foreach (var ball in this.balls) { + public string Balls + { + get + { + foreach (var ball in this.balls) + { Console.Write (ball.Letter); } return ""; diff --git a/TowersOfHanoi/TowersOfHanoi.cs b/TowersOfHanoi/TowersOfHanoi.cs index 6305b7ec..5e6a60fd 100644 --- a/TowersOfHanoi/TowersOfHanoi.cs +++ b/TowersOfHanoi/TowersOfHanoi.cs @@ -11,154 +11,133 @@ static void Main(string[] args) { Console.WriteLine("Towers of Hanoi"); Game myGame = new Game(); - //add myGame.run() - //playagain - + myGame.Run(); } - } public class Game{ Dictionary towers = new Dictionary(); + bool hasWon = false; + string location = ""; + string destination = ""; - public Game(){ - - towers["A"] = new Tower(); - towers["B"] = new Tower(); - towers["C"] = new Tower(); - - Block block1 = new Block(1); - Block block2 = new Block(2); - Block block3 = new Block(3); - Block block4 = new Block(4); - - towers["A"].blockStack.Push(block4); - towers["A"].blockStack.Push(block3); - towers["A"].blockStack.Push(block2); - towers["A"].blockStack.Push(block1); - //this is where constructor should end - bool hasWon = false; - string location = ""; - string destination = ""; - - while (!hasWon) + void GetPlayerInput() { - PrintBoard(); - PlayerMove(); - CheckForWin(); - - } - - void PrintBoard(){ - foreach (string name in towers.Keys) - { - Console.Write(name + ": "); - Tower tower = towers[name]; - Stack stackOfBlocks = tower.blockStack; - - //FINISH THIS - } - // Console.Write("A: "); - // foreach (Block block in towers["A"].blockStack) - // { - // Console.Write(block.weight); - // } - // Console.WriteLine(); - // Console.Write("B: "); - // foreach (Block block in towers["B"].blockStack) - // { - // Console.Write(block.weight); - // } - // Console.WriteLine(); - // Console.Write("C: "); - // foreach (Block block in towers["C"].blockStack) - // { - // Console.Write(block.weight); - // } - // Console.WriteLine(); - } - - void CheckForWin(){ - if (towers["C"].blockStack.Count >= 4){ - Console.WriteLine("You Win!"); - hasWon = true; - } - } - - void PlayerMove(){ - - // this gets player input - // string[] acceptableLetters = new string[] {"a", "A", "b", "B", "c", "C"}; Console.WriteLine("What stack would you like to move the block from?"); string userInput = Console.ReadLine(); - if ((userInput == "a") ||(userInput == "A") || (userInput == "b") ||(userInput == "B") || (userInput == "c") ||(userInput == "C")) - { - location = userInput.ToUpper(); - } - else - { - Console.WriteLine("Sorry, your input is invalid."); - userInput = null; - location = null; - destination = null; - return; - } - + if ((userInput == "a") ||(userInput == "A") || (userInput == "b") ||(userInput == "B") || (userInput == "c") ||(userInput == "C")) + { + location = userInput.ToUpper(); + } + else + { + Console.WriteLine("Sorry, your input is invalid."); + userInput = null; + location = null; + destination = null; + GetPlayerInput(); + } Console.WriteLine("What stack would you like to move the block to?"); userInput = Console.ReadLine(); - - - if ((userInput == "a") ||(userInput == "A") || (userInput == "b") ||(userInput == "B") || (userInput == "c") ||(userInput == "C")) - { - destination = userInput.ToUpper(); - } - else - { - Console.WriteLine("Sorry, your input is invalid."); - userInput = null; - location = null; - destination = null; - return; - } - - - - //this moves the pieces + if ((userInput == "a") ||(userInput == "A") || (userInput == "b") ||(userInput == "B") || (userInput == "c") ||(userInput == "C")) + { + destination = userInput.ToUpper(); + } + else + { + Console.WriteLine("Sorry, your input is invalid."); + userInput = null; + location = null; + destination = null; + GetPlayerInput(); + } + } + void MovePiece(){ + + if (towers[location].blockStack.Count == 0) //checks if "from" input is empty + { + Console.WriteLine("Sorry, that move is invalid"); + return; + } var pieceToMove = towers[location].blockStack.Peek(); - if (towers[destination].blockStack.Count == 0) + if (towers[destination].blockStack.Count == 0) //checks if destination is empty { towers[location].blockStack.Pop(); towers[destination].blockStack.Push(pieceToMove); } - else if (towers[destination].blockStack.Peek().weight > pieceToMove.weight) + else if (towers[destination].blockStack.Peek().weight > pieceToMove.weight) //checks to see if block in destination is bigger { towers[location].blockStack.Pop(); towers[destination].blockStack.Push(pieceToMove); } else { - Console.WriteLine("Sorry, that move is invalid"); + Console.WriteLine("Sorry, that move is invalid"); //if user tries to put a bigger block on a smaller one } } + + void PrintBoard(){ + foreach (string name in towers.Keys) + { + Console.Write(name + ": "); + Tower tower = towers[name]; + Stack stackOfBlocks = tower.blockStack; + Stack stackToPrint = new Stack(); - // void MovePiece(){ - // var pieceToMove = towers[location.ToUpper()].blockStack.Pop(); - // towers[destination.ToUpper()].blockStack.Push(pieceToMove); - // } + foreach(Block b in stackOfBlocks) // this loop dumps the stack into a new stack, so it's printed out the proper way + { + stackToPrint.Push(b); + } + foreach (Block z in stackToPrint) // this loop prints out the stack in the proper order + { + Console.Write(z.weight); + } + Console.WriteLine(); + } + } + void CheckForWin() + { + if (towers["C"].blockStack.Count >= 4){ + Console.WriteLine("You Win!"); + hasWon = true; + } + } + public void Run() // this is the game logic + { + while (!hasWon) + { + PrintBoard(); + GetPlayerInput(); + MovePiece(); + CheckForWin(); + } + } + + public Game(){ //constructor for Game + towers["A"] = new Tower(); + towers["B"] = new Tower(); + towers["C"] = new Tower(); + + Block block1 = new Block(1); + Block block2 = new Block(2); + Block block3 = new Block(3); + Block block4 = new Block(4); + towers["A"].blockStack.Push(block4); + towers["A"].blockStack.Push(block3); + towers["A"].blockStack.Push(block2); + towers["A"].blockStack.Push(block1); } } public class Tower{ public Stack blockStack {get;set;} - - public Tower(){ - + public Tower() + { this.blockStack = new Stack(); } - } public class Block{ public int weight {get; set;} From b10d69c74e98f10c264d476a539680fc15487fd2 Mon Sep 17 00:00:00 2001 From: "James.Woodard" Date: Sun, 19 Aug 2018 11:31:20 -0500 Subject: [PATCH 5/5] finished inheritance / poly proj --- CarInheritance/Program.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/CarInheritance/Program.cs b/CarInheritance/Program.cs index 0e33f846..a7f313d0 100644 --- a/CarInheritance/Program.cs +++ b/CarInheritance/Program.cs @@ -12,16 +12,20 @@ class Program static void Main(string[] args) { - Truck aTruck = new Truck(1,4,"white",4,4,true,6); + Van aVan = new Van(true,true,"white",4,4,true,6); Truck gravedigga = new Truck(1,4,"green and black like a can of Monster",4,4,true,6); Motorcycle aMotorcycle = new Motorcycle(true, true, "red", 3, 2, true, 4); Console.WriteLine("Inheritance practice!"); - Console.WriteLine("aTruck's color is " + aTruck.color); + Console.WriteLine("aTruck's color is " + aVan.color); Console.WriteLine("aMotorcycle's color is " + aMotorcycle.color); Console.WriteLine("Gravedigga's color is " + gravedigga.color); + + Console.WriteLine("aVan honks like " + aVan.honk()); + Console.WriteLine("Gravedigga honks like " + gravedigga.honk()); + Console.WriteLine("aMotorcycle honks like " + aMotorcycle.honk()); } } @@ -31,6 +35,9 @@ public class Vehicle{ public int numPassengers {get;set;} public bool gasPowered {get;set;} public int engineSize {get;set;} + public virtual String honk(){ + return "honk honk"; + } public Vehicle(string color, int numWheels, int numPassengers, bool gasPowered, int engineSize){ @@ -46,16 +53,24 @@ public class Truck : Vehicle { public int cabSize {get;set;} public int towingCapacity {get;set;} + public override String honk(){ + return "B I G G H O N C C"; + } + public Truck(int cabSize, int towingCapacity, string color, int numWheels, int numPassengers, bool gasPowered, int engineSize) : base(color, numWheels, numPassengers, gasPowered, engineSize){ this.numWheels = numWheels; this.cabSize = cabSize; this.towingCapacity = towingCapacity; + } } public class Motorcycle : Vehicle { public bool isCruiser {get;set;} public bool hasSidecar {get;set;} + public override String honk(){ + return "beep beep"; + } public Motorcycle(bool isCruiser, bool hasSidecar, string color, int numWheels, int numPassengers, bool gasPowered, int engineSize) : base(color, numWheels, numPassengers, gasPowered, engineSize){ this.isCruiser = isCruiser; this.hasSidecar = hasSidecar;