Merge branch 'master' of gitea.it:gicorada/java-scuola
This commit is contained in:
commit
1d0c5c576d
3
NetBeans Projects/mastermind/.idea/.gitignore
generated
vendored
Normal file
3
NetBeans Projects/mastermind/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
6
NetBeans Projects/mastermind/.idea/misc.xml
generated
Normal file
6
NetBeans Projects/mastermind/.idea/misc.xml
generated
Normal 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>
|
8
NetBeans Projects/mastermind/.idea/modules.xml
generated
Normal file
8
NetBeans Projects/mastermind/.idea/modules.xml
generated
Normal 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>
|
6
NetBeans Projects/mastermind/.idea/vcs.xml
generated
Normal file
6
NetBeans Projects/mastermind/.idea/vcs.xml
generated
Normal 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>
|
12
NetBeans Projects/mastermind/mastermind.iml
Normal file
12
NetBeans Projects/mastermind/mastermind.iml
Normal 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>
|
94
NetBeans Projects/mastermind/src/mastermind/MasterMind.java
Normal file
94
NetBeans Projects/mastermind/src/mastermind/MasterMind.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
53
NetBeans Projects/mastermind/src/mastermind/Tester.java
Normal file
53
NetBeans Projects/mastermind/src/mastermind/Tester.java
Normal 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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user