View Single Post
Old 02-12-2013, 02:19 AM   PM User | #1
Runner94
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Runner94 is an unknown quantity at this point
How can I put this Java program on a website?

I wrote this program to play tic tac toe against the computer, and I would like to share it with my friends. Unfortunately, I don't know how to put it on a website. I have an account with 20m.com that I put some simple HTML tables and forms on before, but I only used basic javascripts for those pages. I've looked into applets, but I'm not sure if I should convert this into an applet and how I would begin to do so anyway. Thanks for your help!

Code:
import java.util.*;
import java.io.*;
public class ticTacToeVersion2  {
	public static String[] cells = new String[9];
	public static Scanner in = new Scanner(System.in);
	public static String player = "";
	public static void main(String[] args) throws FileNotFoundException {
		Arrays.fill(cells, " ");
		welcome();
		playGame();
	}
	
	public static void playGame() throws FileNotFoundException {
		Arrays.fill(cells, " ");
		Thinker aiBot;
		System.out.print("Choose your symbol (X or O): ");
		player = in.nextLine();
		if(player.equalsIgnoreCase("X")){
			aiBot = new Thinker("O");
		}
		else{
			aiBot = new Thinker("X");
		}
		if(aiBot.playerName.equals("X")){
			cells = aiBot.move(cells);
		}
		board();
		boolean emptySpots = true;
		boolean xWin = false;
		boolean oWin = false;
		String turn = "X";
		if(turn.equals("X") && aiBot.playerName.equals("X")){
			turn = "O";
		}
		while(!xWin && !oWin && emptySpots){
			boolean validMove = false;
			boolean firstTry = true;
			String move = "";
			if(turn.equals(aiBot.playerName)){
				cells = aiBot.move(cells);
				
			}
			else{
			while(!validMove){
				if(!firstTry){
					System.out.println("Let's try that again. Make sure your spot is empty, and pay careful attention to format. Sample entry: (2, 3)\n");
				}
				System.out.printf("Player '%s': enter your move as a coordinate (row, column): ", turn);
				move = in.nextLine() + "   ";
				if(move.length() >= 6 && move.substring(0,1).equals("(") && move.substring(5, 6).equals(")") && move.substring(2, 3).equals(",")){
					//System.out.println("In first if");
					Scanner checkIntX = new Scanner(move.substring(1,2) + "  ");
					Scanner checkIntY = new Scanner(move.substring(4,5) + "  ");
					if(checkIntX.hasNextInt() && checkIntY.hasNextInt()){
						//System.out.println("In second if");
						int xVal = checkIntX.nextInt();
						int yVal = checkIntY.nextInt();
						if(xVal > 0 && xVal < 4 && yVal > 0 && yVal < 4 && cells[(xVal-1)*3+yVal-1].equals(" ")){
							validMove = true;
						}
					}
					checkIntX.close();
					checkIntY.close();
				}
				firstTry = false;
			}
			int x = Integer.parseInt(move.substring(1,2));
			int y = Integer.parseInt(move.substring(4,5));
			cells[(x-1)*3 + y - 1] = turn;
			}
			if(turn.equals("X")){
				turn = "O";
			}
			else{
				turn = "X";
			}
			board();
			xWin = checkX();
			oWin = checkO();
			emptySpots = checkEmpty();
		}
		if(xWin){
			System.out.println("Player 'X' has won!");
		}
		else if(oWin){
			System.out.println("Player 'O' has won!");
		}
		else{
			System.out.println("It's a tie!");
		}
		System.out.print("Play again (Y/N)? ");
		String playAgain = in.next();
		in.nextLine();
		if(playAgain.equalsIgnoreCase("Y")){
			playGame();
		}
		else{
			System.out.println("Thanks for playing! Goodbye!");
		}
	}
	
	public static boolean checkEmpty(){
		boolean empty = false;
		for(int i = 0; i < 9; i++){
			if(cells[i].equals(" ")){
				empty = true;
			}
		}
		return empty;
	}
	
	public static boolean checkX(){
		for(int i = 0; i < 7; i+=3){
			if(cells[i].equals("X") && cells[i+1].equals("X") && cells[i+2].equals("X")){
				return true;
			}
			else if(cells[i/3].equals("X") && cells[i/3+3].equals("X") && cells[i/3+6].equals("X")){
				return true;
			}
		}
		if(cells[0].equals("X") && cells[4].equals("X") && cells[8].equals("X")){
			return true;
		}
		else if(cells[2].equals("X") && cells[4].equals("X") && cells[6].equals("X")){
			return true;
		}
		return false;
	}
	
	public static boolean checkO(){
		for(int i = 0; i < 7; i+=3){
			if(cells[i].equals("O") && cells[i+1].equals("O") && cells[i+2].equals("O")){
				return true;
			}
			else if(cells[i/3].equals("O") && cells[i/3+3].equals("O") && cells[i/3+6].equals("O")){
				return true;
			}
		}
		if(cells[0].equals("O") && cells[4].equals("O") && cells[8].equals("O")){
			return true;
		}
		else if(cells[2].equals("O") && cells[4].equals("O") && cells[6].equals("O")){
			return true;
		}
		return false;
	}
	
	public static void welcome(){
		System.out.printf("Welcome to Ian's Tic Tac Toe program!%n%n");
	}
	
	public static void board(){
		System.out.printf("%n    1   2   3%n 1  %s | %s | %s%n   -----------%n 2  %s | %s | %s%n   -----------%n 3  %s | %s | %s%n%n", 
				cells[0], cells[1], cells[2], cells[3], cells[4], cells[5], cells[6], cells[7], cells[8]);
	}

}

class Thinker {
	//Scanner battlePlans;
	String playerName, opponentName;
	public Thinker(String name) throws FileNotFoundException {
		//battlePlans = new Scanner(new FileReader("battlePlans.in"));
		playerName = name;
		if(playerName.equals("X")){
			opponentName = "O";
		}
		else{
			opponentName = "X";
		}
	}
	
	public String[] move(String[] cellsOld){
		System.out.println("AI Bot move:");
		String[] cells = cellsOld.clone();
		boolean centerEmpty = (cells[4].equals(" "));
		if(centerEmpty){
			cells[4] = playerName;
			return cells;
		}
		int moveCount = 9;
		for(int i = 0; i < 9; i++){
			if(cells[i].equals(" ")){
				moveCount--;
			}
		}
		if(moveCount == 1){
			cells[0] = playerName;
			return cells;
		}
		else if(moveCount == 2){
			if(cells[0].equals(opponentName)){
				cells[8] = playerName;
				return cells;
			}
			else if(cells[2].equals(opponentName)){
				cells[6] = playerName;
				return cells;
			}
			else if(cells[8].equals(opponentName)){
				cells[0] = playerName;
				return cells;
			}
			else if(cells[6].equals(opponentName)){
				cells[2] = playerName;
				return cells;
			}
			else if(cells[1].equals(opponentName)){
				cells[6] = playerName;
				return cells;
			}
			else if(cells[3].equals(opponentName)){
				cells[8] = playerName;
				return cells;
			}
			else if(cells[5].equals(opponentName)){
				cells[0] = playerName;
				return cells;
			}
			else if(cells[7].equals(opponentName)){
				cells[2] = playerName;
				return cells;
			}
		}
		else if(moveCount == 3){
			if((cells[0].equals(opponentName) && cells[8].equals(opponentName) && cells[4].equals(playerName)) || 
					(cells[2].equals(opponentName) && cells[6].equals(opponentName) && cells[4].equals(playerName))){
				cells[5] = playerName;
				return cells;
			}
			if(cells[0].equals(opponentName) && cells[7].equals(opponentName)){
				cells[6] = playerName;
				return cells;
			}
			else if(cells[0].equals(opponentName) && cells[5].equals(opponentName)){
				cells[2] = playerName;
				return cells;
			}
			else if(cells[2].equals(opponentName) && cells[7].equals(opponentName)){
				cells[8] = playerName;
				return cells;
			}
			else if(cells[2].equals(opponentName) && cells[3].equals(opponentName)){
				cells[0] = playerName;
				return cells;
			}
			else if(cells[8].equals(opponentName) && cells[1].equals(opponentName)){
				cells[2] = playerName;
				return cells;
			}
			else if(cells[8].equals(opponentName) && cells[3].equals(opponentName)){
				cells[6] = playerName;
				return cells;
			}
			else if(cells[6].equals(opponentName) && cells[5].equals(opponentName)){
				cells[8] = playerName;
				return cells;
			}
			else if(cells[6].equals(opponentName) && cells[1].equals(opponentName)){
				cells[0] = playerName;
				return cells;
			}
		}
		else{
			if(canWin(cells) > -1){
				cells[canWin(cells)] = playerName;
			}
		}
		for(int i = 0; i < 7; i+=3){
			if(cells[i].equals(opponentName) && cells[i+1].equals(opponentName) && cells[i+2].equals(" ")){
				cells[i+2] = playerName;
				return cells;
			}
			else if(cells[i].equals(" ") && cells[i+1].equals(opponentName) && cells[i+2].equals(opponentName)){
				cells[i] = playerName;
				return cells;
			}
			else if(cells[i].equals(opponentName) && cells[i+1].equals(" ") && cells[i+2].equals(opponentName)){
				cells[i+1] = playerName;
				return cells;
			}
			else if(cells[i/3].equals(opponentName) && cells[i/3+3].equals(opponentName) && cells[i/3+6].equals(" ")){
				cells[i/3+6] = playerName;
				return cells;
			}
			else if(cells[i/3].equals(opponentName) && cells[i/3+3].equals(" ") && cells[i/3+6].equals(opponentName)){
				cells[i/3+3] = playerName;
				return cells;
			}
			else if(cells[i/3].equals(" ") && cells[i/3+3].equals(opponentName) && cells[i/3+6].equals(opponentName)){
				cells[i/3] = playerName;
				return cells;
			}
			else if(cells[0].equals(opponentName) && cells[4].equals(opponentName) && cells[8].equals(" ")){
				cells[8] = playerName;
				return cells;
			}
			else if(cells[0].equals(opponentName) && cells[4].equals(" ") && cells[8].equals(opponentName)){
				cells[4] = playerName;
				return cells;
			}
			else if(cells[0].equals(" ") && cells[4].equals(opponentName) && cells[8].equals(opponentName)){
				cells[0] = playerName;
				return cells;
			}
			else if(cells[2].equals(opponentName) && cells[4].equals(" ") && cells[6].equals(opponentName)){
				cells[4] = playerName;
				return cells;
			}
			else if(cells[2].equals(" ") && cells[4].equals(opponentName) && cells[6].equals(opponentName)){
				cells[2] = playerName;
				return cells;
			}
			else if(cells[2].equals(opponentName) && cells[4].equals(opponentName) && cells[6].equals(" ")){
				cells[8] = playerName;
				return cells;
			}
		
		}

		
		if(cells[0].equals(" ")){
			cells[0] = playerName;
			return cells;
		}
		else if(cells[2].equals(" ")){
			cells[2] = playerName;
			return cells;
		}
		else if(cells[6].equals(" ")){
			cells[6] = playerName;
			return cells;
		}
		else if(cells[8].equals(" ")){
			cells[8] = playerName;
			return cells;
		}
		ArrayList<Integer> possibilities = new ArrayList<Integer>();
		for(int i = 0; i < 9; i++){
			if(cells[i].equals(" ")){
				possibilities.add(i);
			}
		}
		Random rn = new Random();
		cells[possibilities.get(rn.nextInt(possibilities.size()))] = playerName;
		return cells;
	}
	public int canWin(String[] cells){
		for(int i = 0; i < 7; i+=3){
			if(cells[i].equals(playerName) && cells[i+1].equals(playerName) && cells[i+2].equals(" ")){
				return i+2;
			}
			else if(cells[i].equals(" ") && cells[i+1].equals(playerName) && cells[i+2].equals(playerName)){
				return i;
			}
			else if(cells[i].equals(playerName) && cells[i+1].equals(" ") && cells[i+2].equals(playerName)){
				return i+1;
			}
			else if(cells[i/3].equals(playerName) && cells[i/3+3].equals(playerName) && cells[i/3+6].equals(" ")){
				return i/3+6;
			}
			else if(cells[i/3].equals(playerName) && cells[i/3+3].equals(" ") && cells[i/3+6].equals(playerName)){
				return i/3+3;
			}
			else if(cells[i/3].equals(" ") && cells[i/3+3].equals(playerName) && cells[i/3+6].equals(playerName)){
				return i/3;
			}
			else if(cells[0].equals(playerName) && cells[4].equals(playerName) && cells[8].equals(" ")){
				return 8;
			}
			else if(cells[0].equals(playerName) && cells[4].equals(" ") && cells[8].equals(playerName)){
				return 4;
			}
			else if(cells[0].equals(" ") && cells[4].equals(playerName) && cells[8].equals(playerName)){
				return 0;
			}
			else if(cells[2].equals(playerName) && cells[4].equals(" ") && cells[6].equals(playerName)){
				return 4;
			}
			else if(cells[2].equals(" ") && cells[4].equals(playerName) && cells[6].equals(playerName)){
				return 2;
			}
			else if(cells[2].equals(playerName) && cells[4].equals(playerName) && cells[6].equals(" ")){
				return 6;
			}
		}
		return -1;
	}

}

Last edited by Runner94; 02-12-2013 at 11:38 AM.. Reason: Resolved
Runner94 is offline   Reply With Quote