Advertisement
Guest User

Untitled

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