The Artima Developer Community
Sponsored Link

Java Answers Forum
Two Dimensional Arrays

1 reply on 1 page. Most recent reply: Mar 17, 2002 7:33 PM by Hoody

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Two Dimensional Arrays Posted: Mar 17, 2002 4:32 PM
Reply to this message Reply
Advertisement
I just started this program and i dont know why the array is not printing in 3 rows and 3 col when i input the parameters and i dont know why the String s does not input into the positon i set it to in the array can anyone help me get on the right track, just an idea would help...

class TicTacToeBoard 
{   
   private String[][] grid;
   
   
   /**
    * Create a new tic-tac-toe board
    * @param n the size of an n x n board
    */
   public TicTacToeBoard(int n)
   {  grid = new String[n][n];
      // start with the board clear
      clear();
   }  
  
 
   /**
    * Find and return the winner of the game.
    * @return the winner of the game, an "X" or an "O"
    */
   public String getWinner()
   {  // check rows for a match
      for (int i = 0; i < grid.length; i++)
      {  
         // check next row if 1st or 2nd are all blanks
         if (grid[i][0].equals("X ") && i < grid.length-1)
         i++;  
         String p = grid[i][0];
         boolean same = true;
         int j = 1;
         while( j < grid.length && same)
         {
            if (!p.equals(grid[i][j]))
               same = false;
            else
               j++;
         }
 
         if (same) 
            return p;
 
      }
 
      // check columns for a match
       for (int j = 0; j < grid.length; j++)
      {  
        // check next column if 1st or 2nd are all blanks
        if (grid[0][j].equals("X") && j < grid.length-1)
            j++;  
         String p = grid[0][j];
         boolean same = true;
         int i = 1;
         while( i < grid[i].length && same)
         {
            if (!p.equals(grid[i][j]))
               same = false;
            else
               i++;
         }
 
         if (same) 
            return p;
      }
 
      // check columns for a match
       
      // check diagonals
      // YOU WRITE THIS CODE
     
 
 
      return " ";
   }
 
 
   
   /**
    * Sets the position for a given row and a given column
    * The cell must be empty and the row and column coordinates must be valid
    * @param i the ith row 
    * @param j the jth column
    * @param s the string to set
    */    
   public void set(int i, int j, String s)
   {  if (0 <= i && i < grid.length && 0 <= j && j < grid[i].length && grid[i][j].equals(""))
      {
         grid[i][j].equals(s);
        
      }
   }
   
 
   /**
    * Gets the value in the array for a given row and a given column
    * The  row and column coordinates must be valid
    * @param i the ith row 
    * @param j the jth column 
    * @return the string at the given row and column coordinates
    */    
   public String get(int i, int j)
   {  
       
      return  " ";
   }
 
  /**
    * Clears the board - sets all array elements to the empty string, ( " " )
    */   
  public void clear()
  {
    for (int i = 0; i < grid.length; i++) 
    {
    for (int j = 0; j < grid[i].length; j++) 
    {
        grid[i][j] = " ";
    }
    }
  }
 
  /** 
   * Prints the board
   */
   public void printBoard()
   {  
       System.out.println();
        for(int i = 0; i < grid.length; i++)
        {
            for(int j = 0; j < grid[i].length; j++)
            {
                System.out.print(grid[i][j] + " " );
 
                System.out.println();
            }
        } 
   }
 
}


Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: Two Dimensional Arrays Posted: Mar 17, 2002 7:33 PM
Reply to this message Reply
i fixed the problems i was having now im just working on the getWinner() method which is going to be real long i think......
class TicTacToeBoard 
{   
   private String[][] grid;
   private String Xwin = "The winner is X"; 
   private String Owin = "The winner is O";
   
   /**
    * Create a new tic-tac-toe board
    * @param n the size of an n x n board
    */
   public TicTacToeBoard(int n)
   {  
      grid = new String[n][n];
      // start with the board clear
      clear();
   }  
  
   /**
    * Find and return the winner of the game.
    * @return the winner of the game, an "X" or an "O"
    */
   public String getWinner()
   {  // check rows for a match
      for (int i = 0; i < grid.length; i++)
      {  
         // check next row if 1st or 2nd are all blanks
         if (grid[i][0].equals(" X ") && i < grid.length-1)
         i++;  
         String p = grid[i][0];
         boolean same = true;
         int j = 1;
         while( j < grid.length && same)
         {
            if (!p.equals(grid[i][j]))
               same = false;
            else
               j++;
         }
 
         if (same) 
            return p;
 
      }
 
      // check columns for a match
       for (int j = 0; j < grid.length; j++)
      {  
        // check next column if 1st or 2nd are all blanks
        if (grid[0][j].equals("X") && j < grid.length-1)
            j++;  
         String p = grid[0][j];
         boolean same = true;
         int i = 1;
         while( i < grid.length && same)
         {
            if (!p.equals(grid[i][j]))
               same = false;
            else
               i++;
         }
 
         if (same) 
            return p;
      }
 
      // check columns for a match
       
      // check diagonals
     
      
      if ((!(grid[0][0].equals("X"))) && (!(grid[1][1].equals("X"))) && (!(grid[2][2].equals("X")))) 
        {
                 
            return Xwin;
        }
         
        if (((grid[0][0].equals("O"))) && ((grid[1][1].equals("O"))) && ((grid[2][2].equals("O"))))
        { 
            
            return Owin;
        }
 
 
      return " ";
   }
   
 
 
   
   /**
    * Sets the position for a given row and a given column
    * The cell must be empty and the row and column coordinates must be valid
    * @param i the ith row 
    * @param j the jth column
    * @param s the string to set
    */    
   public void set(int i, int j, String s)
   {  if ((0 <= i && i < grid.length) && (0 <= j && j < grid.length) && (grid[i][j].equals(" ")))
      {
         grid[i][j] = s;
        
      }
   }
   
 
   /**
    * Gets the value in the array for a given row and a given column
    * The  row and column coordinates must be valid
    * @param i the ith row 
    * @param j the jth column 
    * @return the string at the given row and column coordinates
    */    
   public String get(int i, int j)
   {  
       
      return grid[i][j];
   }
 
  
 
  /** 
   * Prints the board
   */
   public void printBoard()
   {
        for(int i = 0; i < grid.length; i++)
        {
            for(int j = 0; j <  grid.length; j++)
                System.out.print(grid[i][j] + " ");
                System.out.println();
        }
   }
 
    /**
    * Clears the board - sets all array elements to the empty string, ( " " )
    */   
  public void clear()
  {
    for (int i = 0; i < grid.length; i++) 
    {
    for (int j = 0; j < grid.length; j++) 
    {
        grid[i][j] = " " ;
    }
    }
  }

Flat View: This topic has 1 reply on 1 page
Topic: What is wrong with my start method? Previous Topic   Next Topic Topic: Opening Large files(3MB) in a text editor.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use