Results 1 to 9 of 9

Thread: TTT help

  1. #1
    foxteknique is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default TTT help

    My teacher gave me the basic layout for it and I have added some of my own style too it but the program is not 'smart' enough. It makes random moves - I know I need to put if/if else statements under computer moves but i don't know what to put. Help would be appreciated.

    Java Code:
    package tictactoe;
    
    import java.util.Random;
    import java.util.Scanner;
    
    public class TicTacToe {
    	private char[] cells = new char[9];
    	public static char CELL_EMPTY = ' ';
    	public static char CELL_CIRCLE = 'O';
    	public static char CELL_CROSS = 'X';
    
    	public TicTacToe() {
    		for (int i = 0; i < cells.length; i++) {
    			cells[i] = CELL_EMPTY;
    		}
    	}
    	
    	private void drawBoard() {
    		System.out.println(cells[0] + " | " + cells[1] + " | " + cells[2]);
    		System.out.println("- - - - -");
    		System.out.println(cells[3] + " | " + cells[4] + " | " + cells[5]);
    		System.out.println("- - - - -");
    		System.out.println(cells[6] + " | " + cells[7] + " | " + cells[8]);
    	}
    
    	private boolean isWinner(char symbol) {
    		boolean answer = false;
    		if (checkRows(symbol) || checkColumns(symbol) || checkDiagnals(symbol)) {
    			answer = true;
    		} else {
    			answer = false;
    		}
    		return answer;
    	}
    	
    	public void computerMove() {
    		System.out.println("======================================");
    		System.out.println("Thinking . . .");
    		try {
    			Thread.sleep(2000);
    		}
    		catch (InterruptedException ie) {
    		}
    		System.out.println("Making move . . .");
    		try {
    			Thread.sleep(2000);
    		}
    		catch (InterruptedException ie) {
    		}
    		
    		int emptyCellCount = 0;
    		for (int i = 0; i < cells.length; i++) {
    			if (cells[i] == CELL_EMPTY) {
    				emptyCellCount++;
    			}
    		}
    		
    		int[] emptyCellIndexes = new int[emptyCellCount];
    		emptyCellCount = 0;
    		for (int i = 0; i < cells.length; i++) {
    			if (cells[i] == CELL_EMPTY) {
    				emptyCellIndexes[emptyCellCount] = i;
    				emptyCellCount++;
    			}
    		}
    		
    		Random r = new Random();
    		int randomNumber = r.nextInt(emptyCellCount);
    
    		cells[emptyCellIndexes[randomNumber]] = CELL_CIRCLE;
    		
    		drawBoard();
    		
    		if (isWinner(CELL_CROSS)) {
    			System.out.println("Congratulations! You won.");
    		} else if (isWinner(CELL_CIRCLE)) {
    			System.out.println("Sorry! You lost.");
    		} else if (isFull()) {
    			System.out.println("It's a tie!");
    		} else {
    			humanMove();
    		}		
    	}
    	
    	public void humanMove() {
    		System.out.println("======================================");
    		drawBoard();
    		Scanner input = new Scanner(System.in);
    		int rowNumber = 0, colNumber = 0, index = 0;
    		try {
    			Thread.sleep(1000);
    		}
    		catch (InterruptedException ie) {
    		}
    		System.out.println("You are 'X'. It's your turn. ");
    		System.out.println("Tell me which cell you would like to occupy.");
    		System.out.println("Which row [1 - 3]? ");
    		rowNumber = input.nextInt();
    		System.out.println("Which column [1 - 3]? ");
    		colNumber = input.nextInt();
    		index = (rowNumber - 1) * 3 + (colNumber - 1);
    		while (!isInputValid(index)) {
    			System.out.println("Invalid move! Tell me which cell you would like to occupy.");
    			System.out.println("Which row [1 - 3]? ");
    			rowNumber = input.nextInt();
    			System.out.println("Which column [1 - 3]? ");
    			colNumber = input.nextInt();
    			index = (rowNumber - 1) * 3 + (colNumber - 1);
    		}
    		cells[index] = CELL_CROSS;
    drawBoard();
    		if (isWinner(CELL_CROSS)) {
    			System.out.println("Congratulations! You won.");
    			drawBoard();
    		} else if (isWinner(CELL_CIRCLE)) {
    			System.out.println("Sorry! You lost.");
    			drawBoard();
    		} else if (isFull()) {
    			System.out.println("It's a tie!");
    			drawBoard();
    		}
    		else {
    			computerMove();
    		}
    	}
    	private boolean isFull() {
    		boolean answer = true;
    		for (int i = 0; i < cells.length; i++) {
    			if (cells[i] == CELL_EMPTY) {
    				answer = false;
    				break;
    			}
    		}
    		return answer;
    	}
    	
    	private boolean isInputValid(int index) {
    		boolean answer = false;
    		if (cells[index] == CELL_CIRCLE || cells[index] == CELL_CROSS) {
    			answer = false;
    		} else {
    			answer = true;
    		}
    		return answer;
    	}
    
    	private boolean checkRows(char symbol) {
    		boolean answer = false;
    		if (cells[0] == symbol && cells[1] == symbol && cells[2] == symbol){
    			answer = true;
    		} else if (cells[3] == symbol && cells[4] == symbol && cells[5] == symbol) {
    			answer = true;
    		} else if (cells[6] == symbol && cells[7] == symbol && cells[8] == symbol) {
    			answer = true;
    		} else {
    			answer = false;
    		}
    		return answer;
    	}
    	
    	private boolean checkColumns(char symbol) {
    		boolean answer = false;
    		if (cells[0] == symbol && cells[3] == symbol && cells[6] == symbol){
    			answer = true;
    		} else if (cells[1] == symbol && cells[4] == symbol && cells[7] == symbol) {
    			answer = true;
    		} else if (cells[2] == symbol && cells[5] == symbol && cells[8] == symbol) {
    			answer = true;
    		} else {
    			answer = false;
    		}
    		return answer;
    	}
    	
    	private boolean checkDiagnals(char symbol) {
    		boolean answer = false;
    		if (cells[0] == symbol && cells[4] == symbol && cells[8] == symbol) {
    			answer = true;
    		} else if (cells[2] == symbol && cells[4] == symbol && cells[6] == symbol) {
    			answer = true;
    		} else {
    			answer = false;
    		}
    		return answer;
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    Well, how would you design a strategy for a human player?

  3. #3
    foxteknique is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    you mean analyzing the input and blocking the other players 2 in a row or adding to my own 2 in a row for a win? I know what I need to do, I just don't know how to. Sorry if that was unclear in my first post.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    That's a good place to start. So maybe write a method that detects when the player or the opponent has two in a row?

  5. #5
    foxteknique is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    I don't know how :( That's why I'm here asking for help. I tried searching for help on this before but I couldn't find anything that gave me what I needed. Hence why I'm here.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    Not to go in a circle, but how would a human detect two in a row?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Kevin's right: the first thing you have to do is write out the logic behind solving this without a computer. The algorithm will come from this.

  8. #8
    foxteknique is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    I know you guys are trying to help but I'm just stuck - I have pretty basic understanding and use of Java.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by foxteknique View Post
    I know you guys are trying to help but I'm just stuck - I have pretty basic understanding and use of Java.
    We're not asking you to use Java knowledge yet. For gosh's sake, please write out the logic of the problem itself, how you'd solve it without a computer, and then post it here.

Posting Permissions

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