Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.99 KB | None | 0 0
  1. /*
  2. Authors: Daniel Davis, Dalton Claxton, Peyton White
  3. Date: 17 February 2020
  4. Description: An app that implements 3 different variations on the classic game of Tic-Tac-Toe
  5. */
  6.  
  7. package csci.apsu.tictactoe;
  8.  
  9. import android.content.DialogInterface;
  10. import android.content.Intent;
  11. import android.os.Bundle;
  12. import android.util.Log;
  13. import android.util.SparseIntArray;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.Button;
  17. import android.widget.ImageView;
  18. import android.widget.RadioButton;
  19. import android.widget.RadioGroup;
  20. import android.widget.TextView;
  21.  
  22. import androidx.appcompat.app.AlertDialog;
  23. import androidx.appcompat.app.AppCompatActivity;
  24.  
  25.  
  26. public class PlayNumericalActivity extends AppCompatActivity implements View.OnClickListener {
  27.  
  28. // Constants for the board
  29. private static final int ROWS = 3, COLS = 3;
  30. private static int[][] board = new int[ROWS][COLS];
  31. private static SparseIntArray moves = new SparseIntArray();
  32. private static int turns_taken = 0;
  33. private static final int max_moves = 9;
  34.  
  35. // Objects used frequently
  36. private static Intent intent;
  37. private View child;
  38. private TextView turn_textView;
  39. private RadioGroup number_radioGroup;
  40. private RadioButton number_radioButton;
  41. GameState saveGame;
  42.  
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. super.onCreate(savedInstanceState);
  46. setContentView(R.layout.activity_play_numerical);
  47.  
  48. initializeBoard();
  49. }
  50.  
  51. @Override
  52. public void onBackPressed() {
  53. AlertDialog.Builder alertDiag = new AlertDialog.Builder(PlayNumericalActivity.this);
  54. alertDiag.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  55. @Override
  56. public void onClick(DialogInterface dialogInterface, int i) {
  57. finish();
  58. }
  59. });
  60. alertDiag.setNegativeButton("No", null);
  61. alertDiag.setMessage("Are you sure you want to exit?");
  62. alertDiag.setTitle("Tic-Tac-Toe");
  63. alertDiag.show();
  64. }
  65.  
  66.  
  67. @Override
  68. public void onClick(View view) {
  69. if (view.getId() == R.id.restartBtn) {
  70. saveGame.restartGame();
  71.  
  72. intent = new Intent(getApplicationContext(), PlayNumericalActivity.class);
  73. startActivity(intent);
  74. }
  75.  
  76. for (int i = 0; i < ROWS; i++) {
  77. for (int j = 0; j < COLS; j++) {
  78. if (view.getId() == board[i][j]) {
  79. ImageView position_imageView = findViewById(board[i][j]);
  80.  
  81. for (int k = 0; k < moves.size(); k++) {
  82. number_radioButton = findViewById(moves.keyAt(k));
  83.  
  84. // Set the board square to the value of the selected radio button
  85. if (number_radioButton.isChecked() && position_imageView.getTag() == null) {
  86. position_imageView.setBackgroundResource(getResources().getIdentifier(
  87. "number_" + moves.valueAt(k), "drawable", getPackageName()));
  88. position_imageView.setTag(getResources().getIdentifier(
  89. "number_" + moves.valueAt(k), "drawable", getPackageName()));
  90.  
  91. // Save the current position and number
  92. saveGame.saveGameState(k, Character.forDigit(moves.valueAt(k), 10));
  93. Log.i("K = ", "" + k);
  94.  
  95. // Replace the resource ID in the board array with the number it represents
  96. board[i][j] = moves.valueAt(k);
  97. }
  98. }
  99.  
  100. changeTurn();
  101. }
  102. }
  103. }
  104. }
  105.  
  106. // Set up the listeners and initial appearance of the board
  107. private void initializeBoard() {
  108. ViewGroup layout = findViewById(R.id.board_linearLayout);
  109. number_radioGroup = findViewById(R.id.number_choices_rg);
  110. turn_textView = findViewById(R.id.player_turn_textView);
  111. saveGame = new GameState(getApplicationContext());
  112. turns_taken = 0;
  113.  
  114. // Set up the board for Player 1
  115. changeMoves(R.string.player1_turn);
  116. findViewById(R.id.restartBtn).setOnClickListener(this);
  117.  
  118. for (int i = 0; i < layout.getChildCount(); i++) {
  119. child = layout.getChildAt(i);
  120. ViewGroup rowLayout = findViewById(child.getId());
  121.  
  122. for (int j = 0; j < rowLayout.getChildCount(); j++) {
  123. child = rowLayout.getChildAt(j);
  124. if (child instanceof ImageView) {
  125. child.setOnClickListener(this);
  126.  
  127. if (saveGame.hasCurrentSaveGame()) {
  128. char[] save = saveGame.getGameState().toCharArray();
  129.  
  130. for (int k = 0; k < save.length; k++) {
  131. if (save[k] == (char) (k + 1)) {
  132. child.setBackgroundResource(getResources().getIdentifier(
  133. "number_" + k, "drawable", getPackageName()));
  134. child.setClickable(false);
  135. }
  136. }
  137. }
  138.  
  139. board[i][j] = child.getId();
  140. }
  141. }
  142. }
  143. }
  144.  
  145. // Indicates whose turn it is and checks for a winner
  146. private void changeTurn() {
  147. turns_taken++;
  148.  
  149. if (isWinner()) {
  150. intent = new Intent(getBaseContext(), GameEndActivity.class);
  151.  
  152. if (turn_textView.getText().equals(getString(R.string.player1_turn))) {
  153. intent.putExtra("Player 1", "Numerical");
  154. } else {
  155. intent.putExtra("Player 2", "Numerical");
  156. }
  157.  
  158. saveGame.restartGame();
  159. startActivity(intent);
  160. } else if (turns_taken >= max_moves) {
  161. startActivity(new Intent(getBaseContext(), GameEndActivity.class));
  162. } else if (turn_textView.getText().equals(getString(R.string.player1_turn))) {
  163. turn_textView.setText(R.string.player2_turn);
  164. changeMoves(R.string.player2_turn);
  165. } else {
  166. turn_textView.setText(R.string.player1_turn);
  167. changeMoves(R.string.player1_turn);
  168. }
  169. }
  170.  
  171. // Changes the possible moves depending on the current player
  172. private void changeMoves(int player) {
  173. moves.clear();
  174.  
  175. if (player == R.string.player1_turn) {
  176. number_radioButton = findViewById(R.id.fifth_rb);
  177. number_radioButton.setVisibility(View.VISIBLE);
  178.  
  179. for (int i = 0; i < number_radioGroup.getChildCount(); i++) {
  180. child = number_radioGroup.getChildAt(i);
  181. ((RadioButton) child).setText("");
  182. ((RadioButton) child).append("" + ((i * 2) + 1));
  183. moves.put(child.getId(), (i * 2) + 1);
  184. }
  185. } else {
  186. number_radioButton = findViewById(R.id.fifth_rb);
  187. number_radioButton.setVisibility(View.GONE);
  188.  
  189. for (int i = 0; i < number_radioGroup.getChildCount() - 1; i++) {
  190. child = number_radioGroup.getChildAt(i);
  191. ((RadioButton) child).setText("");
  192. ((RadioButton) child).append("" + ((i * 2) + 2));
  193. moves.put(child.getId(), ((i * 2) + 2));
  194. }
  195. }
  196. }
  197.  
  198. // Algorithm that scans for a sum of 15 anywhere on the board
  199. public boolean isWinner() {
  200. // Check horizontal
  201. for (int row = 0; row < board.length; row++) {
  202. for (int col = 0; col < board[row].length - 2; col++) {
  203. if (board[row][col] + board[row][col + 1] +
  204. board[row][col + 2] == 15) {
  205. return true;
  206. }
  207. }
  208. }
  209.  
  210. // Check vertical
  211. for (int row = 0; row < board.length - 2; row++) {
  212. for (int col = 0; col < board[row].length; col++) {
  213. if (board[row][col] + board[row + 1][col] +
  214. board[row + 2][col] == 15) {
  215. return true;
  216. }
  217. }
  218. }
  219.  
  220. // Check diagonal
  221. for (int row = 0; row < board.length - 2; row++) {
  222. for (int col = 0; col < board[row].length - 2; col++) {
  223. if (board[row][col] + board[row + 1][col + 1] +
  224. board[row + 2][col + 2] == 15) {
  225. return true;
  226. }
  227. }
  228. }
  229.  
  230. // Check reverse-diagonal
  231. for (int row = 0; row < board.length - 2; row++) {
  232. for (int col = 2; col < board[row].length; col++) {
  233. if (board[row][col] + board[row + 1][col - 1] +
  234. board[row + 2][col - 2] == 15) {
  235. return true;
  236. }
  237. }
  238. }
  239.  
  240. return false;
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement