How to resolve the algorithm Rock-paper-scissors step by step in the PARI/GP programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Rock-paper-scissors step by step in the PARI/GP programming language

Table of Contents

Problem Statement

Implement the classic children's game Rock-paper-scissors, as well as a simple predictive   AI   (artificial intelligence)   player. Rock Paper Scissors is a two player game. Each player chooses one of rock, paper or scissors, without knowing the other player's choice. The winner is decided by a set of rules:

If both players choose the same thing, there is no winner for that round. For this task, the computer will be one of the players. The operator will select Rock, Paper or Scissors and the computer will keep a record of the choice frequency, and use that information to make a weighted random choice in an attempt to defeat its opponent.

Support additional choices   additional weapons.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Rock-paper-scissors step by step in the PARI/GP programming language

Source code in the pari/gp programming language

contest(rounds)={
  my(v=[1,1,1],wins,losses); \\ Laplace rule
  for(i=1,rounds,
    my(computer,player,t);
    t=random(v[1]+v[2]+v[3]);
    if(t
      if(t
    );
    print("Rock, paper, or scissors?");
    t = Str(input());
    if(#t,
      player=Vec(t)[1];
      if(player <> "R" && player <> "P", player = "S")
    ,
      player = "S"
    );
    if (player == "R", v[2]++);
    if (player == "P", v[3]++);
    if (player == "S", v[1]++);
    print1(player" vs. "computer": ");
    if (computer <> player,
      if((computer == "R" && player = "P") || (computer == "P" && player = "S") || (computer == "S" && player == "R"),
        print("You win");
        losses++
      ,
        print("I win");
        wins++
      )
    ,
      print("Tie");
    )
  );
  [wins,losses]
};
contest(10)

  

You may also check:How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the Ada programming language
You may also check:How to resolve the algorithm Parsing/Shunting-yard algorithm step by step in the Phix programming language
You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the Objeck programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the R programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the FunL programming language