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?