Guest User

Game.java

a guest
Sep 2nd, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.78 KB | None | 0 0
  1. package com.example.sudoku;
  2.  
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9.  
  10. public class Game extends Activity implements OnClickListener {
  11.     //Two-Dimension array for each square on the board.
  12.     private Button[][] a,b,c,d,e,f,g,h,I;
  13.     //9 Buttons for each square.
  14.     private Button a1,a2,a3,a4,a5,a6,a7,a8,a9;
  15.     private Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
  16.     private Button c1,c2,c3,c4,c5,c6,c7,c8,c9;
  17.     private Button d1,d2,d3,d4,d5,d6,d7,d8,d9;
  18.     private Button e1,e2,e3,e4,e5,e6,e7,e8,e9;
  19.     private Button f1,f2,f3,f4,f5,f6,f7,f8,f9;
  20.     private Button g1,g2,g3,g4,g5,g6,g7,g8,g9;
  21.     private Button h1,h2,h3,h4,h5,h6,h7,h8,h9;
  22.     private Button i1,i2,i3,i4,i5,i6,i7,i8,i9;
  23.  
  24.     @Override
  25.     protected void onCreate(Bundle savedInstanceState) {
  26.         super.onCreate(savedInstanceState);
  27.         setContentView(R.layout.activity_game);
  28.         //Reseting the arrays.
  29.         a=new Button[3][3];
  30.         b=new Button[3][3];
  31.         c=new Button[3][3];
  32.         d=new Button[3][3];
  33.         e=new Button[3][3];
  34.         f=new Button[3][3];
  35.         g=new Button[3][3];
  36.         h=new Button[3][3];
  37.         I=new Button[3][3];
  38.         //View finding by ID.
  39.         //a
  40.                 a1=(Button) findViewById(R.id.a1);
  41.                 a2=(Button) findViewById(R.id.a2);
  42.                 a3=(Button) findViewById(R.id.a3);
  43.                 a4=(Button) findViewById(R.id.a4);
  44.                 a5=(Button) findViewById(R.id.a5);
  45.                 a6=(Button) findViewById(R.id.a6);
  46.                 a7=(Button) findViewById(R.id.a7);
  47.                 a8=(Button) findViewById(R.id.a8);
  48.                 a9=(Button) findViewById(R.id.a9);
  49.                 //b
  50.                 b1=(Button) findViewById(R.id.b1);
  51.                 b2=(Button) findViewById(R.id.b2);
  52.                 b3=(Button) findViewById(R.id.b3);
  53.                 b4=(Button) findViewById(R.id.b4);
  54.                 b5=(Button) findViewById(R.id.b5);
  55.                 b6=(Button) findViewById(R.id.b6);
  56.                 b7=(Button) findViewById(R.id.b7);
  57.                 b8=(Button) findViewById(R.id.b8);
  58.                 b9=(Button) findViewById(R.id.b9);
  59.                 //c
  60.                 c1=(Button) findViewById(R.id.c1);
  61.                 c2=(Button) findViewById(R.id.c2);
  62.                 c3=(Button) findViewById(R.id.c3);
  63.                 c4=(Button) findViewById(R.id.c4);
  64.                 c5=(Button) findViewById(R.id.c5);
  65.                 c6=(Button) findViewById(R.id.c6);
  66.                 c7=(Button) findViewById(R.id.c7);
  67.                 c8=(Button) findViewById(R.id.c8);
  68.                 c9=(Button) findViewById(R.id.c9);
  69.                 //d
  70.                 d1=(Button) findViewById(R.id.d1);
  71.                 d2=(Button) findViewById(R.id.d2);
  72.                 d3=(Button) findViewById(R.id.d3);
  73.                 d4=(Button) findViewById(R.id.d4);
  74.                 d5=(Button) findViewById(R.id.d5);
  75.                 d6=(Button) findViewById(R.id.d6);
  76.                 d7=(Button) findViewById(R.id.d7);
  77.                 d8=(Button) findViewById(R.id.d8);
  78.                 d9=(Button) findViewById(R.id.d9);
  79.                 //e
  80.                 e1=(Button) findViewById(R.id.e1);
  81.                 e2=(Button) findViewById(R.id.e2);
  82.                 e3=(Button) findViewById(R.id.e3);
  83.                 e4=(Button) findViewById(R.id.e4);
  84.                 e5=(Button) findViewById(R.id.e5);
  85.                 e6=(Button) findViewById(R.id.e6);
  86.                 e7=(Button) findViewById(R.id.e7);
  87.                 e8=(Button) findViewById(R.id.e8);
  88.                 e9=(Button) findViewById(R.id.e9);
  89.                 //f
  90.                 f1=(Button) findViewById(R.id.f1);
  91.                 f2=(Button) findViewById(R.id.f2);
  92.                 f3=(Button) findViewById(R.id.f3);
  93.                 f4=(Button) findViewById(R.id.f4);
  94.                 f5=(Button) findViewById(R.id.f5);
  95.                 f6=(Button) findViewById(R.id.f6);
  96.                 f7=(Button) findViewById(R.id.f7);
  97.                 f8=(Button) findViewById(R.id.asd);
  98.                 f9=(Button) findViewById(R.id.sd);
  99.                 //g
  100.                 g1=(Button) findViewById(R.id.g1);
  101.                 g2=(Button) findViewById(R.id.g2);
  102.                 g3=(Button) findViewById(R.id.g3);
  103.                 g4=(Button) findViewById(R.id.g4);
  104.                 g5=(Button) findViewById(R.id.g5);
  105.                 g6=(Button) findViewById(R.id.g6);
  106.                 g7=(Button) findViewById(R.id.g7);
  107.                 g8=(Button) findViewById(R.id.g8);
  108.                 g9=(Button) findViewById(R.id.g9);
  109.                 //h
  110.                 h1=(Button) findViewById(R.id.h1);
  111.                 h2=(Button) findViewById(R.id.h2);
  112.                 h3=(Button) findViewById(R.id.h3);
  113.                 h4=(Button) findViewById(R.id.h4);
  114.                 h5=(Button) findViewById(R.id.h5);
  115.                 h6=(Button) findViewById(R.id.h6);
  116.                 h7=(Button) findViewById(R.id.h7);
  117.                 h8=(Button) findViewById(R.id.h8);
  118.                 h9=(Button) findViewById(R.id.h9);
  119.                 //i
  120.                 i1=(Button) findViewById(R.id.i1);
  121.                 i1=(Button) findViewById(R.id.i1);
  122.                 i1=(Button) findViewById(R.id.i1);
  123.                 i1=(Button) findViewById(R.id.i1);
  124.                 i1=(Button) findViewById(R.id.i1);
  125.                 i1=(Button) findViewById(R.id.i1);
  126.                 i1=(Button) findViewById(R.id.i1);
  127.                 i1=(Button) findViewById(R.id.i1);
  128.                 i1=(Button) findViewById(R.id.i1);
  129.                
  130.                 //Adding OnClick Listeners to the buttons.
  131.                 //a
  132.                 a1.setOnClickListener(this);
  133.                 a2.setOnClickListener(this);
  134.                 a3.setOnClickListener(this);
  135.                 a4.setOnClickListener(this);
  136.                 a5.setOnClickListener(this);
  137.                 a6.setOnClickListener(this);
  138.                 a7.setOnClickListener(this);
  139.                 a8.setOnClickListener(this);
  140.                 a9.setOnClickListener(this);
  141.                 //b
  142.                 b1.setOnClickListener(this);
  143.                 b2.setOnClickListener(this);
  144.                 b3.setOnClickListener(this);
  145.                 b4.setOnClickListener(this);
  146.                 b5.setOnClickListener(this);
  147.                 b6.setOnClickListener(this);
  148.                 b7.setOnClickListener(this);
  149.                 b8.setOnClickListener(this);
  150.                 b9.setOnClickListener(this);
  151.                 //c
  152.                 c1.setOnClickListener(this);
  153.                 c2.setOnClickListener(this);
  154.                 c3.setOnClickListener(this);
  155.                 c4.setOnClickListener(this);
  156.                 c5.setOnClickListener(this);
  157.                 c6.setOnClickListener(this);
  158.                 c7.setOnClickListener(this);
  159.                 c8.setOnClickListener(this);
  160.                 c9.setOnClickListener(this);
  161.                 //d
  162.                 d1.setOnClickListener(this);
  163.                 d2.setOnClickListener(this);
  164.                 d3.setOnClickListener(this);
  165.                 d4.setOnClickListener(this);
  166.                 d5.setOnClickListener(this);
  167.                 d6.setOnClickListener(this);
  168.                 d7.setOnClickListener(this);
  169.                 d8.setOnClickListener(this);
  170.                 d9.setOnClickListener(this);
  171.                 //e
  172.                 e1.setOnClickListener(this);
  173.                 e2.setOnClickListener(this);
  174.                 e3.setOnClickListener(this);
  175.                 e4.setOnClickListener(this);
  176.                 e5.setOnClickListener(this);
  177.                 e6.setOnClickListener(this);
  178.                 e7.setOnClickListener(this);
  179.                 e8.setOnClickListener(this);
  180.                 e9.setOnClickListener(this);
  181.                 //f
  182.                 f1.setOnClickListener(this);
  183.                 f2.setOnClickListener(this);
  184.                 f3.setOnClickListener(this);
  185.                 f4.setOnClickListener(this);
  186.                 f5.setOnClickListener(this);
  187.                 f6.setOnClickListener(this);
  188.                 f7.setOnClickListener(this);
  189.                 f8.setOnClickListener(this);
  190.                 f9.setOnClickListener(this);
  191.                 //g
  192.                 g1.setOnClickListener(this);
  193.                 g2.setOnClickListener(this);
  194.                 g3.setOnClickListener(this);
  195.                 g4.setOnClickListener(this);
  196.                 g5.setOnClickListener(this);
  197.                 g6.setOnClickListener(this);
  198.                 g7.setOnClickListener(this);
  199.                 g8.setOnClickListener(this);
  200.                 g9.setOnClickListener(this);
  201.                 //h
  202.                 h1.setOnClickListener(this);
  203.                 h2.setOnClickListener(this);
  204.                 h3.setOnClickListener(this);
  205.                 h4.setOnClickListener(this);
  206.                 h5.setOnClickListener(this);
  207.                 h6.setOnClickListener(this);
  208.                 h7.setOnClickListener(this);
  209.                 h8.setOnClickListener(this);
  210.                 h9.setOnClickListener(this);
  211.                 //i
  212.                 i1.setOnClickListener(this);
  213.                 i2.setOnClickListener(this);
  214.                 i3.setOnClickListener(this);
  215.                 i4.setOnClickListener(this);
  216.                 i5.setOnClickListener(this);
  217.                 i6.setOnClickListener(this);
  218.                 i7.setOnClickListener(this);
  219.                 i8.setOnClickListener(this);
  220.                 i9.setOnClickListener(this);
  221.                 //Inserting the buttons into the suitable arrays.
  222.                 //a
  223.                 a[0][0]=a1;
  224.                 a[0][1]=a2;
  225.                 a[0][2]=a3;
  226.                 a[1][0]=a4;
  227.                 a[1][1]=a5;
  228.                 a[1][2]=a6;
  229.                 a[2][0]=a7;
  230.                 a[2][1]=a8;
  231.                 a[2][2]=a9;
  232.                 //b
  233.                 b[0][0]=b1;
  234.                 b[0][1]=b2;
  235.                 b[0][2]=b3;
  236.                 b[1][0]=b4;
  237.                 b[1][1]=b5;
  238.                 b[1][2]=b6;
  239.                 b[2][0]=b7;
  240.                 b[2][1]=b8;
  241.                 b[2][2]=b9;
  242.                 //c
  243.                 c[0][0]=c1;
  244.                 c[0][1]=c2;
  245.                 c[0][2]=c3;
  246.                 c[1][0]=c4;
  247.                 c[1][1]=c5;
  248.                 c[1][2]=c6;
  249.                 c[2][0]=c7;
  250.                 c[2][1]=c8;
  251.                 c[2][2]=c9;
  252.                 //d
  253.                 d[0][0]=d1;
  254.                 d[0][1]=d2;
  255.                 d[0][2]=d3;
  256.                 d[1][0]=d4;
  257.                 d[1][1]=d5;
  258.                 d[1][2]=d6;
  259.                 d[2][0]=d7;
  260.                 d[2][1]=d8;
  261.                 d[2][2]=d9;
  262.                 //e
  263.                 e[0][0]=e1;
  264.                 e[0][1]=e2;
  265.                 e[0][2]=e3;
  266.                 e[1][0]=e4;
  267.                 e[1][1]=e5;
  268.                 e[1][2]=e6;
  269.                 e[2][0]=e7;
  270.                 e[2][1]=e8;
  271.                 e[2][2]=e9;
  272.                 //f
  273.                 f[0][0]=f1;
  274.                 f[0][1]=f2;
  275.                 f[0][2]=f3;
  276.                 f[1][0]=f4;
  277.                 f[1][1]=f5;
  278.                 f[1][2]=f6;
  279.                 f[2][0]=f7;
  280.                 f[2][1]=f8;
  281.                 f[2][2]=f9;
  282.                 //g
  283.                 g[0][0]=g1;
  284.                 g[0][1]=g2;
  285.                 g[0][2]=g3;
  286.                 g[1][0]=g4;
  287.                 g[1][1]=g5;
  288.                 g[1][2]=g6;
  289.                 g[2][0]=g7;
  290.                 g[2][1]=g8;
  291.                 g[2][2]=g9;
  292.                 //h
  293.                 h[0][0]=h1;
  294.                 h[0][1]=h2;
  295.                 h[0][2]=h3;
  296.                 h[1][0]=h4;
  297.                 h[1][1]=h5;
  298.                 h[1][2]=h6;
  299.                 h[2][0]=h7;
  300.                 h[2][1]=h8;
  301.                 h[2][2]=h9;
  302.                 //i
  303.                 I[0][0]=i1;
  304.                 I[0][1]=i2;
  305.                 I[0][2]=i3;
  306.                 I[1][0]=i4;
  307.                 I[1][1]=i5;
  308.                 I[1][2]=i6;
  309.                 I[2][0]=i7;
  310.                 I[2][1]=i8;
  311.                 I[2][2]=i9;
  312.     }
  313.  
  314.     @Override
  315.     public boolean onCreateOptionsMenu(Menu menu) {
  316.         // Inflate the menu; this adds items to the action bar if it is present.
  317.         getMenuInflater().inflate(R.menu.game, menu);
  318.         return true;
  319.     }
  320.  
  321.     @Override
  322.     public void onClick(View v) {
  323.         Button btn = (Button)v;
  324.         for(int i=0;i<=2;i++)
  325.         {
  326.             for(int j=0;j<=2;j++)
  327.             {
  328.                 if(a[i][j]==btn)
  329.                 {
  330.                     if(a[i][j].getText()==null)
  331.                         a[i][j].setText("1");
  332.                     if(Integer.parseInt(a[i][j].getText().toString())==9)
  333.                     {
  334.                         a[i][j].setText(" ");
  335.                     }
  336.                     else
  337.                     {
  338.                         int num=Integer.parseInt(a[i][j].getText().toString());
  339.                         num=num+1;
  340.                         String st= ""+num;
  341.                         a[i][j].setText(st);
  342.                     }
  343.                 }
  344.                 if(b[i][j]==btn)
  345.                 {
  346.                     if(b[i][j].getText()==null)
  347.                         b[i][j].setText("1");
  348.                     if(Integer.parseInt(b[i][j].getText().toString())==9)
  349.                     {
  350.                         b[i][j].setText(" ");
  351.                     }
  352.                     else
  353.                     {
  354.                         int num=Integer.parseInt(b[i][j].getText().toString());
  355.                         num=num+1;
  356.                         String st= ""+num;
  357.                         b[i][j].setText(st);
  358.                     }
  359.                 }
  360.                 if(c[i][j]==btn)
  361.                 {
  362.                     if(c[i][j].getText()==null)
  363.                         c[i][j].setText("1");
  364.                     if(Integer.parseInt(c[i][j].getText().toString())==9)
  365.                     {
  366.                         c[i][j].setText(" ");
  367.                     }
  368.                     else
  369.                     {
  370.                         int num=Integer.parseInt(c[i][j].getText().toString());
  371.                         num=num+1;
  372.                         String st= ""+num;
  373.                         c[i][j].setText(st);
  374.                     }
  375.                 }
  376.                 if(d[i][j]==btn)
  377.                 {
  378.                     if(d[i][j].getText()==null)
  379.                         d[i][j].setText("1");
  380.                     if(Integer.parseInt(d[i][j].getText().toString())==9)
  381.                     {
  382.                         d[i][j].setText(" ");
  383.                     }
  384.                     else
  385.                     {
  386.                         int num=Integer.parseInt(d[i][j].getText().toString());
  387.                         num=num+1;
  388.                         String st= ""+num;
  389.                         d[i][j].setText(st);
  390.                     }
  391.                 }
  392.                 if(e[i][j]==btn)
  393.                 {
  394.                     if(e[i][j].getText()==null)
  395.                         e[i][j].setText("1");
  396.                     if(Integer.parseInt(e[i][j].getText().toString())==9)
  397.                     {
  398.                         e[i][j].setText(" ");
  399.                     }
  400.                     else
  401.                     {
  402.                         int num=Integer.parseInt(e[i][j].getText().toString());
  403.                         num=num+1;
  404.                         String st= ""+num;
  405.                         e[i][j].setText(st);
  406.                     }
  407.                 }
  408.                 if(f[i][j]==btn)
  409.                 {
  410.                     if(f[i][j].getText()==null)
  411.                         f[i][j].setText("1");
  412.                     if(Integer.parseInt(f[i][j].getText().toString())==9)
  413.                     {
  414.                         f[i][j].setText(" ");
  415.                     }
  416.                     else
  417.                     {
  418.                         int num=Integer.parseInt(f[i][j].getText().toString());
  419.                         num=num+1;
  420.                         String st= ""+num;
  421.                         f[i][j].setText(st);
  422.                     }
  423.                 }
  424.                 if(g[i][j]==btn)
  425.                 {
  426.                     if(g[i][j].getText()==null)
  427.                         g[i][j].setText("1");
  428.                     if(Integer.parseInt(g[i][j].getText().toString())==9)
  429.                     {
  430.                         g[i][j].setText(" ");
  431.                     }
  432.                     else
  433.                     {
  434.                         int num=Integer.parseInt(g[i][j].getText().toString());
  435.                         num=num+1;
  436.                         String st= ""+num;
  437.                         g[i][j].setText(st);
  438.                     }
  439.                 }
  440.                 if(h[i][j]==btn)
  441.                 {
  442.                     if(h[i][j].getText()==null)
  443.                         h[i][j].setText("1");
  444.                     if(Integer.parseInt(h[i][j].getText().toString())==9)
  445.                     {
  446.                         h[i][j].setText(" ");
  447.                     }
  448.                     else
  449.                     {
  450.                         int num=Integer.parseInt(h[i][j].getText().toString());
  451.                         num=num+1;
  452.                         String st= ""+num;
  453.                         h[i][j].setText(st);
  454.                     }
  455.                 }
  456.                 if(I[i][j]==btn)
  457.                 {
  458.                     if(I[i][j].getText()==null)
  459.                         I[i][j].setText("1");
  460.                     if(Integer.parseInt(I[i][j].getText().toString())==9)
  461.                     {
  462.                         I[i][j].setText(" ");
  463.                     }
  464.                     else
  465.                     {
  466.                         int num=Integer.parseInt(I[i][j].getText().toString());
  467.                         num=num+1;
  468.                         String st= ""+num;
  469.                         I[i][j].setText(st);
  470.                     }
  471.                 }
  472.             }
  473.         }
  474.        
  475.     }
  476.  
  477. }
Advertisement
Add Comment
Please, Sign In to add comment