Aggiunto MasterMind (lab+casa)

This commit is contained in:
Giacomo R. 2023-02-28 17:23:48 +01:00
parent c74e6d9450
commit 0b0d6e11c4
7 changed files with 182 additions and 0 deletions

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" project-jdk-name="19" 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$/mastermind.iml" filepath="$PROJECT_DIR$/mastermind.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>

View File

@ -0,0 +1,12 @@
<?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$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,94 @@
/*
* Copyright (C) 2023 Giacomo Radaelli
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package mastermind;
import java.util.ArrayList;
import java.util.Random;
/**
* Classe che simula il gioco Mastermind
* @author radaelli11353
*/
public class MasterMind {
ArrayList<Character> lettereValide;
ArrayList<Character> segreto;
int tentativi;
public MasterMind() {
lettereValide = new ArrayList<>(6);
lettereValide.add('R');
lettereValide.add('V');
lettereValide.add('G');
lettereValide.add('B');
lettereValide.add('N');
lettereValide.add('M');
tentativi = 0;
segreto = genera();
System.out.println(segreto);
}
static private ArrayList<Character> genera() {
Random gen = new Random();
ArrayList<Character> segreto = new ArrayList<>();
for(int i = 0; i < 4; i++) {
int num = gen.nextInt(5);
switch (num) {
case 0: segreto.add('R'); break;
case 1: segreto.add('V'); break;
case 2: segreto.add('G'); break;
case 3: segreto.add('B'); break;
case 4: segreto.add('N'); break;
default: segreto.add('M'); break;
}
}
return segreto;
}
public int[] muovi(String originale) {
if(tentativi >= 10) throw new IllegalStateException("Hai esaurito i tentativi");
if(originale.length() != 4) throw new IllegalArgumentException("Devi inserire quattro caratteri");
ArrayList<Character> utente = new ArrayList<>(4);
for(int i = 0; i < 4; i++) {
if(!lettereValide.contains(originale.charAt(i))) throw new IllegalArgumentException("I colori inseriti non sono validi");
utente.add(originale.charAt(i));
}
int pgiusto = 0, psbagliato = 0;
for (int i = 0; i < 4; i++) {
if(utente.indexOf(segreto.get(i)) == -1) {
} else if(utente.indexOf(segreto.get(i)) == i) {
pgiusto++;
utente.set(i, Character.MIN_VALUE);
} else {
psbagliato++;
}
}
return new int[] {pgiusto, psbagliato};
}
public boolean hasNext() {
return tentativi < 9;
}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright (C) 2023 Giacomo Radaelli
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package mastermind;
import java.util.Scanner;
/**
*
* @author radaelli11353
*/
public class Tester {
public static void main(String[] args) {
MasterMind test = new MasterMind();
Scanner in = new Scanner(System.in);
int tentativi = 0;
boolean vinto = false;
while(test.hasNext() && !vinto) {
String parola = in.nextLine().toUpperCase().replace(" ","");
int[] risultato = test.muovi(parola);
tentativi++;
if(risultato[0] == 4) {
System.out.println("HAI VINTO!");
vinto = true;
break;
}
if(tentativi == 10) {
System.out.println("Hai perso");
return;
}
System.out.printf("%d %d <-- Risultato del %d tentativo", risultato[0], risultato[1], tentativi);
}
}
}