View Single Post
Old 03-29-2009, 09:36 PM   #1 (permalink)
bmjones
New Member
 
Join Date: Mar 2009
Model: 8330
PIN: N/A
Carrier: Verizon Wireless
Posts: 2
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Building first app, general help needed.

Please Login to Remove!

Hi, im real new to this and have no clue what im doing. I have a java file that does a real simple tic tac toe game with a server. I have to figure out how to install this program on my BB 8330. I was wondering if someone could give me some pointers or steps on what i have to do. The code was written as a Midlet i think and is supposed to be a general java phone app. Just not sure how to install it on my phone. Here is the code in the java file:

Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.*;

interface Constants
{
	public static final int CONDITION_WIN 	=	255;
	public static final int CONDITION_LOST =	254;
	public static final int CONDITION_DRAW	=	253;
}

class TicTacCanvas extends Canvas implements Constants
{
	private TicTacClient app_;

	public TicTacCanvas(TicTacClient app)
	{
		app_ = app;
	}

	public void paint(Graphics g)
	{
		int linesx, linesy, lineex, lineey;
		int circlecx, circlecy, circler;

		g.setColor(255, 255, 255);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(0, 0, 0);

		//g.drawString("Hello World!", 0, 0, Graphics.LEFT | Graphics.TOP);

		int i, j, contents;
		int skipX, skipY;

		g.setColor(0, 0, 0);

		//Draw lines of tic tac toe board
		skipX = getWidth() / 3;
		skipY = getHeight() / 3;

		//Draw game board lines
		for (i = 1; i < 3; i++)
		{
			linesx = i * skipX;
			linesy = 0;

			lineex = i * skipX;
			lineey = getHeight();

			g.drawLine(linesx, linesy, lineex, lineey);

			for (j = 1; j < 3; j++)
			{
				linesx = 0;
				linesy = j * skipY;

				lineex = getWidth();
				lineey = j * skipY;

				g.drawLine(linesx, linesy, lineex, lineey);
			}
		}

		//Draw Xes and Os
		for (i = 0; i < 3; i++)
		{
			for (j = 0; j < 3; j++)
			{
				contents = app_.gameBoard[(j * 3) + i];

				if (contents == 1)
				{
					//Draw blue X
					g.setColor(0, 0, 255);

					linesx = (i * skipX) + 2;
					linesy = (j * skipY) + 2;

					lineex = ((i + 1) * skipX) - 2;
					lineey = ((j + 1) * skipY) - 2;

					g.drawLine(linesx, linesy, lineex, lineey);

					linesx = ((i + 1) * skipX) - 2;
					linesy = (j * skipY) + 2;

					lineex = (i * skipX) + 2;
					lineey = ((j + 1) * skipY) - 2;

					g.drawLine(linesx, linesy, lineex, lineey);
				}
				else if (contents == 2)
				{
					//Draw red O
					g.setColor(255, 0, 0);

					circlecx = (i * skipX) + (skipX / 2) + 1;
					circlecy = (j * skipY) + (skipY / 2);

					if (skipX < skipY)
						circler = skipX / 2;
					else
						circler = skipY / 2;

					circler -= 3;

					//note the ridiculous gymnastics we have to do to draw a simple circle
					g.drawArc(circlecx - (circler), circlecy - (circler), circler * 2, circler * 2, 0, 360);
				}
			}
		}

		//If we are waiting for something, notify user
		if (app_.winner != 0)
		{
			g.setColor(0, 0, 0);

			if (app_.winner == 1)
			{
				g.drawString("You Win!", getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.BASELINE);
			}
			else if (app_.winner == 2)
			{
				g.drawString("You Lose!", getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.BASELINE);
			}
			else
			{
				g.drawString("Draw!", getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.BASELINE);
			}
		}
	}

	public void keyPressed(int keyCode)
  	{
		int cell = -1;

		//no input if winner
		if (app_.winner != 0)
			return;

		switch (keyCode)
		{
			case KEY_NUM1:
			cell = 0;
			break;

			case KEY_NUM2:
			cell = 1;
			break;

			case KEY_NUM3:
			cell = 2;
			break;

			case KEY_NUM4:
			cell = 3;
			break;

			case KEY_NUM5:
			cell = 4;
			break;

			case KEY_NUM6:
			cell = 5;
			break;

			case KEY_NUM7:
			cell = 6;
			break;

			case KEY_NUM8:
			cell = 7;
			break;

			case KEY_NUM9:
			cell = 8;
			break;
		}

		if (cell < 0)
			return;

		//If the cell is clear, fill it in
		if (app_.gameBoard[cell] == 0)
		{
			app_.gameBoard[cell] = 1;
			app_.winner = app_.checkWin();

			if (app_.winner == 0)
			{
				//Send move to server, receive server's move
				if (app_.writeMove(cell))
				{
					app_.readMove();
				}
			}
			else	//Send win condition to server
			{
				if (app_.winner == 1)
					app_.writeMove(CONDITION_WIN);
				else if (app_.winner == 2)
					app_.writeMove(CONDITION_LOST);
				else
					app_.writeMove(CONDITION_DRAW);
			}
		}

		repaint();
		serviceRepaints();
	}
}

public class TicTacClient extends MIDlet implements CommandListener, Constants
{
	private int move_;
	private TicTacCanvas canvas_;
	private Display display_;

	private SocketConnection sc_;
	private InputStream is_;
	private OutputStream os_;

	public int[] gameBoard;
	public int winner;

	public TicTacClient()
	{
		winner = 0;

		canvas_ = new TicTacCanvas(this);

		gameBoard = new int[9];

		for (int i = 0; i < 9; i++)
		{
			gameBoard[i] = 0;
		}
	}

	public void startApp()
	{
		openConnection();

		display_ = Display.getDisplay(this);
		display_.setCurrent(canvas_);
		canvas_.repaint();
	}

	public void pauseApp()
	{
	}

	public void destroyApp(boolean unconditional)
	{
		closeConnection();
	}

	public void commandAction(Command c, Displayable s)
	{

  	}

  	boolean writeMove(int movedata)
  	{
		move_ = (char)movedata;

		try
		{
			os_.write(move_);
		}
		catch(IOException e)
		{
			Alert a = new Alert("TicTacClient", "Write Move: Connection failed...", null, AlertType.ERROR);
			a.setTimeout(Alert.FOREVER);
			display_.setCurrent(a);
			return false;
		}

		if (move_ >= CONDITION_DRAW)
		{
			//game over
			closeConnection();
		}

		return true;
	}

  	boolean readMove()
  	{
		try
		{
			//we only want to read one byte
			int move_ = 0;
			move_ = is_.read();

			if (move_ >= 0)
			{
				gameBoard[move_] = 2;

				winner = checkWin();

				if (winner > 0)
				{
					if (winner == 1)
					{
						writeMove(CONDITION_WIN);
					}
					else if (winner == 2)
					{
						writeMove(CONDITION_LOST);
					}
					else
					{
						writeMove(CONDITION_DRAW);
					}

					closeConnection();

					return (false);
				}
				else
					return (true);
			}
		}
		catch(IOException e)
		{
			Alert a = new Alert("TicTacClient", "ReadMove: " + e, null, AlertType.ERROR);
			a.setTimeout(Alert.FOREVER);
			display_.setCurrent(a);
		}

		return (false);
	}

	void openConnection()
	{
		try
		{
			sc_ = (SocketConnection)Connector.open("socket://SERVER IP AND PORT");
			is_ = sc_.openInputStream();
			os_ = sc_.openOutputStream();
		}
		catch(IOException e)
		{
			Alert a = new Alert("TicTacClient", "Open Connection: connection failed...", null, AlertType.ERROR);
			a.setTimeout(Alert.FOREVER);
			display_.setCurrent(a);
		}
	}

	void closeConnection()
	{
		try
		{
			is_.close();
			os_.close();
			sc_.close();
		}
		catch(IOException e)
		{
			Alert a = new Alert("TicTacClient", "Close Connection: connection failed...", null, AlertType.ERROR);
			a.setTimeout(Alert.FOREVER);
			display_.setCurrent(a);
		}

	}

	int checkWin()
	{
		if (scanBoard(1))
			return 1;

		if (scanBoard(2))
			return 2;

		if (scanBoard(3))
			return 3;

		return 0;
	}

	boolean scanBoard(int player)
	{
		int i;

		//Scan for a draw
		if (player == 3)
		{
			for (i = 0; i < 9; i++)
			{
				if (gameBoard[i] == 0)
					return false;
			}

			return true;
		}

		//Check horizontals and verticals
		for (i = 0; i < 3; i++)
		{
			if ( (gameBoard[(i * 3)] == player) &&
				 (gameBoard[(i * 3) + 1] == player) &&
				 (gameBoard[(i * 3) + 2] == player) )
			{
				return true;
			}

			if ( (gameBoard[i] == player) &&
				 (gameBoard[(1 * 3) + i] == player) &&
				 (gameBoard[(2 * 3) + i] == player) )
			{
				return true;
			}

		}

		//Check diagonals
		if ( (gameBoard[0] == player) &&
			 (gameBoard[1 + 3] == player) &&
			 (gameBoard[2 + 6] == player) )
		{
			return true;
		}

		if ( (gameBoard[2] == player) &&
			 (gameBoard[1 + 3] == player) &&
			 (gameBoard[6] == player) )
		{
			return true;
		}

		return false;
	}
}
Offline   Reply With Quote