From 4d1de56ade852dc347511fd7059b883289aa9726 Mon Sep 17 00:00:00 2001 From: d3marco31 Date: Sun, 25 Nov 2018 16:04:31 -0600 Subject: [PATCH 1/2] Checkpoint2 --- Checkpoint2/Checkpoint2.csproj | 8 ++ Checkpoint2/Program.cs | 235 +++++++++++++++++++++++++++++++++ 2 files changed, 243 insertions(+) create mode 100644 Checkpoint2/Checkpoint2.csproj create mode 100644 Checkpoint2/Program.cs diff --git a/Checkpoint2/Checkpoint2.csproj b/Checkpoint2/Checkpoint2.csproj new file mode 100644 index 00000000..23df6047 --- /dev/null +++ b/Checkpoint2/Checkpoint2.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.1 + + + diff --git a/Checkpoint2/Program.cs b/Checkpoint2/Program.cs new file mode 100644 index 00000000..b2d366fc --- /dev/null +++ b/Checkpoint2/Program.cs @@ -0,0 +1,235 @@ +using System; +using System.Linq; +using System.Collections.Generic; + namespace Checkpoint2 +{ + class Program + { + public static string player = "White"; + static void Main(string[] args) + { + Game(); + } + //Build pieces + public static readonly Checker whitePiece = new Checker() + { + Symbol = char.ConvertFromUtf32(int.Parse("25CB", System.Globalization.NumberStyles.HexNumber)), + Color = "White" + }; + public static readonly Checker blackPiece = new Checker() + { + Symbol = char.ConvertFromUtf32(int.Parse("25CF", System.Globalization.NumberStyles.HexNumber)), + Color = "Black" + }; + public class Checker + { + //attributes + public string Symbol { get; set; } //the actual symbol either an open or a closed dot + public int[] Position { get; set; } //the coordinates of its place on the grid + public string Color { get; set; } // the team name (either "white" or "black") + } + + public class Board + { + //attributes + public string[][] Grid { get; set; } //the "grid" that makes up the board + public List Checkers { get; set; } //the collection of Checkers currently on the board + public void CreateBoard() + { + //string PlaceHolder = " "; + + var newBoard = new string[8][]; + //Populate row objects to grid + for (int i = 0; i < newBoard.Length; i++) + { + string[] BoardRow = new string[8]; + //Build row object + for (int r = 0; r < BoardRow.Length; r++) + { + BoardRow[r] = " "; + } + + newBoard[i] = BoardRow; + } + this.Grid = newBoard; + } + public void GenerateCheckers(){ + this.Checkers = new List(); + + //fill white positions + for (int i = 0; i < 3; i++) + { + for (int r = 0; r < 8; r++) + { + var white = new Checker() + { + Symbol = char.ConvertFromUtf32(int.Parse("25CB", System.Globalization.NumberStyles.HexNumber)), + Color = "White" + }; + if(i%2 == 0 && r%2 != 0) + { + white.Position = new int[] { i, r }; + this.Checkers.Add(white); + }else if(i%2 != 0 && r%2 == 0) { + white.Position = new int[] { i, r }; + this.Checkers.Add(white); + } + + } + } + //fill black positions + for (int i = 5; i < 8; i++) + { + for (int r = 0; r < 8; r++) + { + var black = new Checker() + { + Symbol = char.ConvertFromUtf32(int.Parse("25CF", System.Globalization.NumberStyles.HexNumber)), + Color = "Black" + }; + if(i%2 == 0 && r%2 != 0) + { + black.Position = new int[] { i, r }; + this.Checkers.Add(black); + }else if(i%2 != 0 && r%2 == 0) { + black.Position = new int[] { i, r }; + this.Checkers.Add(black); + } + } + } + } + public void DrawBoard(){ + // var boardGrid = this.Grid; + //print top grid numbers + Console.OutputEncoding = System.Text.Encoding.UTF8; + Console.WriteLine (" 0 1 2 3 4 5 6 7"); + //loop through board rows + for (int i = 0; i < this.Grid.Length; i++) + { + //Get board row + var row = this.Grid[i]; + //get board row index + var rowString = i.ToString(); + + //loop through each row item + for (int r = 0; r < row.Length; r++) + { + var itemValue = " " + row[r]; + rowString = rowString + itemValue; + } + //print row string to console + Console.WriteLine(rowString); + } + } + public void PlaceCheckers() + { + for (var i = 0; i < Checkers.Count; i++) + { + int[] position = Checkers[i].Position; + Grid[position[0]][position[1]] = Checkers[i].Symbol; + } + } + private Checker SelectChecker(int row, int column) + { + var position = new int[] {row, column}; + var checker = Checkers.Where(x => x.Position[0] == row && x.Position[1] == column).SingleOrDefault(); + return checker; + } + public void MoveChecker(int selRow, int selColumn, int row, int column) + { + var checker = SelectChecker(selRow, selColumn); + + if(checker.Color == player) //Prevent player from moving other players checkers + { + if(checker != null) + { + Checkers.Remove(checker); + checker.Position = new int[]{row, column}; + Checkers.Add(checker); + } + } + else{ + Console.WriteLine("Don't try to move the other players checker! Turn forfeited."); + } + } + public void RemoveChecker(int row, int column) + { + var checker = SelectChecker(row, column); + if(checker != null) + { + Checkers.Remove(checker); + } + } + public bool CheckForWin() + { + if(player == "White") + { + var blackCheckerCount = Checkers.Where(x => x.Color == "Black").Count(); + if(blackCheckerCount == 0) + { + return true; + } + } + else{ + var whiteCheckerCount = Checkers.Where(x => x.Color == "White").Count(); + if(whiteCheckerCount == 0) + { + return true; + } + } + return false; + } + } + + public static void Game() + { + Board board = new Board(); + board.CreateBoard(); + board.GenerateCheckers(); + bool winDetected = false; + while(!winDetected) + { + board.PlaceCheckers(); + board.DrawBoard(); + Console.WriteLine("Player " + player +"'s turn" ); + Console.WriteLine("move or remove checker?"); + Console.WriteLine("Select a checker"); + Console.WriteLine("Enter Row:"); + int selRow = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("Enter Column:"); + int selColumn = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("Select new checker location"); + Console.WriteLine("Enter Row:"); + int row = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("Enter Column:"); + int column = Convert.ToInt32(Console.ReadLine()); + board.MoveChecker(selRow, selColumn, row, column); + Console.WriteLine("Remove checker? y/n"); + var userAction = Console.ReadLine().ToLower(); + + if(userAction == "y") + { + Console.WriteLine("Select checker to remove"); + Console.WriteLine("Enter Row:"); + row = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("Enter Column:"); + column = Convert.ToInt32(Console.ReadLine()); + board.RemoveChecker(row, column); + } + winDetected = board.CheckForWin(); + + if(!winDetected) + { + if(player == "White") + { + player = "Black"; + } + else{ + player = "White"; + } + board.CreateBoard(); + } + } + } + } +} From 4114b41a12b6a097903dad83bb3972448a8dee0e Mon Sep 17 00:00:00 2001 From: d3marco31 Date: Mon, 26 Nov 2018 20:21:30 -0600 Subject: [PATCH 2/2] final Checkpoint2 --- Checkpoint2/Program.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Checkpoint2/Program.cs b/Checkpoint2/Program.cs index b2d366fc..a7b79dee 100644 --- a/Checkpoint2/Program.cs +++ b/Checkpoint2/Program.cs @@ -123,7 +123,7 @@ public void DrawBoard(){ } public void PlaceCheckers() { - for (var i = 0; i < Checkers.Count; i++) + for (int i = 0; i < Checkers.Count; i++) { int[] position = Checkers[i].Position; Grid[position[0]][position[1]] = Checkers[i].Symbol; @@ -131,13 +131,13 @@ public void PlaceCheckers() } private Checker SelectChecker(int row, int column) { - var position = new int[] {row, column}; - var checker = Checkers.Where(x => x.Position[0] == row && x.Position[1] == column).SingleOrDefault(); + int[] position = new int[] {row, column}; + Checker checker = Checkers.Where(x => x.Position[0] == row && x.Position[1] == column).SingleOrDefault(); return checker; } public void MoveChecker(int selRow, int selColumn, int row, int column) { - var checker = SelectChecker(selRow, selColumn); + Checker checker = SelectChecker(selRow, selColumn); if(checker.Color == player) //Prevent player from moving other players checkers { @@ -154,7 +154,7 @@ public void MoveChecker(int selRow, int selColumn, int row, int column) } public void RemoveChecker(int row, int column) { - var checker = SelectChecker(row, column); + Checker checker = SelectChecker(row, column); if(checker != null) { Checkers.Remove(checker); @@ -164,14 +164,14 @@ public bool CheckForWin() { if(player == "White") { - var blackCheckerCount = Checkers.Where(x => x.Color == "Black").Count(); + int blackCheckerCount = Checkers.Where(x => x.Color == "Black").Count(); if(blackCheckerCount == 0) { return true; } } else{ - var whiteCheckerCount = Checkers.Where(x => x.Color == "White").Count(); + int whiteCheckerCount = Checkers.Where(x => x.Color == "White").Count(); if(whiteCheckerCount == 0) { return true;