Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:io';
- import 'dart:math';
- import 'dart:math' show Random;
- class InputOutput{
- var history = new List();
- input_output(){
- 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...');
- }
- int input(){
- int x = int.parse(stdin.readLineSync());
- while (x < 1) {
- print('Please enter a positive number...');
- x = int.parse(stdin.readLineSync());
- }
- return x;
- }
- int player_move(int k, int n){
- int m = input();
- int tmp;
- while(m > k || m < 1){
- print('Please enter a positive number...');
- m = input();
- }
- history.add('Player: $m');
- tmp = n-m;
- print('You removed $m, remaining number of balls is $tmp ');
- return m;
- }
- int PC_move(int n_remain, int k_remove){
- print('The PC removed $k_remove balls, the remaining number of balls equals $n_remain.');
- history.add('PC: $k_remove');
- }
- void record(){
- for (var i = 0; i < history.length; i++) print(history[i]);
- }
- }
- class Player extends InputOutput{
- int player_turn(int n, int k){
- int move = player_move(k,n);
- n -= move;
- return n;
- }
- }
- class PC extends InputOutput{
- int PC_turn(int n, int k){
- var rand = new Random();
- int move = (rand.nextInt(k)%k)+1;
- PC_move(n-move, move);
- return move;
- }
- }
- class Game extends InputOutput{
- var human_player, pc_player;
- int num_of_balls, max;
- bool human = false, computer = false;
- void game_turn(){
- print('To start choose a 0 or a 1 to know your turn!');
- int turn = int.parse(stdin.readLineSync());
- while(turn != 0 && turn != 1){
- print('Please enter a 0 or a 1...');
- turn = int.parse(stdin.readLineSync());
- }
- var randomizer = new Random();
- var rand_turn = randomizer.nextInt(2);
- if (turn == rand_turn){
- print('You go first!');
- human = true;
- }
- else if (turn != rand_turn){
- print("The PC goes first!");
- computer = true;
- }
- }
- Game(Player human_player, PC pc_player){
- input_output();
- num_of_balls = input();
- max = input();
- this.human_player = human_player;
- this.pc_player = pc_player;
- game_turn();
- int move;
- while (num_of_balls > 0) {
- if (human) {
- print('Pick a valid number of maximum value equals $max');
- num_of_balls = human_player.player_turn(num_of_balls, max);
- human = false;
- computer = true;
- if (num_of_balls <= 0) print('YOU WIN!!!!');
- }else if (computer){
- move = pc_player.PC_turn(num_of_balls, max);
- num_of_balls -= move;
- computer = false;
- human = true;
- if (num_of_balls <= 0) print('The PC won, good luck next time :(');
- }
- record();
- }
- }
- }
- void main(){
- var inp_out = new InputOutput();
- var human = new Player();
- var pc = new PC();
- var game = new Game(human, pc);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement