Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Authors: Daniel Davis, Dalton Claxton, Peyton White
- Date: 17 February 2020
- Description: An app that implements 3 different variations on the classic game of Tic-Tac-Toe
- */
- package csci.apsu.tictactoe;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.util.SparseIntArray;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import androidx.appcompat.app.AlertDialog;
- import androidx.appcompat.app.AppCompatActivity;
- import java.util.HashMap;
- public class PlayNumericalActivity extends AppCompatActivity implements View.OnClickListener {
- // Constants for the board
- private static final int ROWS = 3, COLS = 3;
- private static int[][] board = new int[ROWS][COLS];
- private static HashMap<Integer, Integer> moves = new HashMap();
- private static int turns_taken = 0;
- private static final int max_moves = 9;
- // Objects used frequently
- private static Intent intent;
- private View child;
- private TextView turn_textView;
- private RadioGroup number_radioGroup;
- private RadioButton number_radioButton;
- GameState saveGame;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_play_numerical);
- initializeBoard();
- }
- @Override
- public void onBackPressed() {
- AlertDialog.Builder alertDiag = new AlertDialog.Builder(PlayNumericalActivity.this);
- alertDiag.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- finish();
- }
- });
- alertDiag.setNegativeButton("No", null);
- alertDiag.setMessage("Are you sure you want to exit?");
- alertDiag.setTitle("Tic-Tac-Toe");
- alertDiag.show();
- }
- @Override
- public void onClick(View view) {
- if (view.getId() == R.id.restartBtn) {
- saveGame.restartGame();
- intent = new Intent(getApplicationContext(), PlayNumericalActivity.class);
- startActivity(intent);
- }
- for (int i = 0; i < ROWS; i++) {
- for (int j = 0; j < COLS; j++) {
- if (view.getId() == board[i][j]) {
- ImageView position_imageView = findViewById(board[i][j]);
- int index = 0;
- for (int key : moves.keySet() ) {
- number_radioButton = findViewById(key);
- // Set the board square to the value of the selected radio button
- if (number_radioButton.isChecked() && position_imageView.getTag() == null) {
- position_imageView.setBackgroundResource(getResources().getIdentifier(
- "number_" + moves.get(key), "drawable", getPackageName()));
- position_imageView.setTag(getResources().getIdentifier(
- "number_" + moves.get(key), "drawable", getPackageName()));
- position_imageView.setClickable(false);
- // Save the current position and number
- saveGame.saveGameState(index, Character.forDigit(moves.get(key), 10));
- //Log.i("K = ", "" + k);
- // Replace the resource ID in the board array with the number it represents
- board[i][j] = moves.get(key);
- break;
- }
- index++;
- }
- changeTurn();
- }
- }
- }
- }
- // Set up the listeners and initial appearance of the board
- private void initializeBoard() {
- ViewGroup layout = findViewById(R.id.board_linearLayout);
- number_radioGroup = findViewById(R.id.number_choices_rg);
- turn_textView = findViewById(R.id.player_turn_textView);
- saveGame = new GameState(getApplicationContext());
- turns_taken = 0;
- // Set up the board for Player 1
- changeMoves(R.string.player1_turn);
- findViewById(R.id.restartBtn).setOnClickListener(this);
- for (int i = 0; i < layout.getChildCount(); i++) {
- child = layout.getChildAt(i);
- ViewGroup rowLayout = findViewById(child.getId());
- for (int j = 0; j < rowLayout.getChildCount(); j++) {
- child = rowLayout.getChildAt(j);
- if (child instanceof ImageView) {
- child.setOnClickListener(this);
- if (saveGame.hasCurrentSaveGame()) {
- char[] save = saveGame.getGameState().toCharArray();
- for (int k = 0; k < save.length; k++) {
- if (save[k] == (char) (k + 1)) {
- child.setBackgroundResource(getResources().getIdentifier(
- "number_" + k, "drawable", getPackageName()));
- child.setClickable(false);
- }
- }
- }
- board[i][j] = child.getId();
- }
- }
- }
- }
- // Indicates whose turn it is and checks for a winner
- private void changeTurn() {
- turns_taken++;
- if (isWinner()) {
- intent = new Intent(getBaseContext(), GameEndActivity.class);
- if (turn_textView.getText().equals(getString(R.string.player1_turn))) {
- intent.putExtra("Player 1", "Numerical");
- } else {
- intent.putExtra("Player 2", "Numerical");
- }
- saveGame.restartGame();
- startActivity(intent);
- } else if (turns_taken >= max_moves) {
- startActivity(new Intent(getBaseContext(), GameEndActivity.class));
- } else if (turn_textView.getText().equals(getString(R.string.player1_turn))) {
- turn_textView.setText(R.string.player2_turn);
- changeMoves(R.string.player2_turn);
- } else {
- turn_textView.setText(R.string.player1_turn);
- changeMoves(R.string.player1_turn);
- }
- }
- // Changes the possible moves depending on the current player
- private void changeMoves(int player) {
- moves.clear();
- if (player == R.string.player1_turn) {
- findViewById(R.id.fifth_rb).setVisibility(View.VISIBLE);
- for (int i = 0; i < number_radioGroup.getChildCount(); i++) {
- child = number_radioGroup.getChildAt(i);
- moves.put(child.getId(), ((i * 2) + 1));
- ((RadioButton) child).setText("");
- ((RadioButton) child).append("" + ((i * 2) + 1));
- }
- Log.i("Odd Moves : ", "" + moves.toString());
- } else {
- findViewById(R.id.fifth_rb).setVisibility(View.GONE);
- for (int i = 0; i < number_radioGroup.getChildCount() - 1; i++) {
- child = number_radioGroup.getChildAt(i);
- moves.put(child.getId(), ((i * 2) + 2));
- ((RadioButton) child).setText("");
- ((RadioButton) child).append("" + ((i * 2) + 2));
- }
- Log.i("Even Moves : ", "" + moves.toString());
- }
- }
- // Algorithm that scans for a sum of 15 anywhere on the board
- public boolean isWinner() {
- // Check horizontal
- for (int row = 0; row < board.length; row++) {
- for (int col = 0; col < board[row].length - 2; col++) {
- if (board[row][col] + board[row][col + 1] +
- board[row][col + 2] == 15) {
- return true;
- }
- }
- }
- // Check vertical
- for (int row = 0; row < board.length - 2; row++) {
- for (int col = 0; col < board[row].length; col++) {
- if (board[row][col] + board[row + 1][col] +
- board[row + 2][col] == 15) {
- return true;
- }
- }
- }
- // Check diagonal
- for (int row = 0; row < board.length - 2; row++) {
- for (int col = 0; col < board[row].length - 2; col++) {
- if (board[row][col] + board[row + 1][col + 1] +
- board[row + 2][col + 2] == 15) {
- return true;
- }
- }
- }
- // Check reverse-diagonal
- for (int row = 0; row < board.length - 2; row++) {
- for (int col = 2; col < board[row].length; col++) {
- if (board[row][col] + board[row + 1][col - 1] +
- board[row + 2][col - 2] == 15) {
- return true;
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement