//*********************** Copy right information ************************* // FAQ's // Q. Is this program copy right protected? // Ans. No. Even if it was you will copy it anyway so what is the point // Q. Can I use it for commercial purposes? // Ans. You are so funny. // Q. How do You feel about my copying this software? // Ans. We think that you are doing us a great honour by copying this // software. We are not worthy of this honour but we will appreciate // your generousity if you will simply e-mail us a thank you note // Q. What do I do if I find a bug? // Ans. Go to KMart. Buy a bug repellent(exterminator preferred) and kill // the beast. We dont like bugs. They are Yuk!!! // Q. Where is the code documentation? // Ans. You should have asked this question before we wrote this program. // you seriously do not expect us to go back and document the code // which we can barely figure out ourselves. We promise to document // our next code. (Cross our hearts. Seriously) // Q. How was the code tested and to what degree? // Ans. Good you asked as we are proud of that part. The code is guaranteed // to work even in a nuclear bomb attack provided (here is the catch) // the computer on which the software is running is able to handle // the extreme temperature and noise and radiation (you got the picture) // We also tested the software for bullet proofing (we will tell you // about that some other time) // // Disclaimer: Although we do not expect you to use this software in any // mission critical application but if you manage to start a // nuclear warhead missile to republic of china or some other // funny place using this piece of code PLEASE do sue us. // You wont get any money out of us but we will really enjoy // the resulting publicity. Kindly break the news to us in a // gentle way otherwise we may die laughing. // If you sue us nice and proper we all may become rich // from TV and film rights and product endorsements. //************************************************************************ // Program: Cows And Bulls (A simple game program) // Programmer: H K Gupta Date: 4 July 1997 //************************************************************************ import java.awt.*; import java.applet.Applet; import java.lang.Math; public class CowsAndBulls extends Applet { Panel pHistory; // History Area TextArea tHistory; TextArea tRules; Button bSubmit; // User wants to submit his selection int submitCount; // keep track of attempts Panel pMain; // Main play area Panel[] pDigits; Label[] lDigits; int[] nDigitValue; // Value of the Label int[] nGuessed; // Guessed number int[] nTempGuessed; int[] nTempDigitValue; Button[][] bRange; int nDigits = -1; // No display set yet int nRange = 6; // default 1 to 6 only allowed Panel pStatus; // status panel Label[] lSelectType; String[] selectStrings = {"Select no of digits", "Select Range"}; Panel[] pSelect; Button[][] bSelect; int nSelect = 9; int nMinSelect = 4; Button bNewGame; Button bGiveUp; boolean newHand = true; public CowsAndBulls() { setLayout(new GridLayout(0,3)); pHistory = new Panel(); pHistory.setLayout(new GridLayout(0,1)); add(pHistory); tHistory = new TextArea(); tHistory.setEditable(false); pHistory.add(tHistory); tRules = new TextArea(); tRules.setEditable(false); pHistory.add(tRules); addRules(); bSubmit = new Button("Submit Selection"); pHistory.add(bSubmit); pMain = new Panel(); add(pMain); pStatus = new Panel(); pStatus.setLayout(new GridLayout(0,1)); add(pStatus); int i, j; pSelect = new Panel[2]; lSelectType = new Label[2]; bSelect = new Button[2][nSelect - nMinSelect + 1]; for(i = 0; i < 2; i++) { lSelectType[i] = new Label(selectStrings[i]); pStatus.add(lSelectType[i]); pSelect[i] = new Panel(); pStatus.add(pSelect[i]); for(j = 0; j < (nSelect - nMinSelect + 1); j++) { bSelect[i][j] = new Button("" + (j + nMinSelect)); pSelect[i].add(bSelect[i][j]); } } bNewGame = new Button("New Game"); pStatus.add(bNewGame); bGiveUp = new Button("Give Up"); pStatus.add(bGiveUp); removeOldAndAddNew(4, 6); resetNewHand(); validate(); } void removeOldAndAddNew(int nD, int nR) { // Digits and range if ((nD == nDigits) && (nR == nRange)) return; int i, j; if (nDigits > 0) { // Remove old for(i = 0; i < nDigits; i ++) { for(j = 0; j < nRange; j++) { pDigits[i].remove(bRange[i][j]); } pDigits[i].remove(lDigits[i]); pMain.remove(pDigits[i]); } } nDigits = nD; nRange = nR; pDigits = new Panel[nDigits]; lDigits = new Label[nDigits]; nDigitValue = new int[nDigits]; nGuessed = new int[nDigits]; nTempGuessed = new int[nDigits]; nTempDigitValue = new int[nDigits]; bRange = new Button[nDigits][nRange]; pMain.setLayout(new GridLayout(0, nDigits, 2, 0)); for(i = 0; i < nDigits; i ++) { pDigits[i] = new Panel(); pDigits[i].setLayout(new GridLayout(0,1)); pMain.add(pDigits[i]); lDigits[i] = new Label("", Label.CENTER); lDigits[i].setBackground(Color.white); pDigits[i].add(lDigits[i]); for(j = 0; j < nRange; j++) { bRange[i][j] = new Button("" + (j + 1)); pDigits[i].add(bRange[i][j]); } } resetNewHand(); } public boolean action(Event e, Object arg) { Object target = e.target; int i, j; for(i = 0; i < nDigits; i ++) { for(j = 0; j < nRange; j++) { if(target == bRange[i][j]) { lDigits[i].setText("" + (j + 1)); nDigitValue[i] = j + 1; activateSubmit(); return true; } } } for(i = 0; i < 2; i++) { // Check level changes for(j = 0; j < (nSelect - nMinSelect + 1); j++) { if(target == bSelect[i][j]) { if (i == 0) { removeOldAndAddNew(j + nMinSelect, nRange); } else { removeOldAndAddNew(nDigits, j + nMinSelect); } validate(); return true; } } } if(target == bNewGame) { setNewHand(); return true; } if(target == bGiveUp) { // Give the assumed number and start new hand processGiveUp(); return true; } if(target == bSubmit) { // Give matches or Victory processSubmit(); return true; } return false; } void addRules() { tRules.appendText(" *** Rules ***\n" + "The objective is to correctly guess\n" + "the number created by the computer.\n" + " You are allowed to guess a number\n" + " and the computer replies with \n" + " No. of exact maching digits as Bulls\n" + " and the digits matching in any place\n" + " as Cows.\n" + " You can keep guessing till you find\n" + " the correct number"); } void resetNewHand() { int i, j; for(i = 0; i < nDigits; i ++) { lDigits[i].setText(""); for(j = 0; j < nRange; j++) { bRange[i][j].disable(); } } for(i = 0; i < 2; i++) { // enable level changes for(j = 0; j < (nSelect - nMinSelect + 1); j++) { bSelect[i][j].enable(); } } bNewGame.enable(); bGiveUp.disable(); bSubmit.disable(); } void setNewHand() { int i, j; for(i = 0; i < nDigits; i ++) { for(j = 0; j < nRange; j++) { bRange[i][j].enable(); } } for(i = 0; i < 2; i++) { for(j = 0; j < (nSelect - nMinSelect + 1); j++) { bSelect[i][j].disable(); } } bNewGame.disable(); bGiveUp.enable(); submitCount = 0; // Guess new number for(i = 0; i < nDigits; i++) { nGuessed[i] = (int)(Math.random() * nDigits) + 1; } clearDigits(); } void clearDigits() { int i; for(i = 0; i < nDigits; i++) { nDigitValue[i] = 0; lDigits[i].setText(""); } bSubmit.disable(); } void activateSubmit() { int i; for(i = 0; i < nDigits; i++) { if(nDigitValue[i] == 0) return; } bSubmit.enable(); } void processSubmit() { int i, j; submitCount++; int bulls = 0; for(i = 0; i < nDigits; i++) { // Look for exact matches nTempGuessed[i] = nGuessed[i]; nTempDigitValue[i] = nDigitValue[i]; if(nTempDigitValue[i] == nTempGuessed[i]) { bulls++; nTempGuessed[i] = -1; // already compared nTempDigitValue[i] = -2; // already compared } } if (bulls == nDigits) { // User Guessed it tHistory.appendText("\nEureka. You guessed it right" + "\n on your attempt number " + submitCount); resetNewHand(); return; } int cows = 0; for(i = 0; i < nDigits; i++) { for(j = 0; j < nDigits; j++) { if(nTempDigitValue[i] == nTempGuessed[j]) { cows++; nTempGuessed[j] = -1; // already compared nTempDigitValue[i] = -2; // already compared } } } tHistory.appendText("\nNo "); for(i = 0; i < nDigits; i++) { tHistory.appendText("" + nDigitValue[i]); } tHistory.appendText(" Bulls " + bulls + " Cows " + cows); clearDigits(); } void processGiveUp() { int i; tHistory.appendText("\nLost interest so easily?\n The no. was "); for(i = 0; i < nDigits; i++) { tHistory.appendText("" + nGuessed[i]); } resetNewHand(); } }