Java Programming Practice: Rek Game Code

Let's see how to write Rek Game in Java.

import java.util.Scanner;
public class RekAlgorithm {

 private int[][] table = new int[8][8];
 int player;
 int bossPlayerOne = 5, bossPlayerTwo = 6;
 int row, col, rowNew, colNew;
 int rUp, rDown, rLeft, rRight;

 public RekAlgorithm() {
  // TODO Auto-generated constructor stub
  player = 1;
  initialGame(player);
  printTable();
  startGame(player);
 }

 private void startGame(int player) {
  // TODO Auto-generated method stub
  rUp = 0;
  rDown = 0;
  rLeft = 0;
  rRight = 0;
  System.out.println("Choose your kid" + player + ": (||, ==) ");
  Scanner mysc = new Scanner(System.in);
  row = mysc.nextInt();
  col = mysc.nextInt();
  System.out.println("Player: " + player);
  while (true) {
   if (table[row][col] == player || table[row][col] == bossPlayerOne) {
    System.out.println("Ok You can go.");
    break;
   } else {
    System.out.println("Invalid. Put Again.");
    row = mysc.nextInt();
    col = mysc.nextInt();
   }
  }

  // Put where?
  // =============================================
  // Check range where u can go
  int row1 = row;
  int row2 = row;
  int col1 = col;
  int col2 = col;
  while (row1 > 0) {
   row1--;
   if (table[row1][col] != 0)
    break;
   rUp += 1;
  }
  while (row2 < 7) {
   row2++;
   if (table[row2][col] != 0)
    break;
   rDown += 1;
  }
  while (col1 > 0) {
   col1--;
   if (table[row][col1] != 0)
    break;
   rLeft += 1;
  }
  while (col2 < 7) {
   col2++;
   if (table[row][col2] != 0)
    break;
   rRight += 1;
  }
  // =============================================

  // Check if new place is valid
  System.out.print("Where you want to put: ");
  int a, b;
  boolean xbool, ybool, xybool;
  do {
   System.out.print("New Place: ");
   mysc = new Scanner(System.in);
   a = mysc.nextInt();
   b = mysc.nextInt();
   xbool = a >= (row - rUp) && a <= (row + rDown);
   ybool = b >= (col - rLeft) && b <= (col + rRight);
   xybool = a == row && b == col;
  } while ((xbool && ybool) == false || xybool);
  System.out.println("Ok Valid Place");
  rowNew = a;
  colNew = b;
  // it's valid now
  checkIfEat(player);
 }

 private void checkIfEat(int player) {
  // TODO Auto-generated method stub
  int oppoNen = 1;
  int oppoBoss = bossPlayerOne;
  if (player == 1) {
   oppoNen = 2;
   oppoBoss = bossPlayerTwo;
  } else if (player == 2) {
   oppoNen = 1;
   oppoBoss = bossPlayerOne;
  }
  if (colNew == 0 || colNew == 7) {
   // If it is not at the edge of table
  } else {
   if (table[rowNew][colNew - 1] == oppoNen
     && table[rowNew][colNew + 1] == oppoNen) {
    System.out.println("Congratulatio. You beat them.");
    table[rowNew][colNew - 1] = 0;
    table[rowNew][colNew + 1] = 0;
    continueGame(player);
    // Eat boss will win
   } else if ((table[rowNew][colNew - 1] == oppoBoss && table[rowNew][colNew + 1] == oppoNen)
     || (table[rowNew][colNew - 1] == oppoNen && table[rowNew][colNew + 1] == oppoBoss)) {
    table[rowNew][colNew - 1] = 0;
    table[rowNew][colNew + 1] = 0;
    System.out.println("Congratulatio.................You Win!");
   }
  }
  if (rowNew == 0 || rowNew == 7) {
   // If it is not at the edge of table
  } else {
   if (table[rowNew - 1][colNew] == oppoNen
     && table[rowNew + 1][colNew] == oppoNen) {
    System.out.println("Congratulatio. You beat them.");
    table[rowNew - 1][colNew] = 0;
    table[rowNew + 1][colNew] = 0;
    continueGame(player);
    // Eat boss will win
   } else if ((table[rowNew - 1][colNew] == oppoBoss && table[rowNew + 1][colNew] == oppoNen)
     || (table[rowNew - 1][colNew] == oppoNen && table[rowNew + 1][colNew] == oppoBoss)) {
    table[rowNew][colNew - 1] = 0;
    table[rowNew][colNew + 1] = 0;
    System.out.println("Congratulatio.................You Win!");
   }
  }
  continueGame(player);
 }

 private void continueGame(int player2) {
  // TODO Auto-generated method stub
  table[rowNew][colNew] = table[row][col];
  table[row][col] = 0;
  printTable();

  // switch player
  if (player == 1)
   player = 2;
  else if (player == 2)
   player = 1;
  startGame(player);
 }

 private void printTable() {
  // TODO Auto-generated method stub
  for (int i = 0; i < 8; i++) {
   for (int j = 0; j < 8; j++) {
    System.out.print(table[i][j] + " ");
   }
   System.out.println();
  }
 }

 private void initialGame(int player2) {
  // TODO Auto-generated method stub
  // Set 0 to all Cell
  for (int i = 0; i < 8; i++) {
   for (int j = 0; j < 8; j++) {
    table[i][j] = 0;
   }
  }
  // Player 1
  for (int i = 0; i < 8; i++) {
   table[0][i] = 1;
   table[2][i] = 1;
  }
  table[0][0] = 0;
  table[1][0] = 5;
  table[3][5] = 5;
  // Player 2
  for (int j = 0; j < 8; j++) {
   table[5][j] = 2;
   table[7][j] = 2;
  }
  table[7][7] = 0;
  table[6][7] = 6;
  table[5][1] = 0;
  table[5][0] = 2;
  table[5][2] = 6;
 }

}