Skip to content

Commit

Permalink
feat: translate and improve base code and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SidonieBouthors committed Sep 4, 2024
1 parent ca6154f commit d10ff63
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 56 deletions.
49 changes: 25 additions & 24 deletions app/public/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,73 @@
#include <vector>
#include <string>
#include <sstream>
#include <optional> // Include the optional header
#include <optional>


// Define the Piece structure similar to Java's record
struct Piece {
char pieceType;
char pieceColor;
char pieceType; // pieceType: 'M' pour pion, 'K' pour dame
char pieceColor; // pieceColor: 'W' pour blanc, 'B' pour noir
Piece(char type, char color) : pieceType(type), pieceColor(color) {}
};

struct Position {
int row;
int column;
int row; // row: ligne de la cellule
int column; // column: colonne de la cellule
};

struct Move {
Position from;
Position to;
Position from; // from: cellule de départ
Position to; // to: cellule d'arrivée
};

// Function to parse the board and return each cell's content as a vector of moves
// Fonction pour trouver les coups à jouer
std::vector<Move> findMove(const std::vector<std::vector<std::optional<Piece>>>& board, char playerColor) {
// TODO: Implement logic to find the next move
// The moves should be returned as a vector of 2-element vectors
// The first element of the sequence should be the starting cell
// Each subsequent 2-element vector should contain the x and y coordinates of the cell to move to
return {};

// TODO: Implémentez ici la logique pour trouver les coups à jouer et les retourner
// Les coups doivent être retournés sous forme d'une liste d'objets Move,
// Chaque objet Move représente un coup, avec une cellule de départ et une cellule d'arrivée
// Les classes Position(row, column) et Move(from, to) sont fournies pour vous

std::vector<Move> moves = {{{6, 1}, {5, 0}}};

return moves;
}

int main() {
// Initialize a 10x10 board with std::optional<Piece>
std::vector<std::vector<std::optional<Piece>>> board(10, std::vector<std::optional<Piece>>(10));
char playerColor;

// Read the player's color
// Lecture de la couleur du joueur depuis la console
std::cin >> playerColor;
std::cin.ignore(); // Ignore the newline character after reading playerColor

// Read input line by line for the board
// Parsage du plateau de jeu depuis la console
for (int r = 0; r < 10; r++) {
std::string line;
std::getline(std::cin, line); // Read the entire line
std::getline(std::cin, line);
std::stringstream ss(line);
std::string pieceCode;
int c = 0;

// Split the line by commas
while (std::getline(ss, pieceCode, ',')) {
if (!pieceCode.empty()) {
board[r][c] = Piece(pieceCode[0], pieceCode[1]); // Use std::optional to directly create a Piece
board[r][c] = Piece(pieceCode[0], pieceCode[1]);
} else {
board[r][c].reset(); // Use reset() to clear the optional if it's empty
board[r][c].reset();
}
c++;
}
}

// Call the findMove function to find the next move
// Appel de la fonction findMove pour trouver les coups à jouer
auto moves = findMove(board, playerColor);

if (moves.empty()) {
// No moves found
std::cerr << "No moves were returned." << std::endl;
return 0;
}

// Print the moves
// Envoi des coups trouvés à la console
for (const auto& pos : moves) {
std::cout
<< pos.from.row << pos.from.column << ","
Expand Down
39 changes: 24 additions & 15 deletions app/public/base.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,63 @@
import java.util.Scanner;

public class base {
// pieceType: 'M' pour pion, 'K' pour dame
// pieceColor: 'W' pour blanc, 'B' pour noir
private record Piece(char pieceType, char pieceColor) {}

// row: numéro de la ligne, column: numéro de la colonne
private record Position(int row, int column) {}

// from: position de départ, to: position d'arrivée
private record Move(Position from, Position to) {}

// This method parses the board and returns each cell's content as an array of moves
// Fonction pour trouver les coups à jouer
private static List<Move> findMove(Piece[][] board, char playerColor) {
// TODO: Implement logic to find the next move
// The moves should be returned as an array of 2-element arrays
// The first element of the sequence should be the starting cell
// Each subsequent 2-element array should contain the x and y coordinates of the cell to move to
return List.of(new Move(new Position(6,1), new Position(5,0)));

// TODO: Implémentez ici la logique pour trouver les coups à jouer et les retourner
// Les coups doivent être retournés sous forme d'une liste d'objets Move,
// Chaque objet Move représente un coup, avec une cellule de départ et une cellule d'arrivée
// Les classes Position(row, column) et Move(from, to) sont fournies pour vous

List<Move> moves = List.of(new Move(new Position(6,1), new Position(5,0)));

return moves;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Piece[][] board = new Piece[10][10]; // Initialize a 10x10 board
Piece[][] board = new Piece[10][10];

// Lecture de la couleur du joueur depuis la console
char playerColor = scanner.nextLine().charAt(0);

// Read input line by line
// Parsage du plateau de jeu depuis la console
for (int r = 0; r < 10; r++) {
String line = scanner.nextLine();
String[] row = line.split(","); // Split the line by commas
String[] row = line.split(",");
Piece[] pieceRow = new Piece[row.length];
for (int c = 0; c < row.length; c++) {
String pieceCode = row[c];
if (!pieceCode.isEmpty()) {
pieceRow[c] = new Piece(pieceCode.charAt(0), pieceCode.charAt(1));
} else {
pieceRow[c] = null; // Empty cells should be null
pieceRow[c] = null;
}
}

board[r] = pieceRow; // Add the row to the board
board[r] = pieceRow;
}

// Close the scanner to avoid resource leak
scanner.close();

// Call the findMove method to find the next move
// Appel de la fonction findMove pour trouver les coups à jouer
List<Move> moves = findMove(board, playerColor);

if (moves == null) {
// Error occured
return;
}

// Print the moves
// Envoi des coups trouvés à la console
for (Move move : moves) {
System.out.print(
move.from.row + "" + move.from.column + ","
Expand Down
55 changes: 38 additions & 17 deletions app/public/base.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,69 @@
class Piece:
# piece_type: M pour pion, K pour dame
# piece_color: B pour noir, W pour blanc
def __init__(self, piece_type, piece_color):
self.piece_type = piece_type
self.piece_color = piece_color

class Position:
# row: ligne de la cellule
# col: colonne de la cellule
def __init__(self, row, col):
self.row = row
self.col = col

class Move:
# start: Position de départ du coup
# end: Position d'arrivée du coup
def __init__(self, start, end):
self.start = start
self.end = end

def __str__(self):
return f"{self.start.row}{self.start.col},{self.end.row}{self.end.col}"

def __repr__(self):
return self.__str__()


def find_move(board, player_color):
# TODO: Implement logic to find the next move
# The moves should be returned as a list of 2-element lists
# The first element of the sequence should be the starting cell
# Each subsequent 2-element list should contain the x and y coordinates of the cell to move to
return []

# TODO: Implémentez ici la logique pour trouver les coups à jouer et les retourner
# Les coups doivent être retournés sous forme d'une liste d'objets Move,
# Chaque objet Move représente un coup, avec une cellule de départ et une cellule d'arrivée
# Les classes Position(row, column) et Move(start, end) sont fournies pour vous

return [Move(Position(6, 1), Position(5, 0))]


def main():
board = [
[None for _ in range(10)] for _ in range(10)
] # Initialize a 10x10 board with None
]

# Read the player's color
# Lecture de la couleur du joueur depuis la console
player_color = input().strip()[0]

# Read input line by line for the board
# Parsage du plateau de jeu depuis la console
for r in range(10):
line = input().strip()
row = line.split(",") # Split the line by commas

# Parse each element to a Piece object or None
row = line.split(",")
for c, piece_code in enumerate(row):
if piece_code:
board[r][c] = Piece(piece_code[0], piece_code[1])
else:
board[r][c] = None # Empty cells should be None
board[r][c] = None

# Call the find_move function to find the next move
# Appel de la fonction findMove pour trouver les coups à jouer
moves = find_move(board, player_color)

if not moves:
# No moves found
raise Exception("No moves were returned.")
return

# Print the moves
for pos in moves:
print(f"{pos[0][0]}{pos[0][1]},{pos[1][0]}{pos[1][1]};", end="")
# Envoi des coups trouvés à la console
for move in moves:
print(move)
print("")


Expand Down

0 comments on commit d10ff63

Please sign in to comment.