gpt4 book ai didi

java - TicTacToe 如何计算胜利,反击 - 需要帮助

转载 作者:行者123 更新时间:2023-11-29 19:47:51 24 4
gpt4 key购买 nike

我卡住了。我需要的是反赢值(value)观。我通过本教程做了 tictactoe:http://hubpages.com/technology/tictactoe .我已经完成了,现在我想自己添加一些东西,它只是计算某人获胜的次数。

//if mWinner = CIRCLE
//increase @+id/cross_wins by 1
//if mWinner = CROSS
//increase @+id/circles_wins by 1
//else increase @+id/draws by 1

这就是我想要的,你能告诉我如何正确编码吗?

I want the "0" on this to change, if crosses,circles wins the game or it's a draw, just to tick +1

package com.example.jkaminski.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class GameActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
makeScreen();
initialize();
}

private void makeScreen() {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
getSupportActionBar().hide();
}

private ImageView[] mBlocks = new ImageView[9];
private TextView mDisplay;
private LinearLayout mExit, mReplay;

private enum TURN {CIRCLE, CROSS}

private TURN mTurn;
private int mExitCounter = 0;
private int mStatusCounter = 0;

private void initialize() {
mExit = (LinearLayout) findViewById(R.id.exit);
mExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mExitCounter == 1) {
finish();
System.exit(0);
} else {
mExitCounter++;
Toast.makeText(getApplicationContext(), "Naciśnij ponownie aby wyjść", Toast.LENGTH_SHORT).show();
}
}
});
mDisplay = (TextView) findViewById(R.id.display_board);
mReplay = (LinearLayout) findViewById(R.id.replay);
mReplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent starter = getIntent();
finish();
starter.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(starter);
}
});
for (int position = 0; position < 9; position++) {
int resId = getResources().getIdentifier("block_" + (position + 1), "id", getPackageName());
mBlocks[position] = (ImageView) findViewById(resId);
final int finalPosition = position;
mBlocks[position].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switchTurn(finalPosition);
}
});

}
}

private void switchTurn(int position) {
if (mTurn == TURN.CIRCLE) {
mBlocks[position].setImageResource(R.drawable.circle);
mBlocks[position].setId(GameLogic.CIRCLE);
mTurn = TURN.CROSS;
mDisplay.setText("teraz KRZYŻYK");
} else {
mBlocks[position].setImageResource(R.drawable.cross);
mBlocks[position].setId(GameLogic.CROSS);
mTurn = TURN.CIRCLE;
mDisplay.setText("teraz KÓŁKO");
}
mBlocks[position].setEnabled(false);
mStatusCounter++;
if (GameLogic.isCompleted(position + 1, mBlocks)) {
mDisplay.setText(GameLogic.sWinner + " zostaje zwycięzcą!");
displayStick(GameLogic.sSet);
disableAll();
} else if (mStatusCounter == 9) {
mDisplay.setText("REMIS. Spróbuj ponownie ;)");
}
}

private void displayStick(int stick) {
View view;
switch (stick) {
case 1:
view = findViewById(R.id.top_horizontal);
break;
case 2:
view = findViewById(R.id.center_horizontal);
break;
case 3:
view = findViewById(R.id.bottom_horizontal);
break;
case 4:
view = findViewById(R.id.left_vertical);
break;
case 5:
view = findViewById(R.id.center_vertical);
break;
case 6:
view = findViewById(R.id.right_vertical);
break;
case 7:
view = findViewById(R.id.left_right_diagonal);
break;
case 8:
view = findViewById(R.id.right_left_diagonal);
break;
default://which will never happen
view = findViewById(R.id.top_horizontal);
}
view.setVisibility(View.VISIBLE);
}

private void disableAll() {
for (int i = 0; i < 9; i++)
mBlocks[i].setEnabled(false);
}

@Override
public void onBackPressed() {

}

private void showResults() {
if (GameLogic.mWinner == GameLogic.CIRCLE) {

} else if (GameLogic.mWinner == GameLogic.CROSS) {

} else {

}
}

package com.example.jkaminski.myapplication;

import android.widget.ImageView;

public class GameLogic {

private static ImageView[] sBlocks;
public static String sWinner;
public static int sSet;

public static final int CIRCLE = 0;
public static final int CROSS = 1;

public static int mWinner;


private static boolean areSameInSet(int first, int second, int third, int set) {
boolean value = sBlocks[first - 1].getId() == sBlocks[second - 1].getId() && sBlocks[second - 1].getId() == sBlocks[third - 1].getId();
if (value) {
if (sBlocks[first - 1].getId() == CIRCLE) {
mWinner = CIRCLE;
sWinner = "KÓŁKO";
} else {
mWinner = CROSS;
sWinner = "KRZYŻYK";
}
sSet = set;
}
return value;
}


public static boolean isCompleted(int position, ImageView[] blocks) {
GameLogic.sBlocks = blocks;
boolean isComplete = false;
switch (position) {
case 1:
isComplete = areSameInSet(1, 2, 3, 1) ||
areSameInSet(1, 4, 7, 4) ||
areSameInSet(1, 5, 9, 7);
break;
case 2:
isComplete = areSameInSet(1, 2, 3, 1) ||
areSameInSet(2, 5, 8, 5);
break;
case 3:
isComplete = areSameInSet(1, 2, 3, 1) ||
areSameInSet(3, 6, 9, 6) ||
areSameInSet(3, 5, 7, 8);
break;
case 4:
isComplete = areSameInSet(4, 5, 6, 2) ||
areSameInSet(1, 4, 7, 4);
break;
case 5:
isComplete = areSameInSet(4, 5, 6, 2) ||
areSameInSet(2, 5, 8, 5) ||
areSameInSet(1, 5, 9, 7) ||
areSameInSet(3, 5, 7, 8);
break;
case 6:
isComplete = areSameInSet(4, 5, 6, 2) ||
areSameInSet(3, 6, 9, 6);
break;
case 7:
isComplete = areSameInSet(7, 8, 9, 3) ||
areSameInSet(1, 4, 7, 4) ||
areSameInSet(3, 5, 7, 8);
break;
case 8:
isComplete = areSameInSet(7, 8, 9, 3) ||
areSameInSet(2, 5, 8, 5);
break;
case 9:
isComplete = areSameInSet(7, 8, 9, 3) ||
areSameInSet(3, 6, 9, 6) ||
areSameInSet(1, 5, 9, 7);
break;
}
return isComplete;
}

最佳答案

我猜你可以使用静态类变量来记录这样的计数器。就在游戏结束的时候,+1 相关变量。

关于java - TicTacToe 如何计算胜利,反击 - 需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37420681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com