Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class kodutöö3 {
  4. static int newGame(int N){
  5. int tikkude_arv = N;
  6. return tikkude_arv;
  7. }
  8. static int makeHumanMove(Scanner scanner, int M, int N){
  9. boolean t2isarv = true;
  10. int arv = 0;
  11. while(t2isarv){
  12. System.out.println("Mitu tikku soovid eemaldada?");
  13. if(scanner.hasNextInt()){
  14. arv = scanner.nextInt();
  15. if(arv >= 1 && arv <= M) t2isarv = false;
  16. else System.out.println("Sisesta arv 1 kuni " + M);
  17. }
  18. else {
  19. String line = scanner.nextLine();
  20. System.out.println("Sisesta arv 1 kuni " + M);
  21. }
  22. }
  23. System.out.println("Eemaldasid " + arv + " tikku");
  24. N = N - arv;
  25. return N;
  26. }
  27. static int makeComputerMove(int M, int N){
  28. int x = 0;
  29. if (N <= M) x = N;
  30. else{
  31. x = (int)(Math.random()*((M-1)+1))+1;
  32. }
  33. N = N - x;
  34. System.out.println("Arvuti eemaldas " + x + " tikku");
  35. return N;
  36. }
  37.  
  38. public static void main(String[] args){
  39. String sisend;
  40. int N = 15;
  41. int M = 3;
  42. int tikkude_arv;
  43. Scanner scanner = new Scanner(System.in);
  44. System.out.print("Tikumäng on kahe mängija mäng, kus mängijad võtavad kordamööda laualt tikke.");
  45. System.out.println("Kes võtab viimase tiku, on võitnud.");
  46. System.out.println("Laual on alguses " + N + " tikku. Mängija tohib oma käigu korral võtta laualt 1 kuni " + M + " tikku");
  47. System.out.println("Kas soovid muuta tikkude arvu laual ja tikkude kogust mida saad korraga eemaldada? (jah/ei)");
  48. sisend = scanner.next();
  49. if(sisend.equals("jah")){
  50. System.out.println("Sisesta laual olevate tikkude arv");
  51. if(scanner.hasNextInt()) N = scanner.nextInt();
  52. System.out.println("Sisesta mitu tikku saab korraga mängulaualt eemaldada");
  53. if(scanner.hasNextInt()) M = scanner.nextInt();
  54. }
  55. tikkude_arv = newGame(N);
  56. while(tikkude_arv > 0){
  57. System.out.println("Laual on " + tikkude_arv + " tikku.");
  58. tikkude_arv = makeHumanMove(scanner, M, tikkude_arv);
  59. if(tikkude_arv == 0){
  60. System.out.println("Palju õnne, võitsid mängu.");
  61. break;
  62. }
  63. System.out.println("Laual on " + tikkude_arv + " tikku.");
  64. tikkude_arv = makeComputerMove(M, tikkude_arv);
  65. if(tikkude_arv == 0){
  66. System.out.println("Kaotasid");
  67. break;
  68. }
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement