Jump to content

Rock-paper-scissors: Difference between revisions

m
Moved Sidef to the right position
m (Added Sidef)
m (Moved Sidef to the right position)
Line 2,837:
Please type in 1 for Rock, 2 for Paper, 3 for Scissors, q to quit q
Goodbye! Thanks for playing!
</pre>
 
=={{header|Tcl}}==
<lang tcl>package require Tcl 8.5
 
### Choices are represented by integers, which are indices into this list:
### Rock, Paper, Scissors
### Normally, idiomatic Tcl code uses names for these sorts of things, but it
### turns out that using integers simplifies the move-comparison logic.
 
# How to ask for a move from the human player
proc getHumanMove {} {
while 1 {
puts -nonewline "Your move? \[R\]ock, \[P\]aper, \[S\]cissors: "
flush stdout
gets stdin line
if {[eof stdin]} {
puts "\nBye!"
exit
}
set len [string length $line]
foreach play {0 1 2} name {"rock" "paper" "scissors"} {
# Do a prefix comparison
if {$len && [string equal -nocase -length $len $line $name]} {
return $play
}
}
puts "Sorry, I don't understand that. Try again please."
}
}
 
# How to ask for a move from the machine player
proc getMachineMove {} {
global states
set choice [expr {int(rand() * [::tcl::mathop::+ {*}$states 3])}]
foreach play {1 2 0} count $states {
if {[incr sum [expr {$count+1}]] > $choice} {
puts "I play \"[lindex {Rock Paper Scissors} $play]\""
return $play
}
}
}
 
# Initialize some state variables
set states {0 0 0}
set humanWins 0
set machineWins 0
 
# The main game loop
while 1 {
# Get the moves for this round
set machineMove [getMachineMove]
set humanMove [getHumanMove]
# Report on what happened
if {$humanMove == $machineMove} {
puts "A draw!"
} elseif {($humanMove+1)%3 == $machineMove} {
puts "I win!"
incr machineWins
} else {
puts "You win!"
incr humanWins
}
puts "Cumulative scores: $humanWins to you, $machineWins to me"
# Update the state of how the human has played in the past
lset states $humanMove [expr {[lindex $states $humanMove] + 1}]
}</lang>
Sample run:
<pre>
Your move? [R]ock, [P]aper, [S]cissors: rock
I play "Scissors"
You win!
Cumulative scores: 1 to you, 0 to me
Your move? [R]ock, [P]aper, [S]cissors: r
I play "Paper"
I win!
Cumulative scores: 1 to you, 1 to me
Your move? [R]ock, [P]aper, [S]cissors: s
I play "Paper"
You win!
Cumulative scores: 2 to you, 1 to me
Your move? [R]ock, [P]aper, [S]cissors: sciss
I play "Paper"
You win!
Cumulative scores: 3 to you, 1 to me
Your move? [R]ock, [P]aper, [S]cissors: p
I play "Paper"
A draw!
Cumulative scores: 3 to you, 1 to me
Your move? [R]ock, [P]aper, [S]cissors: zaphod beeblebrox
Sorry, I don't understand that. Try again please.
Your move? [R]ock, [P]aper, [S]cissors: r
I play "Scissors"
You win!
Cumulative scores: 4 to you, 1 to me
Your move? [R]ock, [P]aper, [S]cissors: ^D
Bye!
</pre>
 
Line 3,033 ⟶ 2,936:
>> My play: r Paper covers rock Your point
4:1 Play: q
</pre>
 
=={{header|Tcl}}==
<lang tcl>package require Tcl 8.5
 
### Choices are represented by integers, which are indices into this list:
### Rock, Paper, Scissors
### Normally, idiomatic Tcl code uses names for these sorts of things, but it
### turns out that using integers simplifies the move-comparison logic.
 
# How to ask for a move from the human player
proc getHumanMove {} {
while 1 {
puts -nonewline "Your move? \[R\]ock, \[P\]aper, \[S\]cissors: "
flush stdout
gets stdin line
if {[eof stdin]} {
puts "\nBye!"
exit
}
set len [string length $line]
foreach play {0 1 2} name {"rock" "paper" "scissors"} {
# Do a prefix comparison
if {$len && [string equal -nocase -length $len $line $name]} {
return $play
}
}
puts "Sorry, I don't understand that. Try again please."
}
}
 
# How to ask for a move from the machine player
proc getMachineMove {} {
global states
set choice [expr {int(rand() * [::tcl::mathop::+ {*}$states 3])}]
foreach play {1 2 0} count $states {
if {[incr sum [expr {$count+1}]] > $choice} {
puts "I play \"[lindex {Rock Paper Scissors} $play]\""
return $play
}
}
}
 
# Initialize some state variables
set states {0 0 0}
set humanWins 0
set machineWins 0
 
# The main game loop
while 1 {
# Get the moves for this round
set machineMove [getMachineMove]
set humanMove [getHumanMove]
# Report on what happened
if {$humanMove == $machineMove} {
puts "A draw!"
} elseif {($humanMove+1)%3 == $machineMove} {
puts "I win!"
incr machineWins
} else {
puts "You win!"
incr humanWins
}
puts "Cumulative scores: $humanWins to you, $machineWins to me"
# Update the state of how the human has played in the past
lset states $humanMove [expr {[lindex $states $humanMove] + 1}]
}</lang>
Sample run:
<pre>
Your move? [R]ock, [P]aper, [S]cissors: rock
I play "Scissors"
You win!
Cumulative scores: 1 to you, 0 to me
Your move? [R]ock, [P]aper, [S]cissors: r
I play "Paper"
I win!
Cumulative scores: 1 to you, 1 to me
Your move? [R]ock, [P]aper, [S]cissors: s
I play "Paper"
You win!
Cumulative scores: 2 to you, 1 to me
Your move? [R]ock, [P]aper, [S]cissors: sciss
I play "Paper"
You win!
Cumulative scores: 3 to you, 1 to me
Your move? [R]ock, [P]aper, [S]cissors: p
I play "Paper"
A draw!
Cumulative scores: 3 to you, 1 to me
Your move? [R]ock, [P]aper, [S]cissors: zaphod beeblebrox
Sorry, I don't understand that. Try again please.
Your move? [R]ock, [P]aper, [S]cissors: r
I play "Scissors"
You win!
Cumulative scores: 4 to you, 1 to me
Your move? [R]ock, [P]aper, [S]cissors: ^D
Bye!
</pre>
 
2,747

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.