0

I am building a basic tic tac toe app for a project and am trying to add basic AI (just a random number) for a player to play against.

It should pick a random number based on the length of the ArrayList of which each value represents a button or square on the board. The idea is that that integer value is converted to a string and used to select the button with the matching tag, using, getTag().

The problem is that where I am looping through the button in the layout, the statement is unable to find the button with the tag and the skips the necessary logic it contains for the AI to play its turn. It should then, fill the relevant button with an X , set it inactive and return.

I have added System.out.println(""); statements to aid debugging and I can see that this is the point at which it stops working as expected.

The problem is in the 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 for use as index by AI: " + randomInt);
        return randomInt;
    }

    public Button aiPlayerPick() {
        Button btn = null;
        TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);

        for (int rowIndex = 0; rowIndex < tableLayout.getChildCount(); rowIndex++) {
            View tableLayoutChild = tableLayout.getChildAt(rowIndex);
            if (tableLayoutChild instanceof TableRow) {
                for (int i = 0; i < ((ViewGroup) tableLayoutChild).getChildCount(); i++) {
                    View view = ((ViewGroup) tableLayoutChild).getChildAt(i);

                    // Change index back to string to find tag
                    String targetButton = (String) String.valueOf(aiPickedButton);

                    System.out.println("Progress 1 and targetButton class: "
                            + targetButton.getClass() + " " + targetButton);

                    if (view instanceof Button && view.getTag() == targetButton) { // This appears to be where the 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 with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        noughtsTurn = true;
        board = new ArrayList();
        int boardSize = getBoardSize();
        for (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);

        for (int rowIndex = 0; rowIndex < tableLayout.getChildCount(); rowIndex++) {
            View tableLayoutChild = tableLayout.getChildAt(rowIndex);
            if (tableLayoutChild instanceof TableRow) {
                for (int i = 0; i < ((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 the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a 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 to 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!  The application may be doing too much work on its main thread.
01-05 20:59:25.378 10380-10386/com.example.richardcurteis.connect3 W/art: Suspending all threads took: 49.644ms
01-05 20:59:51.722 10380-10380/com.example.richardcurteis.connect3 I/System.out: Random Integer Picked for use as index by AI: 6
01-05 20:59:51.722 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 6
01-05 20:59:51.722 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 6
01-05 20:59:51.722 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 6
01-05 20:59:51.722 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 6
01-05 20:59:51.722 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 6
01-05 20:59:51.722 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and 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 for use as index by AI: 5
01-05 21:00:00.751 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 5
01-05 21:00:00.752 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 5
01-05 21:00:00.752 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 5
01-05 21:00:00.752 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 5
01-05 21:00:00.752 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 5
01-05 21:00:00.752 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and 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 for use as index by AI: 0
01-05 21:00:05.129 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 0
01-05 21:00:05.129 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 0
01-05 21:00:05.129 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 0
01-05 21:00:05.129 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 0
01-05 21:00:05.129 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 0
01-05 21:00:05.129 10380-10380/com.example.richardcurteis.connect3 I/System.out: Progress 1 and targetButton class: class java.lang.String 0
3therk1ll
  • 2,056
  • 4
  • 35
  • 64

1 Answers1

0

as mentioned in the comments, instead of:

view.getTag() == targetButton

Use:

view.getTag().equals(targetButton)

This is a good link on the subject How do I compare strings in Java?

Community
  • 1
  • 1