//*********************** 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 noise, radiation and politics (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: Tic Tac Toe game program // Programmer: H K Gupta Date: 25 June 1997 //************************************************************************ import java.awt.*; import java.applet.Applet; import java.lang.Math; public class TicTacToe extends Applet { Panel pName; // To display TicTacToe name Label[] lName; Panel p1; Button[] b; // Grid is represented by 3 X 3 buttons int[] stat; // -1 Cross, 0 empty, +1 not Panel pStatus; Button strt; Label results; boolean laidOut = false; int gameWon = 0; int gameLost= 0; int gameDraw= 0; // List of winning combinations. Row/colum/diagonal final static int w1[] = {0, 3, 6, 0, 1, 2, 0, 2}; final static int w2[] = {1, 4, 7, 3, 4, 5, 4, 4}; final static int w3[] = {2, 5, 8, 6, 7, 8, 8, 6}; static boolean userFirst = false; void drawLabel(int i, int type) {// put selection and disable button if (type == 1) { if (userFirst) { b[i].setLabel("X"); } else { b[i].setLabel("O"); } } else { if (!userFirst) { b[i].setLabel("X"); } else { b[i].setLabel("O"); } } stat[i] = type; // spot occupied by the user b[i].disable(); } void moveOfComp(int i) { drawLabel(i, -1); } void moveFrom(int i) { // Find empty block (only 1 out of three) if (stat[w1[i]] == 0) moveOfComp(w1[i]); if (stat[w2[i]] == 0) moveOfComp(w2[i]); if (stat[w3[i]] == 0) moveOfComp(w3[i]); } void bestMove() { // Select best move for computer int i; for (i = 0; i < 8 ; i++) { // check for winning move if((stat[w1[i]] + stat[w2[i]] + stat[w3[i]]) == -2) { moveFrom(i); return; } } for (i = 0; i < 8 ; i++) { // defend defeat if((stat[w1[i]] + stat[w2[i]] + stat[w3[i]]) == 2) { moveFrom(i); return; } } // move random. we do not want to defeat the user all the time int randomStart = (int)(Math.random() * 9); int j; for (i = randomStart; i < (randomStart + 9); i++) { j = i; if (j > 8) j = j - 8; if (stat[j] == 0) { moveOfComp(j); return; } } } void setNewGame(String str) { // str has the result of last game int i; for(i = 0; i < 9; i++) { // disable any enabled buttons b[i].disable(); } strt.enable(); strt.setLabel(str); results.setText("Won " + gameWon + " Lost " + gameLost + " Draw " + gameDraw); } boolean won(int choice) { // three in a row int i; for (i = 0; i < 8 ; i++) { if((stat[w1[i]] + stat[w2[i]] + stat[w3[i]]) == (3 * choice)) { return true; } } return false; } boolean gameOver() { // check if game is already over if (won(1)) { // User won gameWon++; setNewGame("You won. Start again"); return true; } if (won(-1)) { // Computer won gameLost++; setNewGame("I won. Start again"); return true; } int i; for (i = 0; i < 9; i++) { if (stat[i] == 0) return false; } gameDraw++; setNewGame("Draw. Start again"); return true; } void moveByComputer() { if (gameOver()) return; // user won OR no more moves bestMove(); if (gameOver()) return; // Computer won OR No more moves } void initGame() { int i; for (i = 0; i < 9; i++) { b[i].enable(); b[i].setLabel(""); stat[i] = 0; } strt.disable(); userFirst = !userFirst; if (!userFirst) moveByComputer(); } public void init() { setLayout(new GridLayout(0, 3, 5, 5)); pName = new Panel(); pName.setLayout(new GridLayout(0, 1)); add(pName); int i; lName = new Label[3]; for(i = 0; i < 3; i++) { lName[i] = new Label("TicTacToe".substring(i * 3, (i + 1) * 3), Label.CENTER); lName[i].setFont(new Font("TimesRoman",Font.BOLD,36)); lName[i].setForeground(Color.red); pName.add(lName[i]); } b = new Button[9]; stat = new int[9]; p1 = new Panel(); add(p1); p1.setLayout(new GridLayout(0, 3, 3, 3)); for(i = 0; i < 9; i++) { b[i] = new Button(); b[i].setFont(new Font("TimesRoman",Font.BOLD,36)); p1.add(b[i]); b[i].disable(); } pStatus = new Panel(); pStatus.setLayout(new GridLayout(0, 1, 2, 20)); add(pStatus); strt = new Button("Start Tic Tac Toe"); strt.setFont(new Font("TimesRoman",Font.BOLD,14)); pStatus.add(strt); results = new Label("", Label.CENTER); pStatus.add(results); results.setFont(new Font("TimesRoman",Font.BOLD,14)); validate(); } public boolean action(Event e, Object arg) { Object target = e.target; int i; for(i = 0; i < 9; i++) { if (target == b[i]) { drawLabel(i, 1); // register users move moveByComputer(); // Check for win, loose or move return true; } } if (target == strt) { initGame(); return true; } return false; } }