Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created February 2, 2018 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/59c0697b9d7d60d702307b353071285f to your computer and use it in GitHub Desktop.
Save ssaurel/59c0697b9d7d60d702307b353071285f to your computer and use it in GitHub Desktop.
Main Activity for the Tic-Tac-Toe game on the SSaurel's Blog
package com.ssaurel.tictactoe;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import static com.ssaurel.tictactoe.R.id.board;
public class MainActivity extends AppCompatActivity {
private BoardView boardView;
private GameEngine gameEngine;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boardView = (BoardView) findViewById(board);
gameEngine = new GameEngine();
boardView.setGameEngine(gameEngine);
boardView.setMainActivity(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_new_game) {
newGame();
}
return super.onOptionsItemSelected(item);
}
public void gameEnded(char c) {
String msg = (c == 'T') ? "Game Ended. Tie" : "GameEnded. " + c + " win";
new AlertDialog.Builder(this).setTitle("Tic Tac Toe").
setMessage(msg).
setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
newGame();
}
}).show();
}
private void newGame() {
gameEngine.newGame();
boardView.invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment