Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import javax.lang.model.type.NullType;
- import org.omg.CORBA.PUBLIC_MEMBER;
- /**
- *
- * @author Gosho
- *
- */
- public class Chess {
- public static void main(String[] args) {
- // TODO code application logic here
- Scanner input = new Scanner(System.in);
- char[][] arrChes = new char[7][7];
- boolean found = false;
- int player = 0;
- char rowMatrix = ' ';
- char colMatrix = ' ';
- // Read UserName
- System.out.print("user1: ");
- String firstPlayer = input.nextLine();
- System.out.print("user2: ");
- String secondPlayer = input.nextLine();
- System.out.println();
- boolean firstPlayerSign = false;
- boolean secondPlayerSign = false;
- boolean foundRow1Col1 = false;
- boolean foundRow1Col2 = false;
- boolean foundRow1Col3 = false;
- boolean foundRow2Col1 = false;
- boolean foundRow2Col2 = false;
- boolean foundRow2Col3 = false;
- boolean foundRow3Col1 = false;
- boolean foundRow3Col2 = false;
- boolean foundRow3Col3 = false;
- while (!found) {
- if (player % 2 == 0) {
- System.out.println("User: " + firstPlayer);
- firstPlayerSign = true;
- secondPlayerSign = false;
- } else {
- System.out.println("User: " + secondPlayer);
- firstPlayerSign = false;
- secondPlayerSign = true;
- }
- System.out.print(" please choose row (1,2 or 3): ");
- rowMatrix = input.next().charAt(0);
- System.out.print(" please choose col (1,2 or 3): ");
- colMatrix = input.next().charAt(0);
- // Show when you try to use used box
- while (true) {
- int rowMatrixNum = 0;
- int colMatrixNum = 0;
- if (rowMatrix == '1') {
- rowMatrixNum = 1;
- } else if (rowMatrix == '2') {
- rowMatrixNum = 3;
- } else if (rowMatrix == '3') {
- rowMatrixNum = 5;
- }
- if (colMatrix == '1') {
- colMatrixNum = 1;
- } else if (colMatrix == '2') {
- colMatrixNum = 3;
- } else if (colMatrix == '3') {
- colMatrixNum = 5;
- }
- if (arrChes[rowMatrixNum][colMatrixNum] == 'O'
- || arrChes[rowMatrixNum][colMatrixNum] == 'X') {
- System.out
- .println("This field is used. Please choose another! ");
- System.out.print(" please choose row (1,2 or 3): ");
- rowMatrix = input.next().charAt(0);
- System.out.print(" please choose col (1,2 or 3): ");
- colMatrix = input.next().charAt(0);
- } else {
- break;
- }
- }
- if (firstPlayerSign) {
- if (rowMatrix == '1') {
- if (colMatrix == '1' && foundRow1Col1 == false) {
- arrChes[1][1] = 'O';
- foundRow1Col1 = true;
- }
- if (colMatrix == '2' && foundRow1Col2 == false) {
- arrChes[1][3] = 'O';
- foundRow1Col2 = true;
- }
- if (colMatrix == '3' && foundRow1Col3 == false) {
- arrChes[1][5] = 'O';
- foundRow1Col3 = true;
- }
- }
- if (rowMatrix == '2') {
- if (colMatrix == '1' && foundRow2Col1 == false) {
- arrChes[3][1] = 'O';
- foundRow2Col1 = true;
- }
- if (colMatrix == '2' && foundRow2Col2 == false) {
- arrChes[3][3] = 'O';
- foundRow2Col2 = true;
- }
- if (colMatrix == '3' && foundRow2Col3 == false) {
- arrChes[3][5] = 'O';
- foundRow2Col3 = true;
- }
- }
- if (rowMatrix == '3' && foundRow3Col1 == false) {
- if (colMatrix == '1') {
- arrChes[5][1] = 'O';
- foundRow3Col1 = true;
- }
- if (colMatrix == '2' && foundRow3Col2 == false) {
- arrChes[5][3] = 'O';
- foundRow3Col2 = true;
- }
- if (colMatrix == '3' && foundRow3Col3 == false) {
- arrChes[5][5] = 'O';
- foundRow3Col3 = true;
- }
- }
- }
- if (secondPlayerSign) {
- if (rowMatrix == '1') {
- if (colMatrix == '1' && foundRow1Col1 == false) {
- arrChes[1][1] = 'X';
- foundRow1Col1 = true;
- }
- if (colMatrix == '2' && foundRow1Col2 == false) {
- arrChes[1][3] = 'X';
- foundRow1Col2 = true;
- }
- if (colMatrix == '3' && foundRow1Col3 == false) {
- arrChes[1][5] = 'X';
- foundRow1Col3 = true;
- }
- }
- if (rowMatrix == '2') {
- if (colMatrix == '1' && foundRow2Col1 == false) {
- arrChes[3][1] = 'X';
- foundRow2Col1 = true;
- }
- if (colMatrix == '2' && foundRow2Col2 == false) {
- arrChes[3][3] = 'X';
- foundRow2Col2 = true;
- }
- if (colMatrix == '3' && foundRow2Col3 == false) {
- arrChes[3][5] = 'X';
- foundRow2Col3 = true;
- }
- }
- if (rowMatrix == '3' && foundRow3Col1 == false) {
- if (colMatrix == '1') {
- arrChes[5][1] = 'X';
- foundRow3Col1 = true;
- }
- if (colMatrix == '2' && foundRow3Col2 == false) {
- arrChes[5][3] = 'X';
- foundRow3Col2 = true;
- }
- if (colMatrix == '3' && foundRow3Col3 == false) {
- arrChes[5][5] = 'X';
- foundRow3Col3 = true;
- }
- }
- }
- if ((arrChes[1][1] == arrChes[1][3]
- && arrChes[1][1] == arrChes[1][5] && (arrChes[1][1] == 'O' || arrChes[1][1] == 'X'))) {
- found = true;
- }
- if ((arrChes[3][1] == arrChes[3][3]
- && arrChes[3][1] == arrChes[3][5] && (arrChes[3][1] == 'O' || arrChes[3][1] == 'X'))) {
- found = true;
- }
- if ((arrChes[5][1] == arrChes[5][3]
- && arrChes[5][1] == arrChes[5][5] && (arrChes[5][1] == 'O' || arrChes[5][1] == 'X'))) {
- found = true;
- }
- if ((arrChes[1][1] == arrChes[3][1]
- && arrChes[1][1] == arrChes[5][1] && (arrChes[1][1] == 'O' || arrChes[1][1] == 'X'))) {
- found = true;
- }
- if ((arrChes[1][3] == arrChes[3][3]
- && arrChes[1][3] == arrChes[5][3] && (arrChes[1][3] == 'O' || arrChes[1][3] == 'X'))) {
- found = true;
- }
- if ((arrChes[1][5] == arrChes[3][5]
- && arrChes[1][5] == arrChes[5][5] && (arrChes[1][5] == 'O' || arrChes[1][5] == 'X'))) {
- found = true;
- }
- if ((arrChes[1][1] == arrChes[3][3]
- && arrChes[1][1] == arrChes[5][5] && (arrChes[1][1] == 'O' || arrChes[1][1] == 'X'))) {
- found = true;
- }
- if ((arrChes[1][5] == arrChes[3][3]
- && arrChes[1][5] == arrChes[5][1] && (arrChes[1][5] == 'O' || arrChes[1][5] == 'X'))) {
- found = true;
- }
- if(arrChes[1][1] != '\u0000' && arrChes[1][3] != '\u0000' && arrChes[1][5] != '\u0000' && arrChes[3][1] != '\u0000'
- && arrChes[3][3] != '\u0000' && arrChes[3][5] != '\u0000' && arrChes[5][1] != '\u0000' && arrChes[5][3] != '\u0000' && arrChes[5][5] != '\u0000'){
- System.out.println("Game Over");
- break;
- }
- // Initialize Array
- for (int row = 0; row < 7; row++) {
- for (int col = 0; col < 7; col++) {
- if (row % 2 == 0) {
- if (col % 2 == 0) {
- arrChes[row][col] = '*';
- } else {
- arrChes[row][col] = '-';
- }
- } else {
- if (col % 2 == 0) {
- arrChes[row][col] = '|';
- } else {
- // arrChes[row][col] = ' ';
- }
- }
- }
- }
- for (int row = 0; row < 7; row++) {
- for (int col = 0; col < 7; col++) {
- System.out.print(arrChes[row][col]);
- }
- System.out.println();
- }
- if (found) {
- if (secondPlayerSign) {
- System.out.println("Congratulation " + secondPlayer
- + "! You are the winner!");
- } else {
- System.out.println("Congratulation " + firstPlayer
- + "! You are the winner!");
- }
- break;
- }
- player++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement