Skip to content

Chess piece movement simulator in C - Educational project demonstrating different loop structures and recursion

License

Notifications You must be signed in to change notification settings

rafaumeu/xadrez-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

26 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฎ Chess Piece Movement Simulator

C License: MIT CodeQL GitHub last commit GitHub issues GitHub pull requests Educational

๐Ÿ“– About

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:

Basic Level

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 and while) to move in an "L" shape (2 squares down, 1 square left)

Master Level

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)

๐Ÿš€ Features

  • 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

๐Ÿ“‹ Requirements

  • GCC Compiler
  • Git
  • Operating System: Windows, Linux, or macOS

๐Ÿ› ๏ธ Installation

  1. Clone the repository:
git clone <repository-url>
cd xadrez-c
  1. Compile the program:
gcc -o xadrez main.c tabuleiro.c src/pecas/*.c -Iinclude
  1. Run the program:
./xadrez

๐Ÿ“ Project Structure

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

๐ŸŽฏ Usage

When you run the program, you'll be prompted to choose a challenge level:

  1. 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)
  2. 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

๐Ÿ’ป Code Examples

Recursive Implementation (Rook)

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);
}

Complex Loops Implementation (Knight)

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");
}

๐Ÿค Contributing

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.


Contributing

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.

Security

For information about security policies and how to report vulnerabilities, please see our Security Policy.

Made with โค๏ธ by Rafael Dias Zendron

LinkedIn GitHub