Advertisement
Guest User

bbbbbbbbb

a guest
Feb 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. package org.example.tictactoe;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.ImageView;
  10.  
  11. import java.util.HashMap;
  12. import java.util.Map;
  13.  
  14. // we implement the onClickListener - so this means there
  15. //will be an onClick method defined for ALL the views later
  16. //in the onClick method
  17. public class MainActivity extends Activity implements OnClickListener {
  18.  
  19. int[] fields = new int[9];
  20. int turn = 1;
  21. int turnCount = 0;
  22.  
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27.  
  28. for (int i = 0; i < fields.length; i++) {
  29. fields[i] = 0;
  30. }
  31.  
  32. View table = findViewById(R.id.table);
  33. table.setOnClickListener(this);
  34.  
  35. //we add clicklisteners, this, to all our fields
  36. View field1 = findViewById(R.id.felt1);
  37. field1.setOnClickListener(this);
  38.  
  39. View field2 = findViewById(R.id.felt2);
  40. field2.setOnClickListener(this);
  41.  
  42. //TODO add click listeners like this for the rest of the imageviews
  43. View field3 = findViewById(R.id.felt3);
  44. field3.setOnClickListener(this);
  45.  
  46. View field4 = findViewById(R.id.felt4);
  47. field4.setOnClickListener(this);
  48.  
  49. View field5 = findViewById(R.id.felt5);
  50. field5.setOnClickListener(this);
  51.  
  52. View field6 = findViewById(R.id.felt6);
  53. field6.setOnClickListener(this);
  54.  
  55. View field7 = findViewById(R.id.felt7);
  56. field7.setOnClickListener(this);
  57.  
  58. View field8 = findViewById(R.id.felt8);
  59. field8.setOnClickListener(this);
  60.  
  61. View field9 = findViewById(R.id.felt9);
  62. field9.setOnClickListener(this);
  63. }
  64.  
  65. @Override
  66. public boolean onCreateOptionsMenu(Menu menu) {
  67. // Inflate the menu; this adds items to the action bar if it is present.
  68. getMenuInflater().inflate(R.menu.main, menu);
  69. return true;
  70. }
  71.  
  72. @Override
  73. public boolean onOptionsItemSelected(MenuItem item) {
  74. // Handle action bar item clicks here. The action bar will
  75. // automatically handle clicks on the Home/Up button, so long
  76. // as you specify a parent activity in AndroidManifest.xml.
  77. int id = item.getItemId();
  78. if (id == R.id.action_settings) {
  79. return true;
  80. }
  81. return super.onOptionsItemSelected(item);
  82. }
  83.  
  84.  
  85.  
  86. @Override
  87. public void onClick(View view) {
  88. // TODO Here you need to get the ID of the view
  89. // being pressed and then if the view is pressed
  90. // you need to first put a "X", then next time
  91. // put a "O" and also make sure that you cannot
  92. // put a "O" or a "X" if there is already something.
  93.  
  94. if (view.getId()==R.id.felt1 && fields[0] == 0)
  95. {
  96. turnCount++;
  97. ImageView image = (ImageView) view;
  98. fields[0] = turn;
  99. didIWin();
  100.  
  101. if(turn == 1) {
  102. turn++;
  103. image.setImageResource(R.drawable.kryds);
  104. } else {
  105. turn--;
  106. image.setImageResource(R.drawable.bolle);
  107. }
  108.  
  109. System.out.println("field 1 pressed");
  110. }
  111. if (view.getId()==R.id.felt2 && fields[1] == 0)
  112. {
  113. turnCount++;
  114. ImageView image = (ImageView) view;
  115. fields[1] = turn;
  116. didIWin();
  117.  
  118. if(turn == 1) {
  119. turn++;
  120. image.setImageResource(R.drawable.kryds);
  121. } else {
  122. turn--;
  123. image.setImageResource(R.drawable.bolle);
  124. }
  125.  
  126. System.out.println("field 2 pressed");
  127. }
  128. if (view.getId()==R.id.felt3 && fields[2] == 0)
  129. {
  130. turnCount++;
  131. ImageView image = (ImageView) view;
  132. fields[2] = turn;
  133. didIWin();
  134.  
  135. if(turn == 1) {
  136. turn++;
  137. image.setImageResource(R.drawable.kryds);
  138. } else {
  139. turn--;
  140. image.setImageResource(R.drawable.bolle);
  141. }
  142.  
  143. System.out.println("field 3 pressed");
  144. }
  145. if (view.getId()==R.id.felt4 && fields[3] == 0)
  146. {
  147. turnCount++;
  148. ImageView image = (ImageView) view;
  149. fields[3] = turn;
  150. didIWin();
  151.  
  152. if(turn == 1) {
  153. turn++;
  154. image.setImageResource(R.drawable.kryds);
  155. } else {
  156. turn--;
  157. image.setImageResource(R.drawable.bolle);
  158. }
  159.  
  160. System.out.println("field 4 pressed");
  161. }
  162. if (view.getId()==R.id.felt5 && fields[4] == 0)
  163. {
  164. turnCount++;
  165. ImageView image = (ImageView) view;
  166. fields[4] = turn;
  167. didIWin();
  168.  
  169. if(turn == 1) {
  170. turn++;
  171. image.setImageResource(R.drawable.kryds);
  172. } else {
  173. turn--;
  174. image.setImageResource(R.drawable.bolle);
  175. }
  176.  
  177. System.out.println("field 5 pressed");
  178. }
  179. if (view.getId()==R.id.felt6 && fields[5] == 0)
  180. {
  181. turnCount++;
  182. ImageView image = (ImageView) view;
  183. fields[5] = turn;
  184. didIWin();
  185.  
  186. if(turn == 1) {
  187. turn++;
  188. image.setImageResource(R.drawable.kryds);
  189. } else {
  190. turn--;
  191. image.setImageResource(R.drawable.bolle);
  192. }
  193.  
  194. System.out.println("field 6 pressed");
  195. }
  196. if (view.getId()==R.id.felt7 && fields[6] == 0)
  197. {
  198. turnCount++;
  199. ImageView image = (ImageView) view;
  200. fields[6] = turn;
  201. didIWin();
  202.  
  203. if(turn == 1) {
  204. turn++;
  205. image.setImageResource(R.drawable.kryds);
  206. } else {
  207. turn--;
  208. image.setImageResource(R.drawable.bolle);
  209. }
  210.  
  211. System.out.println("field 7 pressed");
  212. }
  213. if (view.getId()==R.id.felt8 && fields[7] == 0)
  214. {
  215. turnCount++;
  216. ImageView image = (ImageView) view;
  217. fields[7] = turn;
  218. didIWin();
  219.  
  220. if(turn == 1) {
  221. turn++;
  222. image.setImageResource(R.drawable.kryds);
  223. } else {
  224. turn--;
  225. image.setImageResource(R.drawable.bolle);
  226. }
  227.  
  228. System.out.println("field 8 pressed");
  229. }
  230. if (view.getId()==R.id.felt9 && fields[8] == 0)
  231. {
  232. turnCount++;
  233. ImageView image = (ImageView) view;
  234. fields[8] = turn;
  235. didIWin();
  236.  
  237. if(turn == 1) {
  238. turn++;
  239. image.setImageResource(R.drawable.kryds);
  240. } else {
  241. turn--;
  242. image.setImageResource(R.drawable.bolle);
  243. }
  244.  
  245. System.out.println("field 9 pressed");
  246. }
  247. }
  248.  
  249. public void didIWin() {
  250. //hori
  251. int[] C1 = new int[]{0, 1, 2};
  252. int[] C2 = new int[]{3, 4, 5};
  253. int[] C3 = new int[]{6, 7, 8};
  254. //vert
  255. int[] C4 = new int[]{0, 4, 6};
  256. int[] C5 = new int[]{1, 4, 7};
  257. int[] C6 = new int[]{2, 6, 8};
  258. //diag
  259. int[] C7 = new int[]{0, 4, 8};
  260. int[] C8 = new int[]{2, 4, 6};
  261.  
  262. int[][] wins = {C1, C2, C3, C4, C5, C6, C7, C8};
  263.  
  264. for(int i = 0; i < wins.length; i++) {
  265. int index1 = wins[i][0];
  266. int index2 = wins[i][1];
  267. int index3 = wins[i][2];
  268. if (fields[index1]==1 && fields[index2]==1 && fields[index3]==1)
  269. {
  270. //kryds wins
  271. System.out.println("kryds biaaatch");
  272. }
  273. if (fields[index1]==2 && fields[index2]==2 && fields[index3]==2)
  274. {
  275. //kryds wins
  276. System.out.println("bolle biaaatch");
  277. }
  278.  
  279. }
  280.  
  281. }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement