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:
Anonymous2007-03-16 11:46 ID:lma9bMLf
>>1
post your source code and ask what you don't understand in it.
Name:
Anonymous2007-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:
Anonymous2007-03-16 11:57 ID:2TFeIRdr
>>3
Battle-- Mastermind, motherfucker, do you know of it?
Name:
Anonymous2007-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:
Anonymous2007-03-16 12:40 ID:RRc9gyTb
Specify what you do not understand about arrays.
Name:
Anonymous2007-03-16 12:43 ID:+Cao99ew
Well, arrays are supposed to allow me to create that game but I dunno how.
Name:
Anonymous2007-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:
Anonymous2007-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:
Anonymous2007-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:
Anonymous2007-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
>>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:
Anonymous2007-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:
Anonymous2007-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:
Anonymous2007-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:
Anonymous2007-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.
>>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:
Anonymous2007-03-17 11:56 ID:knrv8WBu
>>25 >>8 uses Turbo C extensions, just send it in and she'll be overjoyed (wink, wink!).
Name:
Anonymous2007-03-17 12:32 ID:bpKL43Zg
>>25
your prof is dumbass and incompetent, go to another school.
>>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:
Anonymous2007-03-17 13:37 ID:OlBFoWxb
>>29
>>If there is a correct number in wrong order, 0 will print.
read the specification moran
Name:
Anonymous2007-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!SsWAPkwaM62007-03-17 13:52 ID:bpKL43Zg
IN C, 0 IS FALSE AND EVERYTHING FUCKING ELSE IS TRUE FAGGOTS, HA!
And no, nothing prints whatever I put. It terminates after input for player2.
Name:
Anonymous2007-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:
Anonymous2007-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:
Anonymous2007-03-17 15:18 ID:91y7I78j
>>38 I wish everyone would implement Pelle's little visual dotted line brace match.
Tried SciTE?
>>37
I told you, you need to add system("PAUSE"); at the end
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:
Anonymous2007-03-17 15:50 ID:OlBFoWxb
>>41
sigh
compareDigits _ [] = []
compareDigits xs ys = zipWith (\a b -> if a == b then 1
else if elem a ys then 0 else -1) xs ys
too bad i need ys in the anonymous function or this could be made pointless
Name:
Ze Killer Of Poussin!SsWAPkwaM62007-03-17 16:06 ID:bpKL43Zg
>>42
system("PAUSE") IS NOT STANDARD SO YOU'RE WELCOME, MY DEAR FAGGOT, TO NOT LEARN BAD HABIT TO OUR FRIEND, HE SHALL PUT getchar() AT THE END OF MAIN OR LAUNCH IT FROM THE CONSOLE, HA!
Name:
Anonymous2007-03-17 16:09 ID:OlBFoWxb
OR USE A NONWINDOWS SYSTEM THAT DOESN'T CLOSE THE WINDOW ON PROGRAM EXIT
Name:
Anonymous2007-03-17 16:14 ID:tbIcxepU
int system(const char *); is standard sir.
Name:
Ze Killer Of Poussin!SsWAPkwaM62007-03-17 16:18 ID:bpKL43Zg
>>46
LEARN TO READ MORON, I DIDN'T SAY system() IS NOT STANDARD, I SAID system("PAUSE") IS NOT, TRY YOUR FAGGOTRY ON ANOTHER SHIT THAN WINDOWS AND SEE THE EXTEND OF YOUR RETARDNESS.
Name:
Anonymous2007-03-17 16:26 ID:knrv8WBu
>>47
1. His pile of faggotry won't ever be turned into a real program.
2. Anything he compiles won't run on any platform that doesn't have `pause.'
3. Caps lock is cruse control for COBOL.
4. Remove the system call when compiling.
Name:
Anonymous2007-03-17 16:27 ID:BAPoB7OR
47 fails.
Name:
Anonymous2007-03-17 16:33 ID:tbIcxepU
>>47
TO MAKE MR."I CARE ABOUT MAC AND LINUX AND I LICK MR.TARBALLS' ASS" HAPPY I'LL CORRECT MY CODE.
#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");
}
printf("PRESS ENTER TO LEAVE THE PROGRAM AND EXTEND YOUR FAGGOTRY AND I HOPE YOU ENJOYED USING LINUX TO RUN THIS PROGRAM IF YOU USED IT");
getchar();//BOO
return 0;//you forgot to return 0
}//you forgot this too
Name:
Ze Killer Of Poussin!SsWAPkwaM62007-03-17 16:39 ID:bpKL43Zg
>>48
YOU STILL DON'T UNDERSTAND, LET'S START FROM THE BEGINNING (AND IT SEEMS LIKE I DON'T SCREAM LOUDLY ENOUGH OR YOU'RE THE BIGGEST MORON I'VE EVER SEEN)).
YOUR FIRST MISTAKE IS TO TEACH HIM NON-ANSI C, BUT THE MOST DANGEROUS IS THAT YOU DON'T TELL HIM THIS IS NOT PART OF THE LANGUAGE. HE SHALL KNOW WHAT IS PART OF THE STANDARD C AND WHAT IS NOT, OR WILL HE RUN IN BIG TROUBLES. HOW MANY FUCKING TIMES ON PROGRAMMING BOARDS WE HAVE RETARDED STUDENT THAT COMES TO SAY "BWEE MY CODE DID WORK AT SCHOOL BUT DOESN'T WORK AT HOME HEEEELP !!!!1ONE". IF YOU WANT TO TEACH TO OTHERS, TEACH WELL OR DIE FAGGOT. AND YES I LOVE CAPS LOCK AND SHIT ON YOUR FACE, HA!
>>52
Ha ha, I like how easy it is to troll others here (I'm caps lock-man).
But anyway, I still think what I wrote (without the bad words, though), it's better to use only ANSI-C for beginners, I explained why, and also because it's so easy to shoot yourself in the head with this language, so we must try to not give them more bullets to shoot. It's not about "anyway he will run his crap only on windows", it's about not confuse beginners mind (experience here).
So, yes he can use system("PAUSE"); but he must knows what it implies and where it comes from. Like everything else in C, I must say.
Name:
Anonymous2007-03-17 17:20 ID:knrv8WBu
>>51
ANSI C is almost as gay as K&R C, real men use ISO C.
Name:
Anonymous2007-03-17 17:36 ID:bpKL43Zg
>>53
Ok, and just to show I'm not only a troll, here is my version (the only difference is that I use fgets() instead of scanf() and introducing error detection) #include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX_LENGTH 5
int
main(void)
{
char player1[MAX_LENGTH] = { 0 };
char player2[MAX_LENGTH] = { 0 };
int i;
puts("Player 1 : enter your number or GTFO");
fgets(player1, MAX_LENGTH + 1, stdin);
for (i = 0; i < MAX_LENGTH - 1; i++)
if (!isdigit(player1[i]))
{
puts("YOU'RE WELCOME TO ENTER NUMBER, FAGGOT");
exit(EXIT_FAILURE);
}
puts("Player 2 : enter your number or GTFO");
fgets(player2, MAX_LENGTH + 1, stdin);
for (i = 0; i < MAX_LENGTH - 1; i++)
if (!isdigit(player2[i]))
{
puts("YOU'RE WELCOME TO ENTER NUMBER, FAGGOT");
exit(EXIT_FAILURE);
}
for (i = 0; i < MAX_LENGTH - 1; i++)
if (player1[i] == player2[i])
puts("1");
else
puts("0");
puts
("THIS CODE WAS MADE BY ZE GREAT KILLER OF POUSSIN, ENJOY YOUR AIDS, HA!");
return 0;
}
Maybe with mistakes and/or incomplete, but it works.
Name:
Anonymous2007-03-17 17:41 ID:bpKL43Zg
>>55
shit, it's MAX_LENGTH + 1 in the arrays declarations (for '\n' and '\0')
puts("Player 1 : enter your numbers or GTFO");
fgets(player1, MAX_LENGTH + 1, stdin);
for (i = 0; i < MAX_LENGTH - 1; i++)
{
if (!isdigit(player1[i]))
{
puts("YOU'RE WELCOME TO ENTER NUMBER, FAGGOT");
exit(EXIT_FAILURE);
}
}
puts("Player 2 : enter your numbers or GTFO");
fgets(player2, MAX_LENGTH + 1, stdin);
for (i = 0; i < MAX_LENGTH - 1; i++)
{
if (!isdigit(player2[i]))
{
puts("YOU'RE WELCOME TO ENTER NUMBER, FAGGOT");
exit(EXIT_FAILURE);
}
}
for (i = 0; i < MAX_LENGTH - 1; i++)
{
if (player1[i] == player2[i])
puts("1");
else
puts("0");
}
puts
("THIS CODE WAS MADE BY ZE GREAT KILLER OF POUSSIN, ENJOY YOUR AIDS, HA!");
return EXIT_SUCCESS;
}
void pause()
{
puts("Press ENTER to continue.");
getchar();
}
What do you not understand? A functional sample C program isn't going to do jack shit unless you have some idea of what you need help with.
Name:
Anonymous2007-03-18 4:13 ID:1sFWNjzO
>>62
The program I gave IS simple. I made the effort to do it for anonyfag, if you don't make the effort to try to understand it by yourself (and asking what you don't understand) GO FUCK YOURSELF FUCKTARD. Stop programming and do knitting instead, you're wasting our time and yours.
Name:
Anonymous2007-03-18 4:26 ID:Kt3lB+6U
Sorry, I posted >>62 without looking at the 2nd page of the thread and thought it was hopeless. I'll try it out now, thanks.
Name:
Anonymous2007-03-18 4:28 ID:Kt3lB+6U
Ok I tried it out and it works, but I need to add more. Player 2 gets 10 tries to guess the correct series of numbers before losing the game. Also, i'd like to make proper error messages when invalid numbers are entered - not simply terminate the program.
Name:
Anonymous2007-03-18 4:30 ID:1sFWNjzO
>>66
Ok, one instant, you really thrilled me (btw, I'm >>55). Use >>58 version, he improved it for your convenience.
Name:
Anonymous2007-03-18 4:40 ID:1sFWNjzO
>>67
Try to do it yourself first, it's not the hardiest part. That's why my programm behavior is shitty : its just exits when the user put a wrong number.
Help :
- make a function that read what the user enters and verify if it is 4 digits, if it's not, ask him again (protip : recursion, the function will call itself). If you have understood what I've done, you have just 2 lines of code to write for this.
- for the ten tries, easy too. Define an int that will count until ten. If the user fail, increment it, when it reaches ten, exit the program.
Anything's ok ?
Name:
Anonymous2007-03-18 6:31 ID:Kt3lB+6U
I gave it a shot and it's god awful.. i'll keep working on it. Got 33 errors to find. Anyone wanna show tips?
while(mistake!=0)
{
for(i=0;i<4;i++)
{
mistake=0;
printf("Guess for number %i ?", i+1);
scanf("%i", &player2[i]);
}
puts("Results:");
for(i=0;i<4;i++)
{
if (player1[i]==player2[i])
printf("1");
else
{
printf("0");
mistake++;
}
}
puts("");
if (mistake!=0)
puts("Try again.");
}
puts("A winrar is you !");
return 0;
}
Name:
Anonymous2007-03-18 7:31 ID:1sFWNjzO
>>70 >>55 again.
Ok, here is the input function, try to do the rest, I'll post the full code later : #include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX_LENGTH 6
void player_input(char player[], const int n);
const int player_1 = 1;
const int player_2 = 2;
int main(void)
{
char player1[MAX_LENGTH] = {0};
char player2[MAX_LENGTH] = {0};
int i;
for (i = 0; i < MAX_LENGTH - 2; i++)
if (player1[i] == player2[i])
puts("1");
else
puts("0");
puts("THIS CODE WAS MADE BY ZE GREAT KILLER OF POUSSIN, ENJOY YOUR AIDS, HA!");
return EXIT_SUCCESS;
}
void player_input(char player[], const int n)
{
int i;
printf("player %d : enter numbers or GTFO\n", n);
fgets(player, MAX_LENGTH, stdin);
for (i = 0; i < MAX_LENGTH - 2; i++)
if (!isdigit(player[i]))
{
puts("bad input, try again moron");
player_input(player, n);
}
}
Make the ask_player() function, with a #define MAX_TRIES 10 and an int that will be compared to this value, while its inferior, ask the player, if it's 'y' or 'Y' return to the beginning of the program, else, exit. It's easy, 10 lines or code or something like that.
Name:
Anonymous2007-03-18 8:41 ID:Kt3lB+6U
>>71
It works, but it doesn't limit Player1 to enter a total of 4 digits.
Anyways, thanks for all the input everyone. I'm still working on understanding >>72
I only have a faint idea on what some of the lines do. I guess my professor was really incompetent for asking us to make a program when she didn't even teach us about these stuff.
Name:
Anonymous2007-03-18 8:44 ID:Kt3lB+6U
Another noob question (yet again), how do I make it so that the program won't exit instantly? I'm talking about >>72
STOP HELPING THIS GUY.
He's obviously too stupid and unmotivated.
Name:
Anonymous2007-03-18 11:09 ID:Kt3lB+6U
>>76
Yeah, I gotta admit I WAS unmotivated.. but now i'll try to do it.
Name:
Anonymous2007-03-18 11:14 ID:Kt3lB+6U
But seriously, I don't even know what #include <ctype.h>
#include <stdlib.h> are for.. My teacher said #include <stdio.h> was to call the library for commands, but that's it. Can you blame me if i'm stupid on C?
Name:
Anonymous2007-03-18 11:18 ID:0IsP5Opg
>>78 Can you blame me if i'm stupid on C?
Yes. If you can't learn things on your own when your teachers suck, you are just generally stupid.
Name:
Anonymous2007-03-18 12:48 ID:huXMppok
Yeah. If you want to learn C that bad and your teacher sucks, just go buy a book on C and learn by yourself.
Name:
Anonymous2007-03-18 13:24 ID:GYmEIqLH
The traditional way of teaching doesn't work well with learning programming languages. Rather, it's better to take a mentoring approach.
Name:
Anonymous2007-03-18 13:55 ID:1sFWNjzO
>>78
I must answer "yes". You know, searching on google doesn't involve any programming skills. As a programmer, this is a habit you have to acquire : searching in documentations (RTFM). No one will come knocking at your door and explain to you how to use its library. And I won't do it either. You can ask question about my own function, but fgets and other shits, search by yourself (man function on google), I held your hand too long and finally, I don't help you.
wikipedia has a presentation of the standard library, or else here http://www-ccs.ucsd.edu/c/
Here is also a good point to start from : http://clc-wiki.net/wiki/Main_Page
And like >>80 said, buy a book (K&R for example) or search for tutorials on the web.
I don't know what the fuck you have learned, but when I see how you are, I was smart to not follow my first idea and make my program with pointers, or you'd just killed yourself, because my code is very easy to understand...
Name:
Anonymous2007-03-18 21:05 ID:GM5AIKck
>>78
"ctype.h" contains structs (aggregated data structures) used by other headers, plus the handy dandy character range checkers, e.g. isdigit(), isalpha(), isalnum(), etc.
"stdio.h" contains "standard I/O" functions. Things like showing text, taking keyboard input, reading and writing files. printf(), puts(), putchar(), scanf(), getchar() and all their friends are in here.
"stdlib.h" includes stadard system calls, like exit(), malloc() (for allocating memory manually), free() (opposite), and a whole bunch of other things.
How do you learn all this? You RTFM . Get a book, search on Google. Linux (ha, like you're using it) has manual pages available at your fingertips that tell you exactly in which header a function is available in.