Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Reversi {
  4. public static final int GRID_SIZE = 6; // should be even, greater than 2
  5. public static final int X = 1;
  6. public static final int O = 2;
  7.  
  8. public static void main(String[] args) {
  9.  
  10. int[][] grid = new int[GRID_SIZE][GRID_SIZE];
  11.  
  12. grid[GRID_SIZE / 2][GRID_SIZE / 2] = X;
  13. grid[GRID_SIZE / 2 - 1][GRID_SIZE / 2 - 1] = X;
  14. grid[GRID_SIZE / 2 - 1][GRID_SIZE / 2] = O;
  15. grid[GRID_SIZE / 2 ][GRID_SIZE / 2 - 1] = O;
  16.  
  17. Scanner input = new Scanner(System.in);
  18.  
  19. int currentPlayer = X;
  20.  
  21.  
  22. while (true) {
  23. drawGrid(grid);
  24. System.out.println(stringRep(currentPlayer) + "'s turn. Enter (row col): ");
  25. int row = input.nextInt();
  26. int col = input.nextInt();
  27. grid[row][col] = currentPlayer;
  28.  
  29. currentPlayer = (X + O) - currentPlayer;
  30. }
  31. }
  32.  
  33.  
  34.  
  35.  
  36. public static void drawGrid(int[][] grid) {
  37. String line = "+";
  38. for (int i = 0; i < GRID_SIZE; i++) line += "---+";
  39.  
  40. System.out.println(line);
  41.  
  42. for (int i = 0; i < GRID_SIZE; i++) {
  43.  
  44. System.out.print("| ");
  45. for (int j = 0; j < GRID_SIZE; j++) {
  46. System.out.print(stringRep(grid[i][j]) + " | ");
  47. }
  48. System.out.println();
  49.  
  50. System.out.println(line);
  51. }
  52. }
  53.  
  54. public static String stringRep(int piece) {
  55. if (piece == X) return "X";
  56. if (piece == O) return "O";
  57. return " ";
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement