Android實(shí)現(xiàn)井字游戲
本文實(shí)例為大家分享了Android實(shí)現(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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Button的基本用法詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android Button的基本用法詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
android和服務(wù)器的URLEncodedUtils亂碼編碼問(wèn)題的解決方案
今天小編就為大家分享一篇關(guān)于android和服務(wù)器的URLEncodedUtils亂碼編碼問(wèn)題的解決方案,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Android編程實(shí)現(xiàn)將tab選項(xiàng)卡放在屏幕底部的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)將tab選項(xiàng)卡放在屏幕底部的方法,涉及Android界面布局、tab設(shè)置及權(quán)限控制相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
Android DrawerLayout實(shí)現(xiàn)抽屜效果實(shí)例代碼
這篇文章主要介紹了Android DrawerLayout實(shí)現(xiàn)抽屜效果的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
Android WebView調(diào)用本地相冊(cè)的方法
這篇文章主要為大家詳細(xì)介紹了Android WebView調(diào)用本地相冊(cè)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
淺談Android AsyncTask內(nèi)存安全的一種使用方式
這篇文章主要介紹了淺談Android AsyncTask內(nèi)存安全的一種使用方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Android快速實(shí)現(xiàn)發(fā)送郵件實(shí)例
本篇文章主要介紹了Android快速實(shí)現(xiàn)發(fā)送郵件實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04

