The Artima Developer Community
Sponsored Link

Java Answers Forum
TicTacToe

0 replies on 1 page.

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 0 replies on 1 page
Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

TicTacToe Posted: Mar 15, 2002 6:04 PM
Reply to this message Reply
Advertisement
I just started this program and im getting frustrated because i am getting stupid null errors everywhere and i dont know where i starting can anyone help me get on the right track...

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("") && 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(" ") && j < grid.length-1)
            j++;  
         String p = grid[j][0];
         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
      // YOU WRITE THIS CODE
 
      // 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  "s";
   }
 
   /**
    * Clears the board - sets all array elements to the empty string, ( " " )
    */   
   public void clear()
   {  for (int i = 0; i < grid.length; i++)
        {
             grid[i].equals("");
        }
      for(int j = 0; j < grid.length; j++)
        {
            grid[j].equals("");
        }
   }    
 
  /** 
   * Prints the board
   */
   public void printBoard()
   {  
       System.out.println();
        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();
            }
        } 
   }
 
}

Topic: Hangman Previous Topic   Next Topic Topic: difference between  Application Servers and Web Servers

Sponsored Links



Google
  Web Artima.com   

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