Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args)
- {
- changeJOP();
- directions();
- int totalSticks = 16;
- int[] numSticks = {1,3,5,7};
- int player = 0;
- String name[] = new String[2];
- name[0]=getName();
- name[1]=getName();
- while(totalSticks > 1) {
- if (player==0)
- player=1;
- else player=0;
- totalSticks = playGame(numSticks, totalSticks, name[player]);
- }
- printWinner(name[player], numSticks);
- }
- public static int playGame(int[] numSticks, int totalSticks, String name)
- {
- String[] stickChoices = {"Row 1","Row 2","Row 3","Row 4"};
- int whichRow = JOptionPane.showOptionDialog(null,
- getMatchString(numSticks)+name+", please pick a row"
- ,"Nim",0,3,null,stickChoices,null);
- while (numSticks[whichRow]==0)
- whichRow = JOptionPane.showOptionDialog(null,
- getMatchString(numSticks)+name+", please pick a row"
- ,"Nim",0,3,null,stickChoices,null);
- int howMany = Integer.parseInt(JOptionPane.showInputDialog(getMatchString(numSticks)+
- name+", how many matches would you like to take?"));
- while (howMany<=0 || numSticks[whichRow] - howMany<0){
- howMany = Integer.parseInt(JOptionPane.showInputDialog(getMatchString(numSticks)+
- name+", that is an invalid amount.\nHow many matches would you like to take?"));
- }
- numSticks[whichRow] -= howMany;
- totalSticks -= howMany;
- return totalSticks;
- }
- public static void directions()
- {
- JOptionPane.showMessageDialog(null,
- "Pick as many sticks as you want from\none of the four piles.\n"+
- "The goal of the game is to avoid\npicking the last sticks");
- }
- public static String getName()
- {
- return JOptionPane.showInputDialog("What is your name?");
- }
- public static void printWinner(String winner, int[] numSticks)
- {
- JOptionPane.showMessageDialog(null,
- getMatchString(numSticks)+winner+", you won!");
- }
- public static String getMatchString(int[] numSticks)
- {
- String returnString = "";
- for (int i=0;i<4;i++)
- {
- returnString += "Row "+(i+1)+": ";
- for (int j=0;j<numSticks[i];j++)
- {
- returnString += "| ";
- }
- returnString += "\n";
- }
- return returnString;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement