你好,我是Android编程新手,我需要在Tic tac toe Game中提供一些帮助 我制作非常简单的应用程序,只使用Button这里是代码请看看
XML
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonMainActivitybtn1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="76dp"
android:layout_alignRight="@+id/buttonMainActivitybtn4"
android:layout_alignEnd="@+id/buttonMainActivitybtn4"
android:background="#09851a"
android:textColor="#ffffff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonMainActivitybtn2"
android:layout_above="@+id/buttonMainActivitybtn4"
android:layout_centerHorizontal="true"
android:background="#09851a"
android:textColor="#ffffff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonMainActivitybtn3"
android:layout_alignTop="@+id/buttonMainActivitybtn2"
android:layout_toRightOf="@+id/buttonMainActivitybtn2"
android:layout_toEndOf="@+id/buttonMainActivitybtn2"
android:layout_marginLeft="45dp"
android:layout_marginStart="45dp"
android:background="#09851a"
android:textColor="#ffffff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonMainActivitybtn4"
android:layout_marginTop="41dp"
android:layout_below="@+id/buttonMainActivitybtn1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#09851a"
android:textColor="#ffffff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonMainActivitybtn5"
android:layout_above="@+id/buttonMainActivitybtn7"
android:layout_toLeftOf="@+id/buttonMainActivitybtn3"
android:layout_toStartOf="@+id/buttonMainActivitybtn3"
android:background="#09851a"
android:textColor="#ffffff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonMainActivitybtn6"
android:layout_alignTop="@+id/buttonMainActivitybtn5"
android:layout_alignLeft="@+id/buttonMainActivitybtn3"
android:layout_alignStart="@+id/buttonMainActivitybtn3"
android:background="#09851a"
android:textColor="#ffffff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonMainActivitybtn7"
android:layout_marginTop="39dp"
android:layout_below="@+id/buttonMainActivitybtn4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#09851a"
android:textColor="#ffffff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonMainActivitybtn8"
android:layout_alignBaseline="@+id/buttonMainActivitybtn7"
android:layout_alignBottom="@+id/buttonMainActivitybtn7"
android:layout_alignLeft="@+id/buttonMainActivitybtn5"
android:layout_alignStart="@+id/buttonMainActivitybtn5"
android:background="#09851a"
android:textColor="#ffffff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonMainActivitybtn9"
android:layout_alignTop="@+id/buttonMainActivitybtn8"
android:layout_alignLeft="@+id/buttonMainActivitybtn6"
android:layout_alignStart="@+id/buttonMainActivitybtn6"
android:background="#09851a"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Tic Tac Toe Game Day 3"
android:id="@+id/textViewMainActivityTitle"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
现在我只是应用One Condition我知道它是非常转储编程 但我只想使用按钮可以任何人指导我这个:) thankyou
package com.example.awais_pc.tictactoe;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
Boolean p1=true, p2=true;
String player1= "X";
String player2 = "O";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.buttonMainActivitybtn1);
btn2 = (Button) findViewById(R.id.buttonMainActivitybtn2);
btn3 = (Button) findViewById(R.id.buttonMainActivitybtn3);
btn4 = (Button) findViewById(R.id.buttonMainActivitybtn4);
btn5 = (Button) findViewById(R.id.buttonMainActivitybtn5);
btn6 = (Button) findViewById(R.id.buttonMainActivitybtn6);
btn7 = (Button) findViewById(R.id.buttonMainActivitybtn7);
btn8 = (Button) findViewById(R.id.buttonMainActivitybtn8);
btn9 = (Button) findViewById(R.id.buttonMainActivitybtn9);
final String b1 = (String) btn1.getText();
final String b2 = (String) btn2.getText();
final String b3 = (String) btn3.getText();
final String b4 = (String) btn4.getText();
final String b5 = (String) btn5.getText();
final String b6 = (String) btn6.getText();
final String b7 = (String) btn7.getText();
final String b8 = (String) btn8.getText();
final String b9 = (String) btn9.getText();
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(b1.trim().isEmpty()){
if(p1 == true){
btn1.setText(player1);
p1 = false;
p2 = true;
btn1.setEnabled(false);
}
else if(p2 == true){
btn1.setText(player2);
p1 = true;
p2 = false;
btn1.setEnabled(false);
}
}
}
}); // End Button One
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(b2.trim().isEmpty()){
if(p1 == true){
btn2.setText(player1);
p1 = false;
p2 = true;
btn2.setEnabled(false);
}
else if(p2 == true){
btn2.setText(player2);
p1 = true;
p2 = false;
btn2.setEnabled(false);
}
}
}
}); // end button two
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(b3.trim().isEmpty()){
if(p1 == true){
btn3.setText(player1);
p1 = false;
p2 = true;
btn3.setEnabled(false);
}
else if(p2 == true){
btn3.setText(player2);
p1 = true;
p2 = false;
btn3.setEnabled(false);
}
}
}
}); // end button Three
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(b4.trim().isEmpty()){
if(p1 == true){
btn4.setText(player1);
p1 = false;
p2 = true;
btn4.setEnabled(false);
}
else if(p2 == true){
btn4.setText(player2);
p1 = true;
p2 = false;
btn4.setEnabled(false);
}
}
}
}); // end button Four
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String b5 = btn5.getText().toString();
if(b5.trim().isEmpty()){
if(p1 == true){
btn5.setText(player1);
p1 = false;
p2 = true;
btn5.setEnabled(false);
}
else if(p2 == true){
btn5.setText(player2);
p1 = true;
p2 = false;
btn5.setEnabled(false);
}
}
}
}); // end button Five
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(b6.trim().isEmpty()){
if(p1 == true){
btn6.setText(player1);
p1 = false;
p2 = true;
btn6.setEnabled(false);
}
else if(p2 == true){
btn6.setText(player2);
p1 = true;
p2 = false;
btn6.setEnabled(false);
}
}
}
}); // end button Six
btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(b7.trim().isEmpty()){
if(p1 == true){
btn7.setText(player1);
p1 = false;
p2 = true;
btn7.setEnabled(false);
}
else if(p2 == true){
btn7.setText(player2);
p1 = true;
p2 = false;
btn7.setEnabled(false);
}
}
}
}); // end button Seven
btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(b8.trim().isEmpty()){
if(p1 == true){
btn8.setText(player1);
p1 = false;
p2 = true;
btn8.setEnabled(false);
}
else if(p2 == true){
btn8.setText(player2);
p1 = true;
p2 = false;
btn8.setEnabled(false);
}
}
}
}); // end button Eight
btn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(b9.trim().isEmpty()){
if(p1 == true){
btn9.setText(player1);
p1 = false;
p2 = true;
btn9.setEnabled(false);
}
else if(p2 == true){
btn9.setText(player2);
p1 = true;
p2 = false;
btn9.setEnabled(false);
}
}
}
}); // end button Nine
// WInning Condition But it is not working please look
if(b1.equalsIgnoreCase("X")&&
b2.equalsIgnoreCase("X")&&
b3.equalsIgnoreCase("X")){
Toast.makeText(this,"Player One Win The Game",Toast.LENGTH_SHORT);
}
}//end on create
}
答案 0 :(得分:0)
您已将Strings
声明为final
并将其设置为onCreate()
,但在点击它们之前,它们没有指定文字。所以你不能比较任何东西。相反,请在设置onClickListener()
文字后在button
中指定相应的文字。
btn1.setText(player1);
b1 = btn1.getText().toString();