Commit cc77ce89 authored by kiana's avatar kiana

pentago project

parents
Pipeline #4335 failed with stages
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
boolean isEnd = false;
Scanner scanner = new Scanner(System.in);
PentagoMap ground = new PentagoMap();
Move play = new Move();
ground.first();
ground.map();
int i = 1;
System.out.println("First write x then y of the place you wanna put your nut in " +
"then select a ground to rotate and then write if you want to rotate it to right or left by " +
"typing 'L' or 'R' ");
/**
* while no one hasnt won :
* gets x and y of the place
* then gets the play ground it wants to rotate and which direction to rotate if L or R
* checks if the inputs are valid
* put the nut and rotate the play ground
* same thing happens to the other player
* prints the map after each turn
*/
while(!(play.end(ground.getNuts(), 1) || play.end(ground.getNuts(), 2))) {
i--;
int y1 = scanner.nextInt();
int x1 = scanner.nextInt();
while (!play.check(y1,x1,ground.getNuts())){
System.out.println("invalid input");
y1 = scanner.nextInt();
x1 = scanner.nextInt();
}
y1--;
x1--;
play.move(y1, x1, ground.getNuts(), 1);
play.rotation(scanner.nextInt(), scanner.next().charAt(0), ground.getNuts());
ground.map();
if(play.end(ground.getNuts(),1))
{
System.out.println("winner is white");
return;
}
if(play.end(ground.getNuts(),2))
{
System.out.println("winner is black");
return;
}
int y2 = scanner.nextInt();
int x2 = scanner.nextInt();
while (!play.check(y2,x2,ground.getNuts())){
System.out.println("invalid input");
y2 = scanner.nextInt();
x2 = scanner.nextInt();
}
y2--;
x2--;
play.move(y2, x2, ground.getNuts(), 2);
play.rotation(scanner.nextInt(), scanner.next().charAt(0), ground.getNuts());
ground.map();
if(play.end(ground.getNuts(),1))
{
System.out.println("winner is white");
isEnd = true;
}
if(play.end(ground.getNuts(),2))
{
System.out.println("winner is black");
isEnd = true;
}
if(isEnd)
return;
}
}
}
public class Move {
private int[][] arr = new int[6][6];
/**
* sets the array
* @param arr
*/
public void setArr(int[][] arr) {
this.arr = arr;
}
/**
* checking the validity of inputs
* @param y
* @param x
* @param array
* @return true or false
*/
public boolean check(int y, int x, int[][] array){
int check=0;
if(x>0&&x<=6&&y>0&&y<=6){
check++;
}
if(array[x-1][y-1]==0)
check++;
if(check==2)
return true;
return false;
}
/**
* puts the nut in the given place
* @param y
* @param x
* @param array
* @param turn
*/
public void move(int y, int x, int[][] array, int turn) {
if (array[x][y] == 0)
array[x][y] = turn;
}
/**
* rotates the playground to the given direction
* @param playground
* @param direction
* @param array
*/
public void rotation(int playground, char direction, int[][] array) {
int[][] arr=new int[6][6];
for (int i = 0; i < 6; i++) System.arraycopy(array[i], 0, arr[i], 0, 6);
if (playground == 1) {
if (direction == 'L') {
array[0][0] = arr[0][2];
array[0][2] = arr[2][2];
array[2][2] = arr[2][0];
array[2][0] = arr[0][0];
array[0][1] = arr[1][2];
array[1][2] = arr[2][1];
array[2][1] = arr[1][0];
array[1][0] = arr[0][1];
} else if (direction == 'R') {
array[0][2] = arr[0][0];
array[2][2] = arr[0][2];
array[2][0] = arr[2][2];
array[0][0] = arr[2][0];
array[1][2] = arr[0][1];
array[2][1] = arr[1][2];
array[1][0] = arr[2][1];
array[0][1] = arr[1][0];
}
}
if (playground == 2) {
if (direction == 'L') {
array[0][3] = arr[0][5];
array[0][5] = arr[2][5];
array[2][5] = arr[2][3];
array[2][3] = arr[0][3];
array[0][4] = arr[1][5];
array[1][5] = arr[2][4];
array[2][4] = arr[1][3];
array[1][3] = arr[0][4];
} else if (direction == 'R') {
array[0][5] = arr[0][3];
array[2][5] = arr[0][5];
array[2][3] = arr[2][5];
array[0][3] = arr[2][3];
array[1][5] = arr[0][4];
array[2][4] = arr[1][5];
array[1][3] = arr[2][4];
array[0][4] = arr[1][3];
}
}
if (playground == 3) {
if (direction == 'L') {
setArr(array);
array[3][0] = arr[3][2];
array[3][2] = arr[5][5];
array[5][5] = arr[5][0];
array[5][0] = arr[3][0];
array[3][1] = arr[4][2];
array[4][2] = arr[5][1];
array[5][1] = arr[4][0];
array[4][0] = arr[3][1];
} else if (direction == 'R') {
array[3][2] = arr[3][0];
array[5][5] = arr[3][2];
array[5][0] = arr[5][5];
array[3][0] = arr[5][0];
array[4][2] = arr[3][1];
array[5][1] = arr[4][2];
array[4][0] = arr[5][1];
array[3][1] = arr[4][0];
}
}
if (playground == 4) {
if (direction == 'L') {
array[3][3] = arr[3][5];
array[3][5] = arr[5][5];
array[5][5] = arr[5][3];
array[5][3] = arr[3][3];
array[3][4] = arr[4][5];
array[4][5] = arr[5][4];
array[5][4] = arr[4][3];
array[4][3] = arr[3][4];
} else if (direction == 'R') {
array[3][5] = arr[3][3];
array[5][5] = arr[3][5];
array[5][3] = arr[5][5];
array[3][3] = arr[5][3];
array[4][5] = arr[3][4];
array[5][4] = arr[4][5];
array[4][3] = arr[5][4];
array[3][4] = arr[4][3];
}
}
setArr(array);
}
// public void rotation(int playground, char direction, int[][] array) {
//
// int[][] TopLeft = new int[array.length / 2][array.length / 2];
// int[][] TopRight = new int[array.length / 2][array.length / 2];
// int[][] BottomLeft = new int[array.length / 2][array.length / 2];
// int[][] BottomRight = new int[array.length / 2][array.length / 2];
// for (int i = 0; i < 3; i++)
// for (int j = 0; j < 3; j++)
// TopLeft[i][j] = array[i][j];
// for (int i = 0; i < 3; i++)
// for (int j = 0; j < 3; j++)
// TopRight[i][j] = array[i][j + 3];
// for (int i = 0; i < 3; i++)
// for (int j = 0; j < 3; j++)
// BottomLeft[i][j] = array[i + 3][j];
// for (int i = 0; i < 3; i++)
// for (int j = 0; j < 3; j++)
// BottomRight[i][j] = array[i +3][j + 3];
//
// switch (playground) {
// case 1:
// if (direction == 'L')
// TopLeft = transpose(false, TopLeft);
// else
// TopLeft = transpose(true, TopLeft);
// break;
// case 2:
// if (direction == 'L')
// TopRight = transpose(false, TopRight);
// else
// TopRight = transpose(true, TopRight);
// break;
// case 3:
// if (direction == 'L')
// BottomLeft = transpose(false, BottomLeft);
// else
// BottomLeft = transpose(true, BottomLeft);
// break;
// case 4:
// if (direction == 'L')
// BottomRight = transpose(false, BottomRight);
// else
// BottomRight = transpose(true, BottomRight);
// break;
// }
//
// // Top-Left
// for (int i = 0; i < 3; i++)
// for (int j = 0; j < 3; j++)
// array[i][j] = TopLeft[i][j];
//
// // Top-Right
// for (int i = 0; i < 3; i++)
// for (int j = 0; j < 3; j++)
// array[i][j + 3] = TopRight[i][j];
//
// // Bottom-Left
// for (int i = 0; i < 3; i++)
// for (int j = 0; j < 3; j++)
// array[i + 3][j] = BottomLeft[i][j];
//
// // Bottom-Right
// for (int i = 0; i < 3; i++)
// for (int j = 0; j < 3; j++)
// array[i + 3][j + 3] = BottomRight[i][j];
// }
//
//
// public int[][] transpose(boolean clockWise, int[][] array) {
//
// int[][] tempMap = new int[array.length][array.length];
// if (clockWise) { // 90 Deg
// int k = 2;
// for (int i = 0; i < array.length; i++) {
// for (int j = 0; j < array.length; j++)
// tempMap[j][k] = array[i][j];
// k--;
// }
// } else // -90 Deg
// for (int i = 0; i < array.length; i++) {
// int k = 2;
// for (int j = 0; j < array.length; j++) {
// tempMap[k][i] = array[i][j];
// k--;
// }
// }
//
// // Replace maps
// return tempMap;
// }
/**
* checks if the given player has won or not
* for each nut checks the 8 directions to see if in any of these directions there are at least 5 same color nuts or not
* @param arr
* @param turn
* @return true or false whether the player has won or not
*/
public boolean end(int[][] arr, int turn) {
int sum = 0, max = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
for (int k = j; k < arr.length; k++) {
if (arr[i][k] == turn)
sum++;
else
break;
if (sum > max)
max = sum;
}
sum = 0;
for (int l = j; l >= 0; l--) {
if (arr[i][l] == turn)
sum++;
else
break;
if (sum > max)
max = sum;
}
sum = 0;
for (int m = i; m < arr.length; m++) {
if (arr[m][j] == turn)
sum++;
else
break;
if (sum > max)
max = sum;
}
sum = 0;
for (int n = i; n >= 0; n--) {
if (arr[n][j] == turn)
sum++;
else
break;
if (sum > max)
max = sum;
}
sum = 0;
for (int o = i, p = j; o < arr.length && p < arr.length; o++, p++) {
if (arr[o][p] == turn)
sum++;
else
break;
if (sum > max)
max = sum;
}
sum = 0;
for (int q = i, r = j; q >= 0 && r < arr.length; q--, r++) {
if (arr[q][r] == turn)
sum++;
else
break;
if (sum > max)
max = sum;
}
sum = 0;
for (int s = i, t = j; s < arr.length && t >= 0; s++, t--) {
if (arr[s][t] == turn)
sum++;
else
break;
if (sum > max)
max = sum;
}
sum = 0;
for (int y = i, z = j; y >= 0 && z >= 0; y--, z--) {
if (arr[y][z] == turn)
sum++;
else
break;
if (sum > max)
max = sum;
}
sum = 0;
}
}
return max >= 5;
}
}
import java.util.ArrayList;
public class PentagoMap {
private int[][] nuts = new int[6][6];
/**
* make the array 0 at first
*/
public void first(){
for (int i=0; i<6; i++){
for (int j=0; j<6; j++){
nuts[i][j] = 0;
}
}
}
/**
* sets the nuts
* @param nuts
*/
public void setNuts(int[][] nuts) {
this.nuts = nuts;
}
/**
* gets the nuts
* @return
*/
public int[][] getNuts() {
return nuts;
}
/**
* prints the map
*/
public void map() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (nuts[i][j] == 0)
System.out.print("\u2B55 ");
else if(nuts[i][j] == 1)
System.out.print("\u26AA ");
else if(nuts[i][j] == 2)
System.out.print("\u26AB ");
}
System.out.print("| ");
for (int k = 3; k < 6; k++) {
if (nuts[i][k] == 0)
System.out.print("\u2B55 ");
else if(nuts[i][k] == 1)
System.out.print("\u26AA ");
else if(nuts[i][k] == 2)
System.out.print("\u26AB ");
}
System.out.println();
}
//System.out.print("-");
for (int z=0; z<6; z++)
System.out.print("--- ");
System.out.println();
for (int i = 3; i < 6; i++) {
for (int j = 0; j < 3; j++) {
if (nuts[i][j] == 0)
System.out.print("\u2B55 ");
else if(nuts[i][j] == 1)
System.out.print("\u26AA ");
else if(nuts[i][j] == 2)
System.out.print("\u26AB ");
}
System.out.print("| ");
for (int k = 3; k < 6; k++) {
if (nuts[i][k] == 0)
System.out.print("\u2B55 ");
else if(nuts[i][k] == 1)
System.out.print("\u26AA ");
else if(nuts[i][k] == 2)
System.out.print("\u26AB ");
}
System.out.println();
}
System.out.println(" ");
System.out.println(" ");
}
}
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