Subscribe to Java Codings        RSS Feed
-----

Tic Tac Toe ( A game)

Icon Leave Comment
Hey you all would have been playing tic tac toe from decades(have I told anything wrong, I think it is too much), anyway, this snippet allows two players to play this game and compete to win.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class TicTacToe extends JFrame implements ActionListener
	{   
		int tn = 0;
		JButton but = new JButton();
		int x = 0;
		int o = 0;
		final int DONE = (1 << 9) - 1;
		final int[] WIN =  {((1 << 0) | (1 << 1) | (1 << 2)),
							((1 << 3) | (1 << 4) | (1 << 5)),
							((1 << 6) | (1 << 7) | (1 << 8)),
							((1 << 0) | (1 << 3) | (1 << 6)),
							((1 << 1) | (1 << 4) | (1 << 7)),
							((1 << 2) | (1 << 5) | (1 << 8)),
							((1 << 0) | (1 << 4) | (1 << 8)),
							((1 << 2) | (1 << 4) | (1 << 6))};
		TicTacToe()
			{   
				setLayout(new GridBagLayout());
				setTitle("TicTacToe");
				setSize(300,200);
				setVisible(true);
				player1 = new JLabel("   Player 1 Play");
				player2 = new JLabel("   Player 2 Play");
				buttons = new JButton[3][3];
				for(int i=0;i<3;i++)
					{
						for(int j=0;j<3;j++)
							{
							   buttons[i][j] = new JButton("");
							   Dimension dm = new Dimension(50,50);
							   buttons[i][j].setPreferredSize(dm);
								buttons[i][j].addActionListener(this);
								add(buttons[i][j], new GBC(i,j));
							}
						}
				Toolkit kit = Toolkit.getDefaultToolkit();
				setLocation((kit.getScreenSize().width - this.getSize().width) / 2, (kit.getScreenSize().height - this.getSize().height) / 2);
				add(player1,new GBC(3,0));
				add(player2,new GBC(3,2)); 
					}
				JButton[][] buttons;
				JLabel player1,player2;
		  public void actionPerformed(ActionEvent event)
				{   
					tn++;
					but = new JButton();
					Object source = event.getSource();
					if(source instanceof JButton)
						{
							but = (JButton) source;
						}
					if(source == but)
						{   
							if(tn%2!=0) {
							but.setText("X");
							but.setEnabled(false);
							player1.setEnabled(false);
							player2.setEnabled(true);}
							else {
							but.setText("O");
							but.setEnabled(false);
							player1.setEnabled(true);
							player2.setEnabled(false);}
							switch(update())
								{
									case 1:
										int response1 = JOptionPane.showConfirmDialog(null, "Player 1 wins!\nLike to play again?", "Confirm",
										JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
										if(response1 == JOptionPane.YES_OPTION) 
												playAgain();
										setVisible(false);
										break;
									case 2:
										int response2 = JOptionPane.showConfirmDialog(null, "Player 2 wins!\nLike to play again?", "Confirm",
										JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
										if(response2 == JOptionPane.YES_OPTION) 
												playAgain();
										setVisible(false);
										break;
									case 3:
										int response3 = JOptionPane.showConfirmDialog(null, "That's is a draw!\nLike to play again?", "Confirm",
										JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
										if(response3 == JOptionPane.YES_OPTION) 
												playAgain();
										setVisible(false);
										break;
									default:
										break;
								}
						}
				}
			int update()
				{
					for(int i = 0; i < 3; i++)
						{
							for(int j = 0; j < 3; j++)
								{
									if(but == buttons[i][j])
										{
											if(tn % 2 != 0)
												x |= (1 << (3 * i + j));
											else
												o |= (1 << (3 * i + j));
										}
								}
						}
					return check();
				}
			int check()
				{
					int val = 0;
					for(int i = 0; i < 8; i++)
						{
							if(x == WIN[i])
								val = 1;
							else if(o == WIN[i])
								val = 2;
						}
					if(val == 0)
						{
							if((x | o) == DONE)
							val = 3;
						}
					return val;
				}
	  public static void playAgain()
			{
				new TicTacToe().setVisible(true);
			}
		}


0 Comments On This Entry

 

GBC

Compile this ..

CODE

import java.awt.*;
public class GBC extends GridBagConstraints
    {
        public GBC(int gridx, int gridy)
            {
                this.gridx = gridx;
                this.gridy = gridy;
            }
        public GBC(int gridx, int gridy, int gridwidth, int gridheight)
            {
                this.gridx = gridx;
                this.gridy = gridy;
                this.gridwidth = gridwidth;
                this.gridheight = gridheight;
            }
       public GBC setAnchor(int anchor)
            {
                this.anchor = anchor;
                return this;
            }
       public GBC setFill(int fill)
            {
                this.fill = fill;
                return this;
            }
       public GBC setWeight(double weigtx, double weighty)
            {
                this.weightx = weightx;
                this.weighty = weighty;
                return this;
            }
       public GBC setInsets(int distance)
            {
                this.insets = new Insets(distance, distance, distance, distance);
                return this;
            }
      public GBC setInsets(int top, int left, int bottom, int right)
            {
                this.insets = new Insets(top,left,bottom,right);
                return this;
            }
      public GBC setIpad(int ipadx, int pady)
            {
                this.ipadx = ipadx;
                this.ipady = ipady;
                return this;
            }
      }

June 2013

S M T W T F S
      1
2345678
9101112131415
161718 19 202122
23242526272829
30      

Tags

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)

Recent Entries