我是Android开发的新手,我想制作一个tic tac toe游戏。
如何改进此代码以为计算机播放器制作随机选项?我想在这个tic tac toe游戏中添加一个单一的玩家功能。
我希望如果黄色玩家点击,那么红色玩家也会自动播放而不点击。
这里的代码是: -
package com.osdeveloper.game;
import android.media.MediaActionSound;
import android.media.MediaPlayer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int Activeplayer = 0;
boolean gameisActive = true;
int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
int[][] winningPositions = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};
public void Dropin(View view) {
ImageView counter = (ImageView) view;
System.out.println(counter.getTag().toString());
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if (gameState[tappedCounter] == 2 && gameisActive) {
gameState[tappedCounter] = Activeplayer;
counter.setTranslationY(-1000f);
counter.setImageResource(R.drawable.yellow);
if (Activeplayer == 0) {
counter.setImageResource(R.drawable.yellow);
Activeplayer = 1;
} else {
counter.setImageResource(R.drawable.red);
Activeplayer = 0;
}
counter.animate().translationYBy(1000f).rotation(100).setDuration(300);
for (int[] winningposition : winningPositions) {
if (gameState[winningposition[0]] == gameState[winningposition[1]] &&
gameState[winningposition[1]] == gameState[winningposition[2]]
&& gameState[winningposition[0]] != 2) {
gameisActive = false;
String Winner = "red";
if (gameState[winningposition[0]] == 0) {
Winner = "yellow";
}
TextView TX = (TextView) findViewById(R.id.winmessage);
TX.setText(Winner + " has win ");
Toast.makeText(this,Winner +"Congratulation .",
Toast.LENGTH_SHORT).show();
LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
layout.setVisibility(View.VISIBLE);
} else {
boolean gameIsOver = true;
for (int CounterState : gameState) {
if (CounterState == 2) {
gameIsOver = false;
}
}
if (gameIsOver) {
TextView TX = (TextView) findViewById(R.id.winmessage);
TX.setText("its a draw :(");
LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
layout.setVisibility(View.VISIBLE);
}
}
}
}
}
public void playAgain(View view) {
gameisActive = true;
LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
layout.setVisibility(View.INVISIBLE);
Activeplayer = 0;
for (int i = 0; i < gameState.length; i++) {
gameState[i] = 2;
}
GridLayout grid = (GridLayout) findViewById(R.id.grid);
for (int i = 0; i < grid.getChildCount(); i++) {
((ImageView) grid.getChildAt(i)).setImageResource(0);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
final MediaPlayer Mplayer = MediaPlayer.create(this, R.raw.music2);
Mplayer.start();
Button btn = (Button) findViewById(R.id.mutebtn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Mplayer.stop();
Mplayer.start();
}
});
}
private Boolean exit = false;
@Override
public void onBackPressed() {
if (exit) {
finish(); // finish activity
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
}
答案 0 :(得分:0)
据我了解你的问题,黄色玩家是玩游戏的玩家。红色播放器是汽车播放器。如果这是正确的,那么你可以通过回调方法实现这一目标。一旦黄色玩家轮到他(你必须要打任何方法来打转),在这个方法结束时给回叫红色玩家的召唤方法转为自动播放。(在回叫方法中你将调用的方法是红色球员的转身)。希望对你有所帮助。正如您所说,您是新的Android开发人员,对于回拨实施帮助您可以参考下面的帮助完整链接,
https://stackoverflow.com/questions/3398363/how-to-define-callbacks-in-android