Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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.
- import std.stdio;
- import std.random;
- void main() {
- double limit_rock = 3;
- double limit_paper = 6;
- double limit_scissors = 9;
- int score_ai;
- int score_player;
- int ai_move;
- auto ai_rand = uniform(1, limit_scissors + 1);
- int player_move;
- writeln("Welcome to Rock-Paper-Scissors !");
- while (1) {
- writeln("\n1.Play 2.Quit");
- int menu;
- while ((menu != 1) && (menu != 2)) {
- readf(" %s",&menu);
- if ((menu != 1) && (menu != 2)) {
- writeln("Invalid choice.");
- }
- }
- if (menu == 2) {
- break;
- }
- int count_r;
- while ((count_r <= 0) || (count_r >= 101)) {
- writeln("\nHow many rounds ? Between 1 and 100.");
- readf(" %s",&count_r);
- if ((count_r <= 0) || (count_r >= 101)) {
- writeln("Invalid choice.");
- }
- }
- for (int count = 1; count < count_r; ++count) {
- ai_rand = uniform(1, limit_scissors + 1);
- if (ai_rand <= limit_rock) {
- ai_move = 1;
- } else if (ai_rand > limit_rock && ai_rand <= limit_paper) {
- ai_move = 2;
- } else {
- ai_move = 3;
- }
- player_move = 0;
- writeln("\n1. Rock 2. Paper 3. Scissors");
- while ((player_move != 1) && (player_move != 2) && (player_move != 3)) {
- readf(" %s",&player_move);
- if ((player_move != 1) && (player_move != 2) && (player_move != 3)) {
- writeln("Invalid choice.");
- }
- }
- if ((player_move == 1) && ((limit_scissors - limit_paper) > 1)) {
- ++limit_paper;
- } else if ((player_move == 2) && (limit_scissors < ((1 / 3) * (limit_rock + (limit_paper - limit_rock))))) {
- ++limit_scissors;
- } else if ((player_move == 3) && ((limit_paper - limit_rock) > 1)) {
- ++limit_rock;
- }
- write("\nYou played ");
- if (player_move == 1) {
- write("rock.");
- } else if (player_move == 2) {
- write("paper.");
- } else {
- write("scissors.");
- }
- write("\nAI played ");
- if (ai_move == 1) {
- write("rock.");
- } else if (ai_move == 2) {
- write("paper.");
- } else {
- write("scissors.");
- }
- if (((ai_move == 1) && (player_move == 2)) || ((ai_move == 2) && (player_move == 3)) || ((ai_move == 3) && (player_move == 1))) {
- writeln("\nYou scored a point.");
- ++score_player;
- } else if (ai_move == player_move) {
- writeln("\nDraw round.");
- } else {
- writeln("\nAI scored a point.");
- ++score_ai;
- }
- writeln("Your score : ",score_player);
- writeln("AI score : ",score_ai);
- }
- if (score_player > score_ai) {
- writeln("You won !");
- } else if (score_player == score_ai) {
- writeln("Draw game.");
- } else {
- writeln("You lost.");
- }
- limit_rock = 3;
- limit_paper = 6;
- limit_scissors = 9;
- score_ai = 0;
- score_player = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement