我正在制作一个tic tac toe android应用程序,我希望得分保持。我有一切工作,除了增量。出于某种原因,它不会上升1,而是上升2。
这是我在增加分数并检查游戏以查看是否有赢家时使用的方法:
private Button one;
private Button two;
private Button three;
private Button four;
private Button five;
private Button six;
private Button seven;
private Button eight;
private Button nine;
private TextView winner;
private Button clearBoard;
private int scoreX;
private int scoreY;
private int tie;
private boolean turn = false; //determines who's turn it is
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (Button) findViewById(R.id.box1);
one.setText("-");
two = (Button) findViewById(R.id.box2);
two.setText("-");
three = (Button) findViewById(R.id.box3);
three.setText("-");
four = (Button) findViewById(R.id.box4);
four.setText("-");
five = (Button) findViewById(R.id.box5);
five.setText("-");
six = (Button) findViewById(R.id.box6);
six.setText("-");
seven = (Button) findViewById(R.id.box7);
seven.setText("-");
eight = (Button) findViewById(R.id.box8);
eight.setText("-");
nine = (Button) findViewById(R.id.box9);
nine.setText("-");
scoreX = 0;
scoreY = 0;
tie = 0;
winner = (TextView) findViewById(R.id.win);
clearBoard = (Button) findViewById(R.id.newgame);
clearBoard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
one.setText("-");
one.setEnabled(true);
two.setText("-");
two.setEnabled(true);
three.setText("-");
three.setEnabled(true);
four.setText("-");
four.setEnabled(true);
five.setText("-");
five.setEnabled(true);
six.setText("-");
six.setEnabled(true);
seven.setText("-");
seven.setEnabled(true);
eight.setText("-");
eight.setEnabled(true);
nine.setText("-");
nine.setEnabled(true);
}
});
}
public void game(View view) {
Button b = (Button) view;
moves(b);
}
public void moves(Button b){
if(turn == false){
b.setText("X");
b.setTextSize(15);
} else {
b.setText("O");
b.setTextSize(15);
}
//once X or O is placed, cannot reuse that button till new game
b.setEnabled(false);
b.setTextSize(15);
turn = !turn;
check();
//if there is a winner, make it so buttons can't be pressed and new game must start
if(check() == true){
one.setEnabled(false);
two.setEnabled(false);
three.setEnabled(false);
four.setEnabled(false);
five.setEnabled(false);
six.setEnabled(false);
seven.setEnabled(false);
eight.setEnabled(false);
nine.setEnabled(false);
}
}
public boolean check() {
boolean finished = true;
int player = 1; //1 is player X, 0 is player O, 2 is a tie
//horizontal check
if((!one.getText().equals("-")) && one.getText().toString().equals(two.getText().toString()) && two.getText().toString().equals(three.getText().toString())) {
if (one.getText().equals("O")) {
player = 0;
}
} else if((!four.getText().equals("-")) && four.getText().toString().equals(five.getText().toString()) && five.getText().toString().equals(six.getText().toString())){
if (one.getText().equals("O")) {
player = 0;
}
} else if((!seven.getText().equals("-")) && seven.getText().toString().equals(eight.getText().toString()) && eight.getText().toString().equals(nine.getText().toString())){
if (one.getText().equals("O")) {
player = 0;
}
} else if((!one.getText().equals("-")) && one.getText().toString().equals(four.getText().toString()) && four.getText().toString().equals(seven.getText().toString())){
//vertical check
if (one.getText().equals("O")) {
player = 0;
}
} else if((!two.getText().equals("-")) && two.getText().toString().equals(five.getText().toString()) && five.getText().toString().equals(eight.getText().toString())){
if (one.getText().equals("O")) {
player = 0;
}
} else if((!three.getText().equals("-")) && three.getText().toString().equals(six.getText().toString()) && six.getText().toString().equals(nine.getText().toString())){
if (one.getText().equals("O")) {
player = 0;
}
} else if((!one.getText().equals("-")) && one.getText().toString().equals(five.getText().toString()) && five.getText().toString().equals(nine.getText().toString())){
//diagonal check
if (one.getText().equals("O")) {
player = 0;
}
} else if((!three.getText().equals("-")) && three.getText().toString().equals(five.getText().toString()) && five.getText().toString().equals(seven.getText().toString())){
if (one.getText().equals("O")) {
player = 0;
}
} else if ((!one.getText().equals("-")) && (!two.getText().equals("-")) && (!three.getText().equals("-")) && (!four.getText().equals("-")) && (!five.getText().equals("-"))
&& (!six.getText().equals("-")) && (!seven.getText().equals("-")) && (!eight.getText().equals("-")) && (!nine.getText().equals("-"))){
player = 2;
} else {
return finished = false;
}
if(player == 1){
scoreX = scoreX + 1;
winner.setText(String.format("Player X is the winner!\nGames won by player X: %d\nGames won by player O: %d\nNumber of ties: %d", scoreX, scoreY, tie));
} else if (player == 0){
scoreY = scoreY + 1;
winner.setText(String.format("Player O is the winner!\nGames won by player X: %d\nGames won by player O: %d\nNumber of ties: %d", scoreX, scoreY, tie));
} else if(player == 2){
tie= tie + 1;
winner.setText(String.format("It's a tie!\nGames won by player X: %d\nGames won by player O: %d\nNumber of ties: %d", scoreX, scoreY, tie));
}
return finished;
}
}
我的变量,scoreX,scoreY和tie都是成员变量并设置为0.我必须遗漏一些东西,因为我无法看到我的变量如何递增2而不是1.请帮忙!
编辑:我知道编码不是很好。我还是个初学者。我添加了所有代码。我知道特别是我的check()方法很长,但是当我检查游戏板上的按钮时,我不确定如何缩短它的长度。答案 0 :(得分:0)
之所以发生这种情况是因为您要两次调用检查功能: 我无法真正给你行号,但这就是它发生的地方(在移动函数中):
// Button Constructor
map.Control = function(controlDiv, text, func) {
// Set CSS for the control border.
var controlUI = document.createElement('div');
controlUI.style.backgroundColor = '#ed5929';
controlUI.style.border = '2px solid #ed5929';
controlUI.style.borderRadius = '3px';
controlUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
controlUI.style.cursor = 'pointer';
controlUI.style.marginBottom = '22px';
controlUI.style.marginRight = '22px';
controlUI.style.marginTop = '22px';
controlUI.style.textAlign = 'center';
controlUI.title = 'Click to toggle markers';
controlDiv.appendChild(controlUI);
// Set CSS for the control interior.
var controlText = document.createElement('div');
controlText.style.color = 'rgb(255,255,255)';
controlText.style.fontWeight = 'bold';
controlText.style.fontFamily = 'Roboto,Arial,sans-serif';
controlText.style.fontSize = '16px';
controlText.style.lineHeight = '38px';
controlText.style.paddingLeft = '5px';
controlText.style.paddingRight = '5px';
controlText.innerHTML = text;
controlUI.appendChild(controlText);
controlUI.addEventListener('click', func);
};
您应该做的是保存您第一次检查的结果,然后使用:
check();
//if there is a winner, make it so buttons can't be pressed and new game must start
if(check() == true){