This project simulates the movement of chess pieces (Rook, Bishop, Queen, and Knight) using different loop structures and recursion in C. The project has two challenge levels:
Each piece demonstrates a specific type of movement using a different loop structure:
- Rook: Uses
for
loop to move 5 squares to the right - Bishop: Uses
while
loop to move 5 squares diagonally (up and right) - Queen: Uses
do-while
loop to move 8 squares to the left - Knight: Uses nested loops (
for
andwhile
) to move in an "L" shape (2 squares down, 1 square left)
Advanced implementations using recursion and complex loops:
- Rook: Uses recursion to move 5 squares to the right
- Bishop: Implemented with both recursion and nested loops for diagonal movement
- Queen: Uses recursion to move 8 squares to the left
- Knight: Uses complex nested loops with multiple conditions to move in an "L" shape (2 squares up, 1 square right)
- Simulates four different chess pieces movements
- Implements various loop structures including nested loops
- Demonstrates recursive implementations of movement patterns
- Console-based visualization of movements
- Clean and documented code
- GCC Compiler
- Git
- Operating System: Windows, Linux, or macOS
- Clone the repository:
git clone <repository-url>
cd xadrez-c
- Compile the program:
gcc -o xadrez main.c tabuleiro.c src/pecas/*.c -Iinclude
- Run the program:
./xadrez
xadrez-c/
โโโ include/
โ โโโ pecas.h
โ โโโ tabuleiro.h
โโโ src/
โ โโโ pecas/
โ โ โโโ torre.c
โ โ โโโ bispo.c
โ โ โโโ rainha.c
โ โ โโโ cavalo.c
โ โ โโโ torre_recursivo.c
โ โ โโโ bispo_recursivo.c
โ โ โโโ rainha_recursivo.c
โ โ โโโ bispo_loops_aninhados.c
โ โ โโโ cavalo_complexo.c
โโโ tabuleiro.c
โโโ main.c
โโโ README.md
When you run the program, you'll be prompted to choose a challenge level:
-
Basic Level: Uses simple loop structures
- The Rook will move 5 squares to the right
- The Bishop will move 5 squares diagonally
- The Queen will move 8 squares to the left
- The Knight will move in an "L" shape (2 squares down, 1 square left)
-
Master Level: Uses recursion and complex loops
- The Rook will move 5 squares to the right using recursion
- The Bishop will move 5 squares diagonally using both recursion and nested loops
- The Queen will move 8 squares to the left using recursion
- The Knight will move in an "L" shape (2 squares up, 1 square right) using complex loops
void moverTorreRecursivo(int casasRestantes) {
// Caso base: quando nรฃo hรก mais casas para mover
if (casasRestantes <= 0) {
printf("\n=== Fim do movimento da TORRE (Recursivo) ===\n");
return;
}
// Caso inicial: imprime o cabeรงalho apenas na primeira chamada
if (casasRestantes == TORRE_CASAS) {
printf("\n=== Movimento da Torre (Recursivo - %d casas para a direita) ===\n",
TORRE_CASAS);
}
// Imprime a direรงรฃo do movimento
printf("Direita\n");
// Chamada recursiva para mover a prรณxima casa
moverTorreRecursivo(casasRestantes - 1);
}
void moverCavaloComplexo(int numMovimentos) {
printf("\n=== Movimento do Cavalo (Loops Complexos - %d movimentos em \"L\") ===\n",
numMovimentos);
int movimentosCompletos = 0;
// Loop externo para controlar o nรบmero total de movimentos em "L"
for (int i = 0; i < numMovimentos; i++) {
// Loop aninhado para simular o movimento em "L" (2 para cima, 1 para direita)
for (int j = 0; j < 3; j++) {
// Verifica se estamos nos primeiros dois passos (movimento para cima)
if (j < 2) {
printf("Cima\n");
}
// รltimo passo (movimento para direita)
else if (j == 2) {
printf("Direita\n");
// Incrementa o contador de movimentos completos
movimentosCompletos++;
// Se jรก completamos todos os movimentos, saรญmos do loop
if (movimentosCompletos >= numMovimentos) {
break;
}
}
// Se for o รบltimo passo do movimento em "L", reiniciamos o loop interno
if (j == 2) {
// Usamos continue para pular para a prรณxima iteraรงรฃo do loop externo
continue;
}
}
}
printf("\n=== Fim do movimento do CAVALO (Loops Complexos) ===\n");
}
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
We welcome contributions to this project! Please see CONTRIBUTING.md for details on how to contribute, our code of conduct, and the process for submitting pull requests.
For information about security policies and how to report vulnerabilities, please see our Security Policy.