Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Reversi {
- public static final int GRID_SIZE = 6; // should be even, greater than 2
- public static final int X = 1;
- public static final int O = 2;
- public static void main(String[] args) {
- int[][] grid = new int[GRID_SIZE][GRID_SIZE];
- grid[GRID_SIZE / 2][GRID_SIZE / 2] = X;
- grid[GRID_SIZE / 2 - 1][GRID_SIZE / 2 - 1] = X;
- grid[GRID_SIZE / 2 - 1][GRID_SIZE / 2] = O;
- grid[GRID_SIZE / 2 ][GRID_SIZE / 2 - 1] = O;
- Scanner input = new Scanner(System.in);
- int currentPlayer = X;
- while (true) {
- drawGrid(grid);
- System.out.println(stringRep(currentPlayer) + "'s turn. Enter (row col): ");
- int row = input.nextInt();
- int col = input.nextInt();
- grid[row][col] = currentPlayer;
- currentPlayer = (X + O) - currentPlayer;
- }
- }
- public static void drawGrid(int[][] grid) {
- String line = "+";
- for (int i = 0; i < GRID_SIZE; i++) line += "---+";
- System.out.println(line);
- for (int i = 0; i < GRID_SIZE; i++) {
- System.out.print("| ");
- for (int j = 0; j < GRID_SIZE; j++) {
- System.out.print(stringRep(grid[i][j]) + " | ");
- }
- System.out.println();
- System.out.println(line);
- }
- }
- public static String stringRep(int piece) {
- if (piece == X) return "X";
- if (piece == O) return "O";
- return " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement