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.
Page 1 of 5 123 ... LastLast
Results 1 to 15 of 62
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question help with arrays

    I have completed part of my tic tac toe program. If you can look at the code and tell me what i am doing wrong I would appreciate it. I am working on the board and passing it to the test.

    Code:
    public class TicTacToeBoard {
            private String [][] board;
            
            public TicTacToeBoard(String[][]boardArray){
            	board=boardArray;}
            
            
           public void displayBoard()
           {
            		System.out.println("  |  |  ");
            		System.out.println("--------");
            		System.out.println("  |  |  ");
            		System.out.println("--------");
            		System.out.println("  |  |  ");
            	}
          
    }
    and the board passes to the test..but i have errors..

    Code:
    import java.util.Scanner;
    
    
    public class TicTacToeTest {
    
    	
    	public static void main(String[] args) {
    		
    		TicTacToeBoard board = new TicTacToeBoard(board);
    		
    		System.out.println("Main Menu\n WELCOME TO TIC-TAC-TOE!");
    		System.out.println();
    		System.out.println("1. One player game.\n2. Two player game.");
    		Scanner input= new Scanner(System.in);
    		
    		System.out.print("Please Select: ");
    		int game= input.nextInt();
    		System.out.printf("selection=%d", game);
    		if ( game==1){
    			System.out.println("One Player Game\nWho will go first?\n1. Me.\n2. The Computer.");
    		System.out.print("Please Select: ");
    		int choice= input.nextInt();
    		System.out.printf("selection=%d", choice);}
    		else {
    			System.out.println("Two Player Game");
    		System.out.print("Please enter first players name:\n");
    		String player1=input.next();
    		
    		System.out.print("Please enter Second players name:");
    		String player2=input.next();
    		System.out.printf("second player=%s\n",player1);
    		System.out.printf("first player=%s\n",player1);}
    		}
    		board.displayBoard();	
    
    	}
    thanks for your help
    Last edited by mdnealy; 12-11-2009 at 03:55 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    Code:
    		TicTacToeBoard board = new TicTacToeBoard(board);
    makes no sense.

    Next time, tell us what error message(s) you're getting.

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts

    resolved!!

    the error is in the test.. the highlighted is underlined.

    TicTacToeBoard board = new TicTacToeBoard(board);

    and if this doesn't make sense...could you please explain why. dont i have to do an instance of board so i can execute it through the test?
    Last edited by mdnealy; 12-09-2009 at 06:47 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Board is not defined as a string[][] at this point in time.
    I'd suggest actually writing a default constructor for the TicTacToeBoard class:
    PHP Code:
    public TicTacToeBoard()
    {
        
    String[][] = {{" "" "" "}, {" "" "" "}, {" "" "" "}};
        
    this(s);

    This will prevent it from tossing an error, and let you use just new TicTacToeBoard() without a given board.
    Then in display board, you follow the same basic idea, but you loop you're array to display it (I can't test this atm, sorry):
    PHP Code:
    public String displayBoard()
    {
        
    StringBuilder sbResult = new StringBuilder();
        for (
    int i 0this.board.length; ++i)
        {
            for (
    int j 0this.board[i].length; ++j)
            {
                if (
    0)
                {
                    
    sbResult.append(" |");
                }   
                
    sbResult.append(" " this.board[i][j]);
            }
            
    sbResult.append("-----------\n");
        }

       return 
    sbResult.toString();


    Also, running you're code through the cli compilier will actually tell you what the problem is, in this case its probably complaining about unknown symbol board with a little arrow pointing to it. Most IDE's will also have an output for this, visual studio has theirs in the errors tab (not for java obviously), eclipse has a console tab I believe is what its called. Always post that if you're experiencing a particular compilation error - sometimes its not what it seems.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    But see my answer to your question about enum.

    You don't WANT to use String as the base type of your board array.

    (Well, YOU may want to, but it looks like the instructor doesn't want you to. <grin/>)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I did fix that part. i created an enum..and changed the board. can you look at my board code and see what you think..
    Code:
    //.  THE ARRAY IS HERE.  I SET THE BOARD TO EMPTY SO
    //THAT WHEN A PLAYER MAKES A MOVE IF IT IS E THEN THEY CAN REPLACE IT WITH AND X OR O.
    //  I THEN DID A METHOD THAT WHEN PLAYER 1 WHICH WILL BE X AND HUMAN IT WOULD PLACE AN X IN THE BOARD...PLAYER TWO H
    //HUMAN OR COMPUTER WILL PLACE AN O.  AGAIN I HAVEN'T ASSIGNED THE PLAYER TO X OR O YET.  COULD YOU LOOK AT THIS 
    // AND LET ME KNOW SOME OF MY QUESTIONS.
    
    
    
    import java.util.Arrays;
    
    public class TicTacToeBoard 
    {
        private TicTacToeEnum[][] moves;
           
    	public TicTacToeBoard(){
    			moves= new TicTacToeEnum[3][3];
    	}
             //I AM NOT SURE WHY THE MOVES.ADD(MOVE); IS ERRORING...
           public void addMoves(TicTacToeEnum move){
        	   moves.add(move);
           }
    
           {    // I DID THIS TO SET MY BOARD TO EMPTY     
           TicTacToeEnum[][] letter= new TicTacToeEnum[3][3];
           letter[0][0]= TicTacToeEnum.E;
           letter[0][1]= TicTacToeEnum.E;
           letter[0][2]= TicTacToeEnum.E;
           letter[1][0]= TicTacToeEnum.E;
           letter[1][1]= TicTacToeEnum.E;
           letter[1][2]= TicTacToeEnum.E;
           letter[2][0]= TicTacToeEnum.E;
           letter[2][1]= TicTacToeEnum.E;
           letter[2][2]= TicTacToeEnum.E;
           	//I LOOPED SO THAT WHEN I DO GET A VALUE IT WILL GO ROW= I AND COLUMN = J DO
           // I NEED TO PRINT EACH OUT SEPARATE?  I AM NOT SURE WHY IT IS NOT READING THE [I][J]
            for(int i=0;i<letter.length;i++);
            	for(int j = 0; j< letter.length;j++);
            {System.out.printf("%s",getValue(letter[i][j]));}
            
           //I DID THIS SO THAT IT WILL KNOW TO PUT IN THE STRING.. I HAVENT ATTACHED TO THE PLAYER YET..
           public static String getValue(TicTacToeEnum move)
           { 
        	   String letterDescription = "";
        	   if (move==TicTacToeEnum.X){
        		   letterDescription = "X";
        	   }
        	   else
        		   if (move==TicTacToeEnum.O){
        			   letterDescription = "O";
        		   }
        	   return letterDescription;
           }
           }
    thanks so much for your help

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    So that, eventually, you can play several games without having to restart the program each time, I'd put the board-clearing code (that is, setting all cells to empty) into a method, so you can call it as needed.

    And I'd use a switch in getValue, thus:
    Code:
        public static String getValue(TicTacToeEnum move)
        { 
            switch ( move )
            {
                 case TicTacToeEnum.X: return "X";
                 case TicTacToeEnum.Y: return "Y";
                 case else: return " ";
            }
        }
    There's actually a much more elegant way to do this, using mildly advanced coding for the enum, but this is a start.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    OOPS...your board printing code is flawed! You can *NOT* put ; after the for!

    Also, you REALLY should *ALWAYS* use {...}

    Code:
        for(int i=0;i<letter.length;i++)
        {
            for(int j = 0; j< letter[i].length;j++)
            {
                System.out.printf("%s",getValue(letter[i][j]));
            }
        }
    But that will print an ugly board. You should rethink it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Technically you can have the ; in a for or while. Its just that its evaluated as true once where it then breaks - so logically this kinda defeats the purpose of the loop :P

    Look at my suggestion for the display, although untested it looks nice in my head
    You'll need to modify it a bit to handle the enum to char display, but I suspect it will work.

    Also, as old pendant mentioned, I would always use the {} around block statements even where optional (such as a one line if or for statement). This isn't embedded device code we're talking about, so taking a minor blow on optimization for readability is much better - not to mention that the compiler may take care of optimizing that anyway.

    All and all, this is getting there. Soon you'll be able to integrate the actual play method where it determines if a line is owned by a particular player for the win. A little more work with a standard 2D array, but certainly doable.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Fou-Lu caught me: I was sloppy in saying you can't put the semicolon there. You can. It just won't do what you think it will, then.

    Fou-Lu: When compiled to byte-code (long before even the conversion to machine code) there is no difference between source with { } and source without it (assuming that it's a single statement enclosed in the braces, of course). That's not even an optimization: The "extra" braces are just syntactic sugar and have no impact on the semantics, so by definition the compiled code *should* be identical.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I am really missing something here. I have moved the code around and around and I can get a print out of X, O in a straight line up and down. I need the board to look like this..

    |__|__|__|
    |__|__|__|
    |__|__|__| with the x's and o's going inside. do I make another 2 dimensional array so that i can put the other array inside...

    here is what i have on my board class, remember I have
    other classes in my program for test, player, computer player, and enum...

    I am not trying to be dumb but it seems to be happening on its own...

    Code:
    public class TicTacToeBoard 
    {
        private TicTacToeEnum[][] board;     
    	public TicTacToeBoard(){
    			board=new TicTacToeEnum[3][3];
    			{
    	
    	//TicTacToeEnum[][] board;{
    	    {{board[0][0]= TicTacToeEnum.X;board[0][1]= TicTacToeEnum.X;	board[0][2]= TicTacToeEnum.X; }  
            {board[1][0]= TicTacToeEnum.O;board[1][1]= TicTacToeEnum.X;board[1][2]= TicTacToeEnum.X;}
            {board[2][0]= TicTacToeEnum.O;	board[2][1]= TicTacToeEnum.X;board[2][2]= TicTacToeEnum.X;} }}}
            
        //   public void addMoves(TicTacToeEnum move){
       
           public void printBoard()
           {    
        	  
           for(int row =0;row<board.length;row++)
           {
               for(int column = 0; column< board[ row ].length;column++)
               {
                   System.out.printf("%s\n",getValue(board[ row ][column]));}}
              
           }
            
           
           public static String getValue(TicTacToeEnum board)
           { 
        	   String letterDescription = "";
        	   if (board==TicTacToeEnum.X){
        		   letterDescription = "X";
        	   }
        	   else
        		   if (board==TicTacToeEnum.O){
        			   letterDescription = "O";
        		   }
        	   return letterDescription;
           }
    
    
    	
    }
    here is my test in case it is needed
    Code:
    import java.util.Scanner;
    
    
    public class TicTacToeTest {
    
    	
    	public static void main(String[] args) {
    				
    		System.out.println("Main Menu\n WELCOME TO TIC-TAC-TOE!");
    		System.out.println();
    		System.out.println("1. One player game.\n2. Two player game.");
    		Scanner input= new Scanner(System.in);
    		
    		System.out.print("Please Select: ");
    		int game= input.nextInt();
    		System.out.printf("selection=%d", game);
    		if ( game==1){
    			System.out.println("One Player Game\nWho will go first?\n1. Me.\n2. The Computer.");
    		System.out.print("Please Select: ");
    		int choice= input.nextInt();
    		System.out.printf("selection=%d", choice);}
    		else {
    			System.out.println("Two Player Game");
    			HumanPlayer hp= new HumanPlayer(null, null);
    			hp.getFirstPlayer();
    			hp.getSecondPlayer();
    			TicTacToeBoard bd = new TicTacToeBoard();
    			bd.printBoard();	
    		
    		}
    		
    		
    		
    		
    		
    		
    		
    	}
    	}
    and of course the enum
    Code:
    public enum TicTacToeEnum {
    					X
    					,O
    					,Empty
    }

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    No no no...you do *not* need another array.

    You just need a smarter board printer. Or, actually, a dumber one.

    Don't try to finesse it. Just use brute force:
    Code:
    0,0 | 0,1 | 0,2
    --------------
    1,0 | 1,1 | 1,2
    --------------
    2,0 | 2,1 | 2,2
    where here the r,c notation means "print the string value of the cell with those coordinates".

    Try this:

    Add a showRow(rownumber) method and then have showBoard just do:
    Code:
    showRow(0);
    showLine();
    showRow(1);
    showLine();
    showRow(2);
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    What the heck:
    Code:
         String rowToText(row)
         {
             return getValue(letter[row][0]) + "|" 
                  + getValue(letter[row][1]) + "|" 
                  + getValue(letter[row][0]);
         }
         void showBoard( )
         {
              System.out.println(rowToText(0));
              System.out.println("-----");
              System.out.println(rowToText(1));
              System.out.println("-----");
              System.out.println(rowToText(2));
          }
    See? K.I.S.S.

    There are times to use loops and there are times NOT to use them. This is a time NOT to.

    I'm curious why you chose the name "letter" instead of "board". The latter is more descriptive. And empty cell doesn't contain a letter. And, on top of that, because you are using an enum, none of the cells *actually* contain a letter, at all.
    Last edited by Old Pedant; 12-11-2009 at 06:57 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    mdnealy (12-11-2009)

  • #14
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    do i make this:
    Code:
    0,0 | 0,1 | 0,2
    --------------
    1,0 | 1,1 | 1,2
    --------------
    2,0 | 2,1 | 2,2

    a method? when i put the second code you had in the only error was

    (rownumber)

    I guess i don't understand where in my code do you put this..

  • #15
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    okay, your last post getting rid of the loop worked great...I changed letter back to board because what i did didn't make sense...just got confusted. okay now i am on to getting the inputs from players and validating....Please keep an eye out for me...I am on my way thanks so much!!!


  •  
    Page 1 of 5 123 ... LastLast

    Posting Permissions

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