TicTacToe如何计算胜利,需要反击

时间:2016-05-24 17:57:30

标签: java android

我被困住了。我需要的是赢得价值的反击。 我通过本教程做了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;
}

}

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

游戏结束后,您需要增加一些计数器。你有很多选择。您可以像其他人建议的那样使用static变量:

private static int numWins = 0;

...

if (GameLogic.isCompleted(position + 1, mBlocks)) { 
    numWins++;
    ....

但我的建议是使用SharedPreferences来保持价值。您将需要用于保存和加载计数器的代码。

public static int loadNumWins( Context context )
{
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    return prefs.getInt("numWins", 0);
}

public static void saveNumWins( Context context, int value )
{
    SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();
    prefs.putInt("numWins", value);
    prefs.commit();
}

然后使用计数器代码...

if (GameLogic.isCompleted(position + 1, mBlocks)) 
{ 
    int numWins = loadNumWins(this);   // load value from prefs
    numWins++;                         // increment it
    saveNumWins(this, numWins);        // save it back to prefs
    ....

答案 2 :(得分:0)

我认为你能做到这一点的最快方法是在课堂上添加一些静态计数器变量,然后在确定获胜者时递增它们并更新屏幕。