forked from AustinCodingAcademy/csharp-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Created towers of hanoi game #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mtrich
wants to merge
2
commits into
master
Choose a base branch
from
week6-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,213 @@ | ||
| using System; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| namespace TowersOfHanoi | ||
| { | ||
| class Program | ||
| { | ||
| static void Main(string[] args) | ||
| static void Main() | ||
| { | ||
| Console.WriteLine("Hello World!"); | ||
| //instatiates Game class | ||
| Game game = new Game(); | ||
| //calls run method | ||
| game.run(); | ||
| } | ||
| } | ||
| class Game | ||
| { | ||
| //creates Dictionary of towers with tower name as Key and Tower class as value | ||
| Dictionary<string, Tower> towers = new Dictionary<string, Tower>(); | ||
|
|
||
| //method that runs game logic | ||
| public void run() | ||
| { | ||
| //while loop that runs only while game is not won | ||
| while(!CheckForWin()) | ||
| { | ||
| //calls method that prints the game board | ||
| PrintBoard(); | ||
|
|
||
| //asks user which tower to move block from then stores input as string | ||
| Console.WriteLine("Move block from Tower _? (A,B,C)"); | ||
| string popOff = Console.ReadLine().ToUpper(); | ||
|
|
||
| //asks user where to move block from selected tower to then stores input as string | ||
| Console.WriteLine("Move fromm tower "+popOff+" to tower_?"); | ||
| string pushOn = Console.ReadLine().ToUpper(); | ||
|
|
||
| //checks if move is legal | ||
| bool legal = IsLegal(popOff, pushOn); | ||
| if(legal) | ||
| { | ||
| MovePiece(popOff, pushOn); | ||
| } | ||
| else | ||
| { | ||
| Console.WriteLine("invalid move"); | ||
| } | ||
| //seperates previous move from current move | ||
| Console.Clear(); | ||
| } | ||
| //prints final board | ||
| PrintBoard(); | ||
| //displays winning message | ||
| Console.WriteLine("Congratulations! You win!"); | ||
| } | ||
| public Game() | ||
| { | ||
| //instantiates 4 blocks | ||
| Block block1 = new Block(1," (=)"); | ||
| Block block2 = new Block(2," (===)"); | ||
| Block block3 = new Block(3," (=====)"); | ||
| Block block4 = new Block(4,"(=======)"); | ||
|
|
||
| //instantiates Tower A | ||
| Tower towerA = new Tower("A"); | ||
| //pushes blocks to Tower A's Stack | ||
| towerA.BlockStack.Push(block4); | ||
| towerA.BlockStack.Push(block3); | ||
| towerA.BlockStack.Push(block2); | ||
| towerA.BlockStack.Push(block1); | ||
| //Adds Tower A to dictionary | ||
| towers.Add("A",towerA); | ||
|
|
||
| //Instantiates tower b and c and adds them to dictionary | ||
| Tower towerB = new Tower("B"); | ||
| towers.Add("B",towerB); | ||
| Tower towerC = new Tower("C"); | ||
| towers.Add("C",towerC); | ||
| } | ||
| //method that displayes the board | ||
| public void PrintBoard() | ||
| { | ||
| //foreach loop that writes name of each tower and each block in that tower | ||
| foreach (var key in towers.Keys) | ||
| { | ||
| Console.WriteLine("tower: " + key); | ||
| Tower tower = towers[key]; | ||
| foreach (Block block in tower.BlockStack) | ||
| { | ||
| Console.WriteLine(block); | ||
| } | ||
| } | ||
| } | ||
| //method that moves the blocks | ||
| public void MovePiece(string popOff, string pushOn) | ||
| { | ||
| //block to carry block that user wishes to move as it is being moved | ||
| Block popBlock; | ||
|
|
||
| //gets value from tower that user has selected to move from | ||
| if (towers.TryGetValue(popOff, out Tower Tower)) | ||
| { | ||
| //pops from stack in user selected tower and assigns it to Block popBlock | ||
| popBlock = Tower.BlockStack.Pop(); | ||
| //gets value from tower that user has selected to move to | ||
| if (towers.TryGetValue(pushOn, out Tower tower)) | ||
| { | ||
| //pushes popBlock to selected tower | ||
| tower.BlockStack.Push(popBlock); | ||
| } | ||
| } | ||
| } | ||
| //method that checks if move is legal | ||
| public bool IsLegal(string popOff, string pushOn) | ||
| { | ||
| //2 blocks to carry blocks being checked | ||
| Block checkBlock1; | ||
| Block checkBlock2; | ||
| //gets value from tower that user has selected to move from | ||
| if(towers.TryGetValue(popOff, out Tower Tower)) | ||
| { | ||
| //peeks from stack in user selected tower and assigns it to Block checkBlock1 | ||
| checkBlock1 = Tower.BlockStack.Peek(); | ||
| //gets value from tower that user has selected to move to | ||
| if (towers.TryGetValue(pushOn, out Tower tower)) | ||
| { | ||
| //if destination has no blocks then move is legal | ||
| if (tower.BlockStack.Count == 0) | ||
| { | ||
| return true; | ||
| } | ||
| else | ||
| { | ||
| //peeks from stack in user selected tower and assigns it to Block checkBlock2 | ||
| checkBlock2 = tower.BlockStack.Peek(); | ||
| //if top block from destination tower has lower weight than block being moved then move is legal | ||
| if (checkBlock1.Weight < checkBlock2.Weight || tower.BlockStack.Count() == 0) | ||
| { | ||
| return true; | ||
| } | ||
| else | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| //method to check for win | ||
| public bool CheckForWin() | ||
| { | ||
| //trys to get value from Tower C | ||
| if(towers.TryGetValue("C", out Tower Tower)) | ||
| { | ||
| //if Tower C has 4 blocks then game is won | ||
| if (Tower.BlockStack.Count == 4) | ||
| { | ||
| return true; | ||
| } | ||
| else | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| class Block | ||
| { | ||
| //Constructor for blocks that have a simple ascii graphic display and a weight both of which are privately set | ||
| public Block(int weight, string display) | ||
| { | ||
| this.Weight = weight; | ||
| this.Display = display; | ||
| } | ||
| public int Weight | ||
| { | ||
| get; | ||
| private set; | ||
| } | ||
| public string Display | ||
| { | ||
| get; | ||
| private set; | ||
| } | ||
| //converts blocks display to a string | ||
| override public string ToString() | ||
| { | ||
| string DisplayString = Display; | ||
| return DisplayString; | ||
| } | ||
| } | ||
| class Tower | ||
| { | ||
| //constructor for towers that have a privately set name and stack | ||
| public Tower(String name) | ||
| { | ||
| this.BlockStack = new Stack<Block>(); | ||
| this.Name = name; | ||
| } | ||
| public Stack<Block> BlockStack{ | ||
| get; | ||
| private set; | ||
| } | ||
| public String Name{ | ||
| get; | ||
| private set; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would this be different if the number of blocks was passed in to the game class