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.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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。
看完文章,还可以用支付宝扫描下面的二维码领取一个支付宝红包,目前可领1-88元不等
除了扫码可以领取之外,大家还可以(复制 720087999 打开✔支付宝✔去搜索, h`o`n.g.包哪里来,动动手指就能领)。
看下图所示是好多参与这次活动领取红包的朋友: