Advertisement
nitishsp

Cows And Bulls in Java

Mar 22nd, 2012
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.table.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. /*
  9. * Title: Cows and Bulls in Java
  10. * Author: Nitish
  11. * Date Created: 20/03/2012
  12. * Last Modified: 22/03/2012
  13. * All Files => https://sites.google.com/site/nitishspblog/test/CowsAndBulls.zip
  14. */
  15.  
  16. class CowsBullsScratch extends JFrame implements ActionListener{
  17. JButton submit;
  18. JTextField input;
  19. JLabel word,chanceslbl;
  20. ArrayList<String> words;
  21. ArrayList<String> entered_words;
  22. BufferedReader br;
  23. String selected_word=null;
  24. JScrollPane jsp;
  25. JTable table;
  26. DefaultTableModel model;
  27. int chances=10;
  28.  
  29.     CowsBullsScratch(){
  30.         super("Cows and Bulls");
  31.        
  32.         //Initialize GUI components
  33.         setLayout(null);
  34.    
  35.         submit=new JButton("Submit");
  36.         submit.addActionListener(this);
  37.         submit.setBounds(170,20,100,30);
  38.         add(submit);
  39.        
  40.         input=new JTextField(10);
  41.         input.setBounds(20,20,150,30);
  42.         add(input);
  43.  
  44.         //Fetch words from file and randomly select one
  45.         words=new ArrayList<String>();
  46.         try{
  47.         br=new BufferedReader(new FileReader(new File("cbdata.txt")));
  48.             while((selected_word=br.readLine())!=null) {
  49.                 words.add(selected_word);
  50.             }
  51.         }
  52.         catch(Exception e){
  53.             e.printStackTrace();
  54.         }
  55.         selected_word=words.get(new Random().nextInt(words.size()));
  56.        
  57.         word=new JLabel("Word length :"+Integer.toString(selected_word.length()));
  58.         word.setBounds(300,20,100,30);
  59.         add(word);
  60.        
  61.         chanceslbl=new JLabel("Chances remaining: "+Integer.toString(chances));
  62.         chanceslbl.setBounds(400,20,200,30);
  63.         add(chanceslbl);
  64.  
  65.         String[] columns = {"Word","Bulls","Cows"};      
  66.         Object[][] data = {};
  67.        
  68.         //Override isCellEditable to make Jtable non editable
  69.         model=new DefaultTableModel(data,columns){
  70.             public boolean isCellEditable(int row, int column) {
  71.                 return false;
  72.             }
  73.         };
  74.         table=new JTable(model);
  75.         jsp=new JScrollPane(table);
  76.         jsp.setBounds(20,70,500,200);
  77.         add(jsp);      
  78.        
  79.         setSize(550,350);
  80.         setResizable(false);
  81.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  82.         setVisible(true);
  83.        
  84.         entered_words=new ArrayList<String>();
  85.         //JOptionPane.showMessageDialog(this, "The word is "+selected_word);
  86.     }
  87.    
  88.    
  89.     public static void main(String args[]){
  90.         new CowsBullsScratch();
  91.     }
  92.    
  93.     public void actionPerformed(ActionEvent e){
  94.         String st=input.getText();
  95.         st=st.trim();
  96.         if(st.length()!=selected_word.length()){
  97.             JOptionPane.showMessageDialog(this, "Invalid length");
  98.             return;
  99.         }
  100.        
  101.         if(entered_words.contains(st)){
  102.             JOptionPane.showMessageDialog(this, "You have already entered that word");
  103.             return;
  104.         }
  105.        
  106.         if(st.equals(selected_word)){
  107.             JOptionPane.showMessageDialog(this, "Congratulations! You guessed the word correctly.");
  108.             return;
  109.         }
  110.            
  111.         chances--;
  112.        
  113.         int cows=0;
  114.         int bulls=0;
  115.        
  116.         //calculate bulls
  117.        
  118.         for(int i=0;i<selected_word.length();i++){
  119.             if(selected_word.charAt(i)==st.charAt(i)){
  120.                 bulls++;
  121.             }
  122.         }
  123.        
  124.  
  125.         //remove duplicate characters from input and store the result in tmp
  126.         ArrayList<Character> tmp=new ArrayList<Character>();
  127.         for(int i=0;i<st.length();i++){
  128.             if(!tmp.contains(st.charAt(i))){
  129.                 tmp.add(st.charAt(i));
  130.             }
  131.         }
  132.        
  133.         //calculate cows
  134.         for(int i=0;i<tmp.size();i++){
  135.             char c=tmp.get(i);
  136.             int last_index=0;
  137.            
  138.             //iterate till the string doesn't contain character c
  139.             //with each iteration narrow down the search space
  140.             //This process can be made simpler by comparing characters individually
  141.             //I guess I was experimenting with something which made this process more complex
  142.             while(true){
  143.                 int index=selected_word.indexOf(c,last_index);
  144.  
  145.                 if(index==-1){
  146.                     break;
  147.                 }
  148.                 else{
  149.                     cows++;
  150.                 }
  151.                 last_index=index+1;
  152.             }
  153.         }
  154.        
  155.         entered_words.add(st);
  156.        
  157.         //update GUI
  158.         input.setText("");
  159.         chanceslbl.setText("Chances remaining:"+Integer.toString(chances));
  160.         model.insertRow(model.getRowCount(),new Object[]{st,  new Integer(bulls), new Integer(cows-bulls)});
  161.        
  162.         if(chances<=0){
  163.             JOptionPane.showMessageDialog(this, "You Lost! The word was "+selected_word);
  164.         }
  165.    
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement