Advertisement
kuruku

Chess

May 19th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.16 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. import javax.lang.model.type.NullType;
  4.  
  5. import org.omg.CORBA.PUBLIC_MEMBER;
  6.  
  7. /**
  8.  *
  9.  * @author Gosho
  10.  *
  11.  */
  12. public class Chess {
  13.  
  14.     public static void main(String[] args) {
  15.         // TODO code application logic here
  16.         Scanner input = new Scanner(System.in);
  17.         char[][] arrChes = new char[7][7];
  18.         boolean found = false;
  19.         int player = 0;
  20.         char rowMatrix = ' ';
  21.         char colMatrix = ' ';
  22.  
  23.         // Read UserName
  24.         System.out.print("user1: ");
  25.         String firstPlayer = input.nextLine();
  26.         System.out.print("user2: ");
  27.         String secondPlayer = input.nextLine();
  28.         System.out.println();
  29.         boolean firstPlayerSign = false;
  30.         boolean secondPlayerSign = false;
  31.         boolean foundRow1Col1 = false;
  32.         boolean foundRow1Col2 = false;
  33.         boolean foundRow1Col3 = false;
  34.         boolean foundRow2Col1 = false;
  35.         boolean foundRow2Col2 = false;
  36.         boolean foundRow2Col3 = false;
  37.         boolean foundRow3Col1 = false;
  38.         boolean foundRow3Col2 = false;
  39.         boolean foundRow3Col3 = false;
  40.         while (!found) {
  41.  
  42.             if (player % 2 == 0) {
  43.                 System.out.println("User: " + firstPlayer);
  44.                 firstPlayerSign = true;
  45.                 secondPlayerSign = false;
  46.  
  47.             } else {
  48.                 System.out.println("User: " + secondPlayer);
  49.                 firstPlayerSign = false;
  50.                 secondPlayerSign = true;
  51.             }
  52.             System.out.print(" please choose row (1,2 or 3): ");
  53.             rowMatrix = input.next().charAt(0);
  54.             System.out.print(" please choose col (1,2 or 3): ");
  55.             colMatrix = input.next().charAt(0);
  56.             // Show when you try to use used box
  57.             while (true) {
  58.                 int rowMatrixNum = 0;
  59.                 int colMatrixNum = 0;
  60.                 if (rowMatrix == '1') {
  61.                     rowMatrixNum = 1;
  62.                 } else if (rowMatrix == '2') {
  63.                     rowMatrixNum = 3;
  64.                 } else if (rowMatrix == '3') {
  65.                     rowMatrixNum = 5;
  66.                 }
  67.                 if (colMatrix == '1') {
  68.                     colMatrixNum = 1;
  69.                 } else if (colMatrix == '2') {
  70.                     colMatrixNum = 3;
  71.                 } else if (colMatrix == '3') {
  72.                     colMatrixNum = 5;
  73.                 }
  74.  
  75.                 if (arrChes[rowMatrixNum][colMatrixNum] == 'O'
  76.                         || arrChes[rowMatrixNum][colMatrixNum] == 'X') {
  77.                     System.out
  78.                             .println("This field is used. Please choose another! ");
  79.                     System.out.print(" please choose row (1,2 or 3): ");
  80.                     rowMatrix = input.next().charAt(0);
  81.                     System.out.print(" please choose col (1,2 or 3): ");
  82.                     colMatrix = input.next().charAt(0);
  83.                 } else {
  84.                     break;
  85.                 }
  86.             }
  87.  
  88.             if (firstPlayerSign) {
  89.                 if (rowMatrix == '1') {
  90.                     if (colMatrix == '1' && foundRow1Col1 == false) {
  91.                         arrChes[1][1] = 'O';
  92.                         foundRow1Col1 = true;
  93.                     }
  94.                     if (colMatrix == '2' && foundRow1Col2 == false) {
  95.                         arrChes[1][3] = 'O';
  96.                         foundRow1Col2 = true;
  97.                     }
  98.                     if (colMatrix == '3' && foundRow1Col3 == false) {
  99.                         arrChes[1][5] = 'O';
  100.                         foundRow1Col3 = true;
  101.                     }
  102.                 }
  103.                 if (rowMatrix == '2') {
  104.                     if (colMatrix == '1' && foundRow2Col1 == false) {
  105.                         arrChes[3][1] = 'O';
  106.                         foundRow2Col1 = true;
  107.                     }
  108.                     if (colMatrix == '2' && foundRow2Col2 == false) {
  109.                         arrChes[3][3] = 'O';
  110.                         foundRow2Col2 = true;
  111.                     }
  112.                     if (colMatrix == '3' && foundRow2Col3 == false) {
  113.                         arrChes[3][5] = 'O';
  114.                         foundRow2Col3 = true;
  115.                     }
  116.                 }
  117.                 if (rowMatrix == '3' && foundRow3Col1 == false) {
  118.                     if (colMatrix == '1') {
  119.                         arrChes[5][1] = 'O';
  120.                         foundRow3Col1 = true;
  121.                     }
  122.                     if (colMatrix == '2' && foundRow3Col2 == false) {
  123.                         arrChes[5][3] = 'O';
  124.                         foundRow3Col2 = true;
  125.                     }
  126.                     if (colMatrix == '3' && foundRow3Col3 == false) {
  127.                         arrChes[5][5] = 'O';
  128.                         foundRow3Col3 = true;
  129.                     }
  130.                 }
  131.             }
  132.             if (secondPlayerSign) {
  133.                 if (rowMatrix == '1') {
  134.                     if (colMatrix == '1' && foundRow1Col1 == false) {
  135.                         arrChes[1][1] = 'X';
  136.                         foundRow1Col1 = true;
  137.                     }
  138.                     if (colMatrix == '2' && foundRow1Col2 == false) {
  139.                         arrChes[1][3] = 'X';
  140.                         foundRow1Col2 = true;
  141.                     }
  142.                     if (colMatrix == '3' && foundRow1Col3 == false) {
  143.                         arrChes[1][5] = 'X';
  144.                         foundRow1Col3 = true;
  145.                     }
  146.                 }
  147.                 if (rowMatrix == '2') {
  148.                     if (colMatrix == '1' && foundRow2Col1 == false) {
  149.                         arrChes[3][1] = 'X';
  150.                         foundRow2Col1 = true;
  151.                     }
  152.                     if (colMatrix == '2' && foundRow2Col2 == false) {
  153.                         arrChes[3][3] = 'X';
  154.                         foundRow2Col2 = true;
  155.                     }
  156.                     if (colMatrix == '3' && foundRow2Col3 == false) {
  157.                         arrChes[3][5] = 'X';
  158.                         foundRow2Col3 = true;
  159.                     }
  160.                 }
  161.                 if (rowMatrix == '3' && foundRow3Col1 == false) {
  162.                     if (colMatrix == '1') {
  163.                         arrChes[5][1] = 'X';
  164.                         foundRow3Col1 = true;
  165.                     }
  166.                     if (colMatrix == '2' && foundRow3Col2 == false) {
  167.                         arrChes[5][3] = 'X';
  168.                         foundRow3Col2 = true;
  169.                     }
  170.                     if (colMatrix == '3' && foundRow3Col3 == false) {
  171.                         arrChes[5][5] = 'X';
  172.                         foundRow3Col3 = true;
  173.                     }
  174.                 }
  175.             }
  176.             if ((arrChes[1][1] == arrChes[1][3]
  177.                     && arrChes[1][1] == arrChes[1][5] && (arrChes[1][1] == 'O' || arrChes[1][1] == 'X'))) {
  178.                 found = true;
  179.             }
  180.             if ((arrChes[3][1] == arrChes[3][3]
  181.                     && arrChes[3][1] == arrChes[3][5] && (arrChes[3][1] == 'O' || arrChes[3][1] == 'X'))) {
  182.                 found = true;
  183.             }
  184.             if ((arrChes[5][1] == arrChes[5][3]
  185.                     && arrChes[5][1] == arrChes[5][5] && (arrChes[5][1] == 'O' || arrChes[5][1] == 'X'))) {
  186.                 found = true;
  187.             }
  188.             if ((arrChes[1][1] == arrChes[3][1]
  189.                     && arrChes[1][1] == arrChes[5][1] && (arrChes[1][1] == 'O' || arrChes[1][1] == 'X'))) {
  190.                 found = true;
  191.             }
  192.             if ((arrChes[1][3] == arrChes[3][3]
  193.                     && arrChes[1][3] == arrChes[5][3] && (arrChes[1][3] == 'O' || arrChes[1][3] == 'X'))) {
  194.                 found = true;
  195.             }
  196.             if ((arrChes[1][5] == arrChes[3][5]
  197.                     && arrChes[1][5] == arrChes[5][5] && (arrChes[1][5] == 'O' || arrChes[1][5] == 'X'))) {
  198.                 found = true;
  199.             }
  200.             if ((arrChes[1][1] == arrChes[3][3]
  201.                     && arrChes[1][1] == arrChes[5][5] && (arrChes[1][1] == 'O' || arrChes[1][1] == 'X'))) {
  202.                 found = true;
  203.             }
  204.             if ((arrChes[1][5] == arrChes[3][3]
  205.                     && arrChes[1][5] == arrChes[5][1] && (arrChes[1][5] == 'O' || arrChes[1][5] == 'X'))) {
  206.                 found = true;
  207.             }  
  208.             if(arrChes[1][1] != '\u0000' && arrChes[1][3] != '\u0000' && arrChes[1][5] != '\u0000' && arrChes[3][1] != '\u0000'
  209.                     && arrChes[3][3] != '\u0000' && arrChes[3][5] != '\u0000' && arrChes[5][1] != '\u0000' && arrChes[5][3] != '\u0000' && arrChes[5][5] != '\u0000'){
  210.                 System.out.println("Game Over");
  211.                 break;
  212.             }
  213.  
  214.             // Initialize Array
  215.             for (int row = 0; row < 7; row++) {
  216.                 for (int col = 0; col < 7; col++) {
  217.                     if (row % 2 == 0) {
  218.                         if (col % 2 == 0) {
  219.                             arrChes[row][col] = '*';
  220.                         } else {
  221.                             arrChes[row][col] = '-';
  222.                         }
  223.                     } else {
  224.                         if (col % 2 == 0) {
  225.                             arrChes[row][col] = '|';
  226.                         } else {
  227.                             // arrChes[row][col] = ' ';
  228.                         }
  229.                     }
  230.                 }
  231.             }
  232.             for (int row = 0; row < 7; row++) {
  233.                 for (int col = 0; col < 7; col++) {
  234.                     System.out.print(arrChes[row][col]);
  235.                 }
  236.                 System.out.println();
  237.             }
  238.             if (found) {
  239.                 if (secondPlayerSign) {
  240.                     System.out.println("Congratulation " + secondPlayer
  241.                             + "! You are the winner!");
  242.                 } else {
  243.                     System.out.println("Congratulation " + firstPlayer
  244.                             + "! You are the winner!");
  245.                 }
  246.  
  247.                 break;
  248.             }
  249.             player++;
  250.         }
  251.  
  252.     }
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement