/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // TODO: Add your update logic here if (boardInfo.WhoseMove == ChessPieceColor.Black && blackIsAI) { ChessEngine.EngineMove(boardInfo); } else { MouseState mouseState = Mouse.GetState(); if (mouseState.LeftButton == ButtonState.Pressed) { int xPos = (int)(mouseState.X / Constants.SquareSize); int yPos = (int)(mouseState.Y / Constants.SquareSize); if (xPos < Constants.NumberOfFiles && yPos < Constants.NumberOfRanks) { if (selectedIndex > boardInfo.pieces.Length) { byte index = (byte)(yPos * Constants.NumberOfFiles + xPos); if (boardInfo.pieces[index] != null && boardInfo.pieces[index].PieceColor == boardInfo.WhoseMove) { selectedIndex = index; boardInfo.pieces[index].isSelected = true; } } else { byte index = (byte)(yPos * Constants.NumberOfFiles + xPos); if (index != selectedIndex) { //SourceIndex has no piece if (boardInfo.pieces[selectedIndex] == null) { return; } //Select new piece if same color if (boardInfo.pieces[index] != null && boardInfo.pieces[index].PieceColor == boardInfo.WhoseMove) { boardInfo.pieces[selectedIndex].isSelected = false; boardInfo.pieces[index].isSelected = true; selectedIndex = index; } //Check if this is infact a valid move if (!ChessEngine.IsValidMove(boardInfo, selectedIndex, index)) { return; } ChessEngine.MovePiece(boardInfo, selectedIndex, index); selectedIndex = 99; } else { boardInfo.pieces[selectedIndex].isSelected = false; selectedIndex = 99; } } } else if (selectedIndex < boardInfo.pieces.Length) { boardInfo.pieces[selectedIndex].isSelected = false; selectedIndex = 99; } } } base.Update(gameTime); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } if (boardInfo.WhoseMove == ChessPieceColor.Black && blackIsAI) { ++waitASecond; if (waitASecond > 5) { waitASecond = 0; ChessEngine.EngineMove(boardInfo); } /* * if(boardHistory.Count >= 20) * { * boardHistory.RemoveAt(0); * } * boardHistory.Add(new ChessBoard(boardInfo)); */ } else { ++waitASecond; if (waitASecond > 5) { waitASecond = 0; ChessEngine.EngineMove(boardInfo); } /* * MouseState state = Mouse.GetState(); * if (state.LeftButton == ButtonState.Pressed) * { * int xPos = (int)(state.X / Constants.SquareSize); * int yPos = (int)(state.Y / Constants.SquareSize); * * if (xPos < Constants.NumberOfFiles && yPos < Constants.NumberOfRanks) * { * if (selectedIndex > boardInfo.pieces.Length) * { * byte index = (byte)(yPos * Constants.NumberOfFiles + xPos); * if (boardInfo.pieces[index] != null && boardInfo.pieces[index].PieceColor == boardInfo.WhoseMove) * { * selectedIndex = index; * } * } * else * { * byte index = (byte)(yPos * Constants.NumberOfFiles + xPos); * if (index != selectedIndex) * { * //SourceIndex has no piece * if (boardInfo.pieces[selectedIndex] == null) * { * return; * } * * //Select new piece if same color * if (boardInfo.pieces[index] != null && boardInfo.pieces[index].PieceColor == boardInfo.WhoseMove) * { * selectedIndex = index; * } * * //Check if this is infact a valid move * if (!ChessEngine.IsValidMove(boardInfo, selectedIndex, index)) * { * return; * } * * ChessEngine.MovePiece(boardInfo, selectedIndex, index); * selectedIndex = 99; * } * } * } * else if(undoRectangle.Contains(new Point(state.X,state.Y))) * { * System.Diagnostics.Debug.WriteLine("UNDO PRESSED"); * selectedIndex = 99; * if(boardHistory.Count > 1) * { * boardHistory.RemoveAt(boardHistory.Count - 1); * } * if(boardHistory.Count > 0) * { * boardInfo = new ChessBoard(boardHistory[boardHistory.Count - 1]); * PieceValidMoves.GenerateValidMoves(boardInfo); * } * } * else if (selectedIndex < boardInfo.pieces.Length) * { * selectedIndex = 99; * } * } */ } base.Update(gameTime); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } if (boardInfo.WhoseMove == ChessPieceColor.Black && blackIsAI) { ChessEngine.EngineMove(boardInfo); } else { TouchCollection touchCollection = TouchPanel.GetState(); foreach (TouchLocation tl in touchCollection) { if (tl.State == TouchLocationState.Released) { int xPos = (int)(tl.Position.X / Constants.SquareSize); int yPos = (int)(tl.Position.Y / Constants.SquareSize); if (xPos < Constants.NumberOfFiles && yPos < Constants.NumberOfRanks) { if (selectedIndex > boardInfo.pieces.Length) { byte index = (byte)(yPos * Constants.NumberOfFiles + xPos); if (boardInfo.pieces[index] != null && boardInfo.pieces[index].PieceColor == boardInfo.WhoseMove) { selectedIndex = index; boardInfo.pieces[index].isSelected = true; break; } } else { byte index = (byte)(yPos * Constants.NumberOfFiles + xPos); if (index != selectedIndex) { //SourceIndex has no piece if (boardInfo.pieces[selectedIndex] == null) { return; } //Select new piece if same color if (boardInfo.pieces[index] != null && boardInfo.pieces[index].PieceColor == boardInfo.WhoseMove) { boardInfo.pieces[selectedIndex].isSelected = false; boardInfo.pieces[index].isSelected = true; selectedIndex = index; } //Check if this is infact a valid move if (!ChessEngine.IsValidMove(boardInfo, selectedIndex, index)) { return; } ChessEngine.MovePiece(boardInfo, selectedIndex, index); selectedIndex = 99; break; } else { boardInfo.pieces[selectedIndex].isSelected = false; selectedIndex = 99; } } } else if (selectedIndex < boardInfo.pieces.Length) { boardInfo.pieces[selectedIndex].isSelected = false; selectedIndex = 99; } } } } base.Update(gameTime); }