From e586f8ee7f22d9db5844d3b502ca5edd03f4ed2d Mon Sep 17 00:00:00 2001 From: Sam Mangum-Bostick Date: Sat, 2 Mar 2019 12:31:55 -0600 Subject: [PATCH 1/2] Added Create/Draw Board --- Checkers/.vscode/launch.json | 28 +++++++++++++++++ Checkers/.vscode/tasks.json | 15 +++++++++ Checkers/Checkers.cs | 60 ++++++++++++++++++++++++++++-------- 3 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 Checkers/.vscode/launch.json create mode 100644 Checkers/.vscode/tasks.json diff --git a/Checkers/.vscode/launch.json b/Checkers/.vscode/launch.json new file mode 100644 index 00000000..c56f1844 --- /dev/null +++ b/Checkers/.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/Checkers.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/Checkers/.vscode/tasks.json b/Checkers/.vscode/tasks.json new file mode 100644 index 00000000..a4114962 --- /dev/null +++ b/Checkers/.vscode/tasks.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/Checkers.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Checkers/Checkers.cs b/Checkers/Checkers.cs index 211d2a70..60084be2 100644 --- a/Checkers/Checkers.cs +++ b/Checkers/Checkers.cs @@ -14,10 +14,10 @@ static void Main(string[] args) public class Checker { - public string Symbol { get; set; } - public int[] Position { get; set; } + public string Symbol { get; set; } + public int[] Position { get; set; } public string Color { get; set; } - + public Checker(string color, int[] position) { // Your code here @@ -26,50 +26,86 @@ public Checker(string color, int[] position) public class Board { - public string[][] Grid { get; set; } + public string[][] Grid { get; set; } + + + public List Checkers { get; set; } - + public Board() { // Your code here return; } - + public void CreateBoard() { // Your code here + this.Grid = new string[][] { + new string[] {" ", " ", " ", " ", " ", " ", " ", " "}, + new string[] {" ", " ", " ", " ", " ", " ", " ", " "}, + new string[] {" ", " ", " ", " ", " ", " ", " ", " "}, + new string[] {" ", " ", " ", " ", " ", " ", " ", " "}, + new string[] {" ", " ", " ", " ", " ", " ", " ", " "}, + new string[] {" ", " ", " ", " ", " ", " ", " ", " "}, + new string[] {" ", " ", " ", " ", " ", " ", " ", " "}, + new string[] {" ", " ", " ", " ", " ", " ", " ", " "}, return; } - + public void GenerateCheckers() { // Your code here + int[][] whitePositions = new int[][] + { + new int[] {0,1},new int[] {0,3},new int[] {0,5}, new int[] {0,7}, + new int[] {1,0},new int[] {1,2},new int[] {1,4},new int[] {1,6}, + new int[] {2,1},new int[] {2,3},new int[] {2,5},new int[] {2,7} + }; + int[][] blackPositions = new int[][] + { + new int[] {5,0},new int[] {5,2},new int[] {5,4}, new int[] {5,6}, + new int[] {6,1},new int[] {6,3},new int[] {6,5},new int[] {6,7}, + new int[] {7,0},new int[] {7,2},new int[] {7,4},new int[] {7,6} + }; + for (int i = 0; i < 12; i++) + { + Checker white = new Checker("white", whitePositions[i]); + Checker black = new Checker("black", blackPositions[i]); + this.Checkers.Add(white); + this.Checkers.Add(black); + } return; } - + public void PlaceCheckers() { // Your code here + for (var i = 0; i < Checkers.Count; i++) + { + int[] position = Checkers[i].Position; + Grid[position[0]][position[1]] = Checkers[i].Symbol; + } return; } - + public void DrawBoard() { // Your code here return; } - + public Checker SelectChecker(int row, int column) { return Checkers.Find(x => x.Position.SequenceEqual(new List { row, column })); } - + public void RemoveChecker(int row, int column) { // Your code here return; } - + public bool CheckForWin() { return Checkers.All(x => x.Color == "white") || !Checkers.Exists(x => x.Color == "white"); From c14f15bc7e42a10ee73802a58e6f2ace9b7aadf4 Mon Sep 17 00:00:00 2001 From: Sam Mangum-Bostick Date: Sun, 3 Mar 2019 20:16:18 -0600 Subject: [PATCH 2/2] Finished specs --- Checkers/Checkers.cs | 129 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 120 insertions(+), 9 deletions(-) diff --git a/Checkers/Checkers.cs b/Checkers/Checkers.cs index 60084be2..c1340c74 100644 --- a/Checkers/Checkers.cs +++ b/Checkers/Checkers.cs @@ -8,7 +8,8 @@ class Program { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + Console.OutputEncoding = System.Text.Encoding.UTF8; + Game.Start(); } } @@ -21,20 +22,31 @@ public class Checker public Checker(string color, int[] position) { // Your code here + int circleId; + if (color == "black") + { + circleId = int.Parse("25CB", System.Globalization.NumberStyles.HexNumber); + } + else + { + circleId = int.Parse("25CF", System.Globalization.NumberStyles.HexNumber); + } + this.Symbol = char.ConvertFromUtf32(circleId); + this.Position = position; + this.Color = color; } } public class Board { public string[][] Grid { get; set; } - - - public List Checkers { get; set; } public Board() { // Your code here + this.Checkers = new List(); + this.CreateBoard(); return; } @@ -49,7 +61,8 @@ public void CreateBoard() new string[] {" ", " ", " ", " ", " ", " ", " ", " "}, new string[] {" ", " ", " ", " ", " ", " ", " ", " "}, new string[] {" ", " ", " ", " ", " ", " ", " ", " "}, - new string[] {" ", " ", " ", " ", " ", " ", " ", " "}, + new string[] {" ", " ", " ", " ", " ", " ", " ", " "} + }; return; } @@ -92,20 +105,95 @@ public void PlaceCheckers() public void DrawBoard() { // Your code here + Console.WriteLine(" 0 1 2 3 4 5 6 7"); + for (int i = 0; i < 8; i++) + { + Console.WriteLine(i + " " + String.Join(" ", this.Grid[i])); + } return; } - public Checker SelectChecker(int row, int column) + public Checker SelectChecker() + { + Console.WriteLine("Select checker row"); + int _row = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("Select checker column"); + int col = Convert.ToInt32(Console.ReadLine()); + return Checkers.Find(x => x.Position.SequenceEqual(new List { _row, col })); + } + + public void MoveChecker2(Checker checker) + { + Console.WriteLine("Move to which row"); + int newRow = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("Move to which column"); + int newCol = Convert.ToInt32(Console.ReadLine()); + checker.Position = new int[] { newRow, newCol }; + + } + public bool MoveChecker(Checker checker) + { + + Console.WriteLine("Move to which row"); + int newRow = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("Move to which column"); + int newCol = Convert.ToInt32(Console.ReadLine()); + if (this.Grid[newRow][newCol] == " ") + { + if (checker.Color == "white") + { + if (newRow < checker.Position[0]) + { + if (newRow + newCol == checker.Position[0] + checker.Position[1] || newRow - newCol == checker.Position[0] - checker.Position[1]) + { + checker.Position = new int[] { newRow, newCol }; + return true; + } + + } + } + if (checker.Color == "black") + { + if (newRow > checker.Position[0]) + { + if (newRow + newCol == checker.Position[0] + checker.Position[1] || newRow - newCol == checker.Position[0] - checker.Position[1]) + { + checker.Position = new int[] { newRow, newCol }; + return true; + } + + } + } + + + } + + System.Console.WriteLine("Illegal Move:"); + return false; + + } + public void DeleteChecker(int newRow, int oldRow, int newCol, int oldCol) { - return Checkers.Find(x => x.Position.SequenceEqual(new List { row, column })); + if ((Math.Abs(newRow - oldRow) > 1 && (Math.Abs(oldCol - newCol) > 1))) + { + int row = (oldRow + newRow) / 2; + int col = (oldCol + newCol) / 2; + RemoveChecker(Checkers.Find(x => x.Position.SequenceEqual(new List { row, col }))); + } } - public void RemoveChecker(int row, int column) + public void RemoveChecker(Checker checker) { // Your code here + if (checker == null) + { + return; + } + this.Checkers.Remove(checker); return; } + public bool CheckForWin() { return Checkers.All(x => x.Color == "white") || !Checkers.Exists(x => x.Color == "white"); @@ -114,9 +202,32 @@ public bool CheckForWin() class Game { - public Game() + public static void Start() { // Your code here + Board board = new Board(); + board.GenerateCheckers(); + + board.PlaceCheckers(); + board.DrawBoard(); + while (!board.CheckForWin()) + { + Checker temp = board.SelectChecker(); + int oldRow = temp.Position[0]; + int oldCol = temp.Position[1]; + board.MoveChecker2(temp); + int newRow = temp.Position[0]; + int newCol = temp.Position[1]; + board.DeleteChecker(newRow, oldRow, newCol, oldCol); + board.CreateBoard(); + board.PlaceCheckers(); + board.DrawBoard(); + if (board.CheckForWin()) + { + System.Console.WriteLine("Game Over!"); + return; + } + } } } }