Advertisements
Home > Java > Java Program to write an applet to make tic-tac-toe – Q52

Java Program to write an applet to make tic-tac-toe – Q52


Q52: Java Program to write an applet to make tic-tac-toe.

import java.awt.*;
import java.applet.*;
import java.lang.*;
import java.awt.event.*;
import java.lang.String.*;
import java.awt.Label.*;

public class Ap_Tic_Tac_Toe extends Applet implements ActionListener{
	static int k = 1;
	Button a1, a2, a0, a3, a4, a5, a6, a7, a8;
	static String b1, b2, b3, b4, b5, b6, b7, b8, b9;
	String msg = " ";
	TextField ne,ne1,ne2;

	public void init(){
		setLayout(new GridLayout(4,3));
		ne = new TextField(13);
		ne1 = new TextField(13);
		ne2 = new TextField(13);
		a0 = new Button("1");
		a1 = new Button("2");
		a2 = new Button("3");
		a3 = new Button("4");	
		a4 = new Button("5");
		a5 = new Button("6");
		a6 = new Button("7");
		a7 = new Button("8");
		a8 = new Button("9");
		
		add(a0);
		add(a1);
		add(a2);
		
		add(a3);
		add(a4);
		add(a5);
		
		add(a6);
		add(a7);
		add(a8);
		add(ne);
		add(ne1);
		add(ne2);
		a0.addActionListener(this);
		a1.addActionListener(this);
		a2.addActionListener(this);
		a3.addActionListener(this);
		a4.addActionListener(this);
		a5.addActionListener(this);
		a6.addActionListener(this);
		a7.addActionListener(this);
		a8.addActionListener(this);
		ne.addActionListener(this);
		ne1.addActionListener(this);
		ne2.addActionListener(this);
		ne1.setText("Player 1:  X");
		ne2.setText("Player 2:  0");
	}

	public void actionPerformed(ActionEvent ae){
		String str = ae.getActionCommand();
		if(str.equals("1")){
			if(k%2 == 0)
				a0.setLabel("0");
			else
				a0.setLabel("X");
		}
		if(str.equals("2")){
			if(k%2 == 0)
				a1.setLabel("0");
			else
				a1.setLabel("X");
		}
		if(str.equals("3")){
			if(k%2 == 0)
				a2.setLabel("0");
			else
				a2.setLabel("X");
		}
		if(str.equals("4")){
			if(k%2 == 0)
				a3.setLabel("0");
			else
				a3.setLabel("X");
		}
		if(str.equals("5")){
			if(k%2 == 0)
				a4.setLabel("0");
			else
				a4.setLabel("X");
		}
		if(str.equals("6")){
			if(k%2 == 0)
				a5.setLabel("0");
			else
				a5.setLabel("X");
		}
		if(str.equals("7")){
			if(k%2 == 0)
				a6.setLabel("0");
			else
				a6.setLabel("X");
		}
		if(str.equals("8")){
			if(k%2 == 0)
				a7.setLabel("0");
			else
				a7.setLabel("X");
		}
		if(str.equals("9")){
			if(k%2 == 0)
				a8.setLabel("0");
			else
				a8.setLabel("X");
			}
		k++;
		check();
	}
	
	public void check(){
		
		String msg1 = "Player 1 Wins";
		String msg2 = "Player 2 Wins";
		b1 = a0.getLabel();
		b2 = a1.getLabel();
		b3 = a2.getLabel();
		b4 = a3.getLabel();
		b5 = a4.getLabel();
		b6 = a5.getLabel();
		b7 = a6.getLabel();
		b8 = a7.getLabel();
		b9 = a8.getLabel();
		
		if(b1.equals("X")&&b2.equals("X")&&b3.equals("X"))
		ne.setText(msg1);
		
		if(b1.equals("0")&&b2.equals("0")&&b3.equals("0"))
		ne.setText(msg2);
		
		if(b4.equals("X")&&b5.equals("X")&&b6.equals("X"))
		ne.setText(msg1);
		
		if(b4.equals("0")&&b5.equals("0")&&b6.equals("0"))
		ne.setText(msg2);
		
		if(b7.equals("X")&&b8.equals("X")&&b9.equals("X"))
		ne.setText(msg1);
		
		if(b7.equals("0")&&b8.equals("0")&&b9.equals("0"))
		ne.setText(msg2);
		
		if(b1.equals("X")&&b4.equals("X")&&b7.equals("X"))
		ne.setText(msg1);
		
		if(b1.equals("0")&&b4.equals("0")&&b7.equals("0"))
		ne.setText(msg2);

		if(b2.equals("X")&&b5.equals("X")&&b8.equals("X"))
		ne.setText(msg1);
		if(b2.equals("0")&&b5.equals("0")&&b8.equals("0"))
		ne.setText(msg2);

		if(b6.equals("X")&&b3.equals("X")&&b9.equals("X"))
		ne.setText(msg1);
		if(b9.equals("0")&&b6.equals("0")&&b3.equals("0"))
		ne.setText(msg2);
	
		if(b1.equals("X")&&b5.equals("X")&&b9.equals("X"))
		ne.setText(msg1);
		if(b1.equals("0")&&b5.equals("0")&&b9.equals("0"))
		ne.setText(msg2);

		if(b3.equals("X")&&b5.equals("X")&&b7.equals("X"))
		ne.setText(msg1);
		if(b3.equals("0")&&b5.equals("0")&&b7.equals("0"))
		ne.setText(msg2);
	}
	
	public void paint(Graphics g){
		g.drawString(msg,299,100);
		}
	}

… from College notes (BCA/MCA assignments):


Advertisements
Categories: Java Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: