Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

guessing a-z

Name: Mattew 2007-01-26 2:49

hey, i could use a little help with this code for C++. I have been at it for several hours now and being a fresh new programmer to C++. I was wondering if someone could give me some help or sutocode for this game.

And here is the rand() code that I have:
// ASCII code for lower case a-z.
char randChoice = rand() % 25 + 98;

Write a game that picks a random letter between ‘a’ and ‘z’ (the lowercase alphabet). Read a character (and the newline that must follow it) and tell the user how many letters away they are. Repeat until they guess the right one.

Name: Anonymous 2007-01-26 3:31

pseudo code, not suto

Name: Anonymous 2007-01-26 3:33

dont write this in C++

Name: Anonymous 2007-01-26 3:45

lol, pseudo code, gotcha. wasn't really paying attention there.
ok, well, I have writen a number guessing game and I have been trying to use it as a base to write this letter guessing game. Although, matching the random letter and letter chosen with cin.get() is eluding me.

Name: Mattew 2007-01-26 4:11

Lolz, omg, nm. I finally figured it out myself. I had to force the char userChoice into an int by using (int)userChoice, then compare the two. And then i could use an if-then statement to compare and cout the results.

Name: Anonymous 2007-01-26 4:59

Use C

Name: Anonymous 2007-01-26 16:12

Use C++

Name: 1 2007-01-26 17:31

Thanks >>3, your wise. Here's what I ended up with


my @words;
push @words, $_ foreach a..z;

while (1) {
        print "Guess: ";
        chomp(my $guess = <STDIN>);
        if ($guess eq $words[int rand @words]) {
                print "You guessed right!\n";
        }
        else {
                print "Try harder.\n";
        }
}

Name: Anonymous 2007-01-26 18:41

>>1
shitCase detected, please start over.

Name: Anonymous 2007-01-26 19:16

>>3

import Control.Monad
import Data.Char
import System.Random

play :: Char -> String -> [String]
play letter guesses =
    map offset $ takeWhile (/= letter) guesses
    where
      offset :: Char -> String
      offset guess
          | guess `elem` ['a'..'z'] =
              case (abs ((ord guess)  - (ord letter))) of
                1 -> "You are off by 1 letter."
                n -> "You are off by " ++ show n ++ " letters."
          | otherwise =
              guess : " is not a letter between a and z."

main :: IO ()
main =
    do letter <- randomRIO ('a','z')
       guesses <- liftM (filter (/= '\n')) getContents
       putStrLn "Guess a letter between a and z"
       mapM_ putStrLn $ play letter guesses
       putStrLn "You Win!"

Name: Anonymous 2007-01-26 21:18

>>10
Char *is* an instance of Ord. That use of elem isn't very pretty.

Name: Anonymous 2007-01-26 21:28

>>8
your code
is
STUPID!

Name: Anonymous 2007-01-26 21:40

>>11
It's pretty in its own way. And wonderfully inefficient. But, I assume that someday the compiler will be smart enough to rewrite it to the more efficient:

| guess >= 'a' && guess <= 'z'




Name: Anonymous 2007-01-27 0:26

>>12
MY CODE IS AWESOME >:(

Name: Anonymous 2007-01-27 4:56

>>10
I lol, Haskell lovers pretend their code is awesome and here I see a huge pile of crap that would be more readable in assembly language if I wrote it :D

Name: Anonymous 2007-01-27 6:45

>>15
I lol, [insert language] lovers pretend their code is awesome and here I see a huge pile of crap that would be more readable in assembly language if I wrote it :D

Name: Anonymous 2007-01-27 15:53

>>1-16,18-999999
I lol, assembly lovers pretend their code is awesome and here I see a huge pile of crap that would be more readable in [insert language] if I wrote it :D

Name: Anonymous 2007-01-27 16:31

>>17
I don't see your assembly code there, so stfu

Name: Anonymous 2007-01-27 17:28

>>15
I lol, it's supposed to be an unreadable pile of crap. Who would actually try to implement it that way? :D

Name: Anonymous 2007-01-27 17:40

>>18
I lol, assembly lovers pretend their code is awesome and here I see a huge pile of crap that would be more readable in [insert language] if I wrote it :D

Name: Anonymous 2007-01-28 1:36

Is there any reason at all for Haskell?

Name: Anonymous 2007-01-28 4:14

>>20
copy pasta troll is made of fail

Name: Anonymous 2007-01-28 4:25

>>22

Announcing obvious failure is made of fail.

Name: Anonymous 2007-01-28 4:29

// c++ ftw~
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cctype>

int main (int, char**)
{
    std::srand (std::time(0));
    int truth = std::rand() % 26;
    for (;;)
    {
        std::puts ("YOUR GUESS SIR: ");
        int guess = std::toupper (std::getchar()) - 'A';
        while (std::getchar() != '\n')
            ;
        if (guess >= 0 && guess < 26)
        {
            if (guess == truth)
            {
                std::puts ("YOU ARE A WINNER\n");
                break;
            }
            else
                std::printf ("YOU ARE %d LETTERS AWAY\n", std::abs (truth - guess));
        }
        else
            std::printf ("THAT'S NOT A LETTER\n");
    }
    return 0;
}

Name: Anonymous 2007-01-28 5:46

>>23
not if you're the first, and you were the second, gtfo troll

Name: Anonymous 2010-11-29 14:04

<code>wat</code>

 faggts

<quote>HMM</quote>

<b>testan</b>

Name: Anonymous 2010-11-29 14:05

wa

 faggts

[quote]HMM[/quote]

testan

Name: Anonymous 2010-11-29 14:09

[code=c]printf("BBCODE")[/code]

 faggts

[quote="22"]
HMM
[/quote]

Name: Anonymous 2010-11-29 14:10

| wat the fuck

Name: Anonymous 2010-11-29 14:12

<span class='quote'>not CCIE</span>

Name: Anonymous 2010-11-29 14:15

[quote]faggots[quote]

[quote]fgts

[quot]fgs[/quot]

Name: Anonymous 2010-12-17 1:25

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

Name: Sgt.Kabu狞䙙kiman鮡叞 2012-05-28 22:06

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List