Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. import 'dart:io';
  2. import 'dart:math';
  3. import 'dart:math' show Random;
  4.  
  5.  
  6. class InputOutput{
  7.  
  8. input_output(){
  9. print('Enter the total number of balls to begin with, then press enter and type the maximum number of balls to be removed on a single round...');
  10. }
  11.  
  12. int input(){
  13. int x = int.parse(stdin.readLineSync());
  14. while (x < 1) {
  15. print('Please enter a positive number...');
  16. x = int.parse(stdin.readLineSync());
  17. }
  18. return x;
  19. }
  20.  
  21. int player_move(int k, int n, var history){
  22. int m = input();
  23. int tmp;
  24. while(m > k || m < 1){
  25. print('Please enter a positive number...');
  26. m = input();
  27. }
  28. history.add('Player: $m');
  29. tmp = n-m;
  30. print('You removed $m, remaining number of balls is $tmp ');
  31. return m;
  32. }
  33.  
  34. int PC_move(int n_remain, int k_remove, var history){
  35. print('The PC removed $k_remove balls, the remaining number of balls equals $n_remain.');
  36. history.add('PC: $k_remove');
  37. }
  38.  
  39. void record(var history){
  40. for (var i = 0; i < history.length; i++) print(history[i]);
  41. }
  42.  
  43. }
  44.  
  45. class Player extends InputOutput{
  46. int player_turn(int n, int k, var history){
  47. int move = player_move(k,n, history);
  48. n -= move;
  49. return n;
  50. }
  51. }
  52.  
  53. class PC extends InputOutput{
  54. int PC_turn(int n, int k, var history){
  55. var rand = new Random();
  56. int move = (rand.nextInt(k)%k)+1;
  57. PC_move(n-move, move, history);
  58. return move;
  59. }
  60. }
  61.  
  62. class Game extends InputOutput{
  63. var human_player, pc_player;
  64. int num_of_balls, max;
  65. var history = new List();
  66. bool human = false, computer = false;
  67. void game_turn(){
  68. print('To start choose a 0 or a 1 to know your turn!');
  69. int turn = int.parse(stdin.readLineSync());
  70. while(turn != 0 && turn != 1){
  71. print('Please enter a 0 or a 1...');
  72. turn = int.parse(stdin.readLineSync());
  73. }
  74. var randomizer = new Random();
  75. var rand_turn = randomizer.nextInt(2);
  76. if (turn == rand_turn){
  77. print('You go first!');
  78. human = true;
  79. }
  80. else if (turn != rand_turn){
  81. print("The PC goes first!");
  82. computer = true;
  83. }
  84. }
  85. Game(Player human_player, PC pc_player){
  86. input_output();
  87. num_of_balls = input();
  88. max = input();
  89. this.human_player = human_player;
  90. this.pc_player = pc_player;
  91. game_turn();
  92. int move;
  93. while (num_of_balls > 0) {
  94. if (human) {
  95. print('Pick a valid number of maximum value equals $max');
  96. num_of_balls = human_player.player_turn(num_of_balls, max, history);
  97. human = false;
  98. computer = true;
  99. if (num_of_balls <= 0) print('YOU WIN!!!!');
  100. }else if (computer){
  101. move = pc_player.PC_turn(num_of_balls, max, history);
  102. num_of_balls -= move;
  103. computer = false;
  104. human = true;
  105. if (num_of_balls <= 0) print('The PC won, good luck next time :(');
  106. }
  107. }
  108. record(history);
  109. }
  110. }
  111.  
  112. void main(){
  113. var inp_out = new InputOutput();
  114. var human = new Player();
  115. var pc = new PC();
  116. var game = new Game(human, pc);
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement