Android Studio實現(xiàn)井字游戲
本文實例為大家分享了Android Studio實現(xiàn)井字游戲的具體代碼,供大家參考,具體內(nèi)容如下
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.AppCompatActivity; ? public class MainActivity extends AppCompatActivity { ? ? ? Button b1, b2, b3, b4, b5, b6, b7, b8, b9; ? ? int turn; ? ? int draw; ? ? Button playAgainButton; ? ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? ? b1 = (Button) findViewById(R.id.b1); ? ? ? ? b2 = (Button) findViewById(R.id.b2); ? ? ? ? b3 = (Button) findViewById(R.id.b3); ? ? ? ? b4 = (Button) findViewById(R.id.b4); ? ? ? ? b5 = (Button) findViewById(R.id.b5); ? ? ? ? b6 = (Button) findViewById(R.id.b6); ? ? ? ? b7 = (Button) findViewById(R.id.b7); ? ? ? ? b8 = (Button) findViewById(R.id.b8); ? ? ? ? b9 = (Button) findViewById(R.id.b9); ? ? ? ? playAgainButton = (Button) findViewById(R.id.playAgainButton); ? ? ? ? turn = 1; ? ? ? ? draw = 1; ? ? ? ? ? playAgainButton.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? Intent intent = new Intent(getApplicationContext(), MainActivity.class); ? ? ? ? ? ? ? ? startActivity(intent); ? ? ? ? ? ? ? ? finish(); ? ? ? ? ? ? } ? ? ? ? ? }); ? ? ? ? ? ? b1.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? ? if (b1.getText().toString().equals("")) { ? ? ? ? ? ? ? ? ? ? if (turn == 1) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 2; ? ? ? ? ? ? ? ? ? ? ? ? b1.setText("X"); ? ? ? ? ? ? ? ? ? ? } else if (turn == 2) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 1; ? ? ? ? ? ? ? ? ? ? ? ? b1.setText("O"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? endGame(); ? ? ? ? ? ? ? ? draw++; ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? b2.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? ? if (b2.getText().toString().equals("")) { ? ? ? ? ? ? ? ? ? ? if (turn == 1) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 2; ? ? ? ? ? ? ? ? ? ? ? ? b2.setText("X"); ? ? ? ? ? ? ? ? ? ? } else if (turn == 2) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 1; ? ? ? ? ? ? ? ? ? ? ? ? b2.setText("O"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? endGame(); ? ? ? ? ? ? ? ? draw++; ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? b3.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? ? if (b3.getText().toString().equals("")) { ? ? ? ? ? ? ? ? ? ? if (turn == 1) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 2; ? ? ? ? ? ? ? ? ? ? ? ? b3.setText("X"); ? ? ? ? ? ? ? ? ? ? } else if (turn == 2) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 1; ? ? ? ? ? ? ? ? ? ? ? ? b3.setText("O"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? endGame(); ? ? ? ? ? ? ? ? draw++; ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? b4.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? ? if (b4.getText().toString().equals("")) { ? ? ? ? ? ? ? ? ? ? if (turn == 1) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 2; ? ? ? ? ? ? ? ? ? ? ? ? b4.setText("X"); ? ? ? ? ? ? ? ? ? ? } else if (turn == 2) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 1; ? ? ? ? ? ? ? ? ? ? ? ? b4.setText("O"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? endGame(); ? ? ? ? ? ? ? ? draw++; ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? b5.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? ? if (b5.getText().toString().equals("")) { ? ? ? ? ? ? ? ? ? ? if (turn == 1) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 2; ? ? ? ? ? ? ? ? ? ? ? ? b5.setText("X"); ? ? ? ? ? ? ? ? ? ? } else if (turn == 2) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 1; ? ? ? ? ? ? ? ? ? ? ? ? b5.setText("O"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? endGame(); ? ? ? ? ? ? ? ? draw++; ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? b6.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? ? if (b6.getText().toString().equals("")) { ? ? ? ? ? ? ? ? ? ? if (turn == 1) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 2; ? ? ? ? ? ? ? ? ? ? ? ? b6.setText("X"); ? ? ? ? ? ? ? ? ? ? } else if (turn == 2) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 1; ? ? ? ? ? ? ? ? ? ? ? ? b6.setText("O"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? endGame(); ? ? ? ? ? ? ? ? draw++; ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? b7.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? ? if (b7.getText().toString().equals("")) { ? ? ? ? ? ? ? ? ? ? if (turn == 1) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 2; ? ? ? ? ? ? ? ? ? ? ? ? b7.setText("X"); ? ? ? ? ? ? ? ? ? ? } else if (turn == 2) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 1; ? ? ? ? ? ? ? ? ? ? ? ? b7.setText("O"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? endGame(); ? ? ? ? ? ? ? ? draw++; ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? b8.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? ? if (b8.getText().toString().equals("")) { ? ? ? ? ? ? ? ? ? ? if (turn == 1) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 2; ? ? ? ? ? ? ? ? ? ? ? ? b8.setText("X"); ? ? ? ? ? ? ? ? ? ? } else if (turn == 2) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 1; ? ? ? ? ? ? ? ? ? ? ? ? b8.setText("O"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? endGame(); ? ? ? ? ? ? ? ? draw++; ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? b9.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? ? if (b9.getText().toString().equals("")) { ? ? ? ? ? ? ? ? ? ? if (turn == 1) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 2; ? ? ? ? ? ? ? ? ? ? ? ? b9.setText("X"); ? ? ? ? ? ? ? ? ? ? } else if (turn == 2) { ? ? ? ? ? ? ? ? ? ? ? ? turn = 1; ? ? ? ? ? ? ? ? ? ? ? ? b9.setText("O"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? endGame(); ? ? ? ? ? ? ? ? draw++; ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? } ? ? ? public void endGame() { ? ? ? ? String a, b, c, d, e, f, g, h, i; ? ? ? ? boolean end = false; ? ? ? ? ? a = b1.getText().toString(); ? ? ? ? b = b2.getText().toString(); ? ? ? ? c = b3.getText().toString(); ? ? ? ? ? d = b4.getText().toString(); ? ? ? ? e = b5.getText().toString(); ? ? ? ? f = b6.getText().toString(); ? ? ? ? ? g = b7.getText().toString(); ? ? ? ? h = b8.getText().toString(); ? ? ? ? i = b9.getText().toString(); ? ? ? ? ? if (a.equals("X") && b.equals("X") && c.equals("X")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player X!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? ? } ? ? ? ? if (a.equals("X") && e.equals("X") && i.equals("X")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player X!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? if (a.equals("X") && d.equals("X") && g.equals("X")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player X!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? if (b.equals("X") && e.equals("X") && h.equals("X")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player X!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? if (c.equals("X") && f.equals("X") && i.equals("X")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player X!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? if (d.equals("X") && e.equals("X") && f.equals("X")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player X!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? if (g.equals("X") && h.equals("X") && i.equals("X")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player X!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? if (g.equals("X") && e.equals("X") && c.equals("X")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player X!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? if (a.equals("O") && b.equals("O") && c.equals("O")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player O!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? if (a.equals("O") && e.equals("O") && i.equals("O")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player O!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? if (a.equals("O") && d.equals("O") && g.equals("O")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player O!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? if (b.equals("O") && e.equals("O") && h.equals("O")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player O!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? if (c.equals("O") && f.equals("O") && i.equals("O")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player O!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? if (d.equals("O") && e.equals("O") && f.equals("O")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player O!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? if (g.equals("O") && h.equals("O") && i.equals("O")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player O!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? if (g.equals("O") && e.equals("O") && c.equals("O")) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Winner Player X!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? end = true; ? ? ? ? } ? ? ? ? ? if (end) { ? ? ? ? ? ? b1.setEnabled(false); ? ? ? ? ? ? b2.setEnabled(false); ? ? ? ? ? ? b3.setEnabled(false); ? ? ? ? ? ? b4.setEnabled(false); ? ? ? ? ? ? b5.setEnabled(false); ? ? ? ? ? ? b6.setEnabled(false); ? ? ? ? ? ? b7.setEnabled(false); ? ? ? ? ? ? b8.setEnabled(false); ? ? ? ? ? ? b9.setEnabled(false); ? ? ? ? ? ? playAgainButton.setVisibility(View.VISIBLE); ? ? ? ? } ? ? ? ? ? if (draw == 9 && !end) { ? ? ? ? ? ? Toast.makeText(MainActivity.this, "Draw Game!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? playAgainButton.setVisibility(View.VISIBLE); ? ? ? ? } ? ? } ? ? }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="wrap_content" ? ? android:orientation="vertical" ? ? android:paddingLeft="@dimen/activity_horizontal_margin" ? ? android:paddingTop="@dimen/activity_horizontal_margin" ? ? android:paddingRight="@dimen/activity_horizontal_margin" ? ? android:paddingBottom="@dimen/activity_vertical_margin" ? ? android:weightSum="3" ? ? tools:context="com.boriskonan.tictactoe.MainActivity"> ? ? ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="120dp" ? ? ? ? android:weightSum="3"> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/b1" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:textSize="50dp" /> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/b2" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:textSize="50dp" /> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/b3" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:textSize="50dp" /> ? ? </LinearLayout> ? ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="120dp" ? ? ? ? android:weightSum="3"> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/b4" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:textSize="50dp" /> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/b5" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:textSize="50dp" /> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/b6" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:textSize="50dp" /> ? ? ? </LinearLayout> ? ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="120dp" ? ? ? ? android:weightSum="3"> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/b7" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:textSize="50dp" /> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/b8" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:textSize="50dp" /> ? ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/b9" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:textSize="50dp" /> ? ? </LinearLayout> ? ? ? <Button ? ? ? ? android:id="@+id/playAgainButton" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:onClick="playAgain" ? ? ? ? android:text="Play Again" ? ? ? ? android:visibility="invisible" ? ? ? ? tools:ignore="OnClick" /> ?</LinearLayout>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
實例講解Android app開發(fā)中ListView的基本使用及優(yōu)化
這篇文章主要介紹了Android app開發(fā)中ListView的基本使用及優(yōu)化,ListView視圖組件是Android中最常用的組件之一需要的朋友可以參考下2016-02-02Android編程實現(xiàn)EditText字數(shù)監(jiān)聽并顯示的方法
這篇文章主要介紹了Android編程實現(xiàn)EditText字數(shù)監(jiān)聽并顯示的方法,涉及Android EditText文本框事件監(jiān)聽與響應相關操作技巧,需要的朋友可以參考下2017-02-02Android studio導出APP測試包和構建正式簽名包
大家好,本篇文章主要講的是Android studio導出APP測試包和構建正式簽名包,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2021-12-12Android數(shù)據(jù)庫SD卡創(chuàng)建和圖片存取操作
這篇文章主要介紹了Android數(shù)據(jù)庫SD卡創(chuàng)建和圖片存取操作的相關資料,需要的朋友可以參考下2017-04-04Android頁面之間進行數(shù)據(jù)回傳的方法分析
這篇文章主要介紹了Android頁面之間進行數(shù)據(jù)回傳的方法,結合實例形式分析了Android頁面之間進行數(shù)據(jù)的傳遞與處理技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06詳解SwipeListView框架實現(xiàn)微信\QQ滑動刪除效果
這篇文章主要為大家詳細介紹了SwipeListView框架實現(xiàn)微信\QQ滑動刪除效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08Android開發(fā)框架之自定義ZXing二維碼掃描界面并解決取景框拉伸問題
這篇文章主要介紹了Android開發(fā)框架之自定義ZXing二維碼掃描界面并解決取景框拉伸問題的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06