Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    If you want to run it online, you'll need to wrap it in an applet.
    I'd start by wrapping it in a gui, and then wrap that into the applet. Command line apps don't really work well in web environment, although technically you can write the interception for the input on the applet, but you may as well make it look pretty first.

    Split the classes up as well; the cli interface can be abandoned when you write the gui, but the logic is good for both.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Runner94 (02-12-2013)

  • #3
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks, I'll try to figure out GUI's and Applets using the Java tutorials on Oracle's website. If you have any other tutorial suggestions, please let me know. Also, what exactly do you mean by split up the classes, and what is the cli interface? Are these things I will probably learn about in the tutorials?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Oracle's swing tutorials don't typically do the most graceful approaches, but they are very excellent nonetheless.
    You'll want to hit the swing tutorials up for frames, buttons and event handlers. Then you can move the applet since its super easy to wrap the swing with an applet.

    For splitting, you have two classes there, the thinkiner and the runner. The runner there is your cli application running on a command line with command line input and output. The thinker simply processes what has to happen. Separating them means you can use it in either a command line interface or in a gui, without needing to change the logic at all. The interface simply changes.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Runner94 (02-12-2013)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •