How to resolve the algorithm Rock-paper-scissors step by step in the Fortran programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Rock-paper-scissors step by step in the Fortran 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 Fortran programming language
Source code in the fortran programming language
! compilation
! gfortran -std=f2008 -Wall -ffree-form -fall-intrinsics -fimplicit-none f.f08 -o f
!
! EXAMPLE
!
!$ ./f
!rock, paper, scissors? papier
!scoring computer choice (r) and your choice (p)
!rock, paper, scissors? sizzerz
!scoring computer choice (s) and your choice (s)
!rock, paper, scissors? quit
!scoring computer choice (r) and your choice (q)
! Who's keeping score anyway???
! 0.5 1.5
! you won!
!$
program rpsgame
integer, parameter :: COMPUTER=1, HAPLESSUSER=2
integer, dimension(3) :: rps = (/1,1,1/)
real, dimension(3) :: p
character :: answer, cc ! computer choice
integer :: exhaustion, i
real, dimension(2) :: score = (/0, 0/)
character(len=8), dimension(3) :: choices = (/'rock ','paper ','scissors'/)
real :: harvest
do exhaustion = 1, 30
p = rps/real(sum(rps))
p(2) = p(2) + p(1)
p(3) = p(3) + p(2)
call random_number(harvest)
i = sum(merge(1,0,harvest.le.p)) ! In memory of Ken Iverson, logical is more useful as integer.
cc = 'rsp'(i:i)
write(6, "(2(A,', '),A,'? ')", advance='no')(trim(choices(i)),i=1,size(choices))
read(5, *) answer
write(6, "('scoring computer choice (',A,') and your choice (',A,')')")cc,answer
if (answer.eq.cc) then
score = score + 0.5
else
i = HAPLESSUSER
if (answer.eq.'r') then
if (cc.eq.'p') i = COMPUTER
else if (answer.eq.'p') then
if (cc.eq.'s') i = COMPUTER
else if (answer.eq.'s') then
if (cc.eq.'r') i = COMPUTER
else
exit
endif
score(i) = score(i) + 1
end if
i = scan('rps',answer)
rps(i) = rps(i) + 1
end do
if (25 .lt. exhaustion) write(6, *) "I'm bored out of my skull"
write(6, *)"Who's keeping score anyway???"
write(6, '(2f5.1)') score
if (score(COMPUTER) .lt. score(HAPLESSUSER)) print*,'you won!'
end program rpsgame
$ yes r | ./f # rock
rock, paper, scissors? scoring computer choice (r) and your choice (r)
rock, paper, scissors? scoring computer choice (s) and your choice (r)
rock, paper, scissors? scoring computer choice (r) and your choice (r)
...
rock, paper, scissors? scoring computer choice (p) and your choice (r)
I'm bored out of my skull
Who's keeping score anyway???
25.5 4.5
yes: standard output: Broken pipe
yes: write error
$ yes p 2>/dev/null | ./f # paper
rock, paper, scissors? scoring computer choice (r) and your choice (p)
rock, paper, scissors? scoring computer choice (s) and your choice (p)
rock, paper, scissors? scoring computer choice (r) and your choice (p)
rock, paper, scissors? scoring computer choice (s) and your choice (p)
...
rock, paper, scissors? scoring computer choice (s) and your choice (p)
I'm bored out of my skull
Who's keeping score anyway???
25.5 4.5
$ yes scissors 2>/dev/null | ./f # scissors
rock, paper, scissors? scoring computer choice (r) and your choice (s)
rock, paper, scissors? scoring computer choice (r) and your choice (s)
...
rock, paper, scissors? scoring computer choice (r) and your choice (s)
I'm bored out of my skull
Who's keeping score anyway???
26.5 3.5
$
You may also check:How to resolve the algorithm Roots of a function step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Compile-time calculation step by step in the Tcl programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the Lua programming language
You may also check:How to resolve the algorithm Fractran step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Circles of given radius through two points step by step in the 11l programming language