Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ENGINE
- package com.example.d851.matrix;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.*;
- public class Main extends AppCompatActivity implements View.OnClickListener
- {
- Button btTL, btTM, btTR, btML, btMM, btMR, btBL, btBM, btBR, btReset;
- TextView text;
- boolean turn;
- int numOfTurns;
- int[][] board;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btTL = findViewById(R.id.btTL);
- btTM = findViewById(R.id.btTM);
- btTR = findViewById(R.id.btTR);
- btML = findViewById(R.id.btML);
- btMM = findViewById(R.id.btMM);
- btMR = findViewById(R.id.btMR);
- btBL = findViewById(R.id.btBL);
- btBM = findViewById(R.id.btBM);
- btBR = findViewById(R.id.btBR);
- btReset = findViewById(R.id.btReset);
- text = findViewById(R.id.text);
- turn = true;
- numOfTurns = 0;
- board = new int[3][3];
- for (int x = 0; x < 3; x++)
- {
- for (int y = 0; y < 3; y++)
- {
- board[x][y] = 0;
- }
- }
- btTL.setOnClickListener(this);
- btTM.setOnClickListener(this);
- btTR.setOnClickListener(this);
- btML.setOnClickListener(this);
- btMM.setOnClickListener(this);
- btMR.setOnClickListener(this);
- btBL.setOnClickListener(this);
- btBM.setOnClickListener(this);
- btBR.setOnClickListener(this);
- btReset.setOnClickListener(this);
- }
- @Override
- public void onClick(View view)
- {
- if (view == btReset)
- {
- turn = true;
- numOfTurns = 0;
- for (int x = 0; x < 3; x++)
- {
- for (int y = 0; y < 3; y++)
- {
- board[x][y] = 0;
- }
- }
- btTL.setText("");
- btTM.setText("");
- btTR.setText("");
- btML.setText("");
- btMM.setText("");
- btMR.setText("");
- btBL.setText("");
- btBM.setText("");
- btBR.setText("");
- text.setText("");
- }
- if (!checkWin())
- {
- if (view == btTL && btTL.getText().equals(""))
- {
- btTL.setText(turn ? "X" : "O");
- turn = !turn;
- numOfTurns++;
- board[0][0] = turn ? 1 : -1;
- }
- if (view == btTM && btTM.getText().equals(""))
- {
- btTM.setText(turn ? "X" : "O");
- turn = !turn;
- numOfTurns++;
- board[0][1] = turn ? 1 : -1;
- }
- if (view == btTR && btTR.getText().equals(""))
- {
- btTR.setText(turn ? "X" : "O");
- turn = !turn;
- numOfTurns++;
- board[0][2] = turn ? 1 : -1;
- }
- if (view == btML && btML.getText().equals(""))
- {
- btML.setText(turn ? "X" : "O");
- turn = !turn;
- numOfTurns++;
- board[1][0] = turn ? 1 : -1;
- }
- if (view == btMM && btMM.getText().equals(""))
- {
- btMM.setText(turn ? "X" : "O");
- turn = !turn;
- numOfTurns++;
- board[1][1] = turn ? 1 : -1;
- }
- if (view == btMR && btMR.getText().equals(""))
- {
- btMR.setText(turn ? "X" : "O");
- turn = !turn;
- numOfTurns++;
- board[1][2] = turn ? 1 : -1;
- }
- if (view == btBL && btBL.getText().equals(""))
- {
- btBL.setText(turn ? "X" : "O");
- turn = !turn;
- numOfTurns++;
- board[2][0] = turn ? 1 : -1;
- }
- if (view == btBM && btBM.getText().equals(""))
- {
- btBM.setText(turn ? "X" : "O");
- turn = !turn;
- numOfTurns++;
- board[2][1] = turn ? 1 : -1;
- }
- if (view == btBR && btBR.getText().equals(""))
- {
- btBR.setText(turn ? "X" : "O");
- turn = !turn;
- numOfTurns++;
- board[2][2] = turn ? 1 : -1;
- }
- if (numOfTurns > 4)
- {
- boolean winner = checkWin();
- if (winner)
- text.setText((!turn) ? "X wins!" : "O wins!");
- else if (numOfTurns == 9)
- {
- text.setText("It's a tie!");
- }
- }
- }
- }
- public boolean checkWin()
- {
- for (int x = 0; x < 3; x++)
- {
- if (board[x][0] == board[x][1] && board[x][0] == board[x][2] && board[x][0] != 0)
- {
- return true;
- }
- }
- for (int y = 0;y < 3; y++)
- {
- if (board[0][y] == board[1][y] && board[0][y] == board[2][y] && board[0][y] != 0)
- {
- return true;
- }
- }
- if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != 0)
- {
- return true;
- }
- if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[2][0] != 0)
- {
- return true;
- }
- return false;
- }
- }
- XML
- <?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"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context="com.example.d851.matrix.Main">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="40dp"
- android:gravity="center"
- android:textStyle="bold"
- android:text="Tic Tac Toe"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:weightSum="3"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btTL"
- android:textSize="20dp"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="100dp"/>
- <Button
- android:id="@+id/btTM"
- android:textSize="20dp"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="100dp"/>
- <Button
- android:id="@+id/btTR"
- android:textSize="20dp"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="100dp"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:weightSum="3"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btML"
- android:textSize="20dp"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="100dp"/>
- <Button
- android:id="@+id/btMM"
- android:textSize="20dp"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="100dp"/>
- <Button
- android:id="@+id/btMR"
- android:textSize="20dp"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="100dp"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:weightSum="3"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btBL"
- android:textSize="20dp"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="100dp"/>
- <Button
- android:id="@+id/btBM"
- android:textSize="20dp"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="100dp"/>
- <Button
- android:id="@+id/btBR"
- android:textSize="20dp"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="100dp"/>
- </LinearLayout>
- <TextView
- android:id="@+id/text"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:textSize="35dp"
- android:textStyle="bold" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="0dip"
- android:orientation="vertical"
- android:layout_weight="1"
- android:gravity="bottom|center">
- <Button
- android:id="@+id/btReset"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Reset"
- android:textStyle="bold"
- android:textSize="20dp"
- android:layout_gravity="center"/>
- </LinearLayout>
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement