java - if statement not finding condition -
this question has answer here:
- how compare strings in java? 23 answers
i building basic tic tac toe app project , trying add basic ai (just random number) player play against.
it should pick random number based on length of arraylist of each value represents button or square on board. idea that integer value converted string , used select button matching tag, using, gettag().
the problem looping through button in layout, statement unable find button tag , skips necessary logic contains ai play turn. should then, fill relevant button x , set inactive , return.
i have added system.out.println(""); statements aid debugging , can see point @ stops working expected.
the problem in aiplayerpick() method.
mainactivity.java
package com.example.richardcurteis.connect3; import android.os.bundle; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.view; import android.view.menu; import android.view.menuitem; import android.view.viewgroup; import android.widget.button; import android.widget.tablelayout; import android.widget.tablerow; import java.util.random; import java.util.arraylist; public class mainactivity extends appcompatactivity { boolean noughtsturn; arraylist board; players player; int aipickedbutton; int buttonpressed; public void receiveclick(view view) { string takebutton = string.valueof(view.gettag()); buttonpressed = integer.parseint(takebutton); humanplayerturn(view); aiplayerturn(view); } public void humanplayerturn(view view) { playerclick(view); noughtsturn = false; } public void aiplayerturn(view view) { aipickedbutton = randombuttonpick(); playerclick(view); noughtsturn = true; } public void playerclick(view view) { button b; if (view instanceof button) { b = (button) view; if ( noughtsturn ) { b.settext(player.noughtsplayer()); board.set(buttonpressed, 0); b.setenabled(false); } else { aiplayerpick(); } } } public integer randombuttonpick() { random randomnumber = new random(); int randomint = randomnumber.nextint(board.size()); system.out.println("random integer picked use index ai: " + randomint); return randomint; } public button aiplayerpick() { button btn = null; tablelayout tablelayout = (tablelayout) findviewbyid(r.id.tablelayout); (int rowindex = 0; rowindex < tablelayout.getchildcount(); rowindex++) { view tablelayoutchild = tablelayout.getchildat(rowindex); if (tablelayoutchild instanceof tablerow) { (int = 0; < ((viewgroup) tablelayoutchild).getchildcount(); i++) { view view = ((viewgroup) tablelayoutchild).getchildat(i); // change index string find tag string targetbutton = (string) string.valueof(aipickedbutton); system.out.println("progress 1 , targetbutton class: " + targetbutton.getclass() + " " + targetbutton); if (view instanceof button && view.gettag() == targetbutton) { // appears logic breaks down int targetindex = integer.parseint(targetbutton); system.out.println("progress 2"); view btn_v = view.findviewwithtag(targetbutton); btn = (button) btn_v; system.out.println("progress 3"); btn.settext(player.crossesplayer()); board.set(targetindex, 1); btn.setenabled(false); break; } else { i++; } } } } return btn; } public class players { public string noughtsplayer() { return "o"; } public string crossesplayer() { return "x"; } public string blankbutton() { return ""; } } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "replace own action", snackbar.length_long) .setaction("action", null).show(); } }); noughtsturn = true; board = new arraylist(); int boardsize = getboardsize(); (int boardindex = 0; boardindex < boardsize; boardindex++) { board.add(2); } player = new players(); } public int getboardsize() { int buttoncount = 0; tablelayout tablelayout = (tablelayout) findviewbyid(r.id.tablelayout); (int rowindex = 0; rowindex < tablelayout.getchildcount(); rowindex++) { view tablelayoutchild = tablelayout.getchildat(rowindex); if (tablelayoutchild instanceof tablerow) { (int = 0; < ((viewgroup) tablelayoutchild).getchildcount(); i++) { view view = ((viewgroup) tablelayoutchild).getchildat(i); if (view instanceof button) { buttoncount++; } } } } system.out.println("number of buttons: " + buttoncount); return buttoncount; } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } } content_main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout 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" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.richardcurteis.connect3.mainactivity" tools:showin="@layout/activity_main" android:background="#070000"> <tablelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_alignparentright="false" android:layout_alignparentend="false" android:layout_alignparentstart="false" android:layout_centerinparent="true" android:id="@+id/tablelayout" android:background="#000000"> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"> <button android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/gridbutton1" android:layout_column="4" android:onclick="receiveclick" android:tag="0" /> android:nestedscrollingenabled="false" /> <button android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/gridbutton2" android:layout_column="12" android:onclick="receiveclick" android:tag="1" /> <button android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/gridbutton3" android:layout_column="19" android:onclick="receiveclick" android:tag="2" /> </tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"> <button android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/gridbutton4" android:layout_column="4" android:onclick="receiveclick" android:tag="3"/> <button android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/gridbutton5" android:layout_column="12" android:onclick="receiveclick" android:tag="4"/> <button android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/gridbutton6" android:layout_column="19" android:onclick="receiveclick" android:tag="5"/> </tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"></tablerow> <tablerow android:layout_width="match_parent" android:layout_height="match_parent"> <button android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/gridbutton7" android:layout_column="4" android:onclick="receiveclick" android:tag="6"/> <button android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/gridbutton8" android:layout_column="12" android:onclick="receiveclick" android:tag="7"/> <button android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/gridbutton9" android:layout_column="19" android:onclick="receiveclick" android:tag="8" /> </tablerow> </tablelayout> <button android:layout_width="200dp" android:layout_height="120dp" android:text="new game" android:id="@+id/newgamebutton" android:layout_below="@+id/tablelayout" android:layout_centerhorizontal="true" android:layout_margintop="61dp" /> </relativelayout> log output:
01-05 20:59:23.583 10380-10380/com.example.richardcurteis.connect3 i/art: not late-enabling -xcheck:jni (already on) 01-05 20:59:23.813 10380-10380/com.example.richardcurteis.connect3 w/system: classloader referenced unknown path: /data/app/com.example.richardcurteis.connect3-2/lib/x86 01-05 20:59:24.224 10380-10380/com.example.richardcurteis.connect3 i/system.out: number of buttons: 9 01-05 20:59:24.275 10380-10394/com.example.richardcurteis.connect3 d/openglrenderer: use egl_swap_behavior_preserved: true 01-05 20:59:24.480 10380-10394/com.example.richardcurteis.connect3 i/openglrenderer: initialized egl, version 1.4 01-05 20:59:24.543 10380-10394/com.example.richardcurteis.connect3 w/egl_emulation: eglsurfaceattrib not implemented 01-05 20:59:24.543 10380-10394/com.example.richardcurteis.connect3 w/openglrenderer: failed set egl_swap_behavior on surface 0xad79f5a0, error=egl_success 01-05 20:59:25.192 10380-10380/com.example.richardcurteis.connect3 i/choreographer: skipped 33 frames! application may doing work on main thread. 01-05 20:59:25.378 10380-10386/com.example.richardcurteis.connect3 w/art: suspending threads took: 49.644ms 01-05 20:59:51.722 10380-10380/com.example.richardcurteis.connect3 i/system.out: random integer picked use index ai: 6 01-05 20:59:51.722 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 6 01-05 20:59:51.722 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 6 01-05 20:59:51.722 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 6 01-05 20:59:51.722 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 6 01-05 20:59:51.722 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 6 01-05 20:59:51.722 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 6 01-05 21:00:00.751 10380-10380/com.example.richardcurteis.connect3 i/system.out: random integer picked use index ai: 5 01-05 21:00:00.751 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 5 01-05 21:00:00.752 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 5 01-05 21:00:00.752 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 5 01-05 21:00:00.752 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 5 01-05 21:00:00.752 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 5 01-05 21:00:00.752 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 5 01-05 21:00:05.129 10380-10380/com.example.richardcurteis.connect3 i/system.out: random integer picked use index ai: 0 01-05 21:00:05.129 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 0 01-05 21:00:05.129 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 0 01-05 21:00:05.129 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 0 01-05 21:00:05.129 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 0 01-05 21:00:05.129 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 0 01-05 21:00:05.129 10380-10380/com.example.richardcurteis.connect3 i/system.out: progress 1 , targetbutton class: class java.lang.string 0
as mentioned in comments, instead of:
view.gettag() == targetbutton use:
view.gettag().equals(targetbutton) this link on subject how compare strings in java?
Comments
Post a Comment