Advertisement
Guest User

Untitled

a guest
Apr 26th, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.91 KB | None | 0 0
  1. // This program is written in D language by Alexis Viguié on 26-04-2014. It is not under any license, please feel free to use it in any way you want.
  2. import std.stdio;
  3. import std.random;
  4.  
  5. void main() {
  6.     double limit_rock = 3;
  7.     double limit_paper = 6;
  8.     double limit_scissors = 9;
  9.     int score_ai;
  10.     int score_player;
  11.     int ai_move;
  12.     auto ai_rand = uniform(1, limit_scissors + 1);
  13.     int player_move;
  14.     writeln("Welcome to Rock-Paper-Scissors !");
  15.    
  16.     while (1) {
  17.         writeln("\n1.Play 2.Quit");
  18.         int menu;
  19.         while ((menu != 1) && (menu != 2)) {
  20.             readf(" %s",&menu);
  21.        
  22.             if ((menu != 1) && (menu != 2)) {
  23.                 writeln("Invalid choice.");
  24.             }
  25.         }
  26.        
  27.             if (menu == 2) {
  28.                 break;
  29.             }
  30.        
  31.             int count_r;
  32.        
  33.             while ((count_r <= 0) || (count_r >= 101)) {
  34.                 writeln("\nHow many rounds ? Between 1 and 100.");
  35.                 readf(" %s",&count_r);
  36.                
  37.                 if ((count_r <= 0) || (count_r >= 101)) {
  38.                     writeln("Invalid choice.");
  39.                 }
  40.             }
  41.        
  42.         for (int count = 1; count < count_r; ++count) {
  43.             ai_rand = uniform(1, limit_scissors + 1);
  44.            
  45.             if (ai_rand <= limit_rock) {
  46.                 ai_move = 1;
  47.             } else if (ai_rand > limit_rock && ai_rand <= limit_paper) {
  48.                 ai_move = 2;
  49.             } else {
  50.                 ai_move = 3;
  51.             }
  52.            
  53.             player_move = 0;
  54.             writeln("\n1. Rock 2. Paper 3. Scissors");
  55.            
  56.             while ((player_move != 1) && (player_move != 2) && (player_move != 3)) {
  57.                 readf(" %s",&player_move);
  58.                
  59.                 if ((player_move != 1) && (player_move != 2) && (player_move != 3)) {
  60.                     writeln("Invalid choice.");
  61.                 }
  62.             }
  63.            
  64.             if ((player_move == 1) && ((limit_scissors - limit_paper) > 1)) {
  65.                 ++limit_paper;
  66.             } else if ((player_move == 2) && (limit_scissors < ((1 / 3) * (limit_rock + (limit_paper - limit_rock))))) {
  67.                 ++limit_scissors;
  68.             } else if ((player_move == 3) && ((limit_paper - limit_rock) > 1)) {
  69.                 ++limit_rock;
  70.             }
  71.            
  72.             write("\nYou played ");
  73.            
  74.             if (player_move == 1) {
  75.                 write("rock.");
  76.             } else if (player_move == 2) {
  77.                 write("paper.");
  78.             } else {
  79.                 write("scissors.");
  80.             }
  81.            
  82.             write("\nAI played ");
  83.            
  84.             if (ai_move == 1) {
  85.                 write("rock.");
  86.             } else if (ai_move == 2) {
  87.                 write("paper.");
  88.             } else {
  89.                 write("scissors.");
  90.             }
  91.            
  92.             if (((ai_move == 1) && (player_move == 2)) || ((ai_move == 2) && (player_move == 3)) || ((ai_move == 3) && (player_move == 1))) {
  93.                 writeln("\nYou scored a point.");
  94.                 ++score_player;
  95.             } else if (ai_move == player_move) {
  96.                 writeln("\nDraw round.");
  97.             } else {
  98.                 writeln("\nAI scored a point.");
  99.                 ++score_ai;
  100.             }
  101.            
  102.             writeln("Your score : ",score_player);
  103.             writeln("AI score   : ",score_ai);
  104.         }
  105.        
  106.         if (score_player > score_ai) {
  107.             writeln("You won !");
  108.         } else if (score_player == score_ai) {
  109.             writeln("Draw game.");
  110.         } else {
  111.             writeln("You lost.");
  112.         }
  113.        
  114.         limit_rock = 3;
  115.         limit_paper = 6;
  116.         limit_scissors = 9;
  117.         score_ai = 0;
  118.         score_player = 0;      
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement