I am trying to make a Tic Tac Toe inspired game in Android Studio and I am having trouble when it tries to check if there is a winner. I think it has to do with the method I created to check if there is a winner but I am not sure.. Here is the code:
我试图在Android Studio中制作一个Tic Tac Toe启发游戏,当我试图检查是否有赢家时我遇到了麻烦。我认为它与我创建的方法有关,以检查是否有赢家但我不确定..这是代码:
public class MainActivity extends ActionBarActivity {
String currentplayer = "player1";
TextView tilWins;
TextView ampWins;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Button button6;
Button button7;
Button button8;
Button button9;
Integer tWins;
Integer aWins;
public void checkWinner () {
String check1 = button1.toString();
String check2 = button2.toString();
String check3 = button3.toString();
String check4 = button4.toString();
String check5 = button5.toString();
String check6 = button6.toString();
String check7 = button7.toString();
String check8 = button8.toString();
String check9 = button9.toString();
if (check1.equals(check2) && check1.equals(check3)) {
if (check1.equals("~")) {
tilWins.setText(tWins + 1);
} else {
ampWins.setText(aWins + 1);
}
button1.setText("");
button2.setText("");
button3.setText("");
button4.setText("");
button5.setText("");
button6.setText("");
button7.setText("");
button8.setText("");
button9.setText("");
}
else if (check4.equals(check5) && check4.equals(check6)) {
if (check4.equals("~")) {
tilWins.setText(tWins + 1);
} else {
ampWins.setText(aWins + 1);
}
button1.setText("");
button2.setText("");
button3.setText("");
button4.setText("");
button5.setText("");
button6.setText("");
button7.setText("");
button8.setText("");
button9.setText("");
}
else if (check7.equals(check8) && check7.equals(check9)) {
if (check7.equals("~")) {
tilWins.setText(tWins + 1);
} else {
ampWins.setText(aWins + 1);
}
button1.setText("");
button2.setText("");
button3.setText("");
button4.setText("");
button5.setText("");
button6.setText("");
button7.setText("");
button8.setText("");
button9.setText("");
}
else if (check1.equals(check4) && check1.equals(check7)) {
if (check1.equals("~")) {
tilWins.setText(tWins + 1);
} else {
ampWins.setText(aWins + 1);
}
button1.setText("");
button2.setText("");
button3.setText("");
button4.setText("");
button5.setText("");
button6.setText("");
button7.setText("");
button8.setText("");
button9.setText("");
}
else if (check2.equals(check5) && check2.equals(check8)) {
if (check2.equals("~")) {
tilWins.setText(tWins + 1);
} else {
ampWins.setText(aWins + 1);
}
button1.setText("");
button2.setText("");
button3.setText("");
button4.setText("");
button5.setText("");
button6.setText("");
button7.setText("");
button8.setText("");
button9.setText("");
}
else if (check3.equals(check6) && check3.equals(check9)) {
if (check3.equals("~")) {
tilWins.setText(tWins + 1);
} else {
ampWins.setText(aWins + 1);
}
button1.setText("");
button2.setText("");
button3.setText("");
button4.setText("");
button5.setText("");
button6.setText("");
button7.setText("");
button8.setText("");
button9.setText("");
}
else if (check1.equals(check5) && check1.equals(check9)) {
if (check1.equals("~")) {
tilWins.setText(tWins + 1);
} else {
ampWins.setText(aWins + 1);
}
button1.setText("");
button2.setText("");
button3.setText("");
button4.setText("");
button5.setText("");
button6.setText("");
button7.setText("");
button8.setText("");
button9.setText("");
}
else if (check3.equals(check5) && check3.equals(check7)) {
if (check3.equals("~")) {
tilWins.setText(tWins + 1);
} else {
ampWins.setText(aWins + 1);
}
button1.setText("");
button2.setText("");
button3.setText("");
button4.setText("");
button5.setText("");
button6.setText("");
button7.setText("");
button8.setText("");
button9.setText("");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tilWins = (TextView)findViewById(R.id.tildeWins);
ampWins = (TextView)findViewById(R.id.ampersandWins);
String tiWins = tilWins.getText().toString();
String amWins = ampWins.getText().toString();
tWins = Integer.parseInt(String.valueOf(tiWins));
aWins = Integer.parseInt(String.valueOf(amWins));
button1 = (Button) findViewById(R.id.box1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (button1.getText() == "") {
if (currentplayer.equals("player1")) {
button1.setText("~");
} else {
button1.setText("&");
}
changePlayer(currentplayer);
checkWinner();
}
}
});
button2 = (Button) findViewById(R.id.box2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (button2.getText() == "") {
if (currentplayer.equals("player1")) {
button2.setText("~");
} else {
button2.setText("&");
}
changePlayer(currentplayer);
checkWinner();
}
}
});
button3 = (Button) findViewById(R.id.box3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (button3.getText() == "") {
if (currentplayer.equals("player1")) {
button3.setText("~");
} else {
button3.setText("&");
}
changePlayer(currentplayer);
checkWinner();
}
}
});
button4 = (Button) findViewById(R.id.box4);
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (button4.getText() == "") {
if (currentplayer.equals("player1")) {
button4.setText("~");
} else {
button4.setText("&");
}
changePlayer(currentplayer);
checkWinner();
}
}
});
button5 = (Button) findViewById(R.id.box5);
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (button5.getText() == "") {
if (currentplayer.equals("player1")) {
button5.setText("~");
} else {
button5.setText("&");
}
changePlayer(currentplayer);
checkWinner();
}
}
});
button6 = (Button) findViewById(R.id.box6);
button6.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (button6.getText() == "") {
if (currentplayer.equals("player1")) {
button6.setText("~");
} else {
button6.setText("&");
}
changePlayer(currentplayer);
checkWinner();
}
}
});
button7 = (Button) findViewById(R.id.box7);
button7.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (button7.getText() == "") {
if (currentplayer.equals("player1")) {
button7.setText("~");
} else {
button7.setText("&");
}
changePlayer(currentplayer);
checkWinner();
}
}
});
button8 = (Button) findViewById(R.id.box8);
button8.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (button8.getText() == "") {
if (currentplayer.equals("player1")) {
button8.setText("~");
} else {
button8.setText("&");
}
changePlayer(currentplayer);
checkWinner();
}
}
});
button9 = (Button) findViewById(R.id.box9);
button9.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (button9.getText() == "") {
if (currentplayer.equals("player1")) {
button9.setText("~");
} else {
button9.setText("&");
}
changePlayer(currentplayer);
checkWinner();
}
}
});
}
public void changePlayer (String player) {
if (player.equals("player1")) {
currentplayer = "player2";
} else {
currentplayer = "player1";
}
}
Here is the XML file if needed:
如果需要,这是XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/sJJ">
<GridLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/gridLayout"
android:useDefaultMargins="false"
android:transitionGroup="false"
android:touchscreenBlocksFocus="false"
android:rowOrderPreserved="false"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Tildes and Ampersands"
android:id="@+id/textView"
android:layout_row="0"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_marginLeft="55dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box1"
android:layout_row="1"
android:layout_column="0"
android:maxHeight="100dp"
android:maxLength="100"
android:maxWidth="100dp"
android:minHeight="100dp"
android:layout_marginLeft="40dp"
android:textSize="40dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box2"
android:layout_row="1"
android:layout_column="1"
android:maxHeight="100dp"
android:maxLength="100"
android:maxWidth="100dp"
android:minHeight="100dp"
android:textSize="40dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box3"
android:layout_row="1"
android:layout_column="2"
android:maxHeight="100dp"
android:maxLength="100"
android:maxWidth="100dp"
android:minHeight="100dp"
android:textSize="40dp"
android:layout_marginLeft="3dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box4"
android:layout_row="2"
android:layout_column="0"
android:maxHeight="100dp"
android:maxLength="100"
android:maxWidth="100dp"
android:minHeight="100dp"
android:layout_marginLeft="40dp"
android:textSize="40dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box5"
android:layout_row="2"
android:layout_column="1"
android:maxHeight="100dp"
android:maxLength="100"
android:maxWidth="100dp"
android:minHeight="100dp"
android:textSize="40dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box6"
android:layout_row="2"
android:layout_column="2"
android:maxHeight="100dp"
android:maxLength="100"
android:maxWidth="100dp"
android:minHeight="100dp"
android:textSize="40dp"
android:layout_marginLeft="3dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box7"
android:layout_row="3"
android:layout_column="0"
android:maxHeight="100dp"
android:maxLength="100"
android:maxWidth="100dp"
android:minHeight="100dp"
android:layout_marginLeft="40dp"
android:textSize="40dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box8"
android:layout_row="3"
android:layout_column="1"
android:maxHeight="100dp"
android:maxLength="100"
android:maxWidth="100dp"
android:minHeight="100dp"
android:textSize="40dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box9"
android:layout_row="3"
android:layout_column="2"
android:maxHeight="100dp"
android:maxLength="100"
android:maxWidth="100dp"
android:minHeight="100dp"
android:textSize="40dp"
android:layout_marginLeft="3dp" />
<TextView
android:layout_width="72dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" Wins"
android:id="@+id/textView2"
android:layout_row="4"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_marginLeft="125dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" Tildes:"
android:id="@+id/textView3"
android:layout_row="5"
android:layout_column="0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tildeWins"
android:layout_row="5"
android:layout_column="1"
android:text="0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Ampersands:"
android:id="@+id/textView4"
android:layout_row="6"
android:layout_column="0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/ampersandWins"
android:layout_row="6"
android:layout_column="1"
android:text="0" />
</GridLayout>
2 个解决方案
#1
if (check1.equals(check2) && check1.equals(check3)) {
if(check1.equals(check2)&& check1.equals(check3)){
So... If each row is either all player 1... or all player 2... or all EMPTY?
所以...如果每一行都是所有玩家1 ......或者所有玩家2 ......或者所有EMPTY?
button1.setText("");
Then clear the board regardless.
然后无论如何清除董事会。
After the first move, there IS going to be a row that is entirely empty (and thus, equal to itself), and the board will be immediately cleared. Also player 2 will win because player 1 doesn't, not on the merits of actually winning, making a 'cats game' impossible.
在第一次移动之后,将会有一行完全为空(因此,等于它自己),并且该板将立即被清除。玩家2也会赢,因为玩家1没有,而不是实际获胜的优点,使得'猫游戏'变得不可能。
I'm guessing this is not how you expected the game to operate? You could use more functions... and more arrays... and maybe a pattern or two. ;)
我猜这不是你对游戏运作的预期吗?你可以使用更多的函数......以及更多的数组......也许是一两个模式。 ;)
#2
Firstly, its really clumsy code... But, why are you doing
首先,它真的很笨拙的代码......但是,你为什么这么做
String check1 = button1.toString();
inside checkWinner()
??
里面的checkWinner()??
You must be interested in button1.getText().toString()
instead!
你必须对button1.getText()。toString()感兴趣!
#1
if (check1.equals(check2) && check1.equals(check3)) {
if(check1.equals(check2)&& check1.equals(check3)){
So... If each row is either all player 1... or all player 2... or all EMPTY?
所以...如果每一行都是所有玩家1 ......或者所有玩家2 ......或者所有EMPTY?
button1.setText("");
Then clear the board regardless.
然后无论如何清除董事会。
After the first move, there IS going to be a row that is entirely empty (and thus, equal to itself), and the board will be immediately cleared. Also player 2 will win because player 1 doesn't, not on the merits of actually winning, making a 'cats game' impossible.
在第一次移动之后,将会有一行完全为空(因此,等于它自己),并且该板将立即被清除。玩家2也会赢,因为玩家1没有,而不是实际获胜的优点,使得'猫游戏'变得不可能。
I'm guessing this is not how you expected the game to operate? You could use more functions... and more arrays... and maybe a pattern or two. ;)
我猜这不是你对游戏运作的预期吗?你可以使用更多的函数......以及更多的数组......也许是一两个模式。 ;)
#2
Firstly, its really clumsy code... But, why are you doing
首先,它真的很笨拙的代码......但是,你为什么这么做
String check1 = button1.toString();
inside checkWinner()
??
里面的checkWinner()??
You must be interested in button1.getText().toString()
instead!
你必须对button1.getText()。toString()感兴趣!