public void UpdatePlayerPawnsCounterAndScore(GameBoard i_Board) { int pawnCounter = 0; int kingCounter = 0; for (int i = 0; i < i_Board.GetSize; i++) { for (int j = 0; j < i_Board.GetSize; j++) { if (i_Board.GetSquare(i, j).GetChecker() != null) { if (i_Board.GetSquare(i, j).GetChecker().Color == m_Color) { if (i_Board.GetSquare(i, j).GetChecker().IsKing) { kingCounter++; } else { pawnCounter++; } } } } } m_NumOfPawnAlive = kingCounter + pawnCounter; m_PlayerScore = (4 * kingCounter) + pawnCounter; }
private void moveChecker(GameBoard i_Board, Point i_Start, Point i_End) { bool isKing = i_Board.GetChecker(i_Start.GetX(), i_Start.GetY()).IsKing; char color = i_Board.GetChecker(i_Start.GetX(), i_Start.GetY()).Color; i_Board.GetSquare(i_End.GetX(), i_End.GetY()).SetChecker(color, isKing); i_Board.GetSquare(i_Start.GetX(), i_Start.GetY()).ClearSquare(); }
private void checkMoveOptionsOfaGivenPawn(GameBoard i_Matrix, Point i_Start, char i_PlayerColor, ref List <Point> o_ResDestanation)// returns start,end,start,end.. { Square startSquare = null; Square endSquare = null; int x = i_Start.GetX(); int y = i_Start.GetY(); int matrixSize = i_Matrix.GetSize; startSquare = i_Matrix.GetSquare(x, y); // white move or black king if ((i_PlayerColor == (char)eCheckerGame.WhitePlayer) || (startSquare.GetChecker().IsKing)) { if ((x + 1 < matrixSize) && (y - 1 >= 0)) { endSquare = i_Matrix.GetSquare(x + 1, y - 1); if (endSquare.GetChecker() == null) { o_ResDestanation.Add(new Point(x, y)); o_ResDestanation.Add(new Point(x + 1, y - 1)); } } if ((x + 1 < matrixSize) && (y + 1 < matrixSize)) { endSquare = i_Matrix.GetSquare(x + 1, y + 1); if (endSquare.GetChecker() == null) { o_ResDestanation.Add(new Point(x, y)); o_ResDestanation.Add(new Point(x + 1, y + 1)); } } } if ((i_PlayerColor == (char)eCheckerGame.BlackPlayer) || (startSquare.GetChecker().IsKing)) { if ((y - 1 >= 0) && (x - 1 >= 0)) { endSquare = i_Matrix.GetSquare(x - 1, y - 1); if (endSquare.GetChecker() == null) { o_ResDestanation.Add(new Point(x, y)); o_ResDestanation.Add(new Point(x - 1, y - 1)); } } if ((x - 1 >= 0) && (y + 1 < i_Matrix.GetSize)) { endSquare = i_Matrix.GetSquare(x - 1, y + 1); if (endSquare.GetChecker() == null) { o_ResDestanation.Add(new Point(x, y)); o_ResDestanation.Add(new Point(x - 1, y + 1)); } } } }
public void MakeAMove(GameBoard i_Board, Point i_StartCord, Point i_EndCord, char i_PlayerColor) { m_EatAgain = false; m_DidCapture = false; Square startSquare = i_Board.GetSquare(i_StartCord.GetX(), i_StartCord.GetY()); Square endSquare = i_Board.GetSquare(i_EndCord.GetX(), i_EndCord.GetY()); bool isKing; bool ValidMove = true; try { isStartSquareNotEmpty(startSquare); checkIfStartPawnBelongsToPlayer(startSquare.GetChecker(), i_PlayerColor); checkIfDestIsEmpty(endSquare); ifThereIsACaptureMoveThenCheckIfUserChoseIt(i_Board, i_StartCord, i_EndCord, i_PlayerColor); isKing = startSquare.GetChecker().IsKing; if (!isKing) { if (i_PlayerColor == (char)eCheckerGame.WhitePlayer) { ValidMove = ifLegalMoveManThenMove(i_Board, i_StartCord, i_EndCord, i_PlayerColor, 1, 1); // WhiteMove } else { ValidMove = ifLegalMoveManThenMove(i_Board, i_StartCord, i_EndCord, i_PlayerColor, -1, 1); // BlackMove } } else { if (!ifLegalMoveManThenMove(i_Board, i_StartCord, i_EndCord, i_PlayerColor, 1, 1)) { ValidMove = ifLegalMoveManThenMove(i_Board, i_StartCord, i_EndCord, i_PlayerColor, -1, 1); // BlackMove } } if (!ValidMove) { throw new ArgumentException("Error: unvalid move"); } checkWeHaveANewKing(i_EndCord, endSquare, i_Board.GetSize); if (m_DidCapture) { ifPawnCanEatAgain(i_Board, i_EndCord, i_PlayerColor); } } catch (Exception ex) { throw ex; } }
private bool ifLegalMoveManThenMove(GameBoard i_Board, Point i_Start, Point i_End, char i_PlayerColor, int addX, int addY) { int startX = i_Start.GetX(); int startY = i_Start.GetY(); int endX = i_End.GetX(); int endY = i_End.GetY(); bool res = true; if (((startX + addX == endX) && (startY - addY == endY)) || ((startX + addX == endX) && (startY + addY == endY))) { moveChecker(i_Board, i_Start, i_End); m_DidCapture = false; } else if ((startX + (addX * 2) == endX) && (startY + (addY * 2) == endY)) { if (i_Board.GetChecker(startX + addX, startY + addY) != null && (i_Board.GetChecker(startX + addX, startY + addY).Color != i_PlayerColor)) { moveChecker(i_Board, i_Start, i_End); m_DidCapture = true; i_Board.GetSquare(startX + addX, startY + addY).ClearSquare(); // remove the eaten one } else { res = false; } } else if ((startX + (addX * 2) == endX) && (startY - (addY * 2) == endY)) { if (i_Board.GetChecker(startX + addX, startY - addY) != null && (i_Board.GetChecker(startX + addX, startY - addY).Color != i_PlayerColor)) { moveChecker(i_Board, i_Start, i_End); m_DidCapture = true; i_Board.GetSquare(startX + addX, startY - addY).ClearSquare(); // remove the eaten one } else { res = false; } } else { res = false; } return(res); }
private List <Point> captureOptions(GameBoard i_Matrix, char i_PlayerColor) { List <Point> optionsArray = new List <Point>(1); Square currSquare; for (int i = 0; i < i_Matrix.GetSize; i++) { for (int j = 0; j < i_Matrix.GetSize; j++) { currSquare = i_Matrix.GetSquare(j, i); if ((currSquare.GetChecker() != null) && (currSquare.GetChecker().Color == i_PlayerColor)) { checkCaptureOptionsOfaGivenPawn(i_Matrix, new Point(j, i), i_PlayerColor, ref optionsArray); } } } if (optionsArray.Count == 0) { optionsArray = null; } return(optionsArray); }
private void checkCaptureOptionsOfaGivenPawn(GameBoard i_Matrix, Point i_Start, char i_PlayerColor, ref List <Point> o_ResDestanation)// returns start,end,start,end.. { Square checkSquare1 = null; Square checkSquare2 = null; int x = i_Start.GetX(); int y = i_Start.GetY(); int matrixSize = i_Matrix.GetSize; checkSquare1 = i_Matrix.GetSquare(x, y); if (checkSquare1.GetChecker() != null) { if ((i_PlayerColor == (char)eCheckerGame.WhitePlayer) || (checkSquare1.GetChecker().IsKing)) { if ((x + 2 < matrixSize) && (y - 2 >= 0)) { checkSquare1 = i_Matrix.GetSquare(x + 1, y - 1); checkSquare2 = i_Matrix.GetSquare(x + 2, y - 2); if (checkSquare1.GetChecker() != null && checkSquare2.GetChecker() == null && (checkSquare1.GetChecker().Color != i_PlayerColor)) { o_ResDestanation.Add(new Point(x, y)); o_ResDestanation.Add(new Point(x + 2, y - 2)); } } if ((x + 2 < matrixSize) && (y + 2 < matrixSize)) { checkSquare1 = i_Matrix.GetSquare(x + 1, y + 1); checkSquare2 = i_Matrix.GetSquare(x + 2, y + 2); if (checkSquare1.GetChecker() != null && checkSquare2.GetChecker() == null && (checkSquare1.GetChecker().Color != i_PlayerColor)) { o_ResDestanation.Add(new Point(x, y)); o_ResDestanation.Add(new Point(x + 2, y + 2)); } } } checkSquare1 = i_Matrix.GetSquare(x, y); if ((i_PlayerColor == (char)eCheckerGame.BlackPlayer) || (checkSquare1.GetChecker().IsKing)) { if ((y + 2 < matrixSize) && (x - 2 >= 0)) { checkSquare1 = i_Matrix.GetSquare(x - 1, y + 1); checkSquare2 = i_Matrix.GetSquare(x - 2, y + 2); if (checkSquare1.GetChecker() != null && checkSquare2.GetChecker() == null && (checkSquare1.GetChecker().Color != i_PlayerColor)) { o_ResDestanation.Add(new Point(x, y)); o_ResDestanation.Add(new Point(x - 2, y + 2)); } } if ((x - 2 >= 0) && (y - 2 >= 0)) { checkSquare1 = i_Matrix.GetSquare(x - 1, y - 1); checkSquare2 = i_Matrix.GetSquare(x - 2, y - 2); if (checkSquare1.GetChecker() != null && checkSquare2.GetChecker() == null && (checkSquare1.GetChecker().Color != i_PlayerColor)) { o_ResDestanation.Add(new Point(x, y)); o_ResDestanation.Add(new Point(x - 2, y - 2)); } } } } }