Advertisement
Hakuhun

Basic android tic-tac-toe code-behind

Apr 20th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. package com.bakonyi.TicTacToe2;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.GridLayout;
  9. import android.widget.TextView;
  10.  
  11. public class MainActivity extends Activity {
  12.  
  13.     private Button ujJatek;
  14.     private TextView tvErtesito;
  15.     private GridLayout glJatekter;
  16.     private Button btGomb[]=new Button[10];   //1-9-ig kell
  17.     private int lépésSzám=0;
  18.     private String[] felirat = {"X","Y"};
  19.     private String játékos;
  20.    
  21.    
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.        
  27.         ujJatek = (Button)findViewById(R.id.btNGame);
  28.         tvErtesito = (TextView)findViewById(R.id.tvNNotif);
  29.         glJatekter = (GridLayout)findViewById(R.id.nGField);
  30.         ujJatek.setOnClickListener(ng_ocl);
  31.        
  32.         for(int i=1; i<=9; i++) {
  33.             btGomb[i]= new Button(this);
  34.             btGomb[i].setTextSize(20);
  35.             btGomb[i].setId(i);
  36.             glJatekter.addView(btGomb[i]);
  37.             btGomb[i].setOnClickListener(ocl);
  38.            
  39.           }
  40.     }
  41.    
  42.     View.OnClickListener ocl = new View.OnClickListener() {
  43.        
  44.         @Override
  45.         public void onClick(View v) {
  46.             Button btAktuális = btGomb[v.getId()];
  47.               lépésSzám++;
  48.               játékos=felirat[(lépésSzám+1)%2];
  49.               tvErtesito.setText((lépésSzám+1)+". lépés: "+játékos);
  50.               btAktuális.setText(felirat[lépésSzám%2]);
  51.               btAktuális.setEnabled(false);
  52.               String nyertes=nyertes();
  53.               if(!nyertes.equals("")) {
  54.                 tvErtesito.setText("Eredmény: "+nyertes+" nyert!");
  55.                 for(int i=1; i<=9; i++)
  56.                   btGomb[i].setEnabled(false);
  57.               }
  58.               else if(lépésSzám==9)
  59.                   tvErtesito.setText("Eredmény: döntetlen!");
  60.     }};
  61.    
  62.     View.OnClickListener ng_ocl = new View.OnClickListener() {
  63.        
  64.         @Override
  65.         public void onClick(View v) {
  66.           lépésSzám=0;
  67.           játékos=felirat[(lépésSzám+1)%2];
  68.           tvErtesito.setText((lépésSzám+1)+". lépés: "+játékos);
  69.           for(int i=1; i<=9; i++) {
  70.             btGomb[i].setText("");
  71.             btGomb[i].setEnabled(true);        
  72.         }
  73.     }};
  74.     private String nyertes() {
  75.         int[] nyerő={123, 456, 789, 147, 258, 369, 159, 357};
  76.         String százas="", tízes, egyes;
  77.         for(int i: nyerő) {
  78.           százas=(String) btGomb[i/100].getText();
  79.           tízes= (String) btGomb[i%100/10].getText();
  80.           egyes= (String) btGomb[i%100%10].getText();
  81.           if(százas.equals(tízes) && tízes.equals(egyes))
  82.             return százas;
  83.         }
  84.         return "";
  85.       }
  86.    
  87.    
  88.    
  89.    
  90.     @Override
  91.     public boolean onCreateOptionsMenu(Menu menu) {
  92.         // Inflate the menu; this adds items to the action bar if it is present.
  93.         getMenuInflater().inflate(R.menu.main, menu);
  94.         return true;
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement