Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class jeudenim {
- public static void main (String[] args) {
- int []tas={0,0,0};
- for (int i=0 ; i<tas.length ; i++){
- tas[i]=(int)(Math.random()*20)+1;
- }
- int tour =1;
- afficherJeu(tas);
- while (fini(tas)==false) {
- jouer(tour,tas);
- afficherJeu(tas);
- tour=tour%2+1;
- if (fini(tas)) {
- System.out.println("Jeu fini, joueur 1 gagne");
- }
- }
- }
- public static void afficherJeu (int []t) {
- for (int i=0 ; i<t.length ; i++){
- System.out.print(t[i]+"|");
- }
- System.out.println();
- }
- public static boolean fini (int []tab){
- int somme=0;
- for (int i=0 ; i<tab.length ; i++){
- somme=tab[i]+somme;
- }
- if (somme==0) {
- return true;
- } else {
- return false;
- }
- }
- public static void jouer(int x, int[] tabl){
- System.out.println("Joueur "+x+", quel tas ?");
- Scanner sc = new Scanner(System.in);
- int ntas = sc.nextInt();
- while (ntas>tabl.length){
- System.out.println("Vous ne pouvez choisir que parmi "+tabl.length+" tas, veuillez réessayez :");
- ntas=sc.nextInt();
- }
- while (tabl[ntas-1]==0){
- System.out.println("Vous ne pouvez pas choisir un tas vide, veuillez réessayez :");
- ntas=sc.nextInt();
- }
- System.out.println("Joueur "+x+", combien de jetons ?");
- int npion = sc.nextInt();
- while (npion==0 || npion>tabl[ntas-1]){
- System.out.println("Impossible de choisir 0 ou plus de pions que dans le tas, veuillez réessayez :");
- npion=sc.nextInt();
- }
- tabl[ntas-1]=tabl[ntas-1]-npion;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement