Chris Pollett > Students >
Padmini

    ( Print View )

    [Bio]

    [CS297Proposal]

    [Del1]

    [Del2]

    [Del3]

    [Del4]

    [CS297 Report]

    [CS298Proposal]

    [CS298Report-PDF]

    [CS298Presentation-PDF]

    [CS298Source-ZIP]

                          

























Playing Tic-Tac-Toe Game and Storing data in XMLTYPE Table

Description: :This Deliverable is designed for creating a play on TicTacToe Board.This game will be performed by two players.The client sends each player's move to the server.Server accesses the database and updates the data in the XMLTYPE table.

The basic technologies used in this program are stream sockets and it is a Client/Server application. This application communicates with the database using the JDBC. Computers communicates with each other by using the Transport Control Protocol (TCP). Data transmitted is accompanied by addressing information that identifies the computer and the port for which it is destined. This has the effect of registering the server with the system to receive all data destined for that port.

The JDBC.API is an application that can access virtually any source and run on any platform with Java Virtual Machine. In this deliverable, it used to establish connection with relational database (Oracle9i). SQL is the standard language for accessing relational database.

Storing and Retrieving data from XMLType Table in Oracle9i

Creating an XMLTYPE table in Oracle9i.

SQL>CREATE TABLE ttboard OF XMLType;
Table created.

To store an XML document in an XMLType table or column we need to set up the following :
1.Connect as system/manager
2.Create directory XMLDIR

SQL>create directory XMLDIR as '/padmini/xmlsamples';
Directory created.

3.Provide privileges to all others schemas

SQL> grant read on directory xmldir to public with grant option;
Grant succeeded.

To store an XML document in an XMLType table or column the XML document must first be converted into an XMLType instance. This is done using the different constructors provided by the XMLType datatype.This is done using a PL/SQL function called getDocument().

SQL> create or replace function getDocument(filename varchar2) return clob
  2      authid current_user is
  3  xbfile bfile;
  4  xclob clob;
  5  begin
  6    xbfile  := bfilename('XMLDIR',filename);
  7    dbms_lob.open(xbfile);
  8
  9    dbms_lob.createtemporary(xclob,TRUE,dbms_lob.session);
 10    dbms_lob.loadfromfile(xclob,xbfile, dbms_lob.getlength(xbfile));
 11    dbms_lob.close(xbfile);
 12    return xclob;
 13  end;
 14  /

Function created.

To insert an XML document into XMLType table ttboard

INSERT INTO ttboard VALUES(XMLTYPE(getDocument('board.xml')));
1 row created.

Querying the XMLType table before the TicTacToeBoard play

SQL> set long10000
SQL> select * from ttboard;
SYS_NC_ROWINFO$
-----------------------------------------------
<board>
	<row1>
		<column1>B</column1>
		<column2>B</column2>
		<column3>B</column3>
	</row1>
	<row2>
		<column1>B</column1>
		<column2>B</column2>
		<column3>B</column3>
	</row2>
	<row3>
		<column1>B</column1>
		<column2>B</column2>
		<column3>B</column3>
	</row3>
</board>

To Update an XML document in the XMLType table

UPDATE ttboard t SET value(t) = updateXML(value(t),'/board/row3/column1/text()','O');

Client Program's output after completing the TicTacToe Game

Client Program Output

XMLType table after the TicTacToeBoard play

SQL> set long10000
SQL> select * from ttboard;
SYS_NC_ROWINFO$
-----------------------------------------------
<board>
	<row1>
		<column1>X</column1>
		<column2>B</column2>
		<column3>B</column3>
	</row1>
	<row2>
		<column1>O</column1>
		<column2>X</column2>
		<column3>B</column3>
	</row2>
	<row3>
		<column1>O</column1>
		<column2>B</column2>
		<column3>X</column3>
	</row3>
</board>

Client Server Java Clases

/** Class Name : TicTacToeClient.java
    Author     : Padmini Paladugu    
    Purpose    : This class is designed to play Tic-Tac-Toe Board.This class
    will send each player's move to the server and will get response back from the server.
 */

/*imported Packages*/
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;
import java.net.*;


/** This class was designed to play a TicTacToe game.This will send
each player's move to the server using Stream Sockets. */

public class TicTacToeClient extends JFrame
{

	private static String host="localhost";
	private static String confirm="";
	private static Socket clientSocket=null;
	private static PrintWriter out=null;
    	private static BufferedReader in=null;
	private static BufferedReader in1=null;
	int count=0;
	JButton[][]  buttonArray;
	String[] users;
	boolean gameOver;

	/**Constructor initializes the TicTacToeClient */
	public TicTacToeClient()
	{
		/** Establishing a Socket Connection*/
		try
		{
		   clientSocket=new Socket(host,8088);
		   System.out.println("This is connected to port # 8088 at "+
clientSocket.getInetAddress());
		   out=new PrintWriter(clientSocket.getOutputStream(),true);
		   in=new BufferedReader(new 
InputStreamReader(clientSocket.getInputStream()));
		}
		catch(Exception uhe)
		{
		  uhe.printStackTrace();
		  System.exit(1);
		}
		this.getContentPane().setLayout(null);
		int x=200;
		int y=100;
		buttonArray=new JButton[3][3];
		users=new String[2];
		users[0]="X";
		users[1]="O";
		gameOver=false;
		/** Creating a User Interface with JButtons*/
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<3;j++)
			{
				buttonArray[i][j] = new JButton();
				buttonArray[i][j].setBounds(x+j*51,y+i*51,50,50);
				this.getContentPane().add(buttonArray[i][j]);

				buttonArray[i][j].addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						try
						{
							button_actionPerformed(e);
						}
						catch(Exception ewr)
						{
							ewr.printStackTrace();
							System.exit(1);
						}

					}

				});
			}
		}
		setupEventHandlers();
		setSize(500,500);
	}

	/** This method will check for the proper move for each player and it will 
accept the move */
	public void button_actionPerformed(ActionEvent e)
	{
		JButton button= (JButton)e.getSource();
		for(int i=0;i<3;i++)
		{
	 		for(int j=0;j<3;j++)
	 		{
				if(buttonArray[i][j]==button)
				{
					if(!gameOver && getResponse(i,j,users[count%2]))
					{
						if(button.getText().equals(users[0])
                  ||button.getText().equals(users[1]))
						{
							JOptionPane.showMessageDialog(null,
"oops!! Please click on different cell","Warning",JOptionPane.WARNING_MESSAGE);
						}
						else
						{
							button.setText(users[count%2]);
							isGameOver();
							count++;
						}
					}

				}

			}
		}
	}

	/**This TicTacToe Game will be over if any player's move equal 
either horizontally,vertically ,
	or diagonally.This method will check for the horizontal,vertical ,or 
diagonal validity
   */
	void isGameOver()
	{
		for(int i=0;i<3;i++)
		{
			String user1 = buttonArray[i][0].getText();
			System.out.println("user" + user1);
			if(user1==null || 
(!user1.equals(users[0])&&!user1.equals(users[1])))
				continue;
			for(int j=0;j<3;j++)
			{
				String user2 = buttonArray[i][j].getText();
				if(user2==null|| 
(!user2.equals(users[0])&&!user2.equals(users[1])))
					break;
				if(!user2.equals(user1))
					break;
				if(j==2)
				{
					System.out.println("GameOver");
					JOptionPane.showMessageDialog(null,
"hoooo!! Game Over","Message",JOptionPane.INFORMATION_MESSAGE);
					gameOver=true;
					closeConnection();
					return;
				}
			}
		}

		for(int j=0;j<3;j++)
		{
			String user1 = buttonArray[0][j].getText();
			if(user1==null|| 
(!user1.equals(users[0])&&!user1.equals(users[1])))
				continue;
			for(int i=0;i<3;i++)
			{
				String user2 = buttonArray[i][j].getText();
				
if(user2==null||(!user2.equals(users[0])&&
!user2.equals(users[1])))
				break;
				if(!user2.equals(user1))
				break;
				if(i==2)
				{
					System.out.println("GameOver");
					JOptionPane.showMessageDialog(null,
"hoooo!! Game Over",
"Message",JOptionPane.INFORMATION_MESSAGE);
					gameOver=true;
					closeConnection();
					return;
				}
			}
		}
		String user1=buttonArray[0][0].getText();
		if(user1!=null&& (user1.equals(users[0])||user1.equals(users[1])))
		{
			for(int i=0;i<3;i++)
			{
				String user2 = buttonArray[i][i].getText();
				if(user2==null|| (!user2.equals(users[0])&&
!user2.equals(users[1])))
				break;
				if(!user2.equals(user1))
				break;
				if(i==2)
				{
					System.out.println("GameOver");
					JOptionPane.showMessageDialog(null,
"hoooo!! Game Over",
"Message",JOptionPane.INFORMATION_MESSAGE);
					gameOver=true;
					closeConnection();
					return;
				}
			}
		}
		if(buttonArray[0][2].getText()!=null && 
buttonArray[1][1].getText()!=null && buttonArray[2][0].getText()!=null)
		{
			if(buttonArray[0][2].getText().equals
(buttonArray[1][1].getText())&&
(buttonArray[0][2].getText().equals(users[0])||
buttonArray[0][2].getText().equals(users[1]))&& 
buttonArray[1][1].getText().equals
(buttonArray[2][0].getText()))
			{
				System.out.println("GameOver");
				JOptionPane.showMessageDialog(null,
"hoooo!! Game Over","Message",JOptionPane.INFORMATION_MESSAGE);
				gameOver=true;
				closeConnection();
				return;
			}
		}
	}


	void setupEventHandlers()
	{
		addWindowListener(new WindowHandler());

	}

	/**This method will send the player's move to the server and will get the 
response back from the server*/
	boolean getResponse(int row,int column,String user)
	{

		try
		{

		  out.println(user +"*" + (row+1) +"*"+(column+1));

		}
		catch(Exception ioe)
		{
		   ioe.printStackTrace();
		   System.exit(1);
		}
		try
		{
			confirm=in.readLine();
			System.out.println("returned value "+confirm);
		}
		catch(Exception ioe)
		{
		   ioe.printStackTrace();
		   System.exit(1);
		}

		return true;
	}
	public class WindowHandler extends WindowAdapter
	{
		public void windowClosing(WindowEvent e)
		{
			System.exit(0);
		}
	}

    /**This method will close the connection for socket,inputstream, 
and outputstream*/
	public static void closeConnection()
	{
		try
		{
			out.close();
			in.close();
			clientSocket.close();
		}
		catch(Exception uhe)
		{
		  	uhe.printStackTrace();
		  	System.exit(1);
		}

	}
	//main
	public static void main(String[] args)
	{
		TicTacToeClient f=new TicTacToeClient();
		f.setSize(500,500);
		f.show();
	}
}
/** Class Name : TicTacToeServerThread.java
    Author     : Padmini Paladugu
    Purpose    : This class will create a thread for each client,and 
it will serve
    multiple clients simultaneously.
 */
  
/**Imported packages*/
import java.net.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import java.math.*;


/**This class will accept the request from the client and it will
decode the move into strings using the StringDecoder class.This class will
establish a connection with the database using JDBC and updates 
the data in the XMLType table
*/
public class TicTacToeServerThread extends Thread
{
	private static Socket clientSocket = null;
	private static PrintWriter out=null;
	private static BufferedReader in=null;
	String moveNumber[]=new String[5];

	public TicTacToeServerThread(Socket clientSocket1)
	{
		clientSocket=clientSocket1;
	}
	public void run()
	{
		  runPlayer();
	}

	public synchronized void runPlayer()
	{

		try
		{
			out = new PrintWriter( clientSocket.getOutputStream(),true);
			in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
		}
		catch (IOException e)
		{
			e.printStackTrace();
			System.exit(1);
		}
		Connection connection=null;
	 	Statement statement=null;

	 	while(true)
	 	{
			try
			{

				String move=in.readLine();
				System.out.println("Line="+move);
				/**Decoding the input string using StringDecoder Class*/
				StringDecoder sd=new StringDecoder();
				moveNumber=sd.getMeStrings(move);
				for(int i=0;i<3;i++)
				{
					System.out.println("Parsed String = 
"+moveNumber[i]);
				}
				/** Esablishing a connection with JDBC using OracleDriver*/

				DriverManager.registerDriver(new oracle.jdbc.
OracleDriver());

				String username="scott",
				       password="tiger",
				       dbURL="jdbc:oracle:oci:@pgdev";

				connection=DriverManager.getConnection
(dbURL,username,password);

				statement=connection.createStatement();
				String sql=
"UPDATE ttboard t SET value(t) = updateXML(value(t),'/board/row";
				sql+=moveNumber[1];
				sql+="/column";
				sql+=moveNumber[2];
				sql+="/text()','";
				sql+=moveNumber[0];
				sql+="')";
				System.out.println("Update Statement="+sql);
				statement.executeUpdate(sql);

				out.println("Good_Move");

			  }
			  catch (Exception eh)
			  {
				/**Closing the connection with the database*/
				closeConnection();
				  try
				  {
					statement.close();
					connection.close();
				  }
				  catch(Exception e)
				  {
				  }
				break;
			  }

	        }//end while

	}//end runPlayer method

	/**Closing the connection with the socket ,inputstream, 
and output stream*/
	public static void closeConnection()
	{
		try
		{
			  out.close();
			  in.close();
			  clientSocket.close();
		}
		catch (IOException e)
		{

		}
	}

}//end class


/** Class Name : TicTacToeServer.java
    Author     : Padmini Paladugu
    Purpose    : This class will create a Server socket and 
listens to the client's requests.
 */

/**Imported packages*/
import java.net.*;
import java.io.*;
import java.sql.*;
import java.util.*;

public class TicTacToeServer
{
       public static void main(String args[])
       {
          TicTacToeServer ttserver=new TicTacToeServer();
          ttserver.startServer();
       }

       public static void startServer()
       {
		   Socket clientSocket = null;
		   ServerSocket serverSocket=null;
		   PrintWriter out=null;
		   BufferedReader in=null;

		   try
		   {
			   serverSocket = new ServerSocket(8088);
		   }
		   catch (IOException e)
		   {
			   e.printStackTrace();
			   System.exit(1);
		   }
		   while(true)
		   {
			   try
			   {
				   System.out.println("Waiting for client request");
				   clientSocket = serverSocket.accept();
				   System.out.println("Processing");
				   TicTacToeServerThread ttthread=
new TicTacToeServerThread(clientSocket);
				   ttthread.start();
			   }
			   catch (IOException e)
			   {
					e.printStackTrace();
				        System.exit(1);

			   }


     	       	   }//end while
       }

}