- Get link
- X
- Other Apps
How to make Simple Weather Application in Android Studio
- Get link
- X
- Other Apps
Demo:
Introduction
In this blog, we learn to create a tic-toc-toe game in android studio. Tic toc toe is a very popular pencil and paper game. As a beginerr, it's a very pleasent activity to create such type of game.
MainActivity.java
package com.example.tictokgame; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayout; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { Button tappedButton; LinearLayout linearLayout; TextView winningText; int activePlayer = 0; int[] fill = {2,2,2,2,2,2,2,2,2}; int[][] winningLocation = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}}; int count = 0; boolean gameOver = false; String msg; public void gameLogic(View view){ tappedButton = (Button) view; linearLayout = findViewById(R.id.linearLayout); winningText = findViewById(R.id.winningText); count ++; int tappedButtonLocation = Integer.parseInt(tappedButton.getTag().toString()); if(fill[tappedButtonLocation] == 2 && !gameOver) { fill[tappedButtonLocation] = activePlayer; if (activePlayer == 0) { tappedButton.setBackgroundResource(R.drawable.circle_yellow); activePlayer = 1; } else { tappedButton.setBackgroundResource(R.drawable.circle_yelp); activePlayer = 0; } } for (int[] winningPosition : winningLocation) { if (fill[winningPosition[0]] == fill[winningPosition[1]] && fill[winningPosition[1]] == fill[winningPosition[2]] && fill[winningPosition[0]] != 2) { if (activePlayer == 0) { msg = "Red is winner"; // Toast.makeText(getApplicationContext(), "Red is the gameOver", Toast.LENGTH_LONG).show(); } else if (activePlayer == 1) { msg = "Yellow is winner"; // Toast.makeText(getApplicationContext(),"Yellow is the gameOver",Toast.LENGTH_LONG).show(); } gameOver = true; linearLayout.setVisibility(view.VISIBLE); winningText.setText(msg); } if (count == 9 && gameOver == false) { msg = "Game is Draw"; linearLayout.setVisibility(view.VISIBLE); winningText.setText(msg); } } } public void playAgain(View view){ gameOver = false; count = 0; msg = ""; activePlayer = 0; linearLayout = findViewById(R.id.linearLayout); linearLayout.setVisibility(view.INVISIBLE); GridLayout gridLayout = findViewById(R.id.gridLayout); for(int i=0; i<gridLayout.getChildCount();i++){ ((Button) gridLayout.getChildAt(i)).setBackgroundResource(android.R.drawable.btn_default); } for (int i= 0; i<fill.length; i++){ fill[i] = 2; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<android.support.v7.widget.GridLayout
android:id="@+id/gridLayout"
android:layout_width="365dp"
android:layout_height="432dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:columnCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.717"
app:rowCount="3">
<Button
android:id="@+id/button0"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_margin="0dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="0dp"
android:onClick="gameLogic"
android:tag="0"
app:layout_column="0"
app:layout_row="0" />
<Button
android:id="@+id/button1"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_margin="5dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="25dp"
android:onClick="gameLogic"
android:tag="1"
app:layout_column="1"
app:layout_row="0" />
<Button
android:id="@+id/button2"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_margin="5dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="25dp"
android:onClick="gameLogic"
android:tag="2"
app:layout_column="2"
app:layout_row="0" />
<Button
android:id="@+id/button3"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_margin="5dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="25dp"
android:onClick="gameLogic"
android:tag="3"
app:layout_column="0"
app:layout_row="1" />
<Button
android:id="@+id/button4"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_margin="5dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="25dp"
android:onClick="gameLogic"
android:tag="4"
app:layout_column="1"
app:layout_row="1" />
<Button
android:id="@+id/button5"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_margin="5dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="25dp"
android:onClick="gameLogic"
android:tag="5"
app:layout_column="2"
app:layout_row="1" />
<Button
android:id="@+id/button6"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_margin="5dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="25dp"
android:onClick="gameLogic"
android:tag="6"
app:layout_column="0"
app:layout_row="2" />
<Button
android:id="@+id/button7"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_margin="5dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="25dp"
android:onClick="gameLogic"
android:tag="7"
app:layout_column="1"
app:layout_row="2" />
<Button
android:id="@+id/button8"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_margin="5dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="25dp"
android:onClick="gameLogic"
android:tag="8"
app:layout_column="2"
app:layout_row="2" />
</android.support.v7.widget.GridLayout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="TIC TOC TOE"
android:textColor="#FF040000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/gridLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="@color/colorPrimary"
android:orientation="vertical"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/winningText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#FFD15010"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:textColor="@color/colorAccent"
android:textSize="18sp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#FFD15010"
android:onClick="playAgain"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="PLAY AGAIN"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Drawable Resources
circle_yelp.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#af0606"> </solid>
<size android:height="120dp"
android:width="120dp"> </size>
</shape>
circle_yellow.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFFC00"> </solid>
<size android:height="120dp"
android:width="120dp"> </size>
</shape>
- Get link
- X
- Other Apps
Comments
Post a Comment