Course project for CSE 121: Programming 1, Alexandria University Faculty of Engineering,
Computer and Systems Engineering Dept .
Chess console application made in C programming language. It provides the essential blocks found in any chess game, which are:
- A labelled chess board
- Two players competing against each other (Black and White)
- The special movement of each piece
- The core mechanics of the game (Promotion, Stalemate, and Checkmate)
In addition to the mentioned mechanics, some additional features were added to make the experience more flexible and enjoyable, such as:
- The ability to save and load your concurrent game
- The ability to undo and redo your actions
- Ramy Ahmed El Sayed
- Zyad Samy Ramadan
- Our design is mainly focused on our chess board. Our game functions in an infinite loop that breaks when one of the terminating game conditions occur (Checkmate or Stalemate).
- It checks for the user input depending on the specified player turn (Player 1 or Player 2) and, checks whether the input is valid or not.
- It then checks the piece picked and whether the target position is in the boundaries of the piece’s movements.
- Continuously, checks for a Checkmate or a Stalemate.
- Depending on the user input, various functions can occur, such as:
- Saving
- Loading
- Undo
- Redo
- Once one of the terminating conditions occur, It breaks the loop and terminates the program.
It is assumed that the number of moves will not exceed 1000, since the lack of dynamic arrays in C, and the complexity of using other data structures, which would behave like a dynamic array (e.g., Hash tables or Linked lists).
Our program mainly utilizes one data structure, which is Arrays.
We have several arrays that behave as main parameters for most of the functions, such as:
- Chess board (an 8x8 array that contains the pieces and the tiles)
- Moves (an array that records the moves done in each turn)
- Pre-undo array (records the moves that will be undone)
- Units removed (records the units removed)
- Promotion array
- Black and white king position arrays
print_board():
Our main function that prints our updated Chess board with the removed units and moved units.
is_legalmove():
Checks if the move is valid to proceed to modify the main chess board array, and checks whether the moves will activate any special game states (Promotion/Stalemate/Checkmate).
modify():
Modifies our Chess board, according to the user input, after checking whether it is valid or not. It is also used when redoing a move.
is_check():
Keeps checking on the king’s current position (Black or White depending on the current turn) and checks whether any unit of the opposing team can capture the king, if any piece can capture it; then the king is in check.
is_checkmate():
If the is_check() function shows that a king is in check, then check if it can be denied by the threatened king or his pieces. If it cannot be denied, then a checkmate has been concluded and the game ends.
is_stalemate():
If a stalemate is reached, the game terminates with a draw.
When the user starts the .exe, he is prompted by this screen:
The screen contains:
- The chess board.
- The lost units.
- Instructions for other functions.
- Player’s turn.
- Move to be inserted.
User input:
The user is required to insert his move in this format, namely (A2A3/a2a3):
After inserting the input, the user is prompted with an updated chess board:
Undo/Redo:
If the user inserts ‘u’, the game undoes his last action. Example:
Player 1 will eat Player 2’s pawn located in B5:
The pawn is now added to Player 2’s lost units. If we want to undo the action, we insert ‘u’:
Which will result in:
And to redo what we just undid, the user can simply insert ‘d’:
Save/Load:
Supposing the player wants to save his chess match to continue later, for example:
If the player inserts ‘s’ in the input, the grid board is saved in a text file:
Which will look like this:
The text file contains the grid board, black king’s position, white king’s position, and the turn at which it was saved.
If the player were to proceed and close the game and re-open, then type “l” as an input, the game will load and will proceed as usual:
The loaded game:
Sample run 1
Sample run 2
3. Save/Load
4. Stalemate