Monday, 8 October 2018

Tic Tac Toe Game Coding

 
 Activity_main.xml File
  
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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" 
xmlns:ads="http://schemas.android.com/apk/res-auto"     
android:layout_width="match_parent"     
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".MainActivity">


   <RelativeLayout         
android:layout_width="match_parent"         
android:layout_height="wrap_content"
 
<TextView             
android:id ="@+id/text_view_p1" 
 android:layout_width="wrap_content"             
android:layout_height="wrap_content" 
 android:text = "Player 1:  0" 
 android:textSize="30sp" 
 android:textColor="#FF00FF"            />

        <TextView             
android:id ="@+id/text_view_p2"             
android:layout_width="wrap_content"             
android:layout_height="wrap_content" 
 android:layout_below="@+id/text_view_p1"             
android:text = "Player 2:  0" 
 android:textSize="30sp" 
 android:textColor="#FF00FF"            />

        <Button 
 android:id="@+id/button_reset" 
 android:layout_width="wrap_content"             
android:layout_height="wrap_content" 
 android:layout_alignParentEnd="true" 
 android:layout_centerVertical="true" 
 android:layout_marginEnd="33dp" 
 android:text="reset" 
 android:textColor="#DC143C"            />

    </RelativeLayout>

<LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="0dp"     
android:layout_weight="1"    >

    <Button        android:id="@+id/button_00" 
 android:layout_width="0dp"         
android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"        />
    <Button         
android:id="@+id/button_01"         
android:layout_width="0dp"         
android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"        />
    <Button 
 android:id="@+id/button_02" 
 android:layout_width="0dp" 
 android:layout_height="match_parent"         
android:layout_weight="1" 
 android:textSize="60sp"        />
</LinearLayout>

    <LinearLayout         
android:layout_width="match_parent" 
 android:layout_height="0dp"         
android:layout_weight="1"        >

        <Button 
 android:id="@+id/button_10" 
 android:layout_width="0dp"             
android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
        <Button 
 android:id="@+id/button_11" 
 android:layout_width="0dp" 
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
        <Button 
 android:id="@+id/button_12" 
 android:layout_width="0dp"             
android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
    </LinearLayout>
    <LinearLayout 
 android:layout_width="match_parent"         
android:layout_height="0dp" 
 android:layout_weight="1"        >

        <Button             
android:id="@+id/button_20" 
 android:layout_width="0dp" 
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
        <Button 
 android:id="@+id/button_21" 
 android:layout_width="0dp" 
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
        <Button             
android:id="@+id/button_22" 
 android:layout_width="0dp" 
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
    </LinearLayout>
 
 <com.google.android.gms.ads.AdView         
android:id="@+id/adView" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_centerHorizontal="true" 
 android:layout_alignParentBottom="true" 
 ads:adSize="BANNER"         
ads:adUnitId="ca-app-pub-5864813702378205/9734846806">
    </com.google.android.gms.ads.AdView>

</LinearLayout




MainActivity.Java File

package com.example.irawen.tictactoe;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import android.widget.Button;
import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private AdView mAdView;

    private Button[][] buttons = new Button[3][3];
    private boolean player1turn = true;
    private int roundCount;
    private int player1Points;
    private int player2Points;



    private TextView textViewPlayer1;
    private TextView textViewPlayer2;


    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textViewPlayer1 = findViewById(R.id.text_view_p1);
        textViewPlayer2 = findViewById(R.id.text_view_p2);

 for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
       String buttonID = "button_" + i + j;
        int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
         buttons[i][j] = findViewById(resID);
          buttons[i][j].setOnClickListener(this);

            }
        }
        Button buttonReset = findViewById(R.id.button_reset);
        buttonReset.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                resetGame();

            }
        });

            mAdView = findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);
    }

    @Override    public void onClick(View v) {
        if (!((Button) v).getText().toString().equals("")) {
            return;
        }
        if (player1turn) {
            ((Button) v).setText("X");
        } else {
            ((Button) v).setText("O");
        }
        roundCount++;
         if (checkForWin()){
             if (player1turn){
                 player1Wins();
             }else{
                 player2Wins();
             }
         }else if(roundCount == 9){
             draw();

         }else{
             player1turn = !player1turn;
         }
    }

    private boolean checkForWin() {
        String[][] field = new String[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                field[i][j] = buttons[i][j].getText().toString();
            }
        }
        for (int i = 0; i < 3; i++) {
            if ((field[i][0].equals(field[i][1])
                    && field[i][0].equals(field[i][2])
                    && ! field[i][0].equals(""))){
                return true;
            }
        }
        for (int i = 0; i < 3; i++) {
            if ((field[0][i].equals(field[1][i])
                    && field[0][i].equals(field[2][i])
                    && ! field[0][i].equals(""))){
                return true;
            }
        }
        if ((field[0][0].equals(field[1][1])
                && field[0][0].equals(field[2][2])
                && ! field[0][0].equals(""))){
            return true;
        }
        if ((field[0][2].equals(field[1][1])
                && field[0][2].equals(field[2][0])
                && ! field[0][2].equals(""))){
            return true;
        }
        return false;
    }
    private void player1Wins() {
        player1Points++;
        Toast.makeText(this,  "Player 1 wins!",Toast.LENGTH_SHORT).show();
        updatePointsText();
        resetBoard();
    }


    private void player2Wins() {
        player2Points++;
        Toast.makeText(this,  "Player 2 wins!",Toast.LENGTH_SHORT).show();
        updatePointsText();
        resetBoard();
    }

    private void draw() {
        Toast.makeText(this, "Draw!",Toast.LENGTH_SHORT).show();
        resetBoard();
    }
    private void updatePointsText(){
        textViewPlayer1.setText("Player 1: " +player1Points);
        textViewPlayer2.setText("Player 2: " +player2Points);
    }
    private void resetBoard() {
        for ( int i = 0; i < 3; i++){
            for ( int j = 0; j < 3; j++){
                buttons[i][j].setText("");
            }
        }
        roundCount = 0;
        player1turn = true;

    }
    private void resetGame(){
        player1Points = 0;
        player2Points = 0;
        updatePointsText();
        resetBoard();
    }
}
 
Preview
 


1 comment:

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (83) R (68) Selenium Webdriver (2) Software (13) SQL (27)