網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android實現(xiàn)井字游戲的具體代碼,供大家參考,具體內(nèi)容如下
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>
原文鏈接:https://blog.csdn.net/allway2/article/details/122441086
相關(guān)推薦
- 2022-05-10 thymeleaf跳轉(zhuǎn)到響應(yīng)頁面(modelandview 中的view)
- 2022-05-05 輕量級ORM框架Dapper應(yīng)用之實現(xiàn)In操作_實用技巧
- 2022-04-30 Python代碼顯得Pythonic(區(qū)別于其他語言的寫法)_python
- 2022-05-21 C#編程之AOP編程思想_C#教程
- 2022-03-15 antd-mobile 請求時Loading組件
- 2022-07-24 .Net創(chuàng)建型設(shè)計模式之建造者、生成器模式(Builder)_基礎(chǔ)應(yīng)用
- 2022-06-30 Unity多屏幕設(shè)置的具體方案_C#教程
- 2023-02-12 redis加鎖的三種方式小結(jié)_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支