Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -23,7 +23,14 @@ function twentyfour()
|
|||
print("The 24 Game\nYou will be given any four digits in the range 1 to 9, which may have repetitions.\n
|
||||
Using just the +, -, *, and / operators show how to make an answer of 24.\n
|
||||
Use parentheses, (), to ensure proper order of evaulation.\n
|
||||
Enter 'n' for a new set of digits, and 'q' to quit. Good luck!\n
|
||||
Enter 'n' fDouble>()
|
||||
|
||||
while( scanner.hasNext() ) {
|
||||
if( scanner.hasNextInt() ) {
|
||||
var n = scanner.nextInt()
|
||||
|
||||
// Make sure they're allowed to use n
|
||||
if( n or a new set of digits, and 'q' to quit. Good luck!\n
|
||||
Here's your first 4 digits\n$(answer[1]) $(answer[2]) $(answer[3]) $(answer[4])\n
|
||||
>")
|
||||
while true
|
||||
|
|
|
|||
|
|
@ -7,17 +7,17 @@ Function isNumeric ($x)
|
|||
Return $isNum
|
||||
}
|
||||
|
||||
$NumberOne = Random -Maximum 10 -Minimum 1
|
||||
$NumberTwo = Random -Maximum 10 -Minimum 1
|
||||
$NumberThree = Random -Maximum 10 -Minimum 1
|
||||
$NumberFour = Random -Maximum 10 -Minimum 1
|
||||
$NumberArray = @()
|
||||
$NumberArray += $NumberOne
|
||||
$NumberArray += $NumberTwo
|
||||
$NumberArray += $NumberThree
|
||||
$NumberArray += $NumberFour
|
||||
While( $NumberArray.Count -lt 4 ){
|
||||
$NumberArray += Random -Minimum 1 -Maximum 10
|
||||
}
|
||||
|
||||
Write-Host "Welcome to the 24 game!`n`nHere are your numbers: $NumberOne,$NumberTwo,$NumberThree and $NumberFour.`nUse division, multiplication, subtraction and addition to get 24 as a result with these 4 numbers.`n"
|
||||
Write-Host @"
|
||||
Welcome to the 24 game!
|
||||
|
||||
Here are your numbers: $($NumberArray -join ",").
|
||||
Use division, multiplication, subtraction and addition to get 24 as a result with these 4 numbers.
|
||||
"@
|
||||
|
||||
Do
|
||||
{
|
||||
|
|
@ -26,50 +26,14 @@ $EndResult = $null
|
|||
$TempChar = $null
|
||||
$TempChar2 = $null
|
||||
$Count = $null
|
||||
|
||||
$AllowableCharacters = $NumberArray + "+-*/()".ToCharArray()
|
||||
$Result = Read-Host
|
||||
Foreach($Char in $Result.ToCharArray())
|
||||
{
|
||||
Switch($Char)
|
||||
{
|
||||
$NumberOne
|
||||
{
|
||||
}
|
||||
$NumberTwo
|
||||
{
|
||||
}
|
||||
$NumberThree
|
||||
{
|
||||
}
|
||||
$NumberFour
|
||||
{
|
||||
}
|
||||
"+"
|
||||
{
|
||||
}
|
||||
"-"
|
||||
{
|
||||
}
|
||||
"*"
|
||||
{
|
||||
}
|
||||
"/"
|
||||
{
|
||||
}
|
||||
"("
|
||||
{
|
||||
}
|
||||
")"
|
||||
{
|
||||
}
|
||||
" "
|
||||
{
|
||||
}
|
||||
Default
|
||||
{
|
||||
$Wrong = 1
|
||||
}
|
||||
}
|
||||
If( $AllowableCharacters -notcontains $Char ){ $Wrong = 1 }
|
||||
}
|
||||
|
||||
If($Wrong -eq 1)
|
||||
{
|
||||
Write-Warning "Wrong input! Please use only the given numbers."
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
require "rational"
|
||||
|
||||
def play
|
||||
digits = Array.new(4) {1+rand(9)}
|
||||
digits = Array.new(4){rand(1..9)}
|
||||
loop do
|
||||
guess = get_guess(digits)
|
||||
result = evaluate(guess)
|
||||
|
|
@ -17,13 +17,13 @@ end
|
|||
|
||||
def get_guess(digits)
|
||||
loop do
|
||||
print "\nEnter your guess using #{digits.inspect}: "
|
||||
guess = $stdin.gets.chomp
|
||||
print "\nEnter your guess using #{digits}: "
|
||||
guess = gets.chomp
|
||||
|
||||
# ensure input is safe to eval
|
||||
invalid_chars = guess.scan(%r{[^\d\s()+*/-]})
|
||||
unless invalid_chars.empty?
|
||||
puts "invalid characters in input: #{invalid_chars.inspect}"
|
||||
puts "invalid characters in input: #{invalid_chars}"
|
||||
next
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue