// Class Level Methods (public) public static MoveRequest GetUserInput(CheckerBoard checkerBoard) { if(UserInput == null) UserInput = new MoveRequest(); _CheckerBoard = checkerBoard; String userName = ""; if(checkerBoard.whosTurn() == false) { // false == player1 (Black); true == player2 (White) userName = "******"; } else { userName = "******"; } PrintLine(_CheckerBoard.ToString() + "\n\n"); PrintLine(userName + " please enter your move: "); UserInput.RawUserInput = Console.ReadLine(); return ParseInput(); }
public static MoveRequest ParseInput(String userInput = null) { #if DEBUG Console.WriteLine(userInput); // Echos the input when in DEBUG mode #endif String localUserInput; if(userInput != null){ localUserInput = userInput.ToLower(); } else { localUserInput = UserInput.RawUserInput.ToLower(); } //String localUserInput = UserInput.RawUserInput.ToLower(); List<int?> MoveList = new List<int?>(); // Parsing Section String[] Commands = localUserInput.Split(','); foreach(String word in Commands) { int TempNum = -1; switch(word) { case "q": UserInput.CurrentUser = '******'; break; case "w": UserInput.CurrentUser = '******'; break; case "b": UserInput.CurrentUser = '******'; break; default: if((int.TryParse(word, out TempNum)) == false) { PrintLine("Bad Input Error. Please Re-Enter Your Selection: "); UserInput = new MoveRequest { RawUserInput = Console.ReadLine() }; ParseInput(); return UserInput; } else { UserInput.Moves.Add(TempNum); } break; }// end switch }// end loop // Error Checks Below if(!((UserInput.CurrentUser == 'w') || // Syntax Check (UserInput.CurrentUser == 'b') || (UserInput.CurrentUser == 'q'))){ PrintLine("Syntax Error: Please Re-Enter Your Selection"); UserInput = new MoveRequest { RawUserInput = Console.ReadLine() }; ParseInput(); return UserInput; } else { // turn checks if(UserInput.CurrentUser == 'q') { System.Environment.Exit(1); return UserInput; }else if(UserInput.CurrentUser == 'w') { if(_CheckerBoard.whosTurn() != true) { PrintLine("Out Of Turn Error: Please Re-Enter Your Selection"); UserInput = new MoveRequest { RawUserInput = Console.ReadLine() }; ParseInput(); return UserInput; } }else if(UserInput.CurrentUser == 'b') { if(_CheckerBoard.whosTurn() != false) { PrintLine("Out Of Turn Error: Please Re-Enter Your Selection"); UserInput = new MoveRequest { RawUserInput = Console.ReadLine() }; ParseInput(); return UserInput; } } } foreach(int i in UserInput.Moves) { // check to make sure all the numbers are within range if(i < 0) { PrintLine("Input Out of Bounds Error: Please Re-Enter Your Selection"); UserInput = new MoveRequest { RawUserInput = Console.ReadLine() }; ParseInput(); return UserInput; } else if(i > 32) { PrintLine("Input Out of Bounds Error: Please Re-Enter Your Selection"); UserInput = new MoveRequest { RawUserInput = Console.ReadLine() }; ParseInput(); return UserInput; } } bool firstLoop = true; foreach(int i in UserInput.Moves) { int position = UserInput.Moves[0]; char piece = _CheckerBoard.findPiece(position); if((piece == '#')||(piece == '@')) break; if(firstLoop) { firstLoop = false; continue; } /*if(i == UserInput.Moves[0]) { Console.WriteLine("Move Error: Cannot place a piece where it came from. Re-Enter Your Selection"); UserInput = new MoveRequest { RawUserInput = Console.ReadLine() }; ParseInput(); return UserInput; }*/ } return UserInput; }// end ParseInput
private void MakeMove(MoveRequest request) { char player = request.CurrentUser; List <int> moves = new List <int>(request.Moves); List <int> savedMoves = new List <int>(request.Moves); bool isJump = false; while (moves.Count > 1) { int a = moves[0]; int b = moves[1]; int c = 0; if (moves.Count >= 3) { c = moves[2]; } if (TheCheckerBoard.anyJumps()) { //if there is a jump, they have to take it isJump = true; } //If a and b are neighbors and only two inputs, normal move if (TheCheckerBoard.areNeigbors(a, b) && (moves.Count == 2)) { //normal move if (isJump) { //throw error Console.WriteLine("Required Jump! Re-Enter Your Move"); var RawInput = Console.ReadLine(); InputControl.UserInput = new MoveRequest(); var UsrInput = InputControl.ParseInput(RawInput); MakeMove(UsrInput); return; } break; } //See if a and b can jump if (TheCheckerBoard.isItAJump(a, b, c)) { var x = moves[0]; moves.Remove(b); moves.Remove(c); if (moves.Count < 2) { //ALL jumps correct break; } } else if (TheCheckerBoard.isItAJump(a, b)) { var x = moves[0]; moves.Remove(b); if (moves.Count < 2) { //ALL jumps correct break; } } else { //Invalid jump.Force new input Console.WriteLine("Invalid Jump! Re-Enter Your Move"); var input = Console.ReadLine(); //request = null; MakeMove(InputControl.ParseInput(input)); return; } } //Make the moves using savedMoves for (int i = 0; i < request.Moves.Count - 1; i++) { try { TheCheckerBoard.MovePiece(player, request.Moves[i], request.Moves[i + 1]); }catch (Exception e) { Console.WriteLine("Invalid Input! Re-Enter Your Move"); var input = Console.ReadLine(); MakeMove(InputControl.ParseInput(input)); return; } if (isJump) { //Find shared neigbor, delete it List <int?> aList = new List <int?>(); List <int?> bList = new List <int?>(); aList = TheCheckerBoard.getNeighbors(request.Moves[i]); bList = TheCheckerBoard.getNeighbors(request.Moves[i + 1]); foreach (int?k in aList) { foreach (int?j in bList) { if (k == j) { TheCheckerBoard.RemovePiece((int)k); } } } } } //Clears out cached moves request.Moves.Clear(); moves = null; savedMoves = null; request = null; TheCheckerBoard.player1 = !TheCheckerBoard.player1; }
public static void ProcessMove(MoveRequest Request = null, CheckerBoard TheBoard = null) { // Build Test Data When Params are Null if (Request == null) { Request = new MoveRequest { RawUserInput = "b,1,2,3,4", CurrentUser = '******' }; for (int i = 1; i < 5; i++) { Request.Moves.Add(i); } Debug.WriteLine(Request); } if (TheBoard == null) { TheBoard = new CheckerBoard(); Debug.WriteLine(TheBoard); TheBoard.MovePiece('b', 12, 16); TheBoard.MovePiece('w', 24, 20); TheBoard.MovePiece('b', 11, 15); TheBoard.MovePiece('w', 23, 19); TheBoard.MovePiece('b', 10, 14); TheBoard.MovePiece('w', 22, 18); TheBoard.MovePiece('b', 9, 13); TheBoard.MovePiece('w', 21, 17); TheBoard.RemovePiece(32); TheBoard.RemovePiece(29); Debug.WriteLine(TheBoard); } var BoardArray = TheBoard.Board; // Getting a copy of the array bool CurrentPlayer = TheBoard.whosTurn(); // Black == false; White == True int PieceToMove = Request.Moves[0]; // refrence of whats being moved // Define what a side is int[] RightSideNums = { 1, 5, 13, 21, 29 }; int[] LeftSideNums = { 4, 8, 12, 20, 28 }; int[] TopSideNums = { 29, 30, 31, 32 }; int[] BottomSideNums = { 1, 2, 3, 4 }; // Vars to tell what side a piece is on bool RightSide = IsSide(PieceToMove, RightSideNums); bool LeftSide = IsSide(PieceToMove, LeftSideNums); bool TopSide = IsSide(PieceToMove, TopSideNums); bool BottomSide = IsSide(PieceToMove, BottomSideNums); // Group Data Into Lists List <Piece> BlackPieces = GetSpaces(false, TheBoard); List <Piece> WhitePieces = GetSpaces(true, TheBoard); List <Piece> AllPieces = new List <Piece>(BlackPieces); AllPieces.AddRange(WhitePieces); //List<Piece> MovesToMake = new List<Piece>(); // Currently Empty On Instatiation // Reset Global vars numOfBlackPieces = BlackPieces.Count; numOfWhitePieces = WhitePieces.Count; // Logic // Start itterating through the pieces and find all jumps for any given piece // Need to make a move object that holds all possible moves for said piece }