public Space() { checker = null; symbol = '■'; // ■ }
private bool MoveChecker(int[] coordinates) { bool isMoved = false; if (coordinates != null) { int x1 = coordinates[0]; int y1 = coordinates[1]; int x2 = coordinates[2]; int y2 = coordinates[3]; Checker target = FindChecker(x2, y2); Checker checker = FindChecker(x1, y1); if (checker != null && target == null) { if (checker.side == "player") { if (checker.isDame) { if ((x2 == x1 - 1 || x2 == x1 + 1) && (y2 == y1 + 1 || y2 == y1 - 1)) { spaces[x2, y2] = spaces[x1, y1]; spaces[x1, y1] = new Space(); AddToLog(playerName + " lépett a(z) " + (x2 + 1) + CoordinateConverter(y2) + " pozícióra."); isMoved = true; } } else { if (x2 == x1 - 1 && x2 < x1 && (y2 == y1 + 1 || y2 == y1 - 1)) { spaces[x2, y2] = spaces[x1, y1]; spaces[x1, y1] = new Space(); AddToLog(playerName + " lépett a(z) " + (x2 + 1) + CoordinateConverter(y2) + " pozícióra."); if (x2 == 0 && !spaces[x2, y2].checker.isDame) { spaces[x2, y2].checker.isDame = true; AddToLog("A bábud dáma lett!"); } isMoved = true; } else { AddToLog("Nem lehettséges lépés!"); isMoved = false; } } } else { isMoved = false; AddToLog("Nem található a bábu!"); } } else if (checker != null && target != null && target.side == "computer") { if (checker.side == "player") { if (checker.isDame) { if (x2 < x1) { if (y2 == y1 + 1 && FindChecker(x1 - 2, y1 + 2) == null) { spaces[x2 - 1, y2 + 1] = spaces[x1, y1]; spaces[x2, y2] = new Space(); spaces[x1, y1] = new Space(); isMoved = true; AddToLog(playerName + " lépett a(z) " + (x2 - 2) + CoordinateConverter(y2 + 1) + " pozícióra."); } else if (y2 == y1 - 1 && FindChecker(x1 - 2, y1 - 2) == null) { spaces[x2 - 1, y2 - 1] = spaces[x1, y1]; spaces[x2, y2] = new Space(); spaces[x1, y1] = new Space(); isMoved = true; AddToLog(playerName + " lépett a(z) " + (x2 - 2) + CoordinateConverter(y2 - 1) + " pozícióra."); } else { AddToLog("Nem lehettséges a lépés!"); } } else if (x2 > x1) { if (y2 == y1 + 1 && FindChecker(x1 + 2, y1 + 2) == null) { spaces[x2 + 1, y2 + 1] = spaces[x1, y1]; spaces[x2, y2] = new Space(); spaces[x1, y1] = new Space(); isMoved = true; AddToLog(playerName + " lépett a(z) " + (x2 + 2) + CoordinateConverter(y2 + 1) + " pozícióra."); } else if (y2 == y1 - 1 && FindChecker(x1 + 2, y1 - 2) == null) { spaces[x2 + 1, y2 - 1] = spaces[x1, y1]; spaces[x2, y2] = new Space(); spaces[x1, y1] = new Space(); isMoved = true; AddToLog(playerName + " lépett a(z) " + (x2 + 2) + CoordinateConverter(y2 - 1) + " pozícióra."); } } } else { if (x2 < x1) { if (y2 == y1 + 1 && FindChecker(x1 - 2, y1 + 2) == null) { spaces[x2 - 1, y2 + 1] = spaces[x1, y1]; spaces[x2, y2] = new Space(); spaces[x1, y1] = new Space(); if (x2 - 1 == 0 && !spaces[x2 - 1, y2 + 1].checker.isDame) { spaces[x2 - 1, y2 + 1].checker.isDame = true; AddToLog("A bábud dáma lett!"); } isMoved = true; AddToLog(playerName + " lépett a(z) " + (x2) + CoordinateConverter(y2 + 1) + " pozícióra."); } else if (y2 == y1 - 1 && FindChecker(x1 - 2, y1 - 2) == null) { spaces[x2 - 1, y2 - 1] = spaces[x1, y1]; spaces[x2, y2] = new Space(); spaces[x1, y1] = new Space(); if (x2 - 1 == 0 && !spaces[x2 - 1, y2 - 1].checker.isDame) { spaces[x2 - 1, y2 - 1].checker.isDame = true; AddToLog("A bábud dáma lett!"); } isMoved = true; AddToLog(playerName + " lépett a(z) " + (x2) + CoordinateConverter(y2 - 1) + " pozícióra."); } else { isMoved = false; } } else { AddToLog("Nem lehettséges a lépés!"); isMoved = false; } } } } else if (checker == null) { AddToLog("Nem található ilyen bábu!"); } RefreshScreen(); return(isMoved); } return(false); }
private bool ComputerCheckerController() { bool isMoved = false; int tries = 0; Checker[] checkers = new Checker[CountChecker("computer")]; for (int i = 0; i < checkers.Length; i++) { for (int x = 0; x < spaces.GetLength(0); x++) { for (int y = 0; y < spaces.GetLength(1); y++) { if (spaces[x, y].checker != null && spaces[x, y].checker.side == "computer" && !checkers.Contains(spaces[x, y].checker)) { checkers[i] = spaces[x, y].checker; } } } } Random rnd = new Random(); while (!isMoved && tries <= checkers.Length) { int randomIndex = rnd.Next(0, (checkers.Length - 1)); while (checkers[randomIndex] == null) { randomIndex = rnd.Next(0, (checkers.Length - 1)); } Checker checker = checkers[randomIndex]; int x1 = checker.x; int y1 = checker.y; Space checkSpaceDownLeft = null; Space checkSpaceDownRight = null; Space checkSpaceUpLeft = null; Space checkSpaceUpRight = null; Space checkSpaceDownLeftToJump = null; Space checkSpaceDownRightToJump = null; Space checkSpaceUpLeftToJump = null; Space checkSpaceUpRightToJump = null; if ((x1 + 1) < 8 && (y1 - 1) > -1) // checkSpaceDownLeft { checkSpaceDownLeft = spaces[x1 + 1, y1 - 1]; if ((x1 + 2) < 8 && (y1 - 2) > -1) { checkSpaceDownLeftToJump = spaces[x1 + 2, y1 - 2]; } } if ((x1 + 1) < 8 && (y1 + 1) < 8) //checkSpaceDownRight { checkSpaceDownRight = spaces[x1 + 1, y1 + 1]; if ((x1 + 2) < 8 && (y1 + 2) < 8) { checkSpaceDownRightToJump = spaces[x1 + 2, y1 + 2]; } } if (checker.isDame && (x1 - 1) > -1 && (y1 - 1) > -1) // checkSpaceUpLeft { checkSpaceUpLeft = spaces[x1 - 1, y1 - 1]; if ((x1 - 2) > -1 && (y1 - 2) > -1) { checkSpaceUpLeftToJump = spaces[x1 - 2, y1 - 2]; } } if (checker.isDame && (x1 - 1) > -1 && (y1 + 1) < 8) // checkSpaceUpRight { checkSpaceUpRight = spaces[x1 - 1, y1 + 1]; if ((x1 - 2) > -1 && (y1 + 2) < 8) { checkSpaceUpRightToJump = spaces[x1 - 2, y1 + 2]; } } //Check for player checker in neighbor if (checkSpaceDownLeft != null && checkSpaceDownLeft.checker != null && checkSpaceDownLeft.checker.side == "player" && checkSpaceDownLeftToJump != null && checkSpaceDownLeftToJump.checker == null) { MoveComputerChecker(4, x1, y1); isMoved = true; } else if (checkSpaceDownRight != null && checkSpaceDownRight.checker != null && checkSpaceDownRight.checker.side == "player" && checkSpaceDownRightToJump != null && checkSpaceDownRightToJump.checker == null) { MoveComputerChecker(5, x1, y1); isMoved = true; } else if (checkSpaceUpLeft != null && checkSpaceUpLeft.checker != null && checkSpaceUpLeft.checker.side == "player" && checkSpaceUpLeftToJump != null && checkSpaceUpLeftToJump.checker == null) { MoveComputerChecker(6, x1, y1); isMoved = true; } else if (checkSpaceUpRight != null && checkSpaceUpRight.checker != null && checkSpaceUpRight.checker.side == "player" && checkSpaceUpRightToJump != null && checkSpaceUpRightToJump.checker == null) { MoveComputerChecker(7, x1, y1); isMoved = true; } //Check for player checker in range else if (checkSpaceDownLeft != null && checkSpaceDownLeft.checker == null && checkSpaceDownLeftToJump != null && checkSpaceDownLeftToJump.checker != null && checkSpaceDownLeftToJump.checker.side == "player") { MoveComputerChecker(0, x1, y1); isMoved = true; } else if (checkSpaceDownRight != null && checkSpaceDownRight.checker == null && checkSpaceDownRightToJump != null && checkSpaceDownRightToJump.checker != null && checkSpaceDownRightToJump.checker.side == "player") { MoveComputerChecker(1, x1, y1); isMoved = true; } else if (checkSpaceUpLeft != null && checkSpaceUpLeft.checker == null && checkSpaceUpLeftToJump != null && checkSpaceUpLeftToJump.checker != null && checkSpaceUpLeftToJump.checker.side == "player") { MoveComputerChecker(2, x1, y1); isMoved = true; } else if (checkSpaceUpRight != null && checkSpaceUpRight.checker == null && checkSpaceUpRightToJump != null && checkSpaceUpRightToJump.checker != null && checkSpaceUpRightToJump.checker.side == "player") { MoveComputerChecker(3, x1, y1); isMoved = true; } //random moves else if (checkSpaceDownLeft != null && checkSpaceDownLeft.checker == null && checkSpaceDownRight != null && checkSpaceDownRight.checker == null && checkSpaceUpLeft != null && checkSpaceUpLeft.checker == null && checkSpaceUpRight != null && checkSpaceUpRight.checker == null) { MoveComputerChecker(rnd.Next(0, 4), x1, y1); isMoved = true; } else if (checkSpaceDownLeft != null && checkSpaceDownLeft.checker == null && checkSpaceDownRight != null && checkSpaceDownRight.checker == null) { MoveComputerChecker(rnd.Next(0, 2), x1, y1); isMoved = true; } else if (checkSpaceUpLeft != null && checkSpaceUpLeft.checker == null && checkSpaceUpRight != null && checkSpaceUpRight.checker == null) { MoveComputerChecker(rnd.Next(2, 4), x1, y1); isMoved = true; } //direct moves else if (checkSpaceDownLeft != null && checkSpaceDownLeft.checker == null) { MoveComputerChecker(0, x1, y1); isMoved = true; } else if (checkSpaceDownRight != null && checkSpaceDownRight.checker == null) { MoveComputerChecker(1, x1, y1); isMoved = true; } else if (checkSpaceUpLeft != null && checkSpaceUpLeft.checker == null) { MoveComputerChecker(2, x1, y1); isMoved = true; } else if (checkSpaceUpRight != null && checkSpaceUpRight.checker == null) { MoveComputerChecker(3, x1, y1); isMoved = true; } //If unable to move else { tries++; isMoved = false; checkers[randomIndex] = null; } } return(isMoved); }