
import java.util.*;

class Board
{
  public static void main(String [] args)
  {
    System.out.println("Java 2D Arrays (matrices)");
    System.out.println();
    System.out.println();
    
    // Define a two dimensional array
    // char[][] board = {{'X', 'O', 'X'}, {'X', 'X', 'O'}, {'O', 'O', 'X'}};

    // Or a better way to do it:
    // Think of a spreadsheet.
    // board holds the memory address and is thus
    // a reference variable.
    
    // board.length is the number of rows
    
    // board[0] refers to the first array (row 0)
    // board[0][0] refers to the first character on row 0
    // board[0][1] refers to the second character on row 0
    // board[0][2] refers to the third character on row 0

    // board[1] refers to the second array (row 1)
    // board[1][0] refers to the first character on row 1
    // board[1][1] refers to the second character on row 1
    // board[1][2] refers to the third character on row 1
    
    // board[2] refers to the third array (row 2)
    // board[2][0] refers to the first character on row 2
    // board[2][1] refers to the second character on row 2
    // board[2][2] refers to the third character on row 2

    // What is the value of board[0].length?
    // What is the value of board[1].length?
    // What is the value of board[2].length?

    // REMEMBER: board receives (=) the 
    // memory address of the matrix, and
    // thus refers to or references the object.


    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    char[][] board = {
                       {'X', 'O', 'X'}, // board[0]
                       {'X', 'X', 'O'}, // board[1]
                       {'O', 'O', 'X'}  // board[2]
                     };

    // It is really just a series of
    // one dimensional arrays
   
    // What other things can you use a 2D array for?
    // ?????
    
    
    //     columns   0    1    2
    // board[0] is {'X', 'O', 'X'} or row 0
    // board[1] is {'X', 'X', 'O'} or row 1
    // board[2] is {'O', 'O', 'X'} or row 2
    
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // How could I just print row 0? (board[0])
    System.out.println("Printing row 0");
    
    for (int c = 0; c < board.length; c++)
    {
        System.out.print(board[0][c]);
    }
    
    System.out.println();
    System.out.println("Bye Bye Row 0");
    
    // How could we print the entire matrix in
    // matrix format?
 
    // 0 1 2
    // X O X
    // X X O
    // O O X
 
    // Drink a little RC Cola
    // use r for your row variable (or row)
    // use c for your col variable (or col)
    System.out.println("Print entire matrix");
    
    
    for(int r = 0; r < board.length; r++)
    {
      // for this row r we will print out the elements
      for(int c = 0; c < board[r].length; c++)
      {
        // prints the element at row r, and column c
        // and keeps the cursor on the same line.
        System.out.print(board[r][c] + " ");
      } // end of printing the row
      
      // We now move the cursor down to the next line
      System.out.println();
    }
    System.out.println();
    
   
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    
    // Let's do an 2d array of int values
    // Welcome to mat (that's the name of this matrix)
    int [][] mat = new int[4][3];
    
    // row 0
    mat[0][0] = 98;
    mat[0][1] = 93;
    mat[0][2] = 96;
    
    // row 1
    mat[1][0] = 94;
    mat[1][1] = 96;
    mat[1][2] = 97;

    // row 2
    mat[2][0] = 92;
    mat[2][1] = 83;
    mat[2][2] = 88;

    // row 3
    mat[3][0] = 90;
    mat[3][1] = 75;
    mat[3][2] = 85;

    // *********************************
    // Let's write some methods to print 
    // *********************************
    
    // Let's print the matrix by calling a method
    System.out.println("Print entire matrix with a method");
    printMatrix(mat);
    
    
    // Let's print the matrix and the sum of each row
    System.out.println("Print entire matrix with sum of each row");
    printMatrixAndSumOfEachRow(mat);
    
    
    
    // Let's print the sum of each column
    System.out.println("Print the sums of the columns.");
    printSumOfColumns(mat);
    
    
    
  }

  
  // let's write a print method that will print
  // a 2D array of integers
  // They will need to pass to us (or send) the
  // memory address of where we can find the matrix.
  // Variable m will recieve this memory address or
  // location of the array in RAM memory.
  public static void printMatrix(int[][] m)
  {
   
    for(int r = 0; r < m.length; r++)
    {
      // for this row r we will print out the elements
      for(int c = 0; c < m[r].length; c++)
      {
        // prints the element at row r and column c
        // and keeps the cursor on the same line.
        System.out.print(m[r][c] + " ");
      } // end of printing the row
      
      // We now move the cursor down to the next line
      System.out.println();
    }
    
  }
  
  public static void printMatrixAndSumOfEachRow(int [][] m)
  {
    
    for(int r = 0; r < m.length; r++)
    {
      int sum = 0;
      
      // for this row r we will print out the elements
      for(int c = 0; c < m[r].length; c++)
      {
        // prints the element at row r and column c
        // and keeps the cursor on the same line.
        System.out.print(m[r][c] + " ");
        
        sum = sum + m[r][c];
        // sum += m[r][c];
      } // end of printing the row
      
      
      
      // We now move the cursor down to the next line
      System.out.println(sum);
    }
    
  }
  
  // This method will receive one row.
  // NOTE: It is really just a 1D array
  public static void printRow(int [] m)
  {
    
      // for this row we will print out the elements
      for(int c = 0; c < m.length; c++)
      {
        // prints the element at row r and column c
        // and keeps the cursor on the same line.
        System.out.print(m[c]+ " ");
      } // end of printing the row
  
  }

  
 
  // This method will receive the 2D array.
  // NOTE: Here we need to pass it a 2D array.
  //       And the row!
  //       Why?
  public static int getSumOfRow2D(int [][] m, int row)
  {
    
      // We will need a variable to hold the sum.
      int sum = 0;

      /*
      // for this row we will find the 
      // sum of the elements
      for(int c = 0; c < ???????; c++)
      {
         // update your sum variable
         sum += ????????;
      } // end of for loop
      */
      
      // now return it!
      return sum;
   }
  

  // This method will receive one row.
  // NOTE: It is really just a 1D array
  public static int getSumOfRow(int [] m, int row)
  {
      // We will need a variable to hold the sum.
      int sum = 0;
      
      
      // for this row we will find the 
      // sum of the elements
      for(int c = 0; c < m[row].length; c++)
      {
         // update your sum variable
         sum += m[row][c];
      } // end of for loop
      
      
      
      // now return it!
      // return sum;
   }
 
  
  // This method will receive the 2D array.
  // NOTE: Here we need to pass it a 2D array.
  //       And the column!
  //       Why?
  public static int getSumOfCol2D(int [][] m, int col)
  {
      // We will need a variable to hold the sum.
      int sum = 0;
      
      
      // for this col we will find the 
      // sum of the elements.
      // What will be changing as we move from
      // row to row?
      for(int r = 0; r < m.length; r++)
      {
         // update your sum variable
         sum += m[r][col];
      } // end of for loop
     
      
      // now return it!
      return sum;
   }
  

   public static void printSumOfColumns(int [][] m)
   {
      
      for(int c = 0; c < m[0].length; c++)
      {
         // print the sum of this column
         int sum = getSumOfCol2D(m,c);
         System.out.print(sum + " ");
      } // end of for loop
      System.out.println();
      
   }
   

} // class HelpArrays2D

