From e1e7eb4a57b0573bdf7c07b2646c4acbe738a7e5 Mon Sep 17 00:00:00 2001
From: Sarah CHERCHEM <ls_cherchem@esi.dz>
Date: Thu, 27 Feb 2025 15:48:54 +0100
Subject: [PATCH] =?UTF-8?q?Client=20:=20ajout=20des=20m=C3=A9thodes=20init?=
 =?UTF-8?q?iales=20de=20Client?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/Client.java | 93 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/Code.java   | 74 +++++++++++++++++++++++++++++++++++++++
 src/server.java |  4 +++
 3 files changed, 171 insertions(+)
 create mode 100644 src/Client.java
 create mode 100644 src/server.java

diff --git a/src/Client.java b/src/Client.java
new file mode 100644
index 0000000..13d93e9
--- /dev/null
+++ b/src/Client.java
@@ -0,0 +1,93 @@
+package src;
+
+import java.io.*;
+import java.net.*;
+import java.util.Scanner;
+
+public class Client {
+    private static final String SERVER_ADDRESS = "localhost";
+    private static final int PORT = 1234;
+    private Socket socket;
+    private BufferedReader in;
+    private PrintWriter out;
+    private Scanner scanner;
+
+    public Client() {
+        scanner = new Scanner(System.in);
+    }
+    public void connect() {
+        try {
+            socket = new Socket(SERVER_ADDRESS, PORT);
+            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+            out = new PrintWriter(socket.getOutputStream(), true);
+            System.out.println("Connecté au serveur Mastermind !");
+            play();
+        } catch (IOException e) {
+            System.err.println("Erreur de connexion au serveur : " + e.getMessage());
+        } finally {
+            closeConnection();
+        }
+    }
+
+    private void play() {
+        while (true) {
+            String guess = getUserInput();
+            if (guess == null) continue;
+
+            sendGuess(guess);
+            String response = receiveResponse();
+
+            if (response == null) {
+                System.out.println("Connexion fermée par le serveur.");
+                break;
+            }
+
+            System.out.println("Réponse du serveur : " + response);
+
+            if (response.equals("4 0")) {
+                System.out.println("Félicitations ! Vous avez trouvé la bonne combinaison !");
+                break;
+            }
+        }
+    }
+
+    private String getUserInput() {
+        System.out.print("Entrez une combinaison (4 lettres parmi B,G,O,R,W,Y) : ");
+        String input = scanner.nextLine().toUpperCase();
+
+        if (!input.matches("[BGORWY]{4}")) {
+            System.out.println("Combinaison invalide, veuillez entrer exactement 4 lettres parmi B, G, O, R, W, Y.");
+            return null;
+        }
+        return input;
+    }
+
+    private void sendGuess(String guess) {
+        out.println(guess);
+    }
+
+    private String receiveResponse() {
+        try {
+            return in.readLine();
+        } catch (IOException e) {
+            System.err.println("Erreur de lecture du serveur : " + e.getMessage());
+            return null;
+        }
+    }
+
+    private void closeConnection() {
+        try {
+            if (socket != null) socket.close();
+            if (in != null) in.close();
+            if (out != null) out.close();
+            scanner.close();
+        } catch (IOException e) {
+            System.err.println("Erreur lors de la fermeture des ressources : " + e.getMessage());
+        }
+    }
+
+
+    public static void main(String[] args) {
+        new Client().connect();
+    }
+}
diff --git a/src/Code.java b/src/Code.java
index 4e67f1e..ef623a7 100644
--- a/src/Code.java
+++ b/src/Code.java
@@ -1,4 +1,78 @@
 package src;
 
+import java.util.Random;
+
 public class Code {
+    public static final int CODE_LENGTH = 4;
+    public static char[] COLORS = new char[]{'B','G','O','R','W','Y'};
+    private final  char[] codeWord = new char[CODE_LENGTH];
+
+    public Code(Random random){
+        for (int i = 0; i < CODE_LENGTH; i++) {
+            codeWord[i] = COLORS[random.nextInt(COLORS.length)];
+        }
+    }
+
+    public Code(String codeString){
+        assert(codeString.length() == CODE_LENGTH);
+        for(int i=0; i<CODE_LENGTH; i++)
+            codeWord[i] = codeString.charAt(i);
+    }
+
+    @Override
+    public String toString() {
+        return new String(codeWord);
+    }
+
+    /**
+     * return the number of colors of guess that are correctly positioned
+     */
+    public int numberOfColorsWithCorrectPosition(Code guess){
+        int count = 0;
+        for(char color : COLORS){
+            count += numberOfMatches(color, guess);
+        }
+        return count;
+    }
+
+    /**
+     * return the number of colors of guess that are in this codeWord
+     * but do not have the correct position
+     */
+    public int numberOfColorsWithIncorrectPosition(Code guess){
+        int count = 0;
+        for(char color:COLORS){
+            int nMatchedOccurrences = numberOfMatches(color, guess);
+            int nUnmatchedOccurrencesCode  = numberOfOccurrences(color, this) - nMatchedOccurrences;
+            int nUnmatchedOccurrencesGuess = numberOfOccurrences(color, guess) - nMatchedOccurrences;
+            count += Math.min(nUnmatchedOccurrencesCode, nUnmatchedOccurrencesGuess);
+        }
+        return count;
+    }
+
+
+    private int numberOfOccurrences(char color, Code code){
+        int count = 0;
+        for (int i = 0; i < CODE_LENGTH; i++) {
+            if (code.codeWord[i] == color) count++;
+        }
+        return count;
+    }
+
+    private int numberOfMatches(char color, Code guess){
+        int count = 0;
+        for (int i = 0; i < CODE_LENGTH; i++) {
+            if ((this.codeWord[i] == guess.codeWord[i]) && (this.codeWord[i] == color)){
+                count++;
+            }
+        }
+        return count;
+    }
+
+    @Override
+    public boolean equals(Object o){
+        if (o == null) return false;
+        if (!(o instanceof Code)) return false;
+        return this.toString().equals(o.toString());
+    }
 }
diff --git a/src/server.java b/src/server.java
new file mode 100644
index 0000000..8f410c6
--- /dev/null
+++ b/src/server.java
@@ -0,0 +1,4 @@
+package src;
+
+public class server {
+}
-- 
GitLab