Android实现井字游戏

本文实例为大家分享了Android实现井字游戏的具体代码,供大家参考,具体内容如下

MainActivity.java

package com.mohit.tictactoe;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    char activePlayer = 'X';
    char[] gameState = new char[9];
    boolean gameActive = true;
    boolean isFull = false;
 
 
    int[][] winningPos = {{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6}};
 
    ImageView[] images = new ImageView[9];
 
    @SuppressLint("ResourceAsColor")
    public void onTap(View view) {
 
        ImageView img = (ImageView) view;
        int tappedImage = Integer.parseInt(img.getTag().toString());
        if (gameState[tappedImage] == 0 && gameActive) {
            gameState[tappedImage] = activePlayer;
            if (activePlayer == 'O') {
                img.setImageResource(R.drawable.o);
                activePlayer = 'X';
                TextView status = findViewById(R.id.status);
                status.setText("X's turn - Tap to play");
            } else {
                img.setImageResource(R.drawable.x);
                activePlayer = 'O';
                TextView status = findViewById(R.id.status);
                status.setText("O's turn - Tap to play");
            }
        }
 
        images[0] = findViewById(R.id.imageView1);
        images[1] = findViewById(R.id.imageView2);
        images[2] = findViewById(R.id.imageView3);
        images[3] = findViewById(R.id.imageView4);
        images[4] = findViewById(R.id.imageView5);
        images[5] = findViewById(R.id.imageView6);
        images[6] = findViewById(R.id.imageView7);
        images[7] = findViewById(R.id.imageView8);
        images[8] = findViewById(R.id.imageView9);
 
        // Check if any player has won
        for (int i = 0 ; i < 8 ; i++) {
            if (gameState[winningPos[i][0]] != 0 && gameState[winningPos[i][0]] == gameState[winningPos[i][1]] && gameState[winningPos[i][0]] == gameState[winningPos[i][2]]) {
                TextView status = findViewById(R.id.status);
                status.setText(gameState[winningPos[i][0]] + " HAS WON THE GAME");
                gameActive = false;
 
                for (int j = 0 ; j < 9 ; j++) {
                    int tag = Integer.parseInt(images[j].getTag().toString());
                    if (tag == winningPos[i][0] || tag == winningPos[i][1] || tag == winningPos[i][2]) {
                        images[j].setBackgroundColor(Color.RED);
                    }
                }
            }
        }
 
        for (int j = 0 ; j < 9 ; j++) {
            if (gameState[j] != 0) {
                isFull = true;
            }
            else {
                isFull = false;
                break;
            }
        }
        if (isFull && gameActive) {
            TextView status = findViewById(R.id.status);
            status.setText("GAME DRAWN");
        }
 
    }
 
    
    public void gameRestart(View view) {
        gameActive = true;
        activePlayer = 'X';
        gameState = new char[9];
        images[0] = findViewById(R.id.imageView1);
        images[1] = findViewById(R.id.imageView2);
        images[2] = findViewById(R.id.imageView3);
        images[3] = findViewById(R.id.imageView4);
        images[4] = findViewById(R.id.imageView5);
        images[5] = findViewById(R.id.imageView6);
        images[6] = findViewById(R.id.imageView7);
        images[7] = findViewById(R.id.imageView8);
        images[8] = findViewById(R.id.imageView9);
 
        for (int j = 0 ; j < 9 ; j++) {
            images[j].setImageResource(0);
            images[j].setBackgroundColor(0);
        }
 
        TextView status = findViewById(R.id.status);
        status.setText("X's turn - Tap to play");
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
 
    <ImageView
        android:id="@+id/imageView0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:background="@color/white"
        app:layout_constraintBottom_toBottomOf="@+id/button2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/grid" />
 
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="23dp"
        android:fontFamily="serif"
        android:text="@string/heading"
        android:textSize="34sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="357dp"
        android:layout_height="362dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="@+id/imageView0"
        app:layout_constraintEnd_toEndOf="@+id/imageView0"
        app:layout_constraintStart_toStartOf="@+id/imageView0"
        app:layout_constraintTop_toTopOf="@+id/imageView0">
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">
 
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onTap"
                android:padding="20sp"
                android:tag="0" />
 
            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onTap"
                android:padding="20sp"
                android:tag="1" />
 
            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onTap"
                android:padding="20sp"
                android:tag="2" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">
 
            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onTap"
                android:padding="20sp"
                android:tag="3" />
 
            <ImageView
                android:id="@+id/imageView5"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onTap"
                android:padding="20sp"
                android:tag="4" />
 
            <ImageView
                android:id="@+id/imageView6"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onTap"
                android:padding="20sp"
                android:tag="5" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">
 
            <ImageView
                android:id="@+id/imageView7"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onTap"
                android:padding="20sp"
                android:tag="6" />
 
            <ImageView
                android:id="@+id/imageView8"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onTap"
                android:padding="20sp"
                android:tag="7" />
 
            <ImageView
                android:id="@+id/imageView9"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onTap"
                android:padding="20sp"
                android:tag="8" />
        </LinearLayout>
 
    </LinearLayout>
 
    <TextView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:fontFamily="serif"
        android:text="X's turn - Tap to play"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView0" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:onClick="gameRestart"
        android:text="RESTART THE GAME"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
 
</androidx.constraintlayout.widget.ConstraintLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android Studio实现井字游戏

    本文实例为大家分享了Android Studio实现井字游戏的具体代码,供大家参考,具体内容如下 MainActivity.java import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast;   import androidx.appcompat.app.AppCompat

  • Android实现井字游戏

    本文实例为大家分享了Android实现井字游戏的具体代码,供大家参考,具体内容如下 MainActivity.java package com.mohit.tictactoe;   import androidx.appcompat.app.AppCompatActivity;   import android.annotation.SuppressLint; import android.graphics.Color; import android.os.Bundle; import andr

  • Android辅助功能实现自动抢红包(附源码)

    一.描述 最近看到同事有用抢红包的软件,就想看看抢红包的具体实现是如何的,所以了解了一下,有用辅助功能实现的,所以在下面的示例中会展示一个抢红包的小Demo,附带源码抢红包源码. 二.效果图 在桌面收到红包进行抢 在聊天页面收到口令红包 三.AccessibilityService使用 创建辅助服务类,继承AccessibilityService,实现两个接口,接收系统的事件 public class MyService extends AccessibilityService { @Overr

  • 浅析Android Studio 3.0 升级各种坑(推荐)

    点击 Check for Updates 增量更新: 下载完成,会提示更新 您将 Gradle 更新至 4.1: 这里建议您对老项目先暂时点击 Don't remind me on this project,以防有坑.当然我不入地狱谁入地狱,我点 Update,于是问题来了,一直处于下载中,不过,莫担心,我下载好了,公众号聊天界面回复「 gradle-4.1-all 」,下载 gradle-4.1-all.zip 文件,放到: 重启 Android Studio. gradle 目录: Mac系

  • 使用AngularJS 应用访问 Android 手机的图片库

    Download angularjs.zip- 4.5 KB 介绍 这篇文章来说明如何使用AngularJs调用android Apps暴露的REST APIS来访问图像库. 背景 Android和IOS 有很多远程访问的app,但是开发者缺少远程访问手机特征的API.因此,myMoKit的开发是用来填补软件解决方案的缺陷的. 使用代码 使用代码是很简单的,你只要通过web URL 引用myMoKit 服务,你就可以看见所有暴露的REST API了 这些在手机里面的API列表和流媒体.通过Ang

  • Android开发实现模仿360二维码扫描功能实例详解

    本文实例讲述了Android开发实现模仿360二维码扫描功能的方法.分享给大家供大家参考,具体如下: 一.效果图: 二.框架搭建 1.首先,下载最新zxing开源项目. 下载地址:http://code.google.com/p/zxing/ 或 点击此处本站下载. 2.分析项目结构,明确扫描框架需求.在zxing中,有很多其他的功能,项目结构比较复杂:针对二维码QRCode扫描,我们需要几个包: (1)com.google.zxing.client.android.Camera 基于Camer

  • Android中控制和禁止ScrollView自动滑动到底部的方法

    一.Android 控制ScrollView滚动到底部 在开发中,我们经常需要更新列表,并将列表拉倒最底部,比如发表微博,聊天界面等等, 这里有两种办法,第一种,使用scrollTo(): public static void scrollToBottom(final View scroll, final View inner) { Handler mHandler = new Handler(); mHandler.post(new Runnable() { public void run()

  • Android编程实现压缩图片并加载显示的方法

    本文实例讲述了Android编程实现压缩图片并加载显示的方法.分享给大家供大家参考,具体如下: 解析: 图片压缩的关键就是 options.inSampleSize = scale; 如果scale > 0,表示图片进行了压缩 /** * 压缩图片 * @author chen.lin * */ public class LoadImageActivity extends Activity implements OnClickListener { private Button mBtnLoad;

  • Android如何获取屏幕、状态栏及标题栏的高度详解

    前言 本文主要给大家介绍了关于Android获取屏幕.状态栏及标题栏高度的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 在开始之前,先来看一张图: 绿色区域:屏幕区域 蓝色区域:状态栏区域 红色区域:标题栏区域 黄色区域:view绘制区域 1.Android手机屏幕的高度 整个手机使用发亮,不使用变黑的部分,绿色区域 获取屏幕的高度方法一 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getD

  • Android开发实现图片平移、缩放、倒影及旋转功能的方法

    本文实例讲述了Android开发实现图片平移.缩放.倒影及旋转功能的方法.分享给大家供大家参考,具体如下: 解析: 1)根据原来的图片创建新的图片 Bitmap modBm = Bitmap.createBitmap(bm.getWidth()+20, bm.getHeight()+20, bm.getConfig()); 2)设置到画布 Canvas canvas = new Canvas(modBm); 3)使用矩阵进行平移- Matrix matrix = new Matrix(); ma

随机推荐