Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1.     public static void main(String[] args)
  2.     {
  3.         changeJOP();
  4.         directions();
  5.         int totalSticks = 16;
  6.         int[] numSticks = {1,3,5,7};
  7.         int player = 0;
  8.         String name[] = new String[2];
  9.         name[0]=getName();
  10.         name[1]=getName();
  11.         while(totalSticks > 1) {
  12.             if (player==0)
  13.                 player=1;
  14.             else player=0;
  15.             totalSticks = playGame(numSticks, totalSticks, name[player]);
  16.         }
  17.         printWinner(name[player], numSticks);
  18.     }
  19.     public static int playGame(int[] numSticks, int totalSticks, String name)
  20.     {
  21.         String[] stickChoices = {"Row 1","Row 2","Row 3","Row 4"};
  22.         int whichRow = JOptionPane.showOptionDialog(null,
  23.             getMatchString(numSticks)+name+", please pick a row"
  24.             ,"Nim",0,3,null,stickChoices,null);
  25.         while (numSticks[whichRow]==0)
  26.             whichRow = JOptionPane.showOptionDialog(null,
  27.                 getMatchString(numSticks)+name+", please pick a row"
  28.                 ,"Nim",0,3,null,stickChoices,null);
  29.         int howMany = Integer.parseInt(JOptionPane.showInputDialog(getMatchString(numSticks)+
  30.             name+", how many matches would you like to take?"));
  31.         while (howMany<=0 || numSticks[whichRow] - howMany<0){
  32.             howMany = Integer.parseInt(JOptionPane.showInputDialog(getMatchString(numSticks)+
  33.             name+", that is an invalid amount.\nHow many matches would you like to take?"));
  34.         }
  35.         numSticks[whichRow] -= howMany;
  36.         totalSticks -= howMany;
  37.         return totalSticks;
  38.     }
  39.     public static void directions()
  40.     {
  41.         JOptionPane.showMessageDialog(null,
  42.             "Pick as many sticks as you want from\none of the four piles.\n"+
  43.             "The goal of the game is to avoid\npicking the last sticks");
  44.     }
  45.     public static String getName()
  46.     {
  47.         return JOptionPane.showInputDialog("What is your name?");
  48.     }
  49.     public static void printWinner(String winner, int[] numSticks)
  50.     {
  51.         JOptionPane.showMessageDialog(null,
  52.             getMatchString(numSticks)+winner+", you won!");
  53.     }
  54.     public static String getMatchString(int[] numSticks)
  55.     {
  56.         String returnString = "";
  57.         for (int i=0;i<4;i++)
  58.         {
  59.             returnString += "Row "+(i+1)+": ";
  60.             for (int j=0;j<numSticks[i];j++)
  61.             {
  62.                 returnString += "| ";
  63.             }
  64.             returnString += "\n";
  65.         }
  66.         return returnString;
  67.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement