Java android Studio如何绘制游戏?
我正在尝试创建一个Tic-Tac-Toe游戏。一切正常,如果球员 x或O赢了一条消息显示x或O赢了这场比赛,但问题是当没有赢家时,我应该收到一条消息说“没有赢家,再试一次”。如果你能帮我,你就是我的一天Java android Studio如何绘制游戏?,java,android,Java,Android,我正在尝试创建一个Tic-Tac-Toe游戏。一切正常,如果球员 x或O赢了一条消息显示x或O赢了这场比赛,但问题是当没有赢家时,我应该收到一条消息说“没有赢家,再试一次”。如果你能帮我,你就是我的一天 int activePlayer = 0; // for x player int[] gameState ={2,2,2,2,2,2,2,2,2}; // 2 means unplayed. int[][] winningLocation ={{0,1,2},{3,4,5},{6,7,8}
int activePlayer = 0; // for x player
int[] gameState ={2,2,2,2,2,2,2,2,2}; // 2 means unplayed.
int[][] winningLocation ={{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
boolean gameover =false;
public void gameLogic(View view){
ImageView tappedView =(ImageView) view;
int tappedLocation = Integer.parseInt(view.getTag().toString());
if(gameState[tappedLocation]==2 && !gameover) {
gameState[tappedLocation]=activePlayer;
tappedView.setTranslationY(-3000f);
if (activePlayer == 0) {
tappedView.setImageResource(R.drawable.x);
activePlayer = 1;
} else if (activePlayer == 1) {
tappedView.setImageResource(R.drawable.o);
activePlayer = 0;
}
tappedView.animate().translationYBy(3000f).setDuration(500);
}
String mesg ="";
for(int[]winningPostion :winningLocation){
if(gameState[winningPostion[0]] == gameState [winningPostion[1]]
&& gameState[winningPostion[1]]== gameState [winningPostion[2]]
&& gameState[winningPostion[0]]!=2){
if (activePlayer ==0)
mesg = "O is the winner!";
if(activePlayer==1)
mesg = "X is the winner!";
else if(activePlayer==2)
mesg="there is no winner";
LinearLayout winnerLayout =(LinearLayout)findViewById (R.id.winnerLayout);
winnerLayout.setVisibility(View .VISIBLE);
TextView winnermesg = (TextView) findViewById(R.id.editText);
winnermesg.setText(mesg);
gameover=true;
}
}
}
// a method that let the players play again
public void playagain(View view){
LinearLayout winnerLayout = (LinearLayout) findViewById(R.id.winnerLayout);
winnerLayout.setVisibility(View.INVISIBLE);
gameover=false;
activePlayer=0;
for (int i =0; i < gameState.length;i++)
gameState[i]=2;
GridLayout gridlayout = (GridLayout) findViewById(R.id.gridlayout);
for(int i =0 ; i < gridlayout.getChildCount(); i++)
((ImageView)gridlayout.getChildAt(i)).setImageResource(0);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to play the song
mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.song);
mediaPlayer.start();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// to show and hide the playing again button
LinearLayout winnerLayout = ( LinearLayout) findViewById(R.id.winnerLayout);
winnerLayout.setVisibility(View.INVISIBLE);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
int-activePlayer=0;//对于x播放器
int[]游戏状态={2,2,2,2,2,2,2,2};//2表示未播放。
int[]winningLocation={0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6};
布尔gameover=false;
公共void游戏逻辑(视图){
ImageView tappedView=(ImageView)视图;
int-tappedLocation=Integer.parseInt(view.getTag().toString());
如果(游戏状态[tappedLocation]==2&&!游戏结束){
游戏状态[tappedLocation]=活动玩家;
tappedView.setTranslationY(-3000f);
如果(activePlayer==0){
tappedView.setImageResource(R.drawable.x);
activePlayer=1;
}else if(activePlayer==1){
tappedView.setImageResource(R.drawable.o);
activePlayer=0;
}
tappedView.animate().translationBy(3000f).setDuration(500);
}
字符串mesg=“”;
对于(int[]WinningPosition:winningLocation){
如果(游戏状态[winningPostion[0]]==游戏状态[winningPostion[1]]
&&游戏状态[winningPostion[1]]==游戏状态[winningPostion[2]]
&&游戏状态[winningPostion[0]!=2){
如果(activePlayer==0)
mesg=“O是赢家!”;
如果(activePlayer==1)
mesg=“X是赢家!”;
else if(activePlayer==2)
mesg=“没有赢家”;
LinearLayout winnerLayout=(LinearLayout)findViewById(R.id.winnerLayout);
winnerLayout.setVisibility(View.VISIBLE);
TextView Winnermasg=(TextView)findViewById(R.id.editText);
winnermesg.setText(mesg);
gameover=true;
}
}
}
//一种让玩家再次玩的方法
再次播放公共void(视图){
LinearLayout winnerLayout=(LinearLayout)findViewById(R.id.winnerLayout);
winnerLayout.setVisibility(视图.不可见);
gameover=false;
activePlayer=0;
对于(inti=0;i
我想你还没有学会如何调试你的应用程序,否则你可以一步一步地调试代码,准确地知道变量值是什么,并找出错误所在。感谢你抽出时间给我回信,我确实使用了调试,但我不太明白我真的很坚持,我对android studio很陌生。