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

Programming some game

Name: Anonymous 2007-03-16 11:30 ID:+Cao99ew

Okay.. i'm a noob at C and i'm trying to do some game.
It's a game for 2 players. The first player inputs 4 numbers. The 2nd player tries to guess which numbers those are by inputting numbers too. If there is a correct number in wrong order, 0 will print. If there is a correct number in correct order, 1 will print. Game will end if the 2nd player gets 1111 or uses up all 10 turns.

I can't understand the array stuff much.. help me.

Name: Anonymous 2007-03-16 11:46 ID:lma9bMLf

>>1
post your source code and ask what you don't understand in it.

Name: Anonymous 2007-03-16 11:55 ID:+Cao99ew

>>2
I haven't started yet because I don't even have an idea how to do this.

Name: Anonymous 2007-03-16 11:57 ID:2TFeIRdr

>>3
Battle-- Mastermind, motherfucker, do you know of it?

Name: Anonymous 2007-03-16 12:04 ID:+Cao99ew

>>4
Seriously, I don't. This is the first time i've visited this board, and our teacher was nothing more than a fuckable fucktoy. She taught us the basics yet expects us to make a functional program.

Name: Anonymous 2007-03-16 12:40 ID:RRc9gyTb

Specify what you do not understand about arrays.

Name: Anonymous 2007-03-16 12:43 ID:+Cao99ew

Well, arrays are supposed to allow me to create that game but I dunno how.

Name: Anonymous 2007-03-16 12:52 ID:gfSEJL7e

    module Main where

    import Control.Monad

    playerQuery :: String -> IO [Int]
    playerQuery msg = do
                putStrLn msg
                d <- liftM (map (\x -> (read [x]) :: Int)) getLine
                when (length d /= 4) (error "We want four nonnegative digits in a single word, fatass.")
                return d

    compareDigits :: [Int] -> [Int] -> [Int]
    compareDigits xs ys =
                  if null ys then []
                     else if xs == ys
                              then [1,1,1,1]
                              else map (\x -> case (elem x ys) of True -> 0; _ -> -1) xs


     gameloop :: [Int] -> [Int] -> Int -> IO ()
     gameloop c t r =
            case r of
                 0 -> do
                         c <-playerQuery "Player 1: Input a four nonnegative digit number to start the game: "
                         gameloop c t 11
                 1  -> do putStrLn "You've lost the game, fatass."
                          error "You have five seconds left to live."
                 _  -> do
                         let guess = compareDigits c t
                             case guess of
                              [] -> do return ()
                              [1,1,1,1] -> do putStrLn "You've won the game! Fatass."
                                              error "Congratulations"
                              _         -> do putStrLn $ "Here's how your guess fared: " ++ show guess
                                              putStrLn $ "You have " ++ show (r - 1) ++ " attempt(s) left."
                         t <- playerQuery "Player 2: Guess a four digit number to continue: "
                         gameloop c t (r - 1)

    main :: IO ()
    main = gameloop [] [] 0

hope that helps!

Name: Anonymous 2007-03-16 12:56 ID:RRc9gyTb

Well, let me explain. An array is a way to store severable variables of the same type in one structure. For example, if we wanted to create a structure called "test" containing ints, we'll declare:

int test[X]; //X is the number of variables you want in your array

So if I declare:
 
int array[4];

array contains 4 ints

To store something, you do:

array[X]=4; //where X is where you want to store data

The first term of an array is always 0. So if you want to have an array of 4 ints containing numbers 1, 2, 3 and 4, you'll do:

int array[4];
array[0]=1;
array[1]=2;
array[2]=3;
array[3]=4;

You access your array the same way than you write in it; for example:

printf("First term of arrat is %i", array[0]);

For your game, you could use two arrays, one for the numbers entered by player 1, and one for numbers enter by player 2. Then you would compare the values of the arrays as follows:

int i;
for(i=0;i<4;i++)
{
  if player1[i]==player2[i]
    puts("1");
  else
    puts("0");
}

Name: Anonymous 2007-03-16 14:41 ID:+Cao99ew

>>8
Thanks, but I need C not C++. And the one we're using is quite ancient, Turbo C so it's picky >.>

Meanwhile, i'll try to figure something out based on the replies.

Name: Anonymous 2007-03-16 15:03 ID:+Cao99ew

I'm running out of time.. I need it on C.. something that works on the old Turbo C preferably

Name: Anonymous 2007-03-16 15:12 ID:gfSEJL7e

>>11
Lol. If you don't get it working, submit >>8

Name: Anonymous 2007-03-16 20:20 ID:qNzvkiok

Listen to >>9 he knows what he's talking about.

Name: Anonymous 2007-03-17 6:11 ID:j4jJ2iSQ

How do I specify which parts of the array to compare? The 4 digits should match each other completely in order to get 1111.

Name: Anonymous 2007-03-17 7:04 ID:BAPoB7OR

int i;
for(i=0;i<4;i++)
{
  if player1[i]==player2[i]
    puts("1");
  else
    puts("0");
}

Here, i increases; i specifies which parts of the arrays to compare (hence the player1[i])

Name: Anonymous 2007-03-17 7:07 ID:rbisXppE

>>15

Way to write some shitty code, fuckstick.

Name: Anonymous 2007-03-17 7:07 ID:Heaven

>>10
FYI, that's not C++.

Name: Anonymous 2007-03-17 7:49 ID:BAPoB7OR

>>16

what about a big nice cup of STFU ?

Name: Anonymous 2007-03-17 9:14 ID:Heaven

>>18
I'm sure he'd appreciate it. Now why don't you pour yourself a nice glass of 'noob faggot' and follow that up with a tankard of 'thread over due to retards insulting superior programmers'

kthx

Name: Anonymous 2007-03-17 11:01 ID:j4jJ2iSQ

Someone please tell me what's wrong with this:
#include <stdio.h>
main ();
int player1[4];
int player2[4];
int i;
for(i=0;i<4;i++)
{
int number;
printf ("Enter Player 1 number:/n");
scanf ("%d", &number);
player1[i]=number;
}
printf ("Enter Player 2 number:/n");
for (i=0;i<4;i++)
{
int number;
printf ("Please insert a number:/n");
scanf ("%d", &number);
player2[i]=number;
}

for(i=0;i<4;i++)
{
  if player1[i]==player2[i]
    puts("1");
  else
    puts("0");
}


Aside from i'm a fucking noob. I got 21 errors, using Dev C++ 4.9.9.2

Name: Anonymous 2007-03-17 11:20 ID:tbIcxepU

Here you go but I want you to understand your errors :

#include <stdio.h>
int main ()/*here you didn't specify main return type and you put a semicolon at the end(WTF)*/
{//you forgot it
int player1[4];
int player2[4];
int i;
for(i=0;i<4;i++)
{
int number;
printf ("Enter Player 1 number:/n");
scanf ("%d", &number);
player1[i]=number;
}
printf ("Enter Player 2 number:/n");
for (i=0;i<4;i++)
{
int number;
printf ("Please insert a number:/n");
scanf ("%d", &number);
player2[i]=number;
}

for(i=0;i<4;i++)
{
  if (player1[i]==player2[i])
    puts("1");
  else
    puts("0");
}

return 0;//you forgot to return 0
}//you forgot this too

Compiled with GCC using Code::Blocks you may need to add system("PAUSE"); at the end if you want the program to stop at the end

Name: Anonymous 2007-03-17 11:33 ID:j4jJ2iSQ

>>21
Thanks a lot. Another noob question (Maybe I really shouldn't be programming..), how come the only thing it does is ask me for the Player 1 number?

Name: Anonymous 2007-03-17 11:35 ID:j4jJ2iSQ

>>22
Disregard that. I just found out that it asks for any 4 inputs and I didn't even realize it sooner (What a fucktard.). Now I need to think of a way how to require 4 digits.. Also, it doesn't compare and just terminates when you input 4 times for Player 2.

Name: Anonymous 2007-03-17 11:41 ID:OlBFoWxb

Faggots. I wrote a functioning version in >>8

Name: Anonymous 2007-03-17 11:47 ID:j4jJ2iSQ

>>24
But I need it in C, preferably something that would work on Turbo C. My prof won't accept anything else since she's old fashioned (though quite young and hawt, what a shame). I don't recognize anything from what you wrote so I doubt she'll accept it, and I also need to be able to explain to her how I did it.

Name: Anonymous 2007-03-17 11:56 ID:knrv8WBu

>>25
>>8 uses Turbo C extensions, just send it in and she'll be overjoyed (wink, wink!).

Name: Anonymous 2007-03-17 12:32 ID:bpKL43Zg

>>25
your prof is dumbass and incompetent, go to another school.

Name: Anonymous 2007-03-17 12:43 ID:j4jJ2iSQ

>>27
I know that.. but I really need it.

Name: Anonymous 2007-03-17 13:32 ID:lwMb8DXj

>>24
But your solution is incorrect. The program should print a 1 if there is a correct number in the correct position. Your solution just prints a 0 if there is a correct number regardless of position.

Name: Anonymous 2007-03-17 13:37 ID:OlBFoWxb

>>29
>>If there is a correct number in wrong order, 0 will print.

read the specification moran

Name: Anonymous 2007-03-17 13:39 ID:lwMb8DXj

>>30
>>If there is a correct number in correct order, 1 will print.

NO U

Name: Ze Killer Of Poussin !SsWAPkwaM6 2007-03-17 13:52 ID:bpKL43Zg

IN C, 0 IS FALSE AND EVERYTHING FUCKING ELSE IS TRUE FAGGOTS, HA!

Name: Anonymous 2007-03-17 14:08 ID:OlBFoWxb

>>31
IT DOES, MORAN

Name: Anonymous 2007-03-17 14:19 ID:lwMb8DXj

>>33
No, it doesn't.

The first player enters 1 2 3 4 and the second player guesses 1 2 4 5. Your solution prints 0 0 0 -1, but it should print 1 1 0 -1.

Name: Anonymous 2007-03-17 14:32 ID:OlBFoWxb

oh

Name: Anonymous 2007-03-17 14:45 ID:LX+uL7j4

use putchar((int)itoa(strcmp(&i[player1], &i[player2])));

Name: Anonymous 2007-03-17 14:56 ID:j4jJ2iSQ

>>36
Put that where?

And no, nothing prints whatever I put. It terminates after input for player2.

Name: Anonymous 2007-03-17 15:01 ID:JJuzhXMI

>>21
Yay for Code::Blocks. I downloaded a f-load of IDEs and recommend that one for implementing "code folding," which is in Visual C these days...  If you want something for good old C with a win32 slant, there's Pelle's C.

I wish everyone would implement Pelle's little visual dotted line brace match. It's a life saver when your shit is 90% spaguetty control structures :)

Name: Anonymous 2007-03-17 15:16 ID:OlBFoWxb

>>34
Hope UR happy now!

compareDigits :: [Int] -> [Int] -> [Int]
compareDigits xs ys =
              if null ys then []
                 else map (\(a,b) -> if a == b then 1
                            else if elem a ys then 0
                                  else -1) (zip xs ys)

Name: Anonymous 2007-03-17 15:18 ID:91y7I78j

>>38
I wish everyone would implement Pelle's little visual dotted line brace match.
Tried SciTE?

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