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

Pages: 1-

java beginner question

Name: hank aaron 2009-03-16 22:00

while programming the game mastermind I became stuck when having to figure out how to come up with the correct integers and i will figure the sum for these after i find them
for example

secretCode = 123

input = 222

output is correct integers: 1 and sum: 2

for input = 122

output is correct integers: 2 and sum: 3

Name: Anonymous 2009-03-16 22:01

would it be bestto convert to string then read off characters?

Name: Anonymous 2009-03-16 22:12

can you rephrase that with more clarity?

Name: Anonymous 2009-03-16 22:12

I love this thread!

Name: Anonymous 2009-03-16 22:25

What's this about a sum? The feedback should be # of correct numbers in position, and # of correct numbers out of position.

Name: Anonymous 2009-03-17 1:54

>>5
this is what my teacher wants us to do.

Name: Anonymous 2009-03-17 2:05

basically i need a nested loop that can read through a randomized set of integers and checks it against the user's guess, like in the game mastermind which i am sure you are all familiar with.

i know how to randomize a number/record input from a user/some other shit

what i need help with is checking the input against the randomized set ( i was thinking of converting it into a string and checking that way) also with checking that the input from the user is k digits long (k increases as the difficulty increases).

i am not retarded. i dont want you to do my homework. i just would like a little bit of help. it is 2am because i am naturally just a night person.

Name: Anonymous 2009-03-17 2:05

Better problem description and I will help you.
What is the input, a String, integers? What is the output expected to be. When is an integer 'correct' when it exists in the secretCode string at all; or if it is in the correct position?

Name: Anonymous 2009-03-17 2:15

integers are secretCode, and userInput

When the user puts in their guess userInput should be checked against the secretCode and the output should return the number of characters that are correct such as previously shown.

for secretCode= 1234
userInput = 2222

expected output should be
number of correct integers: 1
sum of correct integers: 1

for userInput = 2233
number of correct integers: 2
sum of correct integers: 5

for userInput = 222 or 22222 or 22 or 222222 etc.
the output should be an error message that you have failed to enter the appropriate amount of digits

i have an increment counter so that you are only allowed 10 guesses so it isnt a retarded infinite loop.

Name: Anonymous 2009-03-17 2:27

var secretCode=genSecret();
var userInput=prompt('Enter number','');
function genSecret(){return Math.floor(Math.random()*10000)}
function checkinp(inp){
var sec=secretCode.toString();var user=inp.toString();var sum=0;var numc=0;
for(var i in user){if(user[i]==sec[i]){numc++;sum+=parseInt(user[i])}}
return 'Correct numbers:'+numc+' Correct numbers sum:'+sum;
}
alert(checkinp(userInput))

Name: Anonymous 2009-03-17 2:31

Like this?

import java.util.Scanner;
import java.util.Random;
public class Mastermind {
    public static final int DEFAULTCODELENGTH = 3;
    private String code;
    private int codeLength;
    private int turns;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for(;;) {
            System.out.println("Would you like to play a game of matermind? (y/n)");
            if(sc.nextLine().toUpperCase().indexOf("Y")>=0) {
                System.out.println("How long would you like the code to be? (Default "+Mastermind.DEFAULTCODELENGTH+")");
                int codeLength = Mastermind.DEFAULTCODELENGTH;
                try {
                    codeLength = Integer.parseInt(sc.nextLine());
                    if(codeLength<=0 || codeLength>80) throw new NumberFormatException("Invalid code length");
                }
                catch(NumberFormatException e) {
                    System.out.println("Invalid code length, defaulting to three.");
                    codeLength = Mastermind.DEFAULTCODELENGTH;
                }
                Mastermind game = new Mastermind(codeLength);
                game.play();
            }
        }
    }
    public Mastermind(int codeLength) {
        Random rand = new Random();
        this.code = "";
        this.turns = 0;
        this.codeLength = codeLength;
        int character;
        for(int i=0; i<codeLength; i++) {
            code+=rand.nextInt(10)+"";
        }
    }
    public void play() {
        Scanner scan = new Scanner(System.in);
        for(;;) {
            System.out.println("Make a guess!");
            String input = scan.nextLine();
            try {
                Integer.parseInt(input); //NaN
                if(input.length()!=codeLength) throw new NumberFormatException("Invalid input!");
                int sum = 0, num = 0;
                for(int i=0; i<codeLength; i++) {
                    if(code.charAt(i)==input.charAt(i)) {
                        sum+=Integer.parseInt(code.charAt(i)+"");
                        num++;
                    }
                }
                turns++;
                System.out.println("Number of correct integers: "+num);
                System.out.println("Sum of correct integers: "+sum);
                if(code.equals(input)) {
                    System.out.println("You completed the puzzle in "+turns+" turns!");
                    return;
                }
            }
            catch(NumberFormatException e) {
                System.out.println("Invalid input specified! Must be a number with exactly "+codeLength+" digits.");
            }
        }
    }
}

Name: Anonymous 2009-03-17 2:36

>>11
yeah thats awesome. how did you import that with the tabs/etc not screwing up?

Name: Anonymous 2009-03-17 2:40

>>12
I have come to the point where I believe everyone who posted in this thread HBT.

Name: Anonymous 2009-03-17 2:46

>>13
Oh. You think that is the case in every thread.

Name: Anonymous 2009-03-17 2:46

>>13
no

Name: Anonymous 2009-03-17 4:11

>>10
var secretCode=(Math.floor(1000+Math.random()*9000)).toString();
var userInput=prompt('Enter 4-digit number','');var sum=0;var numc=0;
for(var i in user){if(user[i]==sec[i]){numc++;sum+=parseInt(user[i])}}
alert('Correct numbers:'+numc+' Correct numbers sum:'+sum);

Name: Anonymous 2009-03-17 4:23

>>16
The fuck are you spamming this shit for?
Somebody provided you with an enterprise quality solution for full marks already, just mangle the code a bit so it looks slightly closer to your level of competence and fuck off.

Name: Anonymous 2009-03-17 4:23

>>16
var sec=gen4();var maxguess=10;
function gen4(){return (Math.floor(1000+Math.random()*9000)).toString()}
function checkn(user){var sum=0;var numc=0;maxguess--;
for(var i in user){if(user[i]==sec[i]){numc++;sum+=parseInt(user[i])}}
alert('Correct numbers:'+numc+' Correct numbers sum:'+sum);
if(sum!=4 && maxguess>0){inp1()}};
function inp1(){checkn(prompt('Enter 4-digit number',''))}
inp1()

Name: Anonymous 2009-03-17 4:26

>>17
alert('YHBT')

Name: Anonymous 2009-03-17 4:45

>>17
The solution was far from enterprise quality. I can spot several beginner programmer mistakes right away, as an example:

public static final int DEFAULTCODELENGTH = 3;
System.out.println("Invalid code length, defaulting to three.");
How is this scalable or modular at all? What if they change DEFAULTCODELENGTH. An enterprise system would have a "NumberToEnglishConverter" class with a singleton and factory method to handle this atrocity. I also recommend loading the DEFAULTCODELENGTH from a constantsfactory that itself pulls from a dynamic multi-user turnkey scalable colocated database server to allow rapid deployment of updates to the DEFAULTCODELENGTH at no expense to the end user.

Name: Anonymous 2009-03-17 4:51

Why java code is ten times larger then javascript?

Name: Anonymous 2009-03-17 4:53

>>21
because like assembler its way faster.

Name: Anonymous 2009-03-17 5:11

>>22
Why assembler code is ten times smaller than java?

Name: Anonymous 2009-03-17 5:25

>>21
Because the Java program actually is and behaves like a fully fledged stand-alone program. The javascript version is just a single function. If I may use an example to show the sparsity of real code when you are making a real world usable peice of software: I have a seven line python script that posts to 4chan when passed appropriate parameters. Compare that to the PHP version which provides a web-form interface, AJAX post queuing and retrieving system; and various options for TOR, proxies, image ordering algorithms (lol image21 comes before image3?) and other miscellaneous features that make it completely usable and intuitive (to the extent possible in a browser-environment) experience. This tops 2000 SLOC, of which the function to actually post to 4chan given parameters is actually SMALLER than the python one (because PHP facilitates multipart by itself). Perhaps the phenomenon you are describing is telling of the real world in this sense? Java is used to actually make programs people will use- and things like javascript and haskell are for making small toy functions only the programmer himself can call from a special command line.

Name: Anonymous 2009-03-17 5:28

>>24
Why java code is ten times larger then jscript.NET?

Name: FrozenVoid 2009-03-17 5:40

>>24
 JavaScript version can be easily embedded in a webpage for anyone wishing to play,
 without loading any runtimes or plugins. Its a “real-world app”.


_____________________________________________
The mind has greater power over the emotions and is less subject thereto, in so far as it understands all things as necessary.

Name: Anonymous 2009-03-17 7:49

haha u use js fag

Name: FrozenVoid 2009-03-17 9:02

>>27 You could rewrite it it in C++ if you wish.



_______________________________
A strange neurosis, evidently contagious, an epidemic mass hysteria. In two weeks, it spread all over town.

Name: Anonymous 2009-03-17 17:30

>>24
Wow, too bad >>21 will never realize how thoroughly they just got owned.

Name: Anonymous 2009-03-17 17:51

haha u use c++ fag

Name: Anonymous 2010-11-26 1:02

Name: Anonymous 2011-02-04 18:24

Name: Anonymous 2011-02-18 14:21

<-- check 'em dubz

Name: Anonymous 2014-01-21 21:14

>>33
>le pedophile sage

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