tp1 partie 1 et partie 2.1/2.2

This commit is contained in:
Kennro03
2025-10-14 17:21:26 +02:00
parent 30458c5907
commit b88b6e9b9d
26 changed files with 414 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 125 KiB

3
TTT_java/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

6
TTT_java/.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
TTT_java/.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/TTT_java.iml" filepath="$PROJECT_DIR$/TTT_java.iml" />
</modules>
</component>
</project>

6
TTT_java/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

19
TTT_java/AIPlayer.java Normal file
View File

@@ -0,0 +1,19 @@
import java.util.Random;
public class AIPlayer extends Player {
private Random random;
public AIPlayer(char mark) {
super(mark);
random = new Random();
}
public void makeMove(Board board) {
int row, col;
do {
row = random.nextInt(3);
col = random.nextInt(3);
} while (!board.isCellEmpty(row, col));
board.placeMark(row, col, getMark());
}
}

64
TTT_java/Board.java Normal file
View File

@@ -0,0 +1,64 @@
public class Board {
private char[][] grid;
private static final int SIZE = 3;
public Board() {
grid = new char[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
grid[i][j] = '-';
}
}
}
public void printBoard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) { //IL FAUT METTRE I ET J INFERIEUR!!! SINON PAS D'AFFICHAGE!!!
System.out.print(grid[i][j] + " ");
}
System.out.println();
}
}
public boolean isCellEmpty(int row, int col) {
return grid[row][col] == '-';
}
public void placeMark(int row, int col, char mark) {
grid[row][col] = mark;
}
public boolean hasWon(char mark) {
// Vérifie les lignes
for (int i = 0; i < SIZE; i++) {
if (grid[i][0] == mark && grid[i][1] == mark && grid[i][2] == mark) {
return true;
}
}
// Vérifie les colonnes
for (int j = 0; j < SIZE; j++) {
if (grid[0][j] == mark && grid[1][j] == mark && grid[2][j] == mark) {
return true;
}
}
// Vérifie les diagonales
if (grid[0][0] == mark && grid[1][1] == mark && grid[2][2] == mark) {
return true;
}
if (grid[0][2] == mark && grid[1][1] == mark && grid[2][0] == mark) {
return true;
}
return false;
}
public boolean isFull() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (grid[i][j] == '-') {
return false;
}
}
}
return true;
}
}

60
TTT_java/Game.java Normal file
View File

@@ -0,0 +1,60 @@
import java.util.Scanner;
public class Game {
private Board board;
private Player human;
private Player ai= new AIPlayer('O');
private Scanner scanner;
public Game() {
board = new Board(); // ATTENTION IL FALLAIT INITIALISER LE BOARD!!!! sinon erreur exit code 1
human = new Player('X');
scanner = new Scanner(System.in);
}
public void start() {
System.out.println("Bienvenue dans Tic-Tac-Toe !");
board.printBoard();
while (true) {
humanTurn();
if (isGameOver(human)) break;
aiTurn();
if (isGameOver(ai)) break;
}
}
private void humanTurn() {
System.out.println("Votre tour ! Entrez la ligne et la colonne (ex: 2 2 pour le centre) :"); //ATTENTION !! LE MILIEU C'EST 2 2 PAS 1 1 !!
int row, col;
while (true) {
row = scanner.nextInt() - 1;
col = scanner.nextInt() - 1;
if (board.isCellEmpty(row, col)) {
board.placeMark(row, col, human.getMark());
board.printBoard();
break;
} else {
System.out.println("Cette case est déjà prise. Essayez à nouveau.");
}
}
}
private void aiTurn() {
System.out.println("Tour de l'IA...");
((AIPlayer) ai).makeMove(board);
board.printBoard();
}
private boolean isGameOver(Player player) {
if (board.hasWon(player.getMark())) {
System.out.println("Le joueur " + player.getMark() + " a gagné !"); //PRENDRE LA MARK DU PLAYER!!! SINON AFFICHE TOUJOURS VICTOIRE IA!!
return true;
} else if (board.isFull()) {
System.out.println("Égalité ! La grille est pleine.");
return true;
}
return false;
}
}

11
TTT_java/Player.java Normal file
View File

@@ -0,0 +1,11 @@
public class Player {
private char mark;
public Player(char mark) {
this.mark = mark;
}
public char getMark() {
return mark;
}
}

11
TTT_java/TTT_java.iml Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@@ -0,0 +1,6 @@
public class TicTacToeGame {
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}

3
TTT_java/out/production/TTT_java/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/TTT_java.iml" filepath="$PROJECT_DIR$/TTT_java.iml" />
</modules>
</component>
</project>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file not shown.

18
pendu/.vscode/c_cpp_properties.json vendored Normal file
View File

@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "windows-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "windows-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}

24
pendu/.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": true,
"cwd": "c:/Users/kennr/Downloads/pendu",
"program": "c:/Users/kennr/Downloads/pendu/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

59
pendu/.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,59 @@
{
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/Build/vcvarsall.bat",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}

84
pendu/pendu_pbs.cpp Normal file
View File

@@ -0,0 +1,84 @@
// Code original généré par ChatGPT - nov 2024
// Prompt : "Ecris moi un programme en C++ pour un jeu de pendu."
// Modifications apportées : ajouts de bugs et d'erreurs de styles...
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
// Fonction pour afficher l'état actuel du mot deviné
void afficherMot(const string& mot, const vector<bool>& lettresDevinees) {
for (size_t i = 0; i <= mot.size(); i++) { // !!! Ne pas inclure "<=" mot size dans la taille de la boucle, crash lors de l'affichage du mot
if (lettresDevinees[i]) {
cout << mot[i] << " ";
}
else {
cout << "_ ";
}
}
cout << endl;
}
// Fonction pour vérifier si le joueur a deviné tout le mot
bool motDevine(const vector<bool>& lettresDevinees) {
for (bool devinee : lettresDevinees) {
if (!devinee) return false;
}
return true;
}
int main() {
// Liste de mots pour le jeu
vector<string> words = { "ordinateur", "programmation", "developpeur", "algorithme", "variable" };
// Initialiser le générateur de nombres aléatoires
srand(static_cast<unsigned int>(((time(0))))); // !!! Manque de deux parenthèses, échec lors de l'éxecution
// Choisir un mot aléatoire dans la liste
string Mot = words[rand() % words.size()];
vector<bool> lettresDevinees(Mot.size(), false);
int essaisRestants = 6; // Nombre maximum de tentatives
cout << "Bienvenue au jeu du pendu !" << endl;
while (essaisRestants > 0 && !motDevine(lettresDevinees)) {
cout << "\nEssais restants: " << essaisRestants << endl;
afficherMot(Mot, lettresDevinees);
cout << "Entrez une lettre : ";
char lettre;
cin >> lettre;
bool lettreTrouvee = false;
for (int i = 0; i < Mot.size(); ++i){
if (Mot[i] == lettre) // !!! deux = pour comparer et pas instancier, crash lors de comparaison
{
lettresDevinees[i] = true;
lettreTrouvee = true;
}
}
if (!lettreTrouvee) {
cout << "Lettre incorrecte !" << endl;
--essaisRestants;
}
if (motDevine(lettresDevinees));
{
cout << "\nFélicitations ! Vous avez deviné le mot : " << Mot << endl;
return 0;
}
}
if (essaisRestants == 0) {
cout << "\nDommage ! Vous avez perdu. Le mot était : " << Mot << endl;
}
return 0;
}

1
testTags Submodule

Submodule testTags added at 6a50b09436