Cows and Bulls Game

Author: Dheeraj Kota
Email:   dheeraj_kota@da-iict.org

import javax.swing.*;
        import java.awt.*;
        import java.awt.event.*;
        import java.util.*;
        
        class prog5 extends JFrame implements ActionListener
        {
        
                
        private String s;
        private boolean q1=false,q2;
        private int a=0,b,cows=0,bulls;
        private int w=0;
        private int q[]=new int [4];
        private int e[]=new int [4];
                private static final String NEWLINE = 
System.getProperty("line.separator");
                private JMenuBar menuBar;
                private JMenu fileMenu;
                private JMenu helpMenu;
                private JLabel won;
                private JLabel lost;
                private JTextArea textArea;
                private JScrollPane scrollPane;
                private JTextField inputLine;
                private JButton guess;
                private JButton resign;
                private JButton clear;
                private Container con;
        private int i=0;
                private int wonCount;
                private int lostCount;
                private int number;
                private int chances;
                public static void main(String args[])
                {
                        prog5 a = new prog5();
                        a.setVisible(true);
                }
        
                public void actionPerformed(ActionEvent event)
                {
                        if(event.getSource() instanceof JButton)
                        {
                                JButton eventButton = (JButton) event.getSource();
                                if(eventButton.getText() == "Guess")
                                //      if(inputLine.getText()!=null)
                
        //      b=Integer.parseInt(s);)
                                        guess();
                                else if(eventButton.getText() == "Clear")
                                        clearTextArea();
                                else if(eventButton.getText() == "Resign")
                                        resign();
                        }
                        if(event.getSource() instanceof JTextField)
                        {
                        //      if(inputLine.getText()!=null)
                                guess();
                        }
                        if(event.getSource() instanceof JMenuItem)
                        {
                                String command = event.getActionCommand();
        
                                if (command == "New Game")
                                        resign();
                                else if(command == "Exit")
                                        System.exit(0);
                                else if(command == "Instructions")
                                {
                                        Instructions a = new Instructions();
                                        a.setVisible(true);
                                }
                                else if(command == "Clear stats.")
                                        clearStats();
                                else if(command == "About")
                                        JOptionPane.showMessageDialog(null, "You are playing Cows and bull 
game Designed by Dheeraj Kota.");
        
                        }
                }
        
                private void clearStats()
                {
                        int choice=JOptionPane.showConfirmDialog(this,"Are you sure you want 
to clear stats.");
                        if(choice == JOptionPane.YES_OPTION)
                        {
                                wonCount=0;
                                lostCount=0;
                                won.setText("Won : " + wonCount);
                                lost.setText("Lost : " + lostCount);
                        }
                }
        
                private void guess()
        
        
        {
                if(i ==0)
                {
                        do
                {
                q1=true;
                a=(int)(Math.floor(Math.random()*9000)+1000);
                s=Integer.toString(a);
                for(int i=0;i<4;i++)
                {
                        e[i]=Integer.parseInt(s.substring(i,i+1));
        }
        for(int i=0;i<4;i++)
        for(int k=i+1;k<4;k++)
        {
        
        
        if(e[i]==e[k])
        {
        q2=false;
        //continue;
        q1=q1&q2;
        }
        else
        {
        q2=true;
        q1=q1&q2;
        }
        
        }
        }while(!q1);
        }
        
        if(i<10)
        
                {
                cows=0;
                bulls=0;
        s=inputLine.getText();
                
                b=Integer.parseInt(s);
                w=0;
                while(w<4)
        {
                
                q[w]=Integer.parseInt(s.substring(w,w+1));
                w++;
        }
                for(int y=0;y<4;y++)
                                for(int t=0;t<4;t++)
                        {
                if(e[y]==q[t] && y==t)
                {
        
                cows=cows+1;
        
        }
                if(e[y]==q[t] && y!=t)
                {
        
                bulls=bulls+1;
        }
        
        
        
        }
        
        
textArea.append(s+"\t"+"cows"+cows+"\t"+"Bulls"+bulls+"\t"+"Try"+(i+1)+NEWLINE);
        i++;
        
                if(cows==4)
                {
                textArea.append("U Won");
        wonCount++;
                won.setText("Won : " + wonCount);
        i=0;
        
        }
        }
        
        if(cows!=4 && i>9)
        {
        textArea.append(NEWLINE+a);
        //System.out.println(a);
        textArea.append(NEWLINE+"Sorry u lost"+NEWLINE);
        lostCount++;
                lost.setText("Lost : " + lostCount);
        i=0;
        }
        }       
                private void clearTextArea()
                {
                        inputLine.setText("");
                        textArea.setText("");
                        textArea.append("Try to guess the number." + NEWLINE);
                        textArea.append("You have " +  (10-i) + " chances left."+NEWLINE);
                }
        
                
                private void resign()
                {
                        textArea.append("You resigned." + NEWLINE);
                        textArea.append("The number was : " + a + NEWLINE);
                        textArea.append(NEWLINE);
                        textArea.append("Try to guess another number." + NEWLINE);
        
                        inputLine.setText("");
        
                        //number = randomNumber();
                        i = 0;
                        lostCount++;
                        lost.setText("Lost : " + lostCount);
                }
        
        
        
                public prog5()
                {
                        setSize(426,600);
                        setLocation(100,100);
                        setTitle("Cows and Bulls Game");
        
                        setDefaultCloseOperation(EXIT_ON_CLOSE);
                        setResizable(false);
        
                        con = getContentPane();
                        con.setLayout(null);
                
                        menuBar = new JMenuBar();
                        createFileMenu();
                        createHelpMenu();
                        setJMenuBar(menuBar);
        
                        createWonLabels();
                        createLostLabels();
        
                        createTextArea();
                        createInputLine();
                        createButtons();
        
                        lostCount=0;
                        wonCount=0;
                        chances=5;
        
        
                        textArea.append("Try to guess a secret number." + NEWLINE);
                        textArea.append(NEWLINE);
                }
        
                
                private void createButtons()
                {
                        guess = new JButton("Guess");
                        guess.setBounds(10,482,100,40);
                        con.add(guess);
                        guess.addActionListener(this);
        
                        resign = new JButton("Resign");
                        resign.setBounds(160,482,100,40);
                        con.add(resign);
                        resign.addActionListener(this);
        
        
                        clear = new JButton("Clear");
                        clear.setBounds(310,482,100,40);
                        con.add(clear);
                        clear.addActionListener(this);
        
                }
        
                private void createInputLine()
                {
                        inputLine = new JTextField();
                        inputLine.setBounds(10,432,400,40);
                        con.add(inputLine);
                        inputLine.addActionListener(this);
                }
        
        
                private void createTextArea()
                {
                        textArea = new JTextArea();
                        textArea.setBounds(10,122,400,300);
                        textArea.setBorder(BorderFactory.createLineBorder(Color.red));
                        textArea.setEditable(false);
                        scrollPane = new JScrollPane(textArea);
                        scrollPane.setBounds(10,122,400,300);
                        con.add(scrollPane);
                }
        
        
                private void createLostLabels()
                {
                
                        lost = new JLabel("Lost : 0");
                        lost.setBounds(304,10,100,102);
        
                        con.add(lost);
                }
        
        
                private void createWonLabels()
                {
                        
                        won = new JLabel("Won : 0");
                        won.setBounds(102,10,100,102);
        
                        con.add(won);
                }
        
                private void createFileMenu()
                {
                        fileMenu = new JMenu("File");
        
                        JMenuItem item;
        
                        item = new JMenuItem("New Game");
                        item.addActionListener(this);
                        fileMenu.add(item);
        
                        item = new JMenuItem("Clear stats.");
                        item.addActionListener(this);
                        fileMenu.add(item);
        
                        fileMenu.addSeparator();
        
                        item = new JMenuItem("Exit");
                        item.addActionListener(this);
                        fileMenu.add(item);
        
                        menuBar.add(fileMenu);
                }
        
                private void createHelpMenu()
                {
                        helpMenu = new JMenu("Help");
        
                        JMenuItem item;
        
                        item = new JMenuItem("Instructions");
                        item.addActionListener(this);
                        helpMenu.add(item);
        
                        helpMenu.addSeparator();
        
                        item = new JMenuItem("About");
                        item.addActionListener(this);
                        helpMenu.add(item);
        
                        menuBar.add(helpMenu);
                }
        }
        
        class Instructions extends JFrame implements ActionListener
        {
                        
                private static final String NEWLINE = 
System.getProperty("line.separator");
                JTextArea textArea;     
                JButton ok = new JButton("Ok");
                
                Instructions()
                {
                        setTitle("Instructions");
                        setLocation(200,200);
                        setSize(450,270);
        
                        Container con = getContentPane();
                        con.setLayout(null);
                        setResizable(false);
                        
                        textArea = new JTextArea();
                        textArea.setBounds(10,10,420,165);
                        textArea.setEditable(false);
                        textArea.setBorder( 
BorderFactory.createTitledBorder("Instructions"));
                        con.add(textArea);
        
                        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        
                        textArea.append("1. The program will think a 4 digit random number 
with out repitition." + NEWLINE);
                        textArea.append(NEWLINE);
                        textArea.append("2. You have ten chances to get that number." + 
NEWLINE);
                        textArea.append(NEWLINE);
                        textArea.append("3. The program will say there is a bull if any 
digit of u'r guessed number"+NEWLINE+"equals the digit in the random number 
generated in a different place" + NEWLINE + " Cow if the digit is at 
the same place. " + NEWLINE);
                        textArea.append(NEWLINE);
                        textArea.append("4. You can start a new game at any time if you wish 
to." + NEWLINE);
                        textArea.append(NEWLINE);
        
                        ok = new JButton("Ok");
                        ok.setBounds(160,185,100,40);
                        con.add(ok);
                        ok.addActionListener(this);
                }
        
                public void actionPerformed(ActionEvent event)
                {
                        dispose();
                }
}

Quick Links:
Have Java Problem
Do you have a Java Question?

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Best Regards,
Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.