Skip to content

Instantly share code, notes, and snippets.

@Bahaaib
Last active July 17, 2016 07:53
Show Gist options
  • Select an option

  • Save Bahaaib/9316a8df04c6e54eb6f7a4df33326944 to your computer and use it in GitHub Desktop.

Select an option

Save Bahaaib/9316a8df04c6e54eb6f7a4df33326944 to your computer and use it in GitHub Desktop.
// Insert the following lines below your package name
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int ActivePlayer = 0; // 0 for Yellow , 1 for Red
int gameState[] = {2, 2, 2, 2, 2, 2, 2, 2, 2}; // An Array of the unfilled positions on the Grid
boolean gameIsActive = true; // A flag to prevent user from droping the coins
int winningPositions[][] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};// The winning combinations on the Grid
public void dropIn(View view) {
ImageView counter = (ImageView) view; // Here's where the user tapping converted into an exact ImgView
int tappedCounter = Integer.parseInt(counter.getTag().toString()); // get the number of the cell on the grid
if (gameState[tappedCounter] == 2 && gameIsActive) // A Flag to check the availability of the grid cell
{
gameState[tappedCounter] = ActivePlayer; // Grid cell filled with the coin in turn
counter.setTranslationY(-1000f); // The ImgView float up 1000f
//////////////////////////////////////////////////////////////
if (ActivePlayer == 0) {
counter.setImageResource(R.drawable.yellow);
ActivePlayer = 1;
} else
counter.setImageResource(R.drawable.red);
ActivePlayer = 0;
// Here's A Flag to refer to the player in turn
counter.animate().translationYBy(1000f).rotation(360).setDuration(300); // The Imgview get replaced by the coin in turn, sink down
}
for (int winningPosition[] : winningPositions)// A loop to check all the winning combinations
{
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[0]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] != 2) ////// the winning comb. must be both from the same type and not empty cell
{
gameIsActive = false;
String winner = "RED";
if (gameState[winningPosition[0]] == 0) {
winner = "YELLOW";
}
TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
winnerMessage.setText(winner + "IS WINNER");
} else {
boolean gameIsOver = true; // if nothing of the comb. satisfied then the game is heading to draw, then over
for(int counterState : gameState){
if(counterState == 2)
{gameIsOver = false;} // if still there's void cells then it's probably still a winner move at least, game is not over yet
}
if (gameIsOver){
TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage); // Creating A PlayAgain layout after game is over
winnerMessage.setText("IT'S A DRAW");
LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
layout.setVisibility(View.VISIBLE); // initially visible once called
}
}
}
}
public void playAgain (View view)// Creating the method that reset the system once PlayAgain is pressed
{
gameIsActive = true; // Game is returned to be Active
LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
layout.setVisibility(View.INVISIBLE); // Hide the Result board and PlayAgain button
ActivePlayer = 0; // Setting the Yellow player in turn by default
for(int i=0; i < gameState.length; i++){ // reset all ImgViews to void and clear the playboard
gameState[i] = 2;
}
GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout);
for(int i=0; i < gridLayout.getChildCount(); i++)
{
((ImageView)gridLayout.getChildAt(i)).setImageResource(0);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.bebo.newconnect_4.MainActivity"
tools:showIn="@layout/activity_main">
<GridLayout
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:columnCount="3"
android:rowCount="3"
android:background="@drawable/board"
android:layout_alignParentEnd="true"
android:id="@+id/gridLayout">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView"
android:layout_row="0"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:onClick="dropIn"
android:tag="0" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView2"
android:layout_row="0"
android:layout_column="1"
android:layout_marginTop="10dp"
android:layout_marginLeft="25dp"
android:onClick="dropIn"
android:tag="1" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView3"
android:layout_row="0"
android:layout_column="2"
android:layout_marginTop="10dp"
android:layout_marginLeft="25dp"
android:onClick="dropIn"
android:tag="2" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView4"
android:layout_row="1"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="24dp"
android:onClick="dropIn"
android:tag="3" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView5"
android:layout_row="1"
android:layout_column="1"
android:layout_marginLeft="25dp"
android:layout_marginTop="24dp"
android:onClick="dropIn"
android:tag="4" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView6"
android:layout_row="1"
android:layout_column="2"
android:layout_marginLeft="25dp"
android:layout_marginTop="24dp"
android:onClick="dropIn"
android:tag="5" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView7"
android:layout_row="2"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="6" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView8"
android:layout_row="2"
android:layout_column="1"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="7" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView9"
android:layout_row="2"
android:layout_column="2"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="8" />
</GridLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fffaff0b"
android:padding="30dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/playAgainLayout"
android:visibility="invisible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/winnerMessage"
android:layout_gravity="center_horizontal"
android:textSize="30sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Again"
android:id="@+id/playAgainButton"
android:layout_gravity="center_horizontal"
android:onClick="playAgain" />
</LinearLayout>
</RelativeLayout>
1- create an empty imgView
2-move it up , replace the coin type then move it back down
3- repeat with every single coin
*Game Logic*
4-Create a flag that determines which player is in turn
5- create the flag that prevent the system from fill an already
filled cell.
6- Tell the system the winning positions
7- create a looping array to check the positions & types.
8- create a new invisible "Play Again" layout
9-create the winning message for either of the users
10-create the playAgain method>> restoring all the game setting to default
*revise the GridLayout way of doing it from sources
11- Let the system stop once one user wins
12-let the system check for the case of draw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment