plain this java code

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

Explain this java codes

 

private void put(int cell,int player)
 {
  int i=-1,j=-1;;
  switch(cell)
  {
  case 1:i=2;j=0;break;
  case 2:i=2;j=1;break;
  case 3:i=2;j=2;break;
  case 4:i=1;j=0;break;
  case 5:i=1;j=1;break;
  case 6:i=1;j=2;break;
  case 7:i=0;j=0;break;
  case 8:i=0;j=1;break;
  case 9:i=0;j=2;break;
  default:display(overridegrid);return;
  }
  char mark='x';
  if(player==0)
   mark='o';
  grid[i][j]=mark;
  display(grid);
 }
 private int startGame()
 {
  init();
  display(grid);
  int status=playingid;
  while(status==playingid)
  {
   put(playerMove(),0);
   if(override==1)
   {
    System.out.println("O wins.");
    return playerid;
   }
   status=checkForWin();
   if(status!=playingid)
    break;
   try{Thread.sleep(1000);}catch(Exception e){System.out.print(e.getMessage());}
   put(compMove(),1);
   status=checkForWin();
  }
  return status;
 }
 private void init()
 {
  movesPlayer="";
  override=0;
  marks=new int[8][6];
  wins=new int[][] //new int[8][3];
  
  weights=new int[]{3,2,3,2,4,2,3,2,3};
  grid=new char[][]{{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
  crossbank=new Hashtable<Integer,Integer>();
  knotbank=new Hashtable<Integer,Integer>();
 }
 private void mark(int m,int player)
 {
  for(int i=0;i<wins.length;i++)
   for(int j=0;j<wins[i].length;j++)
    if(wins[i][j]==m)
    {
     marks[i][j]=1;
     if(player==playerid)
      marks[i][knotcount]++;
     else
      marks[i][crosscount]++;
     marks[i][totalcount]++;
    }
 }
 private void fixWeights()
 {
  for(int i=0;i<3;i++)
   for(int j=0;j<3;j++)
    if(marks[i][j]==1)
     if(weights[wins[i][j]-1]!=Integer.MIN_VALUE)
      weights[wins[i][j]-1]=Integer.MIN_VALUE;
 
  for(int i=0;i<8;i++)
  {
   if(marks[i][totalcount]!=2)
    continue;
   if(marks[i][crosscount]==2)
   {
    int p=i,q=-1;
    if(marks[i][0]==0)
     q=0;
    else if(marks[i][1]==0)
     q=1;
    else if(marks[i][2]==0)
     q=2;
 
    if(weights[wins[p][q]-1]!=Integer.MIN_VALUE)
    {
     weights[wins[p][q]-1]=6;
    }
   }
   if(marks[i][knotcount]==2)
   {
    int p=i,q=-1;
    if(marks[i][0]==0)
     q=0;
    else if(marks[i][1]==0)
     q=1;
    else if(marks[i][2]==0)
     q=2;
 
    if(weights[wins[p][q]-1]!=Integer.MIN_VALUE)
    {
     weights[wins[p][q]-1]=5;
    }
   }
  }
 }

 private int compMove()
 {
  int cell=move();
  System.out.println("Computer plays: "+cell);
  //weights[cell-1]=Integer.MIN_VALUE;
  return cell;
 }
 private int move()
 {
  int max=Integer.MIN_VALUE;
  int cell=0;
  for(int i=0;i<weights.length;i++)
   if(weights[i]>max)
   {
    max=weights[i];
    cell=i+1;
   }

 private int playerMove()
 {
  System.out.print("What's your move?: ");
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  int cell=0;
  int okay=0;
  while(okay==0)
  {
   try
   {
    cell=Integer.parseInt(br.readLine());
   }
   catch(Exception e)
   {
    System.out.println(e.getMessage());
   }
   if(cell==7494)
   {
    override=1;
    return -1;
   }
   if((cell<1||cell>9)||weights[cell-1]==Integer.MIN_VALUE)
    System.out.print("Invalid move. Try again:");
   else
    okay=1;
  }
  playerMoved(cell);
  System.out.println();
  return cell;
 }
 private void playerMoved(int cell)
 {
  movesPlayer+=cell;
  mark(cell,0);
  fixWeights();
  knotbank.put(cell, 0);
 }
 private int checkForWin()
 {
  int crossflag=0,knotflag=0;
  for(int i=0;i<wins.length;i++)
  {
   if(crossbank.containsKey(wins[i][0]))
    if(crossbank.containsKey(wins[i][1]))
     if(crossbank.containsKey(wins[i][2]))
     {
      crossflag=1;
      break;
     }
   if(knotbank.containsKey(wins[i][0]))
    if(knotbank.containsKey(wins[i][1]))
     if(knotbank.containsKey(wins[i][2]))
     {
      knotflag=1;
      break;
     }
  }
  if(knotflag==1)
  {
   display(grid);
   System.out.println("O wins.");
   return playerid;
  }
  else if(crossflag==1)
  {
   display(grid);
   System.out.println("X wins.");
   return compid;
  }
 
  for(int i=0;i<weights.length;i++)
   if(weights[i]!=Integer.MIN_VALUE)
    return playingid;
  System.out.println("Truce");
 
  return truceid;
 }
 private void display(char[][] grid)
 {
  for(int i=0;i<3;i++)
  {
   System.out.println("\n-------");
   System.out.print("|");
   for(int j=0;j<3;j++)
    System.out.print(grid[i][j]+"|");
  }
  System.out.println("\n-------");
 }
}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Concept of pointer parameter
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY