Welcome to Dream.In.Code
Become a Java Expert!

Join 149,598 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,898 people online right now. Registration is fast and FREE... Join Now!




Problems with Mastermind(codebreaker)

 
Reply to this topicStart new topic

Problems with Mastermind(codebreaker), this is like the game Mastermind but instead of colored pegs, i use nu

BreadStix
5 Oct, 2007 - 05:32 PM
Post #1

New D.I.C Head
*

Joined: 5 Oct, 2007
Posts: 3


My Contributions
CODE
import javax.swing.JOptionPane;
import java.io.IOException;
import java.util.*;

//**java.util.* produces a random digit*/

public class Main {

    public static void main(String[] args) {
        int x = 0;
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        int f = 0;
        int z = 0;
        int j = 1;
        int g = 0;
        int l = 1;
        int m = 1;
        int i = 0;
        int h = 0;
        int numArray[]= new int[4];
        int randomArray[]=new int[4];
        String input = "";
        while(j > 0){
            try{
                input=JOptionPane.showInputDialog("CODEBREAKER \n Level of Difficulty \n 1 - Easy \n 2 - Average \n 3 - Difficult");
                x=Integer.parseInt(input); //**level of difficulty */    
                Random random = new Random();
                while(m > 0){
                    while (i != 4){
                        randomArray[i]=random.nextInt(10);
                        i++;
                     }if(randomArray[0] == randomArray[1] | randomArray[0] == randomArray[2] | randomArray[0] == randomArray[3]){
                        m++;
                     }else if(randomArray[1] == randomArray[2] | randomArray[1] == randomArray[3]){
                        m++;
                     }else if(randomArray[2] == randomArray[3]){
                        m++;
                     }else{
                        m=0;
                     }
                }//**Produces a random number,checks if it is unique within the set */
                JOptionPane.showMessageDialog(null,"random array = "+randomArray[0]+randomArray[1]+randomArray[2]+randomArray[3]);
                if(x == 1){
                    a = 10;
                }else if (x == 2){
                    a = 7;
                }else if (x == 3) {
                    a = 4;
                }else {
                    System.out.println("Invalid choice.");
                }j = 0;
            }catch(NumberFormatException re){
                    JOptionPane.showMessageDialog(null, "Invalid Input.");
                j++;
            }  
    }//** checks the level of difficulty and invalid inputs. */
        while ( l > 0){
            try{
                while (b != a){
                    c = c + 1;
                    d=0;
                    while (d != 4){
                    input=JOptionPane.showInputDialog("Try # " +c+ ", Enter guess");
                    numArray[d]=Integer.parseInt(input);
                    g = 0;
                    h = 0;
                    d++;
                    }//** Receives input, puts it in an array. */
                    d=0;    
                    while (d != 4){
                        f=0;
                        while (f != 4){
                            if (randomArray[f] == numArray[d]){
                            if (f == d){
                                    g = g +1;
                                }else{
                                    h = h + 1;
                                }
                        }
                            f++;
                        }d++; //** Compares input with the random numbers, produces markers. *//  
                        }if (numArray[0] == numArray[1] && g > 0){
                            h = h - 1;
                        }if (numArray[1] == numArray[2] && g > 0){
                            h = h - 1;
                        }if (numArray[2] == numArray[3] && g > 0){
                            h = h - 1;
                        }//** checks for repeated/overlapping digits */
                        if(g == 4){
                            JOptionPane.showMessageDialog(null, "YOU WIN!");
                            System.exit(0);
                        }else{
                            JOptionPane.showMessageDialog(null, g+ " Black, " +h+ " White");
                        }//** output */
                        b++;
                    }JOptionPane.showMessageDialog(null, "sorry, you lose.");
                    System.exit(0);
                    
            }catch(NumberFormatException er){
                JOptionPane.showMessageDialog(null, "Invalid input.");
            }l++;
            }
        }
    
    }


This post has been edited by PennyBoki: 5 Oct, 2007 - 05:35 PM


Attached File(s)
Attached File  Main2.txt ( 4.27k ) Number of downloads: 23
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Problems With Mastermind(codebreaker)
5 Oct, 2007 - 05:37 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi maybe you want to post this as a code snippet, if not the please do tell what are the errors or trouble you are encountering with this code.

And please use code tags when posting code.

This post has been edited by PennyBoki: 5 Oct, 2007 - 05:37 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:34PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month