2014-02-25 69 views
1

我遇到了簡單的牛軋糖問題&十字架遊戲我嘗試設置一個獲勝條件,它會顯示一條消息,聲明你已經贏得了比賽並禁用了所有其他的位置/禁區,但是我的舉動很奇怪,例如,如果你試圖通過位置1,4,7贏得比賽,當你點擊位置1時& 4它已顯示消息並禁用所有按鈕。這是下面的代碼:不存在與十字架/井字遊戲贏得條件並在Java/Android中清除遊戲(長帖)

活動:

package com.example.noughtsandcrosses; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 


public class NoughtsAndCrossesXActivity extends Activity implements OnClickListener { 
    Button[] buttons = new Button[10]; 
    int[] squares = new int[10]; 
    Button newGame; 
    Button exitGame; 
    TextView WinGame; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.noughts_and_croses);  

     buttons[1] = (Button) findViewById(R.id.one); 
     buttons[2] = (Button) findViewById(R.id.two); 
     buttons[3] = (Button) findViewById(R.id.three); 
     buttons[4] = (Button) findViewById(R.id.four); 
     buttons[5] = (Button) findViewById(R.id.five); 
     buttons[6] = (Button) findViewById(R.id.six); 
     buttons[7] = (Button) findViewById(R.id.seven); 
     buttons[8] = (Button) findViewById(R.id.eight); 
     buttons[9] = (Button) findViewById(R.id.nine); 
     newGame = (Button) findViewById(R.id.new_game); 
     newGame.setOnClickListener(this); 
     WinGame = (TextView) findViewById(R.id.win_game); 
     exitGame = (Button) findViewById(R.id.exit_game); 
     exitGame.setOnClickListener(this); 


     for (int i = 1; i <= 9; i++){ 
      buttons[i].setOnClickListener(this);  
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.login_page, menu); 
     return true; 
    } 

    public void clearGame(){ 
     for (int i = 1; i <= 9; i++){ 
      buttons[i].setText(""); 
      buttons[i].setEnabled(true); 
      squares[i] = 1; 
     } 
     WinGame.setText(""); 
    } 

    /*public void exitGameToMenu(){ 
     Intent a = new Intent(getApplicationContext(),MenuActivity.class); 
     a.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
     startActivity(a); 
    }*/ 

    @Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
     case R.id.one: 
      makeMove(1); 
      respond(); 
      break; 
     case R.id.two: 
      makeMove(2); 
      respond(); 
      break; 
     case R.id.three: 
      makeMove(3); 
      respond(); 
      break; 
     case R.id.four: 
      makeMove(4); 
      respond(); 
      break; 
     case R.id.five: 
      makeMove(5); 
      respond(); 
      break; 
     case R.id.six: 
      makeMove(6); 
      respond(); 
      break; 
     case R.id.seven: 
      makeMove(7); 
      respond(); 
      break; 
     case R.id.eight: 
      makeMove(8); 
      respond(); 
      break; 
     case R.id.nine: 
      makeMove(9); 
      respond(); 
      break; 
     case R.id.new_game: 
      System.out.println("newgame"); 
      clearGame(); 
      break; 
     case R.id.exit_game: 
      System.out.println("exitgame"); 
      //exitGameToMenu(); 
      Intent a = new Intent (this, MenuActivity.class); 
      startActivity(a); 
      break; 
     } 
    } 

    public void CheckIfPlayerWon (int i) { 
     if (squares[1] == i && squares[2] == i && squares[3] == i){ 
      WinGame.setText("You have won the game! 1"); 
      buttons[1].setEnabled(false); 
      buttons[2].setEnabled(false); 
      buttons[3].setEnabled(false); 
      buttons[4].setEnabled(false); 
      buttons[5].setEnabled(false); 
      buttons[6].setEnabled(false); 
      buttons[7].setEnabled(false); 
      buttons[8].setEnabled(false); 
      buttons[9].setEnabled(false); 
     } 
     else if (squares[4] == i && squares[5] == i && squares[6] == i){ 
      WinGame.setText("You have won the game! 2"); 
      buttons[1].setEnabled(false); 
      buttons[2].setEnabled(false); 
      buttons[3].setEnabled(false); 
      buttons[4].setEnabled(false); 
      buttons[5].setEnabled(false); 
      buttons[6].setEnabled(false); 
      buttons[7].setEnabled(false); 
      buttons[8].setEnabled(false); 
      buttons[9].setEnabled(false); 
     } 
     else if (squares[7] == i && squares[8]== i && squares[9] == i){ 
      WinGame.setText("You have won the game! 3"); 
      buttons[1].setEnabled(false); 
      buttons[2].setEnabled(false); 
      buttons[3].setEnabled(false); 
      buttons[4].setEnabled(false); 
      buttons[5].setEnabled(false); 
      buttons[6].setEnabled(false); 
      buttons[7].setEnabled(false); 
      buttons[8].setEnabled(false); 
      buttons[9].setEnabled(false); 
     } 
     else if (squares[1]== i && squares[4] == i && squares[7] == i){ 
      WinGame.setText("You have won the game! 4"); 
      buttons[1].setEnabled(false); 
      buttons[2].setEnabled(false); 
      buttons[3].setEnabled(false); 
      buttons[4].setEnabled(false); 
      buttons[5].setEnabled(false); 
      buttons[6].setEnabled(false); 
      buttons[7].setEnabled(false); 
      buttons[8].setEnabled(false); 
      buttons[9].setEnabled(false); 
     } 
     else if (squares[2] == i && squares[5] == i && squares[8] == i){ 
      WinGame.setText("You have won the game! 5"); 
      buttons[1].setEnabled(false); 
      buttons[2].setEnabled(false); 
      buttons[3].setEnabled(false); 
      buttons[4].setEnabled(false); 
      buttons[5].setEnabled(false); 
      buttons[6].setEnabled(false); 
      buttons[7].setEnabled(false); 
      buttons[8].setEnabled(false); 
      buttons[9].setEnabled(false); 
     } 
     else if (squares[3] == i && squares[6] == i && squares[9] == i){ 
      WinGame.setText("You have won the game! 6"); 
      buttons[1].setEnabled(false); 
      buttons[2].setEnabled(false); 
      buttons[3].setEnabled(false); 
      buttons[4].setEnabled(false); 
      buttons[5].setEnabled(false); 
      buttons[6].setEnabled(false); 
      buttons[7].setEnabled(false); 
      buttons[8].setEnabled(false); 
      buttons[9].setEnabled(false); 
     } 
     else if (squares[1] == i && squares[5] == i && squares[9] == i){ 
      WinGame.setText("You have won the game! 7"); 
      buttons[1].setEnabled(false); 
      buttons[2].setEnabled(false); 
      buttons[3].setEnabled(false); 
      buttons[4].setEnabled(false); 
      buttons[5].setEnabled(false); 
      buttons[6].setEnabled(false); 
      buttons[7].setEnabled(false); 
      buttons[8].setEnabled(false); 
      buttons[9].setEnabled(false); 
     } 
     else if (squares[3] == i && squares[5] == i && squares[7] == i){ 
      WinGame.setText("You have won the game! 8"); 
      buttons[1].setEnabled(false); 
      buttons[2].setEnabled(false); 
      buttons[3].setEnabled(false); 
      buttons[4].setEnabled(false); 
      buttons[5].setEnabled(false); 
      buttons[6].setEnabled(false); 
      buttons[7].setEnabled(false); 
      buttons[8].setEnabled(false); 
      buttons[9].setEnabled(false); 
     } 
    } 


    public void makeMove(int i) { 
     buttons[i].setText("X"); 
     buttons[i].setEnabled(false); 
     squares[i] = 1; 
     CheckIfPlayerWon(i); 
    } 


    public void respond() { 
     for (int i = 1; i <= 9; i++) { 
      if (buttons[i].isEnabled()) { 
       buttons[i].setText("O"); 
       buttons[i].setEnabled(false); 
       squares[i] = 2; 
       break; 
      } 
     } 
    } 
} 

XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/play_grid" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:layout_marginTop="5dp" > 

    <TableRow android:gravity="center_horizontal"> 
     <Button android:id="@+id/one" android:layout_width="200dp" 
     android:layout_height="200dp" android:text="@string/one" 
     android:textSize="70sp" /> 
     <Button android:id="@+id/two" android:layout_width="200dp" 
     android:layout_height="200dp" android:text="@string/two" 
     android:textSize="70sp" /> 
     <Button android:id="@+id/three" android:layout_width="200dp" 
     android:layout_height="200dp" android:text="@string/three" 
     android:textSize="70sp" /> 
     </TableRow> 

    <TableRow android:gravity="center_horizontal"> 
     <Button android:id="@+id/four" android:layout_width="200dp" 
     android:layout_height="200dp" android:text="@string/four" 
     android:textSize="70sp" /> 
     <Button android:id="@+id/five" android:layout_width="200dp" 
     android:layout_height="200dp" android:text="@string/five" 
     android:textSize="70sp" /> 
     <Button android:id="@+id/six" android:layout_width="200dp" 
     android:layout_height="200dp" android:text="@string/six" 
     android:textSize="70sp" /> 
    </TableRow> 

    <TableRow android:gravity="center_horizontal"> 
     <Button android:id="@+id/seven" 
     android:layout_width="200dp" 
     android:layout_height="200dp" 
     android:text="@string/seven" 
     android:textSize="70sp" /> 
     <Button android:id="@+id/eight" android:layout_width="200dp" 
     android:layout_height="200dp" android:text="@string/eight" 
     android:textSize="70sp" /> 
     <Button android:id="@+id/nine" android:layout_width="200dp" 
     android:layout_height="200dp" android:text="@string/nine" 
     android:textSize="70sp" /> 
    </TableRow> 

    <Button 
     android:id="@+id/new_game" 
     android:layout_width="200dp" 
     android:layout_height="50dp" 
     android:text="@string/new_game" 
     android:textSize="30sp" /> 

    <Button 
     android:id="@+id/exit_game" 
     android:layout_width="200dp" 
     android:layout_height="50dp" 
     android:text="@string/exit_game" 
     android:textSize="30sp" /> 

    <TextView 
     android:id="@+id/win_game" 
     android:layout_width="200dp" 
     android:layout_height="50dp" 
     android:text="@string/win_game" 
     android:textSize="12sp" /> 
</TableLayout> 

字符串(一些字符串的應用程序的其他部分):

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string name="app_name">Noughts And Crosses</string> 
    <string name="action_settings">Settings</string> 
    <string name="Username">Enter Username</string> 
    <string name="Password">Enter Password</string> 
    <string name="LoginButton">Login</string> 
    <string name="RegisterButton">Register</string> 
    <string name="registerUserName">Enter new username</string> 
    <string name="registerPassword">Enter new password</string> 
    <string name="registerConfirmPassword">Confirm new password</string> 
    <string name="registerAccount">Create new Account</string> 
    <string name="one">1</string> 
    <string name="two">2</string> 
    <string name="three">3</string> 
    <string name="four">4</string> 
    <string name="five">5</string> 
    <string name="six">6</string> 
    <string name="seven">7</string> 
    <string name="eight">8</string> 
    <string name="nine">9</string> 
    <string name="new_game_x">Play as a X</string> 
    <string name="new_game_0">Play as a 0</string> 
    <string name="instructions">Instructions to Play</string> 
    <string name="exit_app">Exit App</string> 
    <string name="exit_game">Exit Game</string> 
    <string name="win_game"></string> 
    <string name="new_game">New Game</string> 
    <string name="BackToMenu">Back To Menu</string> 
    <string name="title_activity_NoughtsAndCrossesXActivity">Noughts And Crosses</string> 
    <string name="title_activity_LoginPageActivity">Login Page</string> 
    <string name="title_activity_InstructionActivity">Instruction</string> 

</resources> 
+0

http://codereview.stackexchange.com/可能是一個更好的堆棧交換站點發布此處。 – mah

+0

你知道如何在代碼中設置斷點嗎? –

+0

我試圖給你的問題添加標點符號,但這是不可能的。請學習如何使用逗號和句點。 – Mark

回答

0

你在遊戲開始之前不正確地將所有squares[]陣列元素初始化爲1?

public void clearGame(){ 
    for (int i = 1; i <= 9; i++){ 
     buttons[i].setText(""); 
     buttons[i].setEnabled(true); 
     squares[i] = 1;    <---------------- set all elements to 1 
    } 
    WinGame.setText(""); 
}