-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5c02c9
commit baca55c
Showing
8 changed files
with
184 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 136 additions & 8 deletions
144
app/src/main/java/com/example/jagadeesh545/piczzle/Game.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,153 @@ | ||
package com.example.jagadeesh545.piczzle; | ||
|
||
import android.content.Intent; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.Button; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
public class Game extends AppCompatActivity { | ||
Integer[] numbers = new Integer[15]; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_game); | ||
ViewGroup game_view = (ViewGroup) findViewById(R.id.game_view); | ||
int block_height = game_view.getHeight()/4; | ||
int block_width = game_view.getWidth()/4; | ||
for (int i = 0; i < game_view.getChildCount(); i++) { | ||
Button current_button = (Button) game_view.getChildAt(i); | ||
current_button.setHeight(block_height); | ||
current_button.setWidth(block_width); | ||
current_button.setText(String.valueOf(i)); | ||
|
||
for (int i = 0; i < numbers.length; i++) { | ||
numbers[i] = i+1; | ||
} | ||
Collections.shuffle(Arrays.asList(numbers)); | ||
if(!checkSolvable(numbers)){ | ||
int temp = numbers[0]; | ||
numbers[0] = numbers[1]; | ||
numbers[1] = temp; | ||
} | ||
|
||
final View game_view = findViewById(R.id.game_view); | ||
final ViewGroup game_viewGroup = (ViewGroup) findViewById(R.id.game_view); | ||
game_view.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
int block_width = game_view.getWidth() / 4; | ||
int block_height = game_view.getHeight() / 4; | ||
if (block_height > 0 && block_width > 0) { | ||
for (int i = 0; i < game_viewGroup.getChildCount(); i++) { | ||
Button current_button = (Button) game_viewGroup.getChildAt(i); | ||
ViewGroup.LayoutParams params = current_button.getLayoutParams(); | ||
params.height = block_height; | ||
params.width = block_width; | ||
current_button.setLayoutParams(params); | ||
current_button.setTextSize((float) (block_height * 0.10)); | ||
current_button.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
swap(v); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
int last = game_viewGroup.getChildCount() - 1; | ||
Button current_button = (Button) game_viewGroup.getChildAt(last); | ||
current_button.setText("*"); | ||
for (int i = 0; i < last; i++) { | ||
Button temp_button = (Button) game_viewGroup.getChildAt(i); | ||
temp_button.setText(String.valueOf(numbers[i])); | ||
} | ||
} | ||
|
||
// Check if the puzzle is solvable | ||
public boolean checkSolvable(Integer[] numbers) { | ||
int inversions = 0; | ||
|
||
for(int i = 0; i < numbers.length; i++) { | ||
for(int j = i+1; j < numbers.length; j++) { | ||
if(numbers[j] > numbers[i]){ | ||
inversions++; | ||
} | ||
} | ||
} | ||
|
||
if(inversions%2 == 1){ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// Swap the blocks if they are adjacent to blank block | ||
public void swap(View view) { | ||
if(view.getId() == R.id.blank) { | ||
return; | ||
} | ||
View blank = findViewById(R.id.blank); | ||
int block_height = blank.getHeight(); | ||
int block_width = blank.getWidth(); | ||
if((blank.getX()-view.getX()==block_width && blank.getY()-view.getY()==0)|| | ||
(blank.getX()-view.getX()==-block_width && blank.getY()-view.getY()==0)|| | ||
(blank.getX()-view.getX()==0 && blank.getY()-view.getY()==block_height)|| | ||
(blank.getX()-view.getX()==0 && blank.getY()-view.getY()==-block_height)) | ||
{ | ||
swapBlocks(view); | ||
checkBoard(); | ||
} | ||
} | ||
|
||
// Perform actual swap operation | ||
public void swapBlocks(View view) { | ||
|
||
float viewX = view.getX(); | ||
float viewY = view.getY(); | ||
|
||
View blank = findViewById(R.id.blank); | ||
float blankX = blank.getX(); | ||
float blankY = blank.getY(); | ||
|
||
blank.setX(viewX); | ||
blank.setY(viewY); | ||
|
||
view.setX(blankX); | ||
view.setY(blankY); | ||
} | ||
|
||
// Check board to see if the puzzle is solved. | ||
public void checkBoard() { | ||
View blank = findViewById(R.id.blank); | ||
int block_height = blank.getHeight(); | ||
int block_width = blank.getWidth(); | ||
ViewGroup game_viewGroup = (ViewGroup) findViewById(R.id.game_view); | ||
for (int i = 0; i < game_viewGroup.getChildCount() - 1; i++) { | ||
Button current_button = (Button) game_viewGroup.getChildAt(i); | ||
int current_value = Integer.valueOf(String.valueOf(current_button.getText())) - 1; | ||
int a = (int) current_button.getX() / block_width; | ||
int b = current_value % 4; | ||
int c = (int) current_button.getY() / block_height; | ||
int d = current_value / 4; | ||
if (a != b || c != d) { | ||
return; | ||
} | ||
} | ||
int last = game_viewGroup.getChildCount() - 1; | ||
Button current_button = (Button) game_viewGroup.getChildAt(last); | ||
if(String.valueOf(current_button.getText()).compareTo("*")!=0) { | ||
return; | ||
} | ||
else { | ||
current_button.setText(String.valueOf(last+1)); | ||
} | ||
|
||
for (int i = 0; i < game_viewGroup.getChildCount(); i++) { | ||
Button temp_button = (Button) game_viewGroup.getChildAt(i); | ||
temp_button.setClickable(false); | ||
} | ||
Intent intent = new Intent(this, Victory.class); | ||
startActivity(intent); | ||
} | ||
} |
31 changes: 7 additions & 24 deletions
31
app/src/main/java/com/example/jagadeesh545/piczzle/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,20 @@ | ||
package com.example.jagadeesh545.piczzle; | ||
|
||
import android.content.Intent; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.Button; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_game); | ||
setContentView(R.layout.activity_main); | ||
} | ||
|
||
final View game_view = findViewById(R.id.game_view); | ||
game_view.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
int block_width = game_view.getWidth()/4; | ||
int block_height = game_view.getHeight()/4; | ||
if (block_height > 0 && block_width > 0) { | ||
ViewGroup game_viewGroup = (ViewGroup) findViewById(R.id.game_view); | ||
for (int i = 0; i < game_viewGroup.getChildCount(); i++) { | ||
Button current_button = (Button) game_viewGroup.getChildAt(i); | ||
ViewGroup.LayoutParams params = current_button.getLayoutParams(); | ||
params.height = block_height; | ||
params.width = block_width; | ||
current_button.setLayoutParams(params); | ||
current_button.setText(String.valueOf(i + 1)); | ||
current_button.setTextSize((float)(block_height * 0.10)); | ||
} | ||
} | ||
} | ||
}); | ||
/** Launches game activity */ | ||
public void launchGame(View view) { | ||
Intent intent = new Intent(this, Game.class); | ||
startActivity(intent); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/example/jagadeesh545/piczzle/Victory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.jagadeesh545.piczzle; | ||
|
||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
|
||
public class Victory extends AppCompatActivity { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_victory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:paddingBottom="@dimen/activity_vertical_margin" | ||
android:paddingLeft="@dimen/activity_horizontal_margin" | ||
android:paddingRight="@dimen/activity_horizontal_margin" | ||
android:paddingTop="@dimen/activity_vertical_margin" | ||
tools:context="com.example.jagadeesh545.piczzle.Victory"> | ||
|
||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Congratulations!! You Win!!" | ||
android:id="@+id/textView" | ||
android:layout_centerVertical="true" | ||
android:layout_centerHorizontal="true" /> | ||
</RelativeLayout> |