Search This Blog

Source Code: Tic-Tac-Toe Application in Java using Applet

The source code is given below.


   1:  //created by Aditya Vijayvergia
   2:  //uploaded at thecodingedge.blogspot.com
   3:  import java.awt.*;
   4:  import java.awt.event.ActionEvent;
   5:  import java.awt.event.ActionListener;
   6:  import java.awt.event.WindowAdapter;
   7:  import java.awt.event.WindowEvent;
   8:  import static java.lang.Thread.sleep;
   9:  import java.util.logging.Level;
  10:  import java.util.logging.Logger;
  11:  import javax.swing.plaf.OptionPaneUI;
  12:   
  13:  /**
  14:   *
  15:   * @author Aditya Vijayvergia @ thecodingEdge.blogspot.in
  16:   */
  17:  public class TicTacToe extends Frame{
  18:      Panel gameSpace;
  19:      static int red=0,blue=0;    //number of games won by the players
  20:      Panel Score,side;
  21:      Button[] block;
  22:      Button chance;
  23:      int player=1;   //to keep a check on which player has the turn
  24:      int i,count=0;
  25:      TextField scoreBlue,scoreRed,winner;
  26:      int arr[][];
  27:      
  28:      TicTacToe()
  29:      {
  30:          setLayout(new FlowLayout(FlowLayout.CENTER, 40, 10));
  31:          arr=new int[3][3];
  32:          for(i=0;i<3;i++)
  33:          for(int j=0;j<3;j++)
  34:              arr[i][j]=0;
  35:          gameSpace=new Panel(new GridLayout(3, 3, 5, 5));
  36:          chance=new Button("   ");
  37:          chance.setBackground(Color.red);
  38:          winner=new TextField("             ");
  39:          winner.setBackground(Color.white);
  40:          //winner.setVisible(false);
  41:          block=new Button[10];
  42:          for(i=0;i<9;i++){
  43:              block[i]=new Button("   ");
  44:              block[i].setName(i+"");
  45:              block[i].setPreferredSize(new Dimension(75,75));
  46:              gameSpace.add(block[i]);
  47:              block[i].addActionListener(new ActionListener(){
  48:                  @Override
  49:                  public void actionPerformed(ActionEvent e) {
  50:                      Button b=(Button) e.getSource();
  51:                      if(b.getBackground()!=Color.red&&b.getBackground()!=Color.blue)
  52:                      {
  53:                          count++;
  54:                          if(player==1)
  55:                          {
  56:                          
  57:                              b.setBackground(Color.red);
  58:                              player=2;   //after player 1 has played, player 2 gets the turn 
  59:                              i=Integer.parseInt(b.getName());
  60:                              changearr(i,1);
  61:                              chance.setBackground(Color.blue);
  62:                          }
  63:                          else 
  64:                          {   
  65:                              b.setBackground(Color.BLUE);
  66:                              chance.setBackground(Color.red);
  67:                              i=Integer.parseInt(b.getName());
  68:                              changearr(i,2);
  69:                              player=1;
  70:                          }
  71:                          if(count==9)//if all the boxes are filled
  72:                          {
  73:                              winner.setText("Game Draw");
  74:                              try {
  75:                                  sleep(2000);
  76:                              } catch (InterruptedException ex) {
  77:                                 Logger.getLogger(CrossNZero.class.getName()).log(Level.SEVERE, null, ex);
  78:                              }
  79:                              setVisible(false);
  80:                              new CrossNZero();
  81:                          }
  82:                      }
  83:                  }
  84:              
  85:          });
  86:          }
  87:          add(gameSpace);
  88:          
  89:          Panel Chance=new Panel();
  90:          side=new Panel(new BorderLayout(5,10));
  91:          Chance.add(new Label("Player "),BorderLayout.NORTH);
  92:          Chance.add(chance,BorderLayout.NORTH);
  93:          side.add(Chance,BorderLayout.NORTH);
  94:          Score=new Panel();
  95:          Score.add(new Label("Red"));
  96:          Score.add(new Label(red+""));
  97:          Score.add(new Label("Blue"));
  98:          Score.add(new Label(blue+""));
  99:          side.add(Score,BorderLayout.CENTER);
 100:          winner.setEditable(false);
 101:          winner.setText("            Let's Play!!");
 102:          side.add(winner,BorderLayout.SOUTH);
 103:          add(side);
 104:          
 105:          setVisible(true);
 106:          setTitle("Tic-Tac-Toe");
 107:          setSize(500,400);
 108:          
 109:          addWindowListener(new WindowAdapter(){
 110:              @Override
 111:              public void windowClosing(WindowEvent e)
 112:              {
 113:                  System.exit(0);
 114:              }
 115:          });
 116:          add(new Label("Created by Aditya Vijayvergia"));
 117:          add(new Label("Upladed on     thecodingedge.blogspot.in"));
 118:          
 119:              
 120:      }
 121:      
 122:      public void changearr(int i,int c)//maintain an array to check the winning condition
 123:      {
 124:          if(i>5)
 125:          {
 126:              arr[2][i-6]=c;
 127:              ifWin(2,i-6,c);
 128:          }
 129:          else
 130:              if(i>2)
 131:              {
 132:                  arr[1][i-3]=c;
 133:                  ifWin(1,i-3,c);
 134:              }
 135:          else
 136:              {
 137:                  arr[0][i]=c;
 138:                  ifWin(0,i,c);
 139:              }
 140:      }
 141:   
 142:      public void ifWin(int i1,int j1,int c)//logic to check if a player has won the game
 143:      {
 144:          int p=0;
 145:          int i=i1,j=j1;
 146:          while(i<3)
 147:          {
 148:              if(arr[i][j]==c)
 149:                  p++;
 150:              i++;
 151:          }
 152:          i=i1;j=j1;
 153:          while(i>=0)
 154:          {
 155:              if(arr[i][j]==c)
 156:                  p++;
 157:              i--;
 158:          }
 159:          if(p==4)
 160:          {
 161:              
 162:              winner(c);
 163:          }
 164:          p=0;
 165:          i=i1;j=j1;
 166:          while(j<3)
 167:          {
 168:              if(arr[i][j]==c)
 169:                  p++;
 170:              j++;
 171:          }
 172:          i=i1;j=j1;
 173:          while(j>=0)
 174:          {
 175:              if(arr[i][j]==c)
 176:                  p++;
 177:              j--;
 178:          }
 179:          if(p==4)
 180:          {
 181:             winner(c);
 182:          }
 183:          p=0;
 184:          i=i1;j=j1;
 185:          
 186:          while(i<3&&j<3)
 187:          {
 188:              if(arr[i][j]==c)
 189:                  p++;
 190:              i++;j++;
 191:          }
 192:          i=i1;j=j1;
 193:          while(i>-1&&j>-1)
 194:          {
 195:               if(arr[i][j]==c)
 196:                  p++;
 197:               i--;j--;
 198:          }
 199:          if(p==4)
 200:          {
 201:              winner(c);
 202:          }
 203:          p=0;
 204:          i=i1;j=j1;
 205:          
 206:          while(i>=0&&j<3)
 207:          {
 208:              if(arr[i][j]==c)
 209:                  p++;
 210:              i--;
 211:              j++;
 212:          }
 213:          i=i1;j=j1;
 214:           while(i<3&&j>=0)
 215:          {
 216:              if(arr[i][j]==c)
 217:                  p++;
 218:              i++;
 219:              j--;
 220:          }
 221:          if(p==4)
 222:          {
 223:              winner(c);
 224:          }
 225:      }
 226:      
 227:      public void winner(int i)//if a player wins the game, this function sets the applet
 228:      {
 229:          count=0; 
 230:          if(i==1)
 231:          {
 232:                  winner.setText("red wins");
 233:                  red++;
 234:          }
 235:              else
 236:          {
 237:              blue++;    
 238:              winner.setText("blue wins");
 239:          }
 240:          winner.setBackground(Color.yellow);
 241:          try {
 242:              sleep(2000);
 243:          } catch (InterruptedException ex) {
 244:              Logger.getLogger(CrossNZero.class.getName()).log(Level.SEVERE, null, ex);
 245:          }
 246:          winner.setVisible(false);
 247:          setVisible(false);
 248:          new TicTacToe();
 249:      }
 250:      
 251:     
 252:      public static void main(String[] args) {
 253:          new TicTacToe();
 254:      }
 255:      
 256:  }

No comments:

Post a Comment