Results 1 to 7 of 7
  1. #1
    Alex is offline Member
    Join Date
    Apr 2015
    Posts
    7
    Rep Power
    0

    Default Tic Tac Toe Game Issue

    Hi so Im trying to make a tic tac toe game that you play against the computer using a random number generator and two dimensional arrays for the game board. Im not trying to make a GUI, the assignment is to have the board in the console, which I have done. I have run into a few problems with trying to get the computer player to correctly generate 2 integers and have those two integers be a place on the game board. Here is my code so far. Any help would be great.

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    public class TicTacToe {
    
    	private static Scanner keyboard = new Scanner(System.in);
    	private static char[][] board = new char[3][3];
    	public static int row, col;
    
    	// *******************************************************
    	public static void main(String[] args) {
    		System.out.println("Welcome to Tic-Tac-Toe!!");
    		System.out
    				.println("Would you like to play a game? (Enter yes or no): ");
    		String answer = keyboard.next();
    
    		switch (answer) {
    		case "yes":
    		case "Yes":
    		case "YES":
    			playGame();
    		case "no":
    		case "No":
    		case "NO":
    			isDone(row, col);
    		default:
    			System.out.println("Invalid answer.");
    		}
    
    		System.out.println("Goodbye!");
    	}
    
    	// *******************************************************
    	private static void playGame() {
    		char currentPlayer = determineFirstPlayer();
    
    		System.out.println("The computer is X, the human is O.\n");
    
    		initializeGameBoard();
    		boolean playing = true;
    		displayGameBoard();
    		while (playing) {
    			board[row][col] = currentPlayer;
    			if (isDone(row, col)) {
    				playing = false;
    				System.out.println("Game over! Player " + currentPlayer
    						+ " has won.");
    			}
    
    			displayGameBoard();
    			if (currentPlayer == 'X') { // the computer
    				processComputerMove();
    				currentPlayer = 'O'; // the human
    			} else {
    				displayGameBoard();
    				processHumanMove();
    				currentPlayer = 'X'; // the computer
    			}
    		}
    	}
    
    	// *******************************************************
    	private static char determineFirstPlayer() {
    		System.out.println("Who should move first? Computer = c, human = h.");
    		String firstplayer = keyboard.next();
    		boolean player;
    		switch (firstplayer) {
    		case "c":
    			player = false;
    			processComputerMove();
    			return 'X';
    		case "h":
    			player = true;
    			processHumanMove();
    			return 'O';
    		default:
    			System.out.println("Invalid answer.");
    			isDone(row, col);
    		}
    		return 'X';
    
    	}
    
    	// *******************************************************
    	private static void initializeGameBoard() {
    
    		for (int i = 0; i < 3; i++) {
    			for (int j = 0; j < 3; j++) {
    				board[i][j] = '_';
    			}
    		}
    
    	}
    
    	// *******************************************************
    	private static void processComputerMove() {
    		
    		Random compturn1 = new Random();
    		Random compturn2 = new Random();
    		
    		row = compturn1.nextInt(2);
    		col = compturn2.nextInt(2);
    		
    		System.out.println(row);
    		System.out.print(col);
    		
    		
    	}
    
    	// *******************************************************
    	private static void processHumanMove() {
    		row = keyboard.nextInt() - 1;
    		col = keyboard.nextInt() - 1;
    	}
    
    	// *******************************************************
    	private static void displayGameBoard() {
    		for (int i = 0; i < 3; i++) {
    			System.out.println();
    			for (int j = 0; j < 3; j++) {
    				if (j == 0)
    					System.out.print("| ");
    				System.out.print(board[i][j] + " | ");
    			}
    		}
    	}
    
    	// *******************************************************
    	private static boolean isDone(int rMove, int cMove) {
    		if (board[0][cMove] == board[1][cMove]
    				&& board[0][cMove] == board[2][cMove])
    			return true;
    		if (board[rMove][0] == board[rMove][1]
    				&& board[rMove][0] == board[rMove][2])
    			return true;
    		if (board[0][0] == board[1][1] && board[0][0] == board[2][2]
    				&& board[1][1] != '_')
    			return true;
    		if (board[0][2] == board[1][1] && board[0][2] == board[2][0]
    				&& board[1][1] != '_')
    			return true;
    
    		return false;
    
    	}
    
    	// *******************************************************
    	private static boolean isComputerWin() {
    
    		return false; // return true or false as appropriate
    	}
    
    	// *******************************************************
    	private static boolean isHumanWin() {
    
    		return false; // return true or false as appropriate
    	}
    
    	// *******************************************************
    	private static boolean isTie() {
    
    		return false; // return true or false as appropriate
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Tic Tac Toe Game Issue

    You need to be more specific. It looks like you are already generating the appropriate row/col. On a different note, you should not be using static methods and variables everywhere. Java's OO capabilities could probably help you in writing this program.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Tic Tac Toe Game Issue


  4. #4
    Alex is offline Member
    Join Date
    Apr 2015
    Posts
    7
    Rep Power
    0

    Default Re: Tic Tac Toe Game Issue

    Quote Originally Posted by pbrockway2 View Post
    I need to make it so that under ProcessComputerMove, it selects a random number between 0 and 8 and places and checks if that slot in the 2D array (my game board) is empty, and then if it is empty, it will place an X in that slot and end the turn.

    So far I have:

    Java Code:
    Random compturn = new Random();
    		
    		int index=compturn.nextInt(9) - 1;
    		
    		System.out.println();
    		System.out.println("The computer chooses cell " + index);
    however I am not sure how to make it select that position on the table and place an X on it.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Tic Tac Toe Game Issue

    You do realise that nextInt(9) - 1 gets you a value between -1 and 7 inclusive -- or don't you?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Alex is offline Member
    Join Date
    Apr 2015
    Posts
    7
    Rep Power
    0

    Default Re: Tic Tac Toe Game Issue

    Ok so aside from changing the number of the integer, which I figured out a few minutes ago was wrong, do you know how to have that integer select the position on the board?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Tic Tac Toe Game Issue

    Quote Originally Posted by Alex View Post
    do you know how to have that integer select the position on the board?
    Do you know about the % (mod) operator? Using a combination of the mod operator and the division operator you can take a value from 0 to 8 inclusive and generate a row column where each is 0,1,or 2.

    Note: If you have problems responding to this it is because the % sign screws up the editor (except for a select few in this forum).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. {FPS} Issue in my OWN Java Game ONLY+Slick2D
    By Breton in forum Java Gaming
    Replies: 0
    Last Post: 06-27-2012, 04:40 AM
  2. HELP!Another Memory Game issue
    By allan2020 in forum AWT / Swing
    Replies: 2
    Last Post: 03-06-2012, 05:41 PM
  3. Number Guessing Game issue
    By gabrielpr12 in forum New To Java
    Replies: 14
    Last Post: 01-24-2012, 11:06 PM
  4. Array game issue
    By Arom in forum New To Java
    Replies: 2
    Last Post: 11-30-2011, 08:33 PM
  5. Java Game Timer Issue! Help
    By smithywill in forum Advanced Java
    Replies: 2
    Last Post: 03-11-2010, 09:09 AM

Posting Permissions

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