Skip to content

Commit

Permalink
Created basic version of game.
Browse files Browse the repository at this point in the history
  • Loading branch information
jagadeesh545 committed Feb 2, 2016
1 parent b5c02c9 commit baca55c
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 36 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {

defaultConfig {
applicationId "com.example.jagadeesh545.piczzle"
minSdkVersion 9
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</intent-filter>
</activity>
<activity android:name=".Game"></activity>
<activity android:name=".Victory" ></activity>
</application>

</manifest>
144 changes: 136 additions & 8 deletions app/src/main/java/com/example/jagadeesh545/piczzle/Game.java
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);
}
}
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 app/src/main/java/com/example/jagadeesh545/piczzle/Victory.java
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);
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_game.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/button16"
android:id="@+id/blank"
android:layout_below="@+id/button12"
android:layout_toRightOf="@+id/button15" />

Expand Down
8 changes: 6 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.jagadeesh545.piczzle.MainActivity">

<TextView
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
android:text="Click to Start!"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="launchGame"/>
</RelativeLayout>
19 changes: 19 additions & 0 deletions app/src/main/res/layout/activity_victory.xml
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>

0 comments on commit baca55c

Please sign in to comment.