Tic Tac Toe help

Discussion in 'Programming General' started by FoxTek, Nov 29, 2010.

Tic Tac Toe help
  1. Unread #1 - Nov 29, 2010 at 9:33 AM
  2. FoxTek
    Joined:
    Mar 1, 2010
    Posts:
    1,637
    Referrals:
    4
    Sythe Gold:
    0

    FoxTek Guru
    Banned

    Tic Tac Toe help

    I need to make this program 'smarter' instead of it just making random moves. Any and all help would be appreciated.

    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;
    	}
    }
     
  3. Unread #2 - Dec 5, 2010 at 12:48 PM
  4. aznguy94
    Joined:
    Feb 11, 2007
    Posts:
    304
    Referrals:
    0
    Sythe Gold:
    0

    aznguy94 Forum Addict

    Tic Tac Toe help

    make some sort of scoring system where the computer associates moving a certain position to a certain score, then it will move to the place with the highest score

    basically the ai is thinking one move ahead


    i remember doing a tic tac toe ai when i started learning c++, i may dig it up and see what i did


    maybe score based on how many marks in a row it has? and how many marks in a row the opponent has?
     
  5. Unread #3 - Dec 5, 2010 at 6:39 PM
  6. iskilli
    Joined:
    Oct 23, 2010
    Posts:
    59
    Referrals:
    0
    Sythe Gold:
    0

    iskilli Member
    Banned

    Tic Tac Toe help

    Woo i learned tic tac toe a while ago.. alot of uneeded methods. Good conventions
     
  7. Unread #4 - Dec 6, 2010 at 8:30 AM
  8. Cody
    Joined:
    May 10, 2005
    Posts:
    3,052
    Referrals:
    5
    Sythe Gold:
    30

    Cody Grand Master
    Visual Basic Programmers

    Tic Tac Toe help

    Construct a 2D array with all the possible win combination's for tic-tac-toe, Code your bot to choose a random index of that array and work towards it.

    Example:

    The method says, Ok choose wincombination #3, that one is bottom left, middle, and top right....

    Ok, if bottom left and middle and top right are all empty, then choose a random one of those and place an X.


    If all 3 are NOT empty, then choose another way to win.

    This way your "AI" will always be trying to actually win the game.
     
  9. Unread #5 - Dec 6, 2010 at 8:57 AM
  10. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Tic Tac Toe help

    OMG CODY HOLY CRAP ITS CODY CODY HOLY CRAP hi dude
     
< code to parse an array of hostnames into ips | PrimeGenerator java help >

Users viewing this thread
1 guest


 
 
Adblock breaks this site