Lab 10  Solutions,  ESC101, 2004-2005 Semester-II


1.

class List {

  private double coef;
  private int exp;
  private List next;


  public List() {
    coef = 0;
    exp = 0;
    next = null;
  }


  public void setHead(double coef, int exp) {
    this.coef = coef;
    this.exp  = exp;
    next = null;
  }


  public List addToFront(double coef, int exp) {
    List l1 = new List();
    l1.coef = coef;
    l1.exp  = exp;
    l1.next = this;
    return l1;
  }

  public void print() {
    if(next == null) { System.out.println("("+coef+","+exp+")"); }
    else {
      System.out.print("("+coef+","+exp+"),  ");
      next.print();
    }
  }

  public static int degree(List l){
      int deg;
   
    deg = l.exp;   
   
      while(l.next != null){
        if(l.next.exp > deg)
            deg = l.next.exp;
        l = l.next;   
    }

    return deg;
  }   
       
       

  public static void main(String[] args) {
    List l1 = new List();
    l1.setHead(1.2, 3);
    l1 = l1.addToFront(2.5, 5);
    l1 = l1.addToFront(4, 2);

    System.out.println("The Polynomial is: ");   
    l1.print();
    System.out.println();

    System.out.println("Degree of Polynomial is: "+ degree(l1));   
   
  }

} // End class List



2.


import java.io.*;

public class Tic_Tac_Toe{

  public static boolean check(int[][] a, int reqzero, int reqone){
 
        int n = a.length; //n is 3 in our case, a is assumed to be a square array
        int zero, one;

           for(int i=0; i < n; i++){
             zero=one=0;
                for(int j=0; j < n; j++){
                   if(a[i][j] == 0)zero++;
                   if(a[i][j] == 1)one++;
                }

             if(zero==reqzero && one == reqone)
                 return true;
           }

        for(int i=0; i < n; i++){
             zero=one=0;
                for(int j=0; j<n; j++){
                   if(a[j][i] == 0)zero++;
                   if(a[j][i] == 1)one++;
                }
 
             if(zero==reqzero && one == reqone)
                 return true;
        }
                          
       zero=one=0;
       for(int i=0,j=0; i<n; i++,j++){
                   if(a[i][j] == 0)zero++;
                   if(a[i][j] == 1)one++;
       }
      
       if(zero==reqzero && one == reqone)
               return true;


       zero=one=0;
       for(int i=0,j=2; i<n; i++,j--){
                   if(a[i][j] == 0)zero++;
                   if(a[i][j] == 1)one++;
       }
 
       if(zero==reqzero && one == reqone)
           return true;


       return false;
   }


  
public static void print(int[][] a){
    int n=a.length; // n=3 in our case.
   
    for(int i=0; i < n-1; i++){
            for(int j=0; j < n-1; j++){
            if(a[i][j]<0)
                System.out.print(" ");
            else
                System.out.print(a[i][j]);
               
            System.out.print("|");
        }
       
        if(a[i][n-1]<0)
            System.out.println(" ");
        else
            System.out.println(a[i][n-1]);
    }

        for(int j=0; j < n-1; j++){
            if(a[n-1][j]<0)
            System.out.print(" ");
        else
            System.out.print(a[n-1][j]);
           
        System.out.print("|");
    }
 
        if(a[n-1][n-1]<0)
        System.out.println(" ");
        else
        System.out.println(a[n-1][n-1]);
}
   
   
public static void main(String[] args){
  
    int turn, num_fill;
       
    System.out.println("\n");
    System.out.println("___________________________________________");
        System.out.println("            Welcome to Tic_Tac_Toe         ");
        System.out.println();
        System.out.println("Squares on grid will be denoted by numbers as below");
   
        int[][] a = {{1,2,3},{4,5,6},{7,8,9}};
   
        print(a);
   
        System.out.println();
        System.out.println("First Player moves will be displayed as 0 and Second players'"+
                                          " as 1 on the grid");
                     
    for(int i=0; i<3; i++)
            for(int j=0; j<3; j++)
            a[i][j]=-1;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   
        int n,i,j;
     
    num_fill = 0;
    turn = 1;

  l1:     do{   
             System.out.println();
         
         if(turn == 1)   
             System.out.print("    First ");
         else
             System.out.print("    Second ");
       
             System.out.println("Player move Please!, choose an unfilled square"+
                     "\n       (Type the number corresponding to it followed by Enter)");

             do{
     
            try{  
                n=Integer.parseInt(br.readLine());
                    } catch(IOException e){
                System.out.println("Improper Input");
                        continue l1;
              }
            catch(NumberFormatException e){
                System.out.println("Improper Number");
                continue l1;
                       }
         
            i = (n-1)/3; j = (n-1)%3;
         
            if(a[i][j] >=0){
                System.out.println("This position is already filled");
                System.out.println("Choose an unfilled square"+
                                      " (Type the number corresponding to it)");
                continue;
            }
            else{
                if(turn == 1)
                          a[i][j] = 0;
                else
                    a[i][j] = 1;

                num_fill++;   
           
                break;
            }
         }while(true);
 
        System.out.println();

        if(turn == 1){
            if( check(a,3,0) ){
                print(a);
                System.out.println("First Player wins the game."+
                                                    " Game over."); return;
            }
            turn = 2;
        }
        else{   
                if( check(a,0,3) ){
                print(a);
                System.out.println("Second Player wins the game."+
                        " Game over."); return;
            }
            turn = 1;
        }   
       
            print(a);
   
        if(num_fill != 9)
            continue;
       
        System.out.println("The game is a draw."+" Game over.");
       
        return;
   
    }while(true);
}
}