Advertisement
JackHoughton00

Nim

Apr 18th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. package nim;
  2. import java.util.Scanner;
  3. import java.util.Random;
  4.  
  5.  
  6.  
  7. public class Nim {
  8.  
  9.  
  10. public static void instructions(boolean turn) {
  11. System.out.println("This game is called Nim...");
  12. System.out.println("The objective is to force the computer to grab the last stone...");
  13. System.out.println("You can only take between 1 and 3 stones...");
  14. System.out.println("You will go first...");
  15. turn = false;
  16. }
  17.  
  18. public static void game(boolean turn,int stones, int pickedStones) {
  19. Scanner input = new Scanner(System.in);
  20. System.out.println("There are "+stones+" stones. ");
  21. System.out.print("How many stones do you wish to take?(1,2 or 3)");
  22. pickedStones = input.nextInt();
  23. if (pickedStones<=3 && pickedStones>=1) {
  24. stones -= pickedStones;
  25. turn = false;
  26. } else if(pickedStones>3){
  27. System.out.println("C'mon pick a proper number...");
  28. System.out.print("How many stones do you wish to take?(1,2 or 3)");
  29. pickedStones = input.nextInt();
  30. stones -= pickedStones;
  31. turn = false;
  32.  
  33. }}
  34. public static void isValid(int pickedStones,int stones) {
  35. if (pickedStones>stones && pickedStones > 3) {
  36. System.out.print("That is an invalid number");
  37.  
  38. }}
  39. public static void drawStones(int stones,int startStones,int computerPick, boolean turn) {
  40. Random r = new Random();
  41. if (stones>=3){
  42. int computerTurn = r.nextInt(3);
  43. computerPick = computerTurn;
  44. turn = true;
  45. } else if (stones==2){
  46. int computerTurn = r.nextInt(2);
  47. computerPick = computerTurn;
  48. turn = true;
  49. } else if (stones==1){
  50. System.out.print("Congrats,you won!");
  51. } }
  52.  
  53. public static void main(String[] args) {
  54. Scanner input = new Scanner(System.in);
  55. boolean turn;
  56. turn = false;
  57. Random rand = new Random();
  58. Random r = new Random();
  59. int pickedStones=0;
  60. int fifteen = 15;
  61. int thirty = 30;
  62. int startStones = r.nextInt(thirty-fifteen)+fifteen;
  63. int stones = startStones;
  64. int computerPick = 0;
  65. instructions(turn);
  66. System.out.println("There are "+stones+" stones. ");
  67. System.out.print("How many stones do you wish to take?(1,2 or 3)");
  68. pickedStones = input.nextInt();
  69. pickedStones -= stones;
  70. System.out.print(stones+" stones remain...");
  71. if (pickedStones<=3 && pickedStones>=1) {
  72. stones -= pickedStones;
  73. turn = false;
  74.  
  75. } else {
  76. System.out.println("C'mon pick a proper number...");
  77. System.out.print("How many stones do you wish to take?(1,2 or 3)");
  78. pickedStones = input.nextInt();
  79. stones -= pickedStones;
  80. turn = false;
  81. while (stones>0) {
  82. if (turn == true){
  83. game(turn,stones,pickedStones);
  84. isValid(stones,pickedStones);
  85. }
  86. if (turn == false ){
  87. drawStones(stones,startStones,computerPick,turn);
  88. }}}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement