forked from AustinCodingAcademy/csharp-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Checkpoint2 #15
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
jamestglh
wants to merge
8
commits into
master
Choose a base branch
from
checkpoint2
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
Checkpoint2 #15
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b25f5eb
started whiteboarding checkers
a4cdd18
working on checkerboard
c8d267b
working on cp 2
9035780
continuing work on checkpoint
f1dd04c
still working on checkpoint2
45e9e4c
close to finishing checkers
7273036
still cleaning up checkers
6e104d4
i dont think anything has changed since the last commit but im just m…
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 |
|---|---|---|
| @@ -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/checkpoint2.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}" | ||
| } | ||
| ,] | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "version": "2.0.0", | ||
| "tasks": [ | ||
| { | ||
| "label": "build", | ||
| "command": "dotnet", | ||
| "type": "process", | ||
| "args": [ | ||
| "build", | ||
| "${workspaceFolder}/checkpoint2.csproj" | ||
| ], | ||
| "problemMatcher": "$msCompile" | ||
| } | ||
| ] | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,348 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace checkpoint2 | ||
| { | ||
| class Program | ||
| { | ||
| static void Main(string[] args) | ||
| { | ||
| Console.WriteLine("Checkerspoint 2. WHITE = X BLACK = O. WHITE GOES FIRST!"); | ||
| Game myGame = new Game(); | ||
| myGame.Start(); | ||
| } | ||
| } | ||
| public class Game | ||
| { | ||
| //"the only thing in the Game class should be the Start method" | ||
| public void Start() | ||
| { | ||
| Board aBoard = new Board(); | ||
| aBoard.GenerateCheckers(); | ||
| while (!aBoard.hasWon) | ||
| { | ||
| aBoard.PlaceCheckers(); | ||
| aBoard.DrawBoard(); | ||
| aBoard.MoveChecker(); | ||
| } | ||
| aBoard.WhoWon(); | ||
| } | ||
| public Game(){} | ||
| } | ||
| public class Board | ||
| { | ||
| public String[,] grid = new String[8,8]; | ||
| public bool hasWon = false; | ||
| public string playerColor = "white"; | ||
| public List<Checker> checkers {get;set;} | ||
| public int whiteScore {get;set;} | ||
| public int blackScore {get;set;} | ||
| public Board() | ||
| { | ||
| this.checkers = new List<Checker>(); | ||
| } | ||
| public void DrawBoard() | ||
| { | ||
| Console.WriteLine(" 0 1 2 3 4 5 6 7 "); | ||
| for (int i = 0; i < grid.GetLength(0); i++) | ||
| { | ||
| Console.Write(i + ":"); | ||
| for (int j = 0; j < grid.GetLength(1); j++) | ||
| { | ||
| if (grid[i,j] == null) // if spot on grid doesnt have a checker, a space is written so the spacing doesnt get all fucked up | ||
| { | ||
| Console.Write(" "); | ||
| } | ||
| Console.Write(" " + grid[i,j]); | ||
| } | ||
| Console.WriteLine(); | ||
| } | ||
| } | ||
| public void GenerateCheckers(){ // generate checkers only runs once at the beginning of the game. it creates all of the white and black checkers and gives them the correct coords | ||
| for (int i = 0; i < 3; i++) // iterate through columns where whites should go | ||
| { | ||
| for (int j = 0; j < 8; j++) //iterate through rows | ||
| { | ||
| if ((i % 2 == 0 && j % 2 != 0) || (i % 2 != 0 && j % 2 == 0)) | ||
| { | ||
| checkers.Add(new Checker(true, i,j)); | ||
| } | ||
| } | ||
| } | ||
| for (int i = 5; i < 8; i++) // iterate through columns where black should go | ||
| { | ||
| for (int j = 0; j < 8; j++) //iterate through rows | ||
| { | ||
| if ((i % 2 == 0 && j % 2 != 0) || (i % 2 != 0 && j % 2 == 0)) | ||
| { | ||
| checkers.Add(new Checker(false, i, j)); | ||
| // Console.WriteLine("adding black checker to list at coordinate {0} {1}", i,j); | ||
| } | ||
| } | ||
| } | ||
| // foreach (Checker c in checkers) //INTIAL CHECKER PLACEMENT CHECK | ||
| // { | ||
| // Console.WriteLine("Checker at X " + c.xPos + " and Y " + c.yPos); | ||
| // } | ||
| } | ||
| public void PlaceCheckers() //this is what reads the checker coordinates and puts it in the grid in the right place | ||
| { | ||
| for (var i = 0; i < checkers.Count; i++) | ||
| { | ||
| int[] position = checkers[i].position; | ||
| grid[position[0],position[1]] = checkers[i].symbol; | ||
| } | ||
| return; | ||
| } | ||
| public void MoveChecker(){ | ||
| int[] validInput = new int[] {0,1,2,3,4,5,6,7}; | ||
| int fromX = 0; | ||
| int fromY = 0; | ||
| int toX = 0; | ||
| int toY = 0; | ||
| string input = ""; | ||
| PlayerMove(); | ||
| void PlayerMove(){ | ||
| GetPlayerInputFrom(); | ||
| // CheckPlayerInputFrom(); // i ended up daisy chaining these at the ends of each other to make it work the way i wanted it to | ||
| // GetPlayerInputTo(); | ||
| // CheckPlayerMove(); | ||
| // SwitchPlayers(); | ||
| } | ||
|
|
||
| void GetPlayerInputFrom(){ | ||
| //GETTING PLAYER INPUT / FROM | ||
| Console.WriteLine("Enter Pickup Row, or type \"c\" to call it a day and tally up the final score: "); | ||
| input = Console.ReadLine(); | ||
| if (input.ToLower() == "c") | ||
| { | ||
| CallItADay(); | ||
| return; | ||
| } | ||
| if(!Int32.TryParse(input, out fromX)) | ||
| { | ||
| System.Console.WriteLine("Your input is invalid. Only input numbers 0-7. Try again."); | ||
| return; | ||
| } | ||
|
|
||
| Console.WriteLine("Enter Pickup Column: "); | ||
| input = Console.ReadLine(); | ||
| if(!Int32.TryParse(input, out fromY)) | ||
| { | ||
| System.Console.WriteLine("Your input is invalid. Only input numbers 0-7. Try again."); | ||
| return; | ||
| } | ||
| CheckPlayerInputFrom(); | ||
| } | ||
| void CheckPlayerInputFrom(){ | ||
| //INPUT CHECKS / FROM | ||
| Checker checkerToMove = SelectChecker(fromX, fromY); | ||
| if (!validInput.Contains(fromX) || !validInput.Contains(fromY)) // CHECKS IF INPUT IS OUT OF BOUNDS | ||
| { | ||
| Console.WriteLine("Your selection is out of bounds of the board"); | ||
| return; | ||
| } | ||
| else if (checkerToMove == null) // CHECKS IF A CHECKER IS THERE | ||
| { | ||
| Console.WriteLine("You chose an empty space. Please choose again."); | ||
| return; | ||
| } | ||
| else if (checkerToMove.color != playerColor) //CHECKS TO SEE IF IT'S THE RIGHT PLAYER's CHECKER | ||
| { | ||
| Console.WriteLine("You chose the wrong colored checker. Please choose again."); | ||
| return; | ||
| } | ||
| GetPlayerInputTo(); | ||
| } | ||
| void GetPlayerInputTo(){ | ||
| //GETTING PLAYER INPUT / TO | ||
| Console.WriteLine("Enter Placement Row: "); | ||
| input = Console.ReadLine(); | ||
| if(!Int32.TryParse(input, out toX)) | ||
| { | ||
| System.Console.WriteLine("Your input is invalid. Only input numbers 0-7. Try again."); | ||
| return; | ||
| } | ||
| Console.WriteLine("Enter Placement Column: "); | ||
| input = Console.ReadLine(); | ||
| if(!Int32.TryParse(input, out toY)) | ||
| { | ||
| System.Console.WriteLine("Your input is invalid. Only input numbers 0-7. Try again."); | ||
| return; | ||
| } | ||
| CheckPlayerMove(); | ||
| } | ||
| void CheckPlayerMove(){ | ||
| //MOVEMENT CHECKS / TO | ||
| Checker checkerToMove = SelectChecker(fromX, fromY); | ||
| if (!validInput.Contains(toX) || !validInput.Contains(toY)) // CHECKS IF DESTINATION IS OUT OF BOUNDS | ||
| { | ||
| Console.WriteLine("Your destination is out of bounds of the board"); | ||
| return; | ||
| } | ||
| if ((grid[toX,toY] != null) && (SelectChecker(toX, toY).color == checkerToMove.color)) //CHECKS IF DESTINATION HAS A CHECKER OF THE SAME COLOR | ||
| { | ||
| Console.WriteLine("You aren't allowed to land on another checker of your color"); | ||
| return; | ||
| } | ||
| if (Math.Abs(fromY - toY) > 1 || Math.Abs(fromX - toX) > 1) //making sure coordinate only increases by 1 | ||
| { | ||
| Console.WriteLine("Your destination is illegal. You can only move diagonally 1 space. If you want to jump an enemy checker, choose it's location."); | ||
| return; | ||
| } | ||
| if ((fromX == toX) || (fromY == toY)) //ensures they only move diagonally | ||
| { | ||
| Console.WriteLine("Your destination is illegal. You can only move diagonally."); | ||
| return; | ||
| } | ||
| //checkers can only move forward | ||
| if (((checkerToMove.color == "black") && (fromX <= toX)) || ((checkerToMove.color == "white") && (fromX >= toX))) | ||
| { | ||
| Console.WriteLine("Sorry, you can only move forward diagonally, not back."); | ||
| return; | ||
| } | ||
| //JUMPING A PIECE!! | ||
| if ((grid[toX,toY] != null) && (SelectChecker(toX, toY).color != checkerToMove.color)) //ensuring space is not empty, and space has enemy checker in it | ||
| { | ||
| if ((grid[fromX + ((toX-fromX)*2), fromY + ((toY-fromY)*2)] == null)) // ensuring space past checker to jump is empty | ||
| { | ||
| System.Console.WriteLine("You just jumped a checker!"); | ||
| if (checkerToMove.color == "white") | ||
| { | ||
| whiteScore++; | ||
| } | ||
| else | ||
| { | ||
| blackScore++; | ||
| } | ||
| checkers.Remove(SelectChecker(toX, toY)); | ||
| grid[toX,toY] = null; | ||
| toX = fromX + ((toX-fromX)*2); | ||
| toY = fromY + ((toY-fromY)*2); | ||
| } | ||
| else | ||
| { | ||
| System.Console.WriteLine("Sorry, you tried to jump a checker, but there's nowhere for you to land."); | ||
| return; | ||
| } | ||
| } | ||
| checkerToMove.position[0] = toX; | ||
| checkerToMove.position[1] = toY; | ||
| grid[fromX,fromY] = null; | ||
| SwitchPlayers(); | ||
| } | ||
| // foreach (Checker c in checkers) // this is just a console for troubleshooting to ensure that the coordinates of the checkers are really updating | ||
| // { | ||
| // Console.WriteLine(c.color + " checker at X " + c.xPos + " and Y " + c.yPos); | ||
| // } | ||
| } | ||
| public void CallItADay(){ | ||
| System.Console.WriteLine("The current score is WHITE {0}, BLACK {1}", whiteScore, blackScore); | ||
| System.Console.WriteLine("Would you like to move again, or call it a day?"); | ||
| System.Console.WriteLine("Type \"m\" to move again, or \"c\" to call it a day. No backsies."); | ||
| string input = Console.ReadLine(); | ||
| if (input.ToLower() == "m") | ||
| { | ||
| return; | ||
| } | ||
| else if (input.ToLower() == "c") | ||
| { | ||
| System.Console.WriteLine("Final Score: WHITE {0} BLACK {1}", whiteScore, blackScore); | ||
| if (whiteScore > blackScore) | ||
| { | ||
| System.Console.WriteLine("White wins!"); | ||
| hasWon = true; | ||
| } | ||
| else if (whiteScore < blackScore) | ||
| { | ||
| System.Console.WriteLine("Black wins!"); | ||
| hasWon = true; | ||
| } | ||
| else if (whiteScore == blackScore) | ||
| { | ||
| System.Console.WriteLine("It's a tie! Incredible!"); | ||
| hasWon = true; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| System.Console.WriteLine("Your input is invalid, my dude."); | ||
| CallItADay(); | ||
| } | ||
| } | ||
| public Checker SelectChecker(int row, int column) //selectchecker takes in x,y, returns checker or null, or throws exception | ||
| { | ||
| return checkers.Find(x => x.position.SequenceEqual(new List<int> { row, column })); | ||
| } | ||
| public bool CheckForWin() | ||
| { | ||
| return checkers.All(x => x.color == "white") || !checkers.Exists(x => x.color == "white"); | ||
| } | ||
| public void WhoWon(){ | ||
| foreach (Checker c in checkers) | ||
| { | ||
| if (whiteScore == 12) | ||
| { | ||
| System.Console.WriteLine("White won! Wow! You actually played through a whole game!"); | ||
| } | ||
| else if (blackScore == 12) | ||
| { | ||
| System.Console.WriteLine("Black won! Incredible! I bet you wasted a lot of time to get here!"); | ||
| } | ||
| } | ||
| } | ||
| public void SwitchPlayers() | ||
| { | ||
| if (playerColor == "white") | ||
| { | ||
| Console.WriteLine("Black Turn"); | ||
| System.Console.WriteLine("White score: {0} Black score: {1}", whiteScore, blackScore); | ||
| playerColor = "black"; | ||
| } | ||
| else | ||
| { | ||
| Console.WriteLine("White Turn"); | ||
| System.Console.WriteLine("White score: {0} Black score: {1}", whiteScore, blackScore); | ||
| playerColor = "white"; | ||
| } | ||
| } | ||
| } | ||
| public class Checker | ||
| { | ||
| public String symbol {get;private set;} | ||
| public String color {get;private set;} | ||
| public int[] position = new int[2]; | ||
| public bool isWhite {get;private set;} | ||
|
|
||
| public int xPos {get;set;} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont see you making use or updating xPos and yPos. |
||
| public int yPos {get;set;} | ||
|
|
||
| public Checker(bool isWhite, int xPos, int yPos) | ||
| { | ||
| this.color = color; | ||
| this.symbol = symbol; | ||
| int openCircleId = int.Parse(" 25CE", System.Globalization.NumberStyles.HexNumber); | ||
| string openCircle = char.ConvertFromUtf32(openCircleId); | ||
| int closedCircleId = int.Parse(" 25C9", System.Globalization.NumberStyles.HexNumber); | ||
| string closedCircle = char.ConvertFromUtf32(closedCircleId); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it does not look like you are using openCircle or closedCircle anywhere. |
||
| this.xPos = xPos; | ||
| this.yPos = yPos; | ||
| position[0] = xPos; | ||
| position[1] = yPos; | ||
|
|
||
| if (isWhite) | ||
| { | ||
| color = "white"; | ||
| // symbol = openCircle; | ||
| symbol = "X"; | ||
| } | ||
| else | ||
| { | ||
| color = "black"; | ||
| // symbol = closedCircle; | ||
| symbol = "O"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
you call SelectChecker(toX, toY) multiple times, could probably have made a targetChecker variable to hold that and used it.
Checker targetChecker = SelectChecker(toX,toY);Seems like you are relying on the grid[toX,toY] to tell if the destination is empty square.
You could use the targetChecker to check, and that way the grid is just for drawing