我正在做一个简单的井字游戏,但有一个相当大的问题。基本上,当一行中有任意3个符号时(不一定是3个o或3个x,只要是任意3个,游戏就认为有人赢了)。我知道这是我的checkwin方法的一个问题,但我不太清楚是什么。
需要注意的是,我将我的按钮命名为1-10,而不是0-9,这在事后看来是个坏主意,因为数组从0开始。我想这可能是我的问题所在。
以下是我到目前为止的应用程序截图。
非常感谢,
卢克
package com.example.tictactoe;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView PlayerOneScore, PlayerTwoScore, winStatus;
private Button [] mButtons = new Button[10];
private Button reset;
private int PlayerOneScoreCount, PlayerTwoScoreCount, roundCount;
boolean PlayerTurn;
int [] gameState = {2,2,2,2,2,2,2,2,2,2};
int [][] winStates = {
{1,2,3},{4,5,6},{7,8,9},
{1,4,7},{2,5,8},{3,6,9},
{1,5,9},{3,5,7}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PlayerOneScore = (TextView)findViewById(R.id.P1Score);
PlayerTwoScore = (TextView)findViewById(R.id.P2Score);
winStatus = (TextView)findViewById(R.id.winStatus);
reset = (Button) findViewById(R.id.reset);
for (int i=1; i < mButtons.length; i++){
String buttonID = "btn" + i;
int resourceID = getResources().getIdentifier(buttonID, "id", getPackageName());
mButtons[i] = (Button) findViewById(resourceID);
mButtons[i].setOnClickListener(this);
}
roundCount = 0;
PlayerOneScoreCount = 0;
PlayerTwoScoreCount = 0;
PlayerTurn = true;
}
@Override
public void onClick(View v) {
if (!((Button)v).getText().toString().equals("")){
return;
}
String buttonID = v.getResources().getResourceEntryName(v.getId());
int gameStatePointer = Integer.parseInt(buttonID.substring
(buttonID.length()-1, buttonID.length()));
if (PlayerTurn){
((Button)v).setText("X");
((Button)v).setTextColor(Color.parseColor("#545454"));
gameState[gameStatePointer] = 0;
}else {
((Button)v).setText("O");
((Button)v).setTextColor(Color.parseColor("#F2EBD3"));
gameState[gameStatePointer] = 0;
}
roundCount++;
if(checkWin()){
if(PlayerTurn){
PlayerOneScoreCount++;
updatePlayerScore();
Toast.makeText(this, "Player One Won!", Toast.LENGTH_SHORT).show();
playAgain();
}else {
PlayerTwoScoreCount++;
updatePlayerScore();
Toast.makeText(this, "Player Two Won!", Toast.LENGTH_SHORT).show();
playAgain();
}
}else if(roundCount == 9){
playAgain();
Toast.makeText(this, "It's a Draw!", Toast.LENGTH_SHORT).show();
}else {
PlayerTurn = !PlayerTurn;
}
if (PlayerOneScoreCount > PlayerTwoScoreCount){
winStatus.setText("Player One is Winning!");
}else if (PlayerTwoScoreCount > PlayerOneScoreCount){
winStatus.setText("Player Two is Winning!");
}else {
winStatus.setText("");
}
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playAgain();
PlayerOneScoreCount = 0;
PlayerTwoScoreCount = 0;
winStatus.setText("");
updatePlayerScore();
}
});
}
public boolean checkWin(){
boolean Winner = false;
for (int [] winningState : winStates){
if (gameState[winningState[0]] == gameState[winningState[1]] &&
gameState[winningState[1]] == gameState[winningState[2]] &&
gameState[winningState[0]]!=2){
Winner = true;
}
}
return Winner;
}
public void updatePlayerScore(){
PlayerOneScore.setText(Integer.toString(PlayerOneScoreCount));
PlayerTwoScore.setText(Integer.toString(PlayerTwoScoreCount));
}
public void playAgain(){
roundCount = 0;
PlayerTurn = true;
for (int i = 1; i < mButtons.length; i++){
gameState[i] = 2;
mButtons[i].setText("");
}
}
}
2条答案
按热度按时间pprl5pva1#
在
checkWin()
你反复浏览各个获胜的州,通过比较所有的州,看看是否有玩家赢了Button
“属于”某一特定获胜州的。因为如果
Button
已被点击,无论轮到谁,你都会有一个“赢家”尽快所有Button
已单击属于某个获胜状态的。热释光;您可以通过在中设置不同的值来解决此问题
onClick()
取决于轮到谁了:kninwzqo2#
我不太确定,但我想可能是你在游戏状态下设定的值。据我所知,在onclick方法中总是将其设置为0
然后当你检查游戏状态中的值是否相等时,点击的每个字段中都有一个0,所以它们相等。你可能想把它们中的一个设成另一个整数