קוד:
/************************************************** *
* intro111/ipis111: Third assignment *
* *
* This class is for assignment #3 - Part 3 *
* *
* Author(s): ### YOUR NAME(S) AND EMAIL(S). ##### *
* Date: ##/11/2010 *
* *
************************************************** */
/*
* Important! Add comments to every method!
*
* The "return" statement at the end of each method and the initial value
* is just so this skeleton file compiles,
* change it according to your needs
*/
public class Ex3 {
public static int north(int[] tile){
return tile[0];
}
public static int east(int[] tile){
return tile[1];
}
public static int south(int[] tile){
return tile[2];
}
public static int west(int[] tile){
return tile[3];
}
/******************** Task 1 ********************/
public static boolean canPut(int[]tile, int x, int y, int[][][] board) {
boolean ans = true;
// לבדוק שאין אפור באמצע?
//המיקום אכן נמצא על הלוח והינו פנוי עוד לא מיקמנו שם אריח אחר
if (x<0 || x> board.length-1) ans=false; // check if the column x is on the board
if (y<0 || y> board.length-1) ans=false;// check if the row y is on the board
if (ans=true && ((board[x][y])!= null)) ans=false; // board[x][y] is empty
if(ans=true)
{
// אם המיקום נמצא על מסגרת הלוח, אזי צלעות הסף הינן בצבע אפור
if (x==0)
{
if (west(tile)!=0) ans=false;
}
if (x==board.length-1)
{
if (east(tile)!=0) ans=false;
}
if (y==0)
{
if (north(tile)!=0) ans=false;
}
if (y==board.length-1)
{
if (south(tile)!=0) ans=false;
}
}
if(ans==true)
{
// בדיקת התאמה לאיבר המזרחי בתנאי שהשכן מימין קיים
if((x<board.length-1) && ((board[x+1][y])!= null))
{
if(east(tile)!= board [x+1][y][3]) ans=false;
}
// בדיקת התאמה לאיבר הדרומי בתנאי שהשכן מלמטה קיים
if((y<board.length-1) && ((board[x][y+1])!= null))
{
if(south(tile)!= board [x][y+1][0]) ans=false;
}
///שיניתי [y-1] [x][y-1][2]
// בדיקת התאמה לאיבר המערבי בתנאי שהשכן משמאל קיים
if((x>0) && ((board[x-1][y])!= null))
{
if(west(tile)!= board [x-1][y][1]) ans=false;
}
// בדיקת התאמה לאיבר הצפוני בתנאי שהשכן מלמעלה קיים
if((y>0) && ((board[x][y-1])!= null))
{
if(north(tile)!= board [x][y-1][2]) ans=false;
}
}
return ans;
}
/******************** Task 2 ********************/
public static int[][][] put(int[] tile, int x, int y, int[][][] board) {
int n = board.length;
int[][][] newBoard= new int[n][n][];
for(int row=0; row<n; row++)
{
for(int col=0; col<n; col++)
{
if(board[row][col]!=null)
{
{
newBoard[row][col]=board[row][col];
}
}
else newBoard[row][col]=null;
}
}
newBoard[x][y]=tile;
return newBoard;
}
/******************** Task 3 ********************/
public static int[][] delete(int i, int[][] tiles) {
int[][] restTiles = new int[tiles.length-1][tiles[0].length];
int pointerRowTiles=0;
int pointerColTiles=0;
int pointerRowRestTiles=0;
int pointerColRestTiles=0;
///לתקן 2 3
while(pointerRowTiles<tiles.length && pointerRowRestTiles<restTiles.length )
{
if( pointerRowTiles==i)
{
pointerRowTiles++;
//Kבדוק יעילות א נמצאה השורה שצריך למחוק להפסיק?
}
/// Kבדוק במקרה שאי הוא השורה האחרונה שאותה צריך להוציא
pointerColTiles=0;
pointerColRestTiles=0;
while(pointerColTiles<4 && pointerColRestTiles<4 )
{
restTiles[pointerRowRestTiles][pointerColRestTiles]=tiles[pointerRowTiles][ pointerColTiles];
pointerColRestTiles++;
pointerColTiles++;
}
pointerRowTiles++;
pointerRowRestTiles++;
}
return restTiles;
}
public static int[] rotate(int j, int[] tile){
int[] nextTile = new int[tile.length];
int temp;
int counter=1;
for(int i=0; i<tile.length;i++)
nextTile[i]=tile[i];
while(counter<=j)
{
temp= nextTile[3];
for(int r=nextTile.length-1;r>= 1; r--)
nextTile[r]= nextTile[r-1];
nextTile[0]=temp;
counter++;
}
return nextTile;
}
/******************** Task 4 ********************/
public static int[][][] solve(int[][] tiles){
int size = (int) Math.sqrt(tiles.length);
int[][][] board = new int[size][size][];
return solve(board,tiles);
}
public static int[][][] solve(int[][][] board, int[][] tiles){
int[][][] solution = null;
int y;
int x;
if(tiles.length>0 )
{
for (int i=0; i<tiles.length; i++)
{
x=0;
while (x<board.length) // column
{
y=0;
while (y<board.length) // lines
{
if(canPut(tiles[i], x, y, board))
{
board= put(tiles[i], x, y, board);
solve(board, delete( i, tiles));
y=board.length; // ??
x=board.length; // ??
}
else y++;
}
x++;
}
}
}
return board;
}
public static void main(String[] args) {
Tests.testCanPut(2);
int[][] solver ={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0 },{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
int[][][] boardnu = solve(solver);
if(boardnu==null)
System.out.println("it is null");
else
{
for(int i=0; i< (int) Math.sqrt(solver.length);i++)
{
for(int j=0;j< (int) Math.sqrt(solver.length);j++)
{
if (boardnu[i][j]!=null)
{
for(int k=0;k< 4;k++)
System.out.print( boardnu[j][i][k]);
}
else System.out.print(boardnu[i][j]+" ");
System.out.println(" ");
}
}
}
}
}
/*
int x=8;
if(x!=8)
{
int [] tile={1,2,3,4};
int[]try2=new int[4];
try2= rotate(1,tile);
for(int j=0;j< 4;j++)
System.out.print( tile[j]);
System.out.println( " the new ");
for(int j=0;j< 4;j++)
System.out.print( try2[j]);
//int[][][] board = new int[5][5][];
//EternityPrint.showBoard(board);
int[][] tiles = {{1,2,3,4},{0,2,4,5},{5,2,5,1}};
int[][]ans=new int[tiles.length-1][tiles[0].length];
ans= delete(1,tiles);
for(int i=0; i< tiles.length;i++)
{
for(int j=0;j< tiles[i].length;j++)
System.out.print( tiles[i][j]);
System.out.println(" h ");
}
Tests.testPut(0);
int [] tilemm={1,2,3,4};
int[]tryk2=new int[4];
try2= rotate(1,tile);
for(int j=0;j< 4;j++)
System.out.print( tile[j]);
System.out.println( " the new ");
for(int j=0;j< 4;j++)
System.out.print( try2[j]);
}
*/