Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class jeudenim {
  4. public static void main (String[] args) {
  5. int []tas={0,0,0};
  6. for (int i=0 ; i<tas.length ; i++){
  7. tas[i]=(int)(Math.random()*20)+1;
  8. }
  9. int tour =1;
  10. afficherJeu(tas);
  11.  
  12. while (fini(tas)==false) {
  13. jouer(tour,tas);
  14. afficherJeu(tas);
  15. tour=tour%2+1;
  16. if (fini(tas)) {
  17. System.out.println("Jeu fini, joueur 1 gagne");
  18. }
  19.  
  20. }
  21.  
  22. }
  23.  
  24. public static void afficherJeu (int []t) {
  25. for (int i=0 ; i<t.length ; i++){
  26. System.out.print(t[i]+"|");
  27. }
  28. System.out.println();
  29. }
  30.  
  31. public static boolean fini (int []tab){
  32. int somme=0;
  33. for (int i=0 ; i<tab.length ; i++){
  34. somme=tab[i]+somme;
  35. }
  36. if (somme==0) {
  37. return true;
  38. } else {
  39. return false;
  40. }
  41. }
  42.  
  43. public static void jouer(int x, int[] tabl){
  44. System.out.println("Joueur "+x+", quel tas ?");
  45. Scanner sc = new Scanner(System.in);
  46. int ntas = sc.nextInt();
  47. while (ntas>tabl.length){
  48. System.out.println("Vous ne pouvez choisir que parmi "+tabl.length+" tas, veuillez réessayez :");
  49. ntas=sc.nextInt();
  50. }
  51. while (tabl[ntas-1]==0){
  52. System.out.println("Vous ne pouvez pas choisir un tas vide, veuillez réessayez :");
  53. ntas=sc.nextInt();
  54. }
  55.  
  56. System.out.println("Joueur "+x+", combien de jetons ?");
  57. int npion = sc.nextInt();
  58.  
  59. while (npion==0 || npion>tabl[ntas-1]){
  60. System.out.println("Impossible de choisir 0 ou plus de pions que dans le tas, veuillez réessayez :");
  61. npion=sc.nextInt();
  62. }
  63.  
  64.  
  65. tabl[ntas-1]=tabl[ntas-1]-npion;
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement