How to resolve the algorithm Rock-paper-scissors step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Rock-paper-scissors step by step in the J 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 J programming language

Source code in the j programming language

require'general/misc/prompt strings' NB. was 'misc strings' in older versions of J
game=:3 :0
  outcomes=. rps=. 0 0 0
  choice=. 1+?3
  while.#response=. prompt'  Choose Rock, Paper or Scissors: ' do.
    playerchoice=. 1+'rps' i. tolower {.deb response
    if.4 = playerchoice do.
      smoutput 'Unknown response.'
      smoutput 'Enter an empty line to quit'
      continue.
    end.
    smoutput '    I choose ',choice {::;:'. Rock Paper Scissors'
    smoutput (wintype=. 3 | choice-playerchoice) {:: 'Draw';'I win';'You win'
    outcomes=. outcomes+0 1 2 = wintype
    rps=. rps+1 2 3=playerchoice
    choice=. 1+3|(?0) I.~ (}:%{:)+/\ 0, rps
  end.
  ('Draws:','My wins:',:'Your wins: '),.":,.outcomes
)


   game''
  Choose Rock, Paper or Scissors: rock
    I choose Scissors
You win
  Choose Rock, Paper or Scissors: rock
    I choose Paper
I win
  Choose Rock, Paper or Scissors: rock
    I choose Paper
I win
  Choose Rock, Paper or Scissors: rock
    I choose Paper
I win
  Choose Rock, Paper or Scissors: rock
    I choose Paper
I win
  Choose Rock, Paper or Scissors: 
Draws:     0
My wins:   4
Your wins: 1


  

You may also check:How to resolve the algorithm Assertions step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Infinity step by step in the Klingphix programming language
You may also check:How to resolve the algorithm Matrix chain multiplication step by step in the Rust programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Pure programming language
You may also check:How to resolve the algorithm Cheryl's birthday step by step in the Visual Basic .NET programming language