4 Replies - 2514 Views - Last Post: 10 November 2011 - 07:11 PM Rate Topic: -----

#1 Finch32  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-November 11

Getting user input placed into 2d array

Posted 09 November 2011 - 06:55 PM

I have been working hard to get this tic tac toe game working. I have searched the forum and really couldn't find any post that would help me. The problem that I have is that I cant figure out how to get the input from the user then enter that into my 2d array marked by a X or O.
Any suggestions will be appreciated Thanks
import java.util.Scanner;

public class TicTacToe {
	public static void main(String[] args) {
		//Create scanner
		Scanner input = new Scanner(System.in);
	
		//create array for board
			char[][]board = new char[3][3];
		//initialize the board/array
			
			
		//make final char for board display
			final char empty = ' ';
			final char playerX = 'X';
			final char playerO = 'O';
			
		
		
		//create print out of board
			for (int x = 0; x < 3; x++) {      
				System.out.println("---------");      
				for (int y = 0; y < 3; y++) {        
					System.out.print("|" + board[x][y]);      
					}      
				System.out.println("|");    
				}    
			System.out.println("---------");
			
			
		//create loop to keep game going until there is a winner
		//while(?){
			
		//Printout for player X
		System.out.println("\nEnter a row (1, 2, or 3) for player X:");
			int row = input.nextInt();
		System.out.println("Enter a column (1, 2, or 3) for player X:");
			int column = input.nextInt();
				
		//Need to printout the new board with an X where user input
			displayBoard(board,row,column);
			
		
			//call method that will check for winner
				checkWinner(board,empty);
				
	   //Printout for player O
		System.out.println("\nEnter a row (1, 2, or 3) for player O:");
			row = input.nextInt();
		System.out.println("Enter a column (1, 2, or 3) for player O:");
			column = input.nextInt();
			
		//Need to printout the new board with an O where user input
		System.out.println();
		
			//call method that will check for winner
				checkWinner(board,empty);
}
	//}while loop end

	//Create method to display board
	public static void displayBoard(char[][]board,int row,int column){
		for (int x = 0; x < 3; x++) {      
			System.out.println("---------");      
			for (int y = 0; y < 3; y++) {        
				System.out.print("|" + board[row][column]);      
				}      
			System.out.println("|");    	
		}    
		System.out.println("---------");
		
			
		}
	
	//Create method to check for winner
	public static char checkWinner(char[][]board,final char empty){
		for(int i = 0; i < board.length; i ++){
			//check horizontally
			if (board[i][0] == board[i][1] &&  
	                board[i][0] == board[i][2]) {  
	            if (board[i][0] != empty) {  
	                return board[i][0];
	                
	            }  
	        }  
			//check vertically
			if (board[0][i] == board[1][i] &&  
	                board[0][i] == board[2][i]) {  
	            if (board[0][i] != empty) {  
	                return board[0][i];  
	            }  
	        }  
		
			//check diagonally
			 if (board[0][0] == board[1][1] &&  
			            board[0][0] == board[2][2]) {  
			        if (board[0][0] != empty) {  
			            return board[0][0];  
			        }  
			    }  
			    if (board[0][2] == board[1][1] &&  
			             board[0][2] == board[2][0]) {  
			        if (board[0][2] != empty) {  
			            return board[0][2];  
			     }  
			}  
		}
		return empty;
		
	}
}
	



Is This A Good Question/Topic? 0
  • +

Replies To: Getting user input placed into 2d array

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Getting user input placed into 2d array

Posted 09 November 2011 - 08:09 PM

I have few things to comment in your code:
- First, you take user input but you didnt update your board array. I was expecting after taking user input, then you say:
 board[row -1][column - 1] = 'X';
than your displayBoard() will only need to pass the array and display it without caring what user input this time.

- You ask for either 1, 2 or 3, but you forget to -1 from them because your array index starts from 0 to 2.
Was This Post Helpful? 2
  • +
  • -

#3 acll19  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 20-October 10

Re: Getting user input placed into 2d array

Posted 09 November 2011 - 09:28 PM

You are asking the user to type in a number between 1 and 3 so you should concider taking your 2d array to a length of 4, that way you won't have to worry about the array index, but your FOR iterations should start from 1.
I also changed the checkWinner method. It wasn't working for a horizontally winning configuration.(I didn't check the vertically or the diagonally winning configurations).
Take a look at the fixed code. I hope this will help you.(I already fixed what smohd said)
 public static void main(String[] args) {
        //Create scanner
        Scanner input = new Scanner(System.in);

        //create array for board
        char[][]board = new char[4][4];
        //initialize the board/array
        for (int i= 1; i < 4; i++) {
            for (int j= 1; j < 4; j++) {
                board[i][j] = ' ';
            }
        }


        //make final char for board display
        char empty = ' ';
        final char playerX = 'X';
        final char playerO = 'O';



        //create print out of board
        displayBoard(board);


        //create loop to keep game going until there is a winner
        while(empty == ' '){
            //Printout for player X
            System.out.println("\nEnter a row (1, 2, or 3) for player X:");
            int row = input.nextInt();
            System.out.println("Enter a column (1, 2, or 3) for player X:");
            int column = input.nextInt();
            board[row][column] = 'X';
            //Need to printout the new board with an X where user input
            displayBoard(board);


            //call method that will check for winner
            empty = checkWinner(board,empty);

            //check if X won
            if(empty == 'X')
                break;
            //Printout for player O
            System.out.println("\nEnter a row (1, 2, or 3) for player O:");
            row = input.nextInt();
            System.out.println("Enter a column (1, 2, or 3) for player O:");
            column = input.nextInt();
            board[row][column] = 'O';
            //Need to printout the new board with an O where user input
            System.out.println();
            displayBoard(board);
            //call method that will check for winner
            empty = checkWinner(board,empty);
        }
        System.out.println(empty + "'s win");
    }

    //Create method to display board
	public static void displayBoard(char[][]board){
            for (int x = 1; x < 4; x++) {
                System.out.println("-------");
                for (int y = 1; y < 4; y++) {
                        System.out.print("|" + board[x][y]);
                }
                System.out.println("|");
            }
            System.out.println("-------");
        }

	//Create method to check for winner
	public static char checkWinner(char[][]board, char empty){
             //check horizontally
            for (int i= 1; i < 4; i++) {
                if (board[i][1] == board[i][2] && board[i][1] == board[i][3]) {
                    if (board[i][1] != empty) {
                        return board[i][1];

                    }
                }
            }
            for(int i = 1; i < board.length; i ++){
               
                
                //check vertically
                if (board[1][i] == board[2][i] &&   board[1][i] == board[3][i]) {
                    if (board[1][i] != empty) {
                        return board[1][i];
                    }
                }

                //check diagonally
                 if (board[1][1] == board[2][2] && board[1][1] == board[3][3]) {
                        if (board[1][1] != empty) {
                            return board[1][1];
                        }
                 }
                if (board[1][3] == board[2][2] && board[1][3] == board[3][1]) {
                    if (board[1][3] != empty) {
                        return board[1][3];
                    }
                }
            }
            return empty;

	}


Was This Post Helpful? 1
  • +
  • -

#4 acll19  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 20-October 10

Re: Getting user input placed into 2d array

Posted 09 November 2011 - 09:36 PM

My bad, your checkWinner method was working, so leave it the way it was before. You just need to check the value of EMPTY after calling the method for X.
Sorry again.
Was This Post Helpful? 0
  • +
  • -

#5 Finch32  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-November 11

Re: Getting user input placed into 2d array

Posted 10 November 2011 - 07:11 PM

O i see i just needed to have the array board after getting the user input.
Thanks alot both of you for the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1