Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program that will display an o on the board after randomly chosen
- here is the code:
- import java.util.*;
- public class compbrain{
- public static void main(String [] args)
- {
- char x = 'X';
- char [][] ar = new char [3][3];
- System.out.println("Player X: Enter the coordinate where you want an X placed. Example 1 0");
- int choice1 = input.nextInt();
- int choice2 = input.nextInt();
- char N = ar[choice1][choice2] = x;
- char N1 = ar[0][0];
- char N2 = ar[0][1];
- char N3 = ar[0][2];
- char N4 = ar[1][0];
- char N5 = ar[1][1];
- char N6 = ar[1][2];
- char N7 = ar[2][0];
- char N8 = ar[2][1];
- char N9 = ar[2][2];
- int r = (int)(Math.random() * 4);
- char [] arrayN1 = {N4,N7,N2,N3,N5,N9};
- if(N == N1){arrayN1[r] = x;}
- System.out.println(" 0 1 2 ");
- System.out.println(" --------------------");
- System.out.println("0 | "+N1+" | "+N4+" | "+N7+" |");
- System.out.println(" --------------------");
- System.out.println("1 | "+N2+" | "+N5+" | "+N8+" |");
- System.out.println(" --------------------");
- System.out.println("2 | "+N3+" | "+N6+" | "+N9+" |");
- System.out.println(" --------------------");
- }
- }
- // Setting a random field to 'o'
- Random r = new Random();
- int a = r.nextInt(3);
- int b = r.nextInt(3);
- ar[a][b] = o;
- // Printing the fields
- System.out.println(" 0 1 2 ");
- System.out.println(" --------------------");
- System.out.println("0 | " + ar[0][0] + " | " + ar[1][0] + " | " + ar[2][0] + " |");
- // and so on
Add Comment
Please, Sign In to add comment