public bool move( Play _play ) { if (!isPtInTable(_play.oldPosition)) return false; if (!isPtInTable(_play.newPosition)) return false; TableSquare oldSquare = m_table[_play.oldPosition.X, _play.oldPosition.Y]; TableSquare newSquare = m_table[_play.newPosition.X, _play.newPosition.Y]; Piece pieceEaten = null; if( oldSquare.Piece == null ) { // There's no piece moving in this play return false; } // Now we double check if this play is possible //if( !oldSquare.Piece.vetPossibleMovements.Contains( _play.newPosition ) ) //{ // // Trying to make a move to an invalid position // return false; //} if( newSquare.Piece != null ) { // Check if a piece is eaten pieceEaten = newSquare.Piece; if( oldSquare.Piece.isBlack == pieceEaten.isBlack ) { // Trying to eat a piece of your own team return false; } } // Move the piece. newSquare.Piece = oldSquare.Piece; oldSquare.Piece = null; newSquare.Piece.Position = _play.newPosition; // Hold the eaten piece for later count or something. if( pieceEaten != null ) { pieceEaten.SetPosition( -1, -1 ); if( pieceEaten.isBlack ) { m_vecBlackPieces.Remove( pieceEaten ); m_vecDeadBlackPieces.Add( pieceEaten ); } else { m_vecWhitePieces.Remove( pieceEaten ); m_vecDeadWhitePieces.Add( pieceEaten ); } if( pieceEaten is King ) m_bCheckMate = true; } // Calculate the next possible movements if( newSquare.Piece.isBlack ) { //calculateBlackPiecesPosition( false ); calculateWhitePiecesPosition( m_bVerifyCheck ); } else { //calculateWhitePiecesPosition( false ); calculateBlackPiecesPosition( m_bVerifyCheck ); } // Verify if the game is over (checkmate) if( m_bVerifyCheck ) { m_bCheckMate = isInCheckMate( !newSquare.Piece.isBlack ); } return true; }
public static Play operator -(Table newTable, Table oldTable) { // return the play that represents the difference between these 2 tables. TableSquare tableSquareNew1 = null, tableSquareOld1 = null, tableSquareNew2 = null, tableSquareOld2 = null; Point pos1 = new Point(), pos2 = new Point(); bool bFinish = false; for (int nRow = 0; nRow < TABLE_SIZE; ++nRow) { for (int nCol = 0; nCol < TABLE_SIZE; ++nCol) { TableSquare tableSquareTempNew = newTable.m_table[nRow, nCol], tableSquareTempOld = oldTable.m_table[nRow, nCol]; if ((tableSquareTempNew.Piece == null) && (tableSquareTempOld.Piece == null)) continue; if ((tableSquareTempNew.Piece != null) && (tableSquareTempOld.Piece != null) && tableSquareTempNew.Piece.Equals(tableSquareTempOld.Piece)) continue; // The piece changed between these two tables. // Save the positions. if ((tableSquareNew1 == null) && (tableSquareOld1 == null)) { tableSquareNew1 = tableSquareTempNew; tableSquareOld1 = tableSquareTempOld; pos1.X = nRow; pos1.Y = nCol; } else { tableSquareNew2 = tableSquareTempNew; tableSquareOld2 = tableSquareTempOld; pos2.X = nRow; pos2.Y = nCol; bFinish = true; break; } } if (bFinish) break; } if (!bFinish) return null; Play play = new Play(); if ((tableSquareOld1.Piece != null) && (tableSquareNew1.Piece == null)) { play.oldPosition = pos1; play.newPosition = pos2; } else if ((tableSquareOld2.Piece != null) && (tableSquareNew2.Piece == null)) { play.oldPosition = pos2; play.newPosition = pos1; } return play; }
private List<Table> generateTables(Table table) { List<Table> lstTables = new List<Table>(); for (int nRow = 0; nRow < table.TableSize; ++nRow) { for (int nCol = 0; nCol < table.TableSize; ++nCol) { Table.TableSquare tableSquare = table.getTableSquare(nRow, nCol); // Verify if the is a piece in this square. if (tableSquare.Piece == null) continue; // Verify if the piece is mine. if (tableSquare.Piece.isBlack != m_bTeamTemp) continue; List<Point> lstPositions = tableSquare.Piece.vetPossibleMovements; foreach (Point position in lstPositions) { Play play = new Play(); play.oldPosition = new Point(nRow, nCol); play.newPosition = position; Table newTable = table + play; lstTables.Add(newTable); } } } return lstTables; }
protected bool isValidMove( Point _newPosition ) { Play play = new Play( ); play.oldPosition = m_position; play.newPosition = _newPosition; m_parentTable.VerifyCheck = false; Table newTable = m_parentTable + play; m_parentTable.VerifyCheck = true; if( newTable.isInCheck( m_bBlack ) ) { return false; } return true; }
protected bool ClickOnTarget(Rectangle rectMouse) { if (m_listTargetsMovements == null) return false; if (m_listTargetsMovements.Count == 0) return false; for (int index = 0; index < m_listTargetsMovements.Count; index++ ) { Point ptSquare = m_listTargetsMovements[index]; Rectangle rectTarget = m_gameTable.getTableSquare(ptSquare.X, ptSquare.Y).BoundingBox; if(rectTarget.Intersects(rectMouse) ) { bool bKilled = false; if (m_gameTable.getTableSquare(ptSquare.X, ptSquare.Y).Piece != null) { bKilled = true; } Play play = new Play(); play.newPosition = ptSquare; play.oldPosition = m_pointSquareSelection; m_gameTable.move(play); m_soundKlik.Play(); if (bKilled) m_soundKill.Play(); return true; } } return false; }