// ClickHandler for piece. private void Piece_Click(object sender, EventArgs e) { int[] arr; PictureBox b = (PictureBox)sender; if ((game.GetAIState() && playerColour == game.GetTurn()) || !game.GetAIState()) { if (!didChoose) { if (game.GetPiece(b.Location.Y / 57, b.Location.X / 57) != null && game.GetPiece(b.Location.Y / 57, b.Location.X / 57).GetColour() == game.GetTurn()) { srcX = b.Location.Y / 57; srcY = b.Location.X / 57; didChoose = true; // Selects the piece. this.piecesImg[srcX, srcY].Image = ImageHandler.PlaceImage(game.GetPiece(srcX, srcY).GetColour(), game.GetPiece(srcX, srcY).IsDux(), true); } } else { if (game.GetPiece(b.Location.Y / 57, b.Location.X / 57) != null && game.GetPiece(b.Location.Y / 57, b.Location.X / 57).GetColour() == game.GetTurn()) { // Reverts the previous selected piece. this.piecesImg[srcX, srcY].Image = ImageHandler.PlaceImage(game.GetPiece(srcX, srcY).GetColour(), game.GetPiece(srcX, srcY).IsDux(), false); srcX = b.Location.Y / 57; srcY = b.Location.X / 57; // Selects the piece. this.piecesImg[srcX, srcY].Image = ImageHandler.PlaceImage(game.GetPiece(srcX, srcY).GetColour(), game.GetPiece(srcX, srcY).IsDux(), true); } else if (game.GetPiece(b.Location.Y / 57, b.Location.X / 57) == null && game.UpdateBoard(srcX, srcY, b.Location.Y / 57, b.Location.X / 57)) { UpdateBoard(srcX, srcY, b.Location.Y / 57, b.Location.X / 57); didChoose = false; } } } if (game.GetTurn() == game.GetPlayerColour() * -1 && (game.GetAIState() && !didChoose)) { Application.DoEvents(); arr = AI.MakeTurn(game, 0); if (game.UpdateBoard(arr[0], arr[1], arr[2], arr[3])) { UpdateBoard(arr[0], arr[1], arr[2], arr[3]); } } }
// Updates the board with moves, deaths, etc. public void UpdateBoard(int srcX, int srcY, int dstX, int dstY) { if (game.GetTurn() == 0) { this.msgBox.Text = msg; } this.piecesImg[srcX, srcY].Image = null; for (int i = -1; i < 2; i += 2) { try { this.piecesImg[dstX + i, dstY].Image = game.GetPiece(dstX + i, dstY) == null ? null : ImageHandler.PlaceImage(game.GetPiece(dstX + i, dstY).GetColour(), game.GetPiece(dstX + i, dstY).IsDux(), false); } catch (System.IndexOutOfRangeException) { } try { this.piecesImg[dstX, dstY + i].Image = game.GetPiece(dstX, dstY + i) == null ? null : ImageHandler.PlaceImage(game.GetPiece(dstX, dstY + i).GetColour(), game.GetPiece(dstX, dstY + i).IsDux(), false); } catch (System.IndexOutOfRangeException) { } } this.piecesImg[dstX, dstY].Image = ImageHandler.PlaceImage(game.GetPiece(dstX, dstY).GetColour(), game.GetPiece(dstX, dstY).IsDux(), false); }
// Initialy prints the board. private void PrintBoard() { this.piecesImg = new PictureBox[8, 8]; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { this.piecesImg[i, j] = new PictureBox(); this.piecesImg[i, j].ClientSize = new Size(52, 52); if (game.GetPiece(i, j) != null) { this.piecesImg[i, j].Image = ImageHandler.PlaceImage(game.GetPiece(i, j).GetColour(), game.GetPiece(i, j).IsDux(), false); } this.piecesImg[i, j].BackColor = Color.Transparent; this.piecesImg[i, j].Location = new Point(j * 57, i * 57); this.piecesImg[i, j].Click += new System.EventHandler(Piece_Click); this.board.Controls.Add(this.piecesImg[i, j]); } } }
// Restarts the game. private void RestartBoard() { game.Restart(); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { this.piecesImg[i, j].Image = game.GetPiece(i, j) == null ? null : ImageHandler.PlaceImage(game.GetPiece(i, j).GetColour(), game.GetPiece(i, j).IsDux(), false); } } }