Advertisement
MightyD33r

Android Tic Tac Toe

Jun 14th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.54 KB | None | 0 0
  1. ENGINE
  2.  
  3. package com.example.d851.matrix;
  4.  
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.*;
  9.  
  10. public class Main extends AppCompatActivity implements View.OnClickListener
  11. {
  12. Button btTL, btTM, btTR, btML, btMM, btMR, btBL, btBM, btBR, btReset;
  13. TextView text;
  14. boolean turn;
  15. int numOfTurns;
  16. int[][] board;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState)
  19. {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22.  
  23. btTL = findViewById(R.id.btTL);
  24. btTM = findViewById(R.id.btTM);
  25. btTR = findViewById(R.id.btTR);
  26. btML = findViewById(R.id.btML);
  27. btMM = findViewById(R.id.btMM);
  28. btMR = findViewById(R.id.btMR);
  29. btBL = findViewById(R.id.btBL);
  30. btBM = findViewById(R.id.btBM);
  31. btBR = findViewById(R.id.btBR);
  32. btReset = findViewById(R.id.btReset);
  33. text = findViewById(R.id.text);
  34. turn = true;
  35. numOfTurns = 0;
  36. board = new int[3][3];
  37. for (int x = 0; x < 3; x++)
  38. {
  39. for (int y = 0; y < 3; y++)
  40. {
  41. board[x][y] = 0;
  42. }
  43. }
  44.  
  45. btTL.setOnClickListener(this);
  46. btTM.setOnClickListener(this);
  47. btTR.setOnClickListener(this);
  48. btML.setOnClickListener(this);
  49. btMM.setOnClickListener(this);
  50. btMR.setOnClickListener(this);
  51. btBL.setOnClickListener(this);
  52. btBM.setOnClickListener(this);
  53. btBR.setOnClickListener(this);
  54. btReset.setOnClickListener(this);
  55. }
  56.  
  57. @Override
  58. public void onClick(View view)
  59. {
  60. if (view == btReset)
  61. {
  62. turn = true;
  63. numOfTurns = 0;
  64. for (int x = 0; x < 3; x++)
  65. {
  66. for (int y = 0; y < 3; y++)
  67. {
  68. board[x][y] = 0;
  69. }
  70. }
  71. btTL.setText("");
  72. btTM.setText("");
  73. btTR.setText("");
  74. btML.setText("");
  75. btMM.setText("");
  76. btMR.setText("");
  77. btBL.setText("");
  78. btBM.setText("");
  79. btBR.setText("");
  80. text.setText("");
  81. }
  82. if (!checkWin())
  83. {
  84. if (view == btTL && btTL.getText().equals(""))
  85. {
  86. btTL.setText(turn ? "X" : "O");
  87. turn = !turn;
  88. numOfTurns++;
  89. board[0][0] = turn ? 1 : -1;
  90. }
  91. if (view == btTM && btTM.getText().equals(""))
  92. {
  93. btTM.setText(turn ? "X" : "O");
  94. turn = !turn;
  95. numOfTurns++;
  96. board[0][1] = turn ? 1 : -1;
  97. }
  98. if (view == btTR && btTR.getText().equals(""))
  99. {
  100. btTR.setText(turn ? "X" : "O");
  101. turn = !turn;
  102. numOfTurns++;
  103. board[0][2] = turn ? 1 : -1;
  104. }
  105. if (view == btML && btML.getText().equals(""))
  106. {
  107. btML.setText(turn ? "X" : "O");
  108. turn = !turn;
  109. numOfTurns++;
  110. board[1][0] = turn ? 1 : -1;
  111. }
  112. if (view == btMM && btMM.getText().equals(""))
  113. {
  114. btMM.setText(turn ? "X" : "O");
  115. turn = !turn;
  116. numOfTurns++;
  117. board[1][1] = turn ? 1 : -1;
  118. }
  119. if (view == btMR && btMR.getText().equals(""))
  120. {
  121. btMR.setText(turn ? "X" : "O");
  122. turn = !turn;
  123. numOfTurns++;
  124. board[1][2] = turn ? 1 : -1;
  125. }
  126. if (view == btBL && btBL.getText().equals(""))
  127. {
  128. btBL.setText(turn ? "X" : "O");
  129. turn = !turn;
  130. numOfTurns++;
  131. board[2][0] = turn ? 1 : -1;
  132. }
  133. if (view == btBM && btBM.getText().equals(""))
  134. {
  135. btBM.setText(turn ? "X" : "O");
  136. turn = !turn;
  137. numOfTurns++;
  138. board[2][1] = turn ? 1 : -1;
  139. }
  140. if (view == btBR && btBR.getText().equals(""))
  141. {
  142. btBR.setText(turn ? "X" : "O");
  143. turn = !turn;
  144. numOfTurns++;
  145. board[2][2] = turn ? 1 : -1;
  146. }
  147.  
  148. if (numOfTurns > 4)
  149. {
  150. boolean winner = checkWin();
  151. if (winner)
  152. text.setText((!turn) ? "X wins!" : "O wins!");
  153. else if (numOfTurns == 9)
  154. {
  155. text.setText("It's a tie!");
  156. }
  157. }
  158. }
  159. }
  160.  
  161. public boolean checkWin()
  162. {
  163. for (int x = 0; x < 3; x++)
  164. {
  165. if (board[x][0] == board[x][1] && board[x][0] == board[x][2] && board[x][0] != 0)
  166. {
  167. return true;
  168. }
  169. }
  170.  
  171. for (int y = 0;y < 3; y++)
  172. {
  173. if (board[0][y] == board[1][y] && board[0][y] == board[2][y] && board[0][y] != 0)
  174. {
  175. return true;
  176. }
  177. }
  178.  
  179. if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != 0)
  180. {
  181. return true;
  182. }
  183.  
  184. if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[2][0] != 0)
  185. {
  186. return true;
  187. }
  188.  
  189. return false;
  190. }
  191. }
  192.  
  193. XML
  194.  
  195. <?xml version="1.0" encoding="utf-8"?>
  196. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  197. xmlns:app="http://schemas.android.com/apk/res-auto"
  198. xmlns:tools="http://schemas.android.com/tools"
  199. android:layout_width="match_parent"
  200. android:layout_height="match_parent"
  201. android:orientation="vertical"
  202. tools:context="com.example.d851.matrix.Main">
  203. <TextView
  204. android:layout_width="match_parent"
  205. android:layout_height="wrap_content"
  206. android:textSize="40dp"
  207. android:gravity="center"
  208. android:textStyle="bold"
  209. android:text="Tic Tac Toe"/>
  210. <LinearLayout
  211. android:layout_width="match_parent"
  212. android:layout_height="wrap_content"
  213. android:weightSum="3"
  214. android:orientation="horizontal">
  215. <Button
  216. android:id="@+id/btTL"
  217. android:textSize="20dp"
  218. android:layout_width="0dp"
  219. android:layout_weight="1"
  220. android:layout_height="100dp"/>
  221. <Button
  222. android:id="@+id/btTM"
  223. android:textSize="20dp"
  224. android:layout_width="0dp"
  225. android:layout_weight="1"
  226. android:layout_height="100dp"/>
  227. <Button
  228. android:id="@+id/btTR"
  229. android:textSize="20dp"
  230. android:layout_width="0dp"
  231. android:layout_weight="1"
  232. android:layout_height="100dp"/>
  233. </LinearLayout>
  234. <LinearLayout
  235. android:layout_width="match_parent"
  236. android:layout_height="wrap_content"
  237. android:weightSum="3"
  238. android:orientation="horizontal">
  239. <Button
  240. android:id="@+id/btML"
  241. android:textSize="20dp"
  242. android:layout_width="0dp"
  243. android:layout_weight="1"
  244. android:layout_height="100dp"/>
  245. <Button
  246. android:id="@+id/btMM"
  247. android:textSize="20dp"
  248. android:layout_width="0dp"
  249. android:layout_weight="1"
  250. android:layout_height="100dp"/>
  251. <Button
  252. android:id="@+id/btMR"
  253. android:textSize="20dp"
  254. android:layout_width="0dp"
  255. android:layout_weight="1"
  256. android:layout_height="100dp"/>
  257. </LinearLayout>
  258. <LinearLayout
  259. android:layout_width="match_parent"
  260. android:layout_height="wrap_content"
  261. android:weightSum="3"
  262. android:orientation="horizontal">
  263. <Button
  264. android:id="@+id/btBL"
  265. android:textSize="20dp"
  266. android:layout_width="0dp"
  267. android:layout_weight="1"
  268. android:layout_height="100dp"/>
  269. <Button
  270. android:id="@+id/btBM"
  271. android:textSize="20dp"
  272. android:layout_width="0dp"
  273. android:layout_weight="1"
  274. android:layout_height="100dp"/>
  275. <Button
  276. android:id="@+id/btBR"
  277. android:textSize="20dp"
  278. android:layout_width="0dp"
  279. android:layout_weight="1"
  280. android:layout_height="100dp"/>
  281. </LinearLayout>
  282.  
  283. <TextView
  284. android:id="@+id/text"
  285. android:layout_width="match_parent"
  286. android:layout_height="wrap_content"
  287. android:gravity="center"
  288. android:textSize="35dp"
  289. android:textStyle="bold" />
  290. <LinearLayout
  291. android:layout_width="match_parent"
  292. android:layout_height="0dip"
  293. android:orientation="vertical"
  294. android:layout_weight="1"
  295. android:gravity="bottom|center">
  296. <Button
  297. android:id="@+id/btReset"
  298. android:layout_width="wrap_content"
  299. android:layout_height="wrap_content"
  300. android:text="Reset"
  301. android:textStyle="bold"
  302. android:textSize="20dp"
  303. android:layout_gravity="center"/>
  304. </LinearLayout>
  305.  
  306. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement