Commits

Anonymous committed 75e82a8

refactor code and modify a little.

  • Participants
  • Parent commits e9318ac

Comments (0)

Files changed (1)

src/com/selab/oxgame/OxGameActivity.java

 
 import android.app.Activity;
 import android.app.AlertDialog;
+import android.content.DialogInterface;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 				btn.setText("O");
 				gameArr[btnList.indexOf(btn)] = 1;
 			}
-			int result = OxGameActivity.this.checkIsEndGame();
-			if (result == 1) {
-
-				if (OxGameActivity.this.play_state == false) {
-					int wins = Integer.valueOf(OxGameActivity.this.
-							tvPlayer1Wins.getText().toString());
-					OxGameActivity.this.tvPlayer1Wins.setText(""+ ++wins);
-					new AlertDialog.Builder(OxGameActivity.this)
-					.setTitle("°TŽ§")
-					.setMessage("Player1 Win")
-					.setNegativeButton("˝TŠw", null)
-					.show();
-				} 
-				else {
-					int wins = Integer.valueOf(OxGameActivity.this.
-							tvPlayer2Wins.getText().toString());
-					OxGameActivity.this.tvPlayer2Wins.setText(""+ ++wins);
-					new AlertDialog.Builder(OxGameActivity.this)
-					.setTitle("°TŽ§")
-					.setMessage("Player2 Win")
-					.setNegativeButton("˝TŠw", null)
-					.show();
-				}
-				OxGameActivity.this.resetGame();
-			} else if (result == -1) {
-				new AlertDialog.Builder(OxGameActivity.this)
-				.setTitle("°TŽ§")
-				.setMessage("No Winner")
-				.setNegativeButton("˝TŠw", null)
-				.show();
-				OxGameActivity.this.resetGame();
-			}
-			else {
-				OxGameActivity.this.play_state = 
-					!OxGameActivity.this.play_state;
-//				Toast.makeText(
-//						OxGameActivity.this, 
-//						"Swith Player",	
-//						Toast.LENGTH_SHORT
-//				).show();
-			}
+			OxGameActivity.this.checkIsEndGame();
 		}
 	};
 	
 		
 		@Override
 		public void onClick(View v) {
-			// TODO Auto-generated method stub
 			tvPlayer1Wins.setText("0");
 			tvPlayer2Wins.setText("0");
 			OxGameActivity.this.resetGame();
 		}
 	};
 	
-	private int checkIsEndGame() {
+	private DialogInterface.OnClickListener  playButtonOnClickListener = 
+		new	DialogInterface.OnClickListener() {
+		
+		@Override
+		public void onClick(DialogInterface dialog, int which) {
+			OxGameActivity.this.resetGame();
+		}
+	};
+		
+	private void checkIsEndGame() {
+		switch (OxGameActivity.this.checkhasConnected()) {
+		case -1:
+			new AlertDialog.Builder(OxGameActivity.this)
+			.setTitle("°TŽ§")
+			.setMessage("No Winner")
+			.setNegativeButton("˝TŠw", playButtonOnClickListener)
+			.show();
+			
+			break;
+		case 1:
+			if (OxGameActivity.this.play_state == false) {
+				int wins = Integer.valueOf(OxGameActivity.this.
+						tvPlayer1Wins.getText().toString());
+				OxGameActivity.this.tvPlayer1Wins.setText(""+ ++wins);
+				new AlertDialog.Builder(OxGameActivity.this)
+				.setTitle("°TŽ§")
+				.setMessage("Player1 Win")
+				.setNegativeButton("˝TŠw", playButtonOnClickListener)
+				.show();
+			} 
+			else {
+				int wins = Integer.valueOf(OxGameActivity.this.
+						tvPlayer2Wins.getText().toString());
+				OxGameActivity.this.tvPlayer2Wins.setText(""+ ++wins);
+				new AlertDialog.Builder(OxGameActivity.this)
+				.setTitle("°TŽ§")
+				.setMessage("Player2 Win")
+				.setNegativeButton("˝TŠw", playButtonOnClickListener)
+				.show();
+			}
+			break;
+		default:
+			OxGameActivity.this.play_state = 
+				!OxGameActivity.this.play_state;
+			break;
+		}
+	}
+	
+	private int checkhasConnected() {
 		if (gameArr[0] == gameArr[1] && 
 				gameArr[1] == gameArr[2] && gameArr[1] != 2) 
 			return 1;
 			btn.setEnabled(true);
 		}
 		play_state = !play_state;
+		Toast.makeText(this, 
+				play_state == false?"Player 1 first":"Player 2 first", 
+						Toast.LENGTH_LONG)
+						.show();
 	}
 	
 	private void initializeView() {