Commit 58e04d78 authored by amir's avatar amir

New!!

parent 505f5e40
package com.company; package com.company;
import java.util.Scanner;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// write your code here // write your code here
Player1 player1 = new Player1(); Player1 player1 = new Player1();
Player2 player2 = new Player2(); Player2 player2 = new Player2();
smallBoards s = new smallBoards();
System.out.println(" 0 1 2 3 4 5"); board1 b1 = new board1();
for (int i = 0; i < 3; i++){ board2 b2 = new board2();
System.out.print(i + "|"); board3 b3 = new board3();
for (int j = 0; j < 6; j++){ board4 b4 = new board4();
System.out.print("\uD83D\uDD34"); Player p = new Player();
if ((j == 2) || (j == 5)) p.display(player1.getarr(), player2.getarr());
System.out.print("|"); int turns = 0;
while ((!player1.searchIfMatchEnds(player1.getarr()))
&& (!player2.searchIfMatchEnds(player2.getarr()))){
Scanner scanner = new Scanner(System.in);
turns++;
if (turns %2 == 1){
byte x = scanner.nextByte();
byte y = scanner.nextByte();
System.out.println("Player1, it's your turn.");
byte b = s.whichBoard(x, y);
player1.addDisc(player1.getarr(), player2.getarr(), x, y);
if (x > 2) x -= 3;
if (y > 2) y -= 3;
if (b == 1) b1.addDiscToTheBoard(x, y, (byte) 1);
if (b == 2) b2.addDiscToTheBoard(x, y, (byte) 1);
if (b == 3) b3.addDiscToTheBoard(x, y, (byte) 1);
if (b == 4) b4.addDiscToTheBoard(x, y, (byte) 1);
p.display(player1.getarr(), player2.getarr());
if ((player1.searchIfMatchEnds(player1.getarr())) || (player2.searchIfMatchEnds(player2.getarr())))
break;
else else
System.out.print(" "); System.out.println("Please select a board to rotate(0 for none of them)");
byte r = scanner.nextByte();
switch (r){
case 1:
b1.rotate();
break;
case 2:
b2.rotate();
break;
case 3:
b3.rotate();
break;
case 4:
b4.rotate();
break;
default:
break;
}
p.display(player1.getarr(), player2.getarr());
if ((player1.searchIfMatchEnds(player1.getarr())) || (player2.searchIfMatchEnds(player2.getarr())))
break;
} }
System.out.println(); else{
} byte x = scanner.nextByte();
System.out.print(" "); byte y = scanner.nextByte();
for (int j = 1; j < 15; j++) System.out.println("Player2, it's your turn.");
System.out.print("_"); byte b = s.whichBoard(x, y);
System.out.println(); player2.addDisc(player2.getarr(), player1.getarr(), x, y);
for (int i = 3; i < 6; i++) { if (x > 2) x -= 3;
System.out.print(i + "|"); if (y > 2) y -= 3;
for (int j1 = 0; j1 < 6; j1++) { if (b == 1) b1.addDiscToTheBoard(x, y, (byte) 2);
System.out.print("\uD83D\uDD34"); if (b == 2) b2.addDiscToTheBoard(x, y, (byte) 2);
if ((j1 == 2) || (j1 == 5)) if (b == 3) b3.addDiscToTheBoard(x, y, (byte) 2);
System.out.print("|"); if (b == 4) b4.addDiscToTheBoard(x, y, (byte) 2);
p.display(player1.getarr(), player2.getarr());
if ((player1.searchIfMatchEnds(player1.getarr())) || (player2.searchIfMatchEnds(player2.getarr())))
break;
else else
System.out.print(" "); System.out.println("Please select a board to rotate(0 for none of them)");
byte r = scanner.nextByte();
switch (r){
case 1:
b1.rotate();
break;
case 2:
b2.rotate();
break;
case 3:
b3.rotate();
break;
case 4:
b4.rotate();
break;
default:
break;
}
p.display(player1.getarr(), player2.getarr());
if ((player1.searchIfMatchEnds(player1.getarr())) || (player2.searchIfMatchEnds(player2.getarr())))
break;
} }
if (i < 5)
System.out.println();
} }
int turns = 1;
} }
} }
package com.company; package com.company;
public class Player { public class Player {
private int[][] playerCoordinate; private byte[][] playerCoordinate;
public Player(){ public Player(){
playerCoordinate = new int[6][6]; playerCoordinate = new byte[6][6];
} }
public int[][] getPlayerCoordinate() { public byte[][] getarr() {
return playerCoordinate; return playerCoordinate;
} }
public boolean containsInArray(int[][] arr, int x, int y){ public boolean containsInArray(byte[][] arr, int x, int y){
if (arr[x][y] != 0) if (arr[x][y] != 0)
return true; return true;
else else
return false; return false;
} }
public void addDisc(int[][] arr, int[][]arrOpponent, int x, int y){ public void addDisc(byte[][] arr, byte[][]arrOpponent, int x, int y){
if ((!containsInArray(arr, x, y)) && (!containsInArray(arrOpponent, x, y))) if ((!containsInArray(arr, x, y)) && (!containsInArray(arrOpponent, x, y)))
arr[x][y]++; arr[x][y]++;
} }
public boolean searchIfMatchEnds(int[][] arr, int[][]arrOpponent){ public boolean searchIfMatchEnds(byte[][] arr){
for (int i = 0; i < 6; i++){ for (int i = 0; i < 6; i++){
for (int j = 0; j < 6; j++){ for (int j = 0; j < 6; j++){
if (containsInArray(arr, j, i)){ if (containsInArray(arr, j, i)){
if (checkSides(arr, j, i)) if (checkSides(j, i))
return true; return true;
} }
} }
...@@ -35,66 +35,153 @@ public class Player { ...@@ -35,66 +35,153 @@ public class Player {
return false; return false;
} }
public boolean checkSides(int[][] arr,int x, int y){ public boolean checkSides(int x, int y){
if (x != 0){ if (x != 0){
if (containsInArray(arr, x - 1, y)){ if (containsInArray(playerCoordinate, x - 1, y)){
if (countTheMoves(arr, x, y, -1, 0) == 5) if (countTheMoves(x, y, -1, 0) == 5)
return true; return true;
} }
if (y != 0){ if (y != 0){
if (containsInArray(arr, x - 1, y - 1)) if (containsInArray(playerCoordinate, x - 1, y - 1))
if (countTheMoves(arr, x, y, -1, -1) == 5) if (countTheMoves(x, y, -1, -1) == 5)
return true; return true;
} }
if (y != 5){ if (y != 5){
if (containsInArray(arr, x - 1, y + 1)) if (containsInArray(playerCoordinate, x - 1, y + 1))
if (countTheMoves(arr, x, y, -1, 1) == 5) if (countTheMoves(x, y, -1, 1) == 5)
return true; return true;
} }
} }
if (x != 5){ if (x != 5){
if (containsInArray(arr, x + 1, y)){ if (containsInArray(playerCoordinate, x + 1, y)){
if (countTheMoves(arr, x, y, 1, 0) == 5) if (countTheMoves(x, y, 1, 0) == 5)
return true; return true;
} }
if (y != 0){ if (y != 0){
if (containsInArray(arr, x + 1, y - 1)) if (containsInArray(playerCoordinate, x + 1, y - 1))
if (countTheMoves(arr, x, y, 1, -1) == 5) if (countTheMoves(x, y, 1, -1) == 5)
return true; return true;
} }
if (y != 5){ if (y != 5){
if (containsInArray(arr, x + 1, y + 1)) { if (containsInArray(playerCoordinate, x + 1, y + 1)) {
if (countTheMoves(arr, x, y, 1, -1) == 5) if (countTheMoves(x, y, 1, -1) == 5)
return true; return true;
} }
} }
} }
if (y != 0){ if (y != 0){
if (containsInArray(arr, x, y - 1)) if (containsInArray(playerCoordinate, x, y - 1))
if (countTheMoves(arr, x, y, 0, -1) == 5) if (countTheMoves(x, y, 0, -1) == 5)
return true; return true;
} }
if (y != 5){ if (y != 5){
if (containsInArray(arr, x, y + 1)) { if (containsInArray(playerCoordinate, x, y + 1)) {
if (countTheMoves(x, y, 0, 1) == 5)
if (countTheMoves(arr, x, y, 0, 1) == 5)
return true; return true;
} }
} }
return false; return false;
} }
public int countTheMoves(int[][]arr, int x, int y, int moveX, int moveY){
public int countTheMoves(int x, int y, int moveX, int moveY){
int x1 = x + moveX; int x1 = x + moveX;
int y1 = y + moveY; int y1 = y + moveY;
int counter = 1; int counter = 1;
while ((counter < 5) && (x1 > -1) && (x1 < 6) while ((counter < 5) && (x1 > -1) && (x1 < 6)
&& (y1 > -1) && (y1 < 6) && (containsInArray(arr, x1, y1))){ && (y1 > -1) && (y1 < 6) && (containsInArray(playerCoordinate, x1, y1))){
counter++; counter++;
x1 += moveX; x1 += moveX;
y1 += moveY; y1 += moveY;
} }
return counter; return counter;
} }
public void display(byte[][] arr, byte[][]arr2){
System.out.println(" 0 1 2 3 4 5");
for (int i = 0; i < 3; i++){
System.out.print(i + "|");
for (int j = 0; j < 6; j++){
if (containsInArray(arr, j, i))
System.out.println("R");
else {
if (containsInArray(arr2, j, i))
System.out.println("B");
else
System.out.print("\uD83D\uDD34");
}
if ((j == 2) || (j == 5))
System.out.print("|");
else
System.out.print(" ");
}
System.out.println();
}
System.out.print(" ");
for (int j = 1; j < 15; j++)
System.out.print("_");
System.out.println();
for (int i = 3; i < 6; i++) {
System.out.print(i + "|");
for (int j1 = 0; j1 < 6; j1++) {
if (containsInArray(arr, j1, i))
System.out.println("R");
else {
if (containsInArray(arr2, j1, i))
System.out.println("B");
else
System.out.print("\uD83D\uDD34");
}
if ((j1 == 2) || (j1 == 5))
System.out.print("|");
else
System.out.print(" ");
}
System.out.println();
}
}
public void rotate(byte boardNumber){
byte[] bytes = new byte[2];
bytes = searchCoordinate(boardNumber);
byte tmp = playerCoordinate[bytes[0] + 2][bytes[1]];
playerCoordinate[bytes[0] + 2][bytes[1]] = playerCoordinate[bytes[0]][bytes[1]];
byte tmp1 = playerCoordinate[bytes[0] + 2][bytes[1] + 2];
playerCoordinate[bytes[0] + 2][bytes[1] + 2] = tmp;
byte tmp2 = playerCoordinate[bytes[0]][bytes[1] + 2];
playerCoordinate[bytes[0]][bytes[1] + 2] = tmp1;
playerCoordinate[bytes[0]][bytes[1]] = tmp2;
tmp = playerCoordinate[bytes[0] + 1][bytes[1]];
playerCoordinate[bytes[0] + 1][bytes [1]] = playerCoordinate[bytes[0]][bytes[1] + 1];
tmp1 = playerCoordinate[bytes[0] + 2][bytes[1] + 1];
playerCoordinate[bytes[0] + 2][bytes[1] + 1] = tmp;
tmp2 = playerCoordinate[bytes[0] + 1][bytes[1] + 2];
playerCoordinate[bytes[0] + 1][bytes[1] + 2] = tmp1;
playerCoordinate[bytes[0]][bytes[1] + 1] = tmp2;
}
public byte[] searchCoordinate(byte boardNumber){
byte[] c = new byte[2];
switch (boardNumber){
case 1:
c[0] = 0;
c[1] = 0;
break;
case 2:
c[0] = 3;
c[1] = 0;
break;
case 3:
c[0] = 0;
c[1] = 3;
break;
case 4:
c[0] = 3;
c[1] = 3;
break;
}
return c;
}
} }
package com.company; package com.company;
public class smallBoards { public class smallBoards {
private int[][] boardsCoordinate; private byte[][] boardsCoordinate;
public smallBoards(){ public smallBoards(){
boardsCoordinate = new int[3][3]; boardsCoordinate = new byte[3][3];
} }
public int whichBoard(int x, int y){ public byte whichBoard(byte x, byte y){
if ((x >= 0) && (x < 3) && (y >= 0) && (y < 3)) if ((x >= 0) && (x < 3) && (y >= 0) && (y < 3))
return 1; return 1;
if ((x >= 3) && (x < 6) && (y >= 0) && (y < 3)) if ((x >= 3) && (x < 6) && (y >= 0) && (y < 3))
...@@ -19,12 +19,12 @@ public class smallBoards { ...@@ -19,12 +19,12 @@ public class smallBoards {
return 0; return 0;
} }
public int rotate(int boardNumber){ public void rotate(){
int tmp = boardsCoordinate[2][0]; byte tmp = boardsCoordinate[2][0];
boardsCoordinate[2][0] = boardsCoordinate[0][0]; boardsCoordinate[2][0] = boardsCoordinate[0][0];
int tmp1 = boardsCoordinate[2][2]; byte tmp1 = boardsCoordinate[2][2];
boardsCoordinate[2][2] = tmp; boardsCoordinate[2][2] = tmp;
int tmp2 = boardsCoordinate[0][2]; byte tmp2 = boardsCoordinate[0][2];
boardsCoordinate[0][2] = tmp1; boardsCoordinate[0][2] = tmp1;
boardsCoordinate[0][0] = tmp2; boardsCoordinate[0][0] = tmp2;
...@@ -36,11 +36,10 @@ public class smallBoards { ...@@ -36,11 +36,10 @@ public class smallBoards {
boardsCoordinate[1][2] = tmp1; boardsCoordinate[1][2] = tmp1;
boardsCoordinate[0][1] = tmp2; boardsCoordinate[0][1] = tmp2;
return boardNumber;
} }
public void addDiscToTheBoard(int x, int y, int playerNumber){ public void addDiscToTheBoard(byte x, byte y, byte playerNumber){
if (boardsCoordinate[x][y] == 0) if (boardsCoordinate[x][y] == 0)
boardsCoordinate[x][y] = playerNumber; boardsCoordinate[x][y] = playerNumber;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment