Guest User

Untitled

a guest
Aug 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. program that will display an o on the board after randomly chosen
  2. here is the code:
  3.  
  4. import java.util.*;
  5. public class compbrain{
  6. public static void main(String [] args)
  7. {
  8. char x = 'X';
  9. char [][] ar = new char [3][3];
  10. System.out.println("Player X: Enter the coordinate where you want an X placed. Example 1 0");
  11. int choice1 = input.nextInt();
  12. int choice2 = input.nextInt();
  13. char N = ar[choice1][choice2] = x;
  14. char N1 = ar[0][0];
  15. char N2 = ar[0][1];
  16. char N3 = ar[0][2];
  17. char N4 = ar[1][0];
  18. char N5 = ar[1][1];
  19. char N6 = ar[1][2];
  20. char N7 = ar[2][0];
  21. char N8 = ar[2][1];
  22. char N9 = ar[2][2];
  23. int r = (int)(Math.random() * 4);
  24. char [] arrayN1 = {N4,N7,N2,N3,N5,N9};
  25.  
  26.  
  27. if(N == N1){arrayN1[r] = x;}
  28.  
  29. System.out.println(" 0 1 2 ");
  30. System.out.println(" --------------------");
  31. System.out.println("0 | "+N1+" | "+N4+" | "+N7+" |");
  32. System.out.println(" --------------------");
  33. System.out.println("1 | "+N2+" | "+N5+" | "+N8+" |");
  34. System.out.println(" --------------------");
  35. System.out.println("2 | "+N3+" | "+N6+" | "+N9+" |");
  36. System.out.println(" --------------------");
  37. }
  38. }
  39.  
  40. // Setting a random field to 'o'
  41. Random r = new Random();
  42. int a = r.nextInt(3);
  43. int b = r.nextInt(3);
  44.  
  45. ar[a][b] = o;
  46.  
  47. // Printing the fields
  48. System.out.println(" 0 1 2 ");
  49. System.out.println(" --------------------");
  50. System.out.println("0 | " + ar[0][0] + " | " + ar[1][0] + " | " + ar[2][0] + " |");
  51. // and so on
Add Comment
Please, Sign In to add comment