Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.sudoku;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class Game extends Activity implements OnClickListener {
- //Two-Dimension array for each square on the board.
- private Button[][] a,b,c,d,e,f,g,h,I;
- //9 Buttons for each square.
- private Button a1,a2,a3,a4,a5,a6,a7,a8,a9;
- private Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
- private Button c1,c2,c3,c4,c5,c6,c7,c8,c9;
- private Button d1,d2,d3,d4,d5,d6,d7,d8,d9;
- private Button e1,e2,e3,e4,e5,e6,e7,e8,e9;
- private Button f1,f2,f3,f4,f5,f6,f7,f8,f9;
- private Button g1,g2,g3,g4,g5,g6,g7,g8,g9;
- private Button h1,h2,h3,h4,h5,h6,h7,h8,h9;
- private Button i1,i2,i3,i4,i5,i6,i7,i8,i9;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_game);
- //Reseting the arrays.
- a=new Button[3][3];
- b=new Button[3][3];
- c=new Button[3][3];
- d=new Button[3][3];
- e=new Button[3][3];
- f=new Button[3][3];
- g=new Button[3][3];
- h=new Button[3][3];
- I=new Button[3][3];
- //View finding by ID.
- //a
- a1=(Button) findViewById(R.id.a1);
- a2=(Button) findViewById(R.id.a2);
- a3=(Button) findViewById(R.id.a3);
- a4=(Button) findViewById(R.id.a4);
- a5=(Button) findViewById(R.id.a5);
- a6=(Button) findViewById(R.id.a6);
- a7=(Button) findViewById(R.id.a7);
- a8=(Button) findViewById(R.id.a8);
- a9=(Button) findViewById(R.id.a9);
- //b
- b1=(Button) findViewById(R.id.b1);
- b2=(Button) findViewById(R.id.b2);
- b3=(Button) findViewById(R.id.b3);
- b4=(Button) findViewById(R.id.b4);
- b5=(Button) findViewById(R.id.b5);
- b6=(Button) findViewById(R.id.b6);
- b7=(Button) findViewById(R.id.b7);
- b8=(Button) findViewById(R.id.b8);
- b9=(Button) findViewById(R.id.b9);
- //c
- c1=(Button) findViewById(R.id.c1);
- c2=(Button) findViewById(R.id.c2);
- c3=(Button) findViewById(R.id.c3);
- c4=(Button) findViewById(R.id.c4);
- c5=(Button) findViewById(R.id.c5);
- c6=(Button) findViewById(R.id.c6);
- c7=(Button) findViewById(R.id.c7);
- c8=(Button) findViewById(R.id.c8);
- c9=(Button) findViewById(R.id.c9);
- //d
- d1=(Button) findViewById(R.id.d1);
- d2=(Button) findViewById(R.id.d2);
- d3=(Button) findViewById(R.id.d3);
- d4=(Button) findViewById(R.id.d4);
- d5=(Button) findViewById(R.id.d5);
- d6=(Button) findViewById(R.id.d6);
- d7=(Button) findViewById(R.id.d7);
- d8=(Button) findViewById(R.id.d8);
- d9=(Button) findViewById(R.id.d9);
- //e
- e1=(Button) findViewById(R.id.e1);
- e2=(Button) findViewById(R.id.e2);
- e3=(Button) findViewById(R.id.e3);
- e4=(Button) findViewById(R.id.e4);
- e5=(Button) findViewById(R.id.e5);
- e6=(Button) findViewById(R.id.e6);
- e7=(Button) findViewById(R.id.e7);
- e8=(Button) findViewById(R.id.e8);
- e9=(Button) findViewById(R.id.e9);
- //f
- f1=(Button) findViewById(R.id.f1);
- f2=(Button) findViewById(R.id.f2);
- f3=(Button) findViewById(R.id.f3);
- f4=(Button) findViewById(R.id.f4);
- f5=(Button) findViewById(R.id.f5);
- f6=(Button) findViewById(R.id.f6);
- f7=(Button) findViewById(R.id.f7);
- f8=(Button) findViewById(R.id.asd);
- f9=(Button) findViewById(R.id.sd);
- //g
- g1=(Button) findViewById(R.id.g1);
- g2=(Button) findViewById(R.id.g2);
- g3=(Button) findViewById(R.id.g3);
- g4=(Button) findViewById(R.id.g4);
- g5=(Button) findViewById(R.id.g5);
- g6=(Button) findViewById(R.id.g6);
- g7=(Button) findViewById(R.id.g7);
- g8=(Button) findViewById(R.id.g8);
- g9=(Button) findViewById(R.id.g9);
- //h
- h1=(Button) findViewById(R.id.h1);
- h2=(Button) findViewById(R.id.h2);
- h3=(Button) findViewById(R.id.h3);
- h4=(Button) findViewById(R.id.h4);
- h5=(Button) findViewById(R.id.h5);
- h6=(Button) findViewById(R.id.h6);
- h7=(Button) findViewById(R.id.h7);
- h8=(Button) findViewById(R.id.h8);
- h9=(Button) findViewById(R.id.h9);
- //i
- i1=(Button) findViewById(R.id.i1);
- i1=(Button) findViewById(R.id.i1);
- i1=(Button) findViewById(R.id.i1);
- i1=(Button) findViewById(R.id.i1);
- i1=(Button) findViewById(R.id.i1);
- i1=(Button) findViewById(R.id.i1);
- i1=(Button) findViewById(R.id.i1);
- i1=(Button) findViewById(R.id.i1);
- i1=(Button) findViewById(R.id.i1);
- //Adding OnClick Listeners to the buttons.
- //a
- a1.setOnClickListener(this);
- a2.setOnClickListener(this);
- a3.setOnClickListener(this);
- a4.setOnClickListener(this);
- a5.setOnClickListener(this);
- a6.setOnClickListener(this);
- a7.setOnClickListener(this);
- a8.setOnClickListener(this);
- a9.setOnClickListener(this);
- //b
- b1.setOnClickListener(this);
- b2.setOnClickListener(this);
- b3.setOnClickListener(this);
- b4.setOnClickListener(this);
- b5.setOnClickListener(this);
- b6.setOnClickListener(this);
- b7.setOnClickListener(this);
- b8.setOnClickListener(this);
- b9.setOnClickListener(this);
- //c
- c1.setOnClickListener(this);
- c2.setOnClickListener(this);
- c3.setOnClickListener(this);
- c4.setOnClickListener(this);
- c5.setOnClickListener(this);
- c6.setOnClickListener(this);
- c7.setOnClickListener(this);
- c8.setOnClickListener(this);
- c9.setOnClickListener(this);
- //d
- d1.setOnClickListener(this);
- d2.setOnClickListener(this);
- d3.setOnClickListener(this);
- d4.setOnClickListener(this);
- d5.setOnClickListener(this);
- d6.setOnClickListener(this);
- d7.setOnClickListener(this);
- d8.setOnClickListener(this);
- d9.setOnClickListener(this);
- //e
- e1.setOnClickListener(this);
- e2.setOnClickListener(this);
- e3.setOnClickListener(this);
- e4.setOnClickListener(this);
- e5.setOnClickListener(this);
- e6.setOnClickListener(this);
- e7.setOnClickListener(this);
- e8.setOnClickListener(this);
- e9.setOnClickListener(this);
- //f
- f1.setOnClickListener(this);
- f2.setOnClickListener(this);
- f3.setOnClickListener(this);
- f4.setOnClickListener(this);
- f5.setOnClickListener(this);
- f6.setOnClickListener(this);
- f7.setOnClickListener(this);
- f8.setOnClickListener(this);
- f9.setOnClickListener(this);
- //g
- g1.setOnClickListener(this);
- g2.setOnClickListener(this);
- g3.setOnClickListener(this);
- g4.setOnClickListener(this);
- g5.setOnClickListener(this);
- g6.setOnClickListener(this);
- g7.setOnClickListener(this);
- g8.setOnClickListener(this);
- g9.setOnClickListener(this);
- //h
- h1.setOnClickListener(this);
- h2.setOnClickListener(this);
- h3.setOnClickListener(this);
- h4.setOnClickListener(this);
- h5.setOnClickListener(this);
- h6.setOnClickListener(this);
- h7.setOnClickListener(this);
- h8.setOnClickListener(this);
- h9.setOnClickListener(this);
- //i
- i1.setOnClickListener(this);
- i2.setOnClickListener(this);
- i3.setOnClickListener(this);
- i4.setOnClickListener(this);
- i5.setOnClickListener(this);
- i6.setOnClickListener(this);
- i7.setOnClickListener(this);
- i8.setOnClickListener(this);
- i9.setOnClickListener(this);
- //Inserting the buttons into the suitable arrays.
- //a
- a[0][0]=a1;
- a[0][1]=a2;
- a[0][2]=a3;
- a[1][0]=a4;
- a[1][1]=a5;
- a[1][2]=a6;
- a[2][0]=a7;
- a[2][1]=a8;
- a[2][2]=a9;
- //b
- b[0][0]=b1;
- b[0][1]=b2;
- b[0][2]=b3;
- b[1][0]=b4;
- b[1][1]=b5;
- b[1][2]=b6;
- b[2][0]=b7;
- b[2][1]=b8;
- b[2][2]=b9;
- //c
- c[0][0]=c1;
- c[0][1]=c2;
- c[0][2]=c3;
- c[1][0]=c4;
- c[1][1]=c5;
- c[1][2]=c6;
- c[2][0]=c7;
- c[2][1]=c8;
- c[2][2]=c9;
- //d
- d[0][0]=d1;
- d[0][1]=d2;
- d[0][2]=d3;
- d[1][0]=d4;
- d[1][1]=d5;
- d[1][2]=d6;
- d[2][0]=d7;
- d[2][1]=d8;
- d[2][2]=d9;
- //e
- e[0][0]=e1;
- e[0][1]=e2;
- e[0][2]=e3;
- e[1][0]=e4;
- e[1][1]=e5;
- e[1][2]=e6;
- e[2][0]=e7;
- e[2][1]=e8;
- e[2][2]=e9;
- //f
- f[0][0]=f1;
- f[0][1]=f2;
- f[0][2]=f3;
- f[1][0]=f4;
- f[1][1]=f5;
- f[1][2]=f6;
- f[2][0]=f7;
- f[2][1]=f8;
- f[2][2]=f9;
- //g
- g[0][0]=g1;
- g[0][1]=g2;
- g[0][2]=g3;
- g[1][0]=g4;
- g[1][1]=g5;
- g[1][2]=g6;
- g[2][0]=g7;
- g[2][1]=g8;
- g[2][2]=g9;
- //h
- h[0][0]=h1;
- h[0][1]=h2;
- h[0][2]=h3;
- h[1][0]=h4;
- h[1][1]=h5;
- h[1][2]=h6;
- h[2][0]=h7;
- h[2][1]=h8;
- h[2][2]=h9;
- //i
- I[0][0]=i1;
- I[0][1]=i2;
- I[0][2]=i3;
- I[1][0]=i4;
- I[1][1]=i5;
- I[1][2]=i6;
- I[2][0]=i7;
- I[2][1]=i8;
- I[2][2]=i9;
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.game, menu);
- return true;
- }
- @Override
- public void onClick(View v) {
- Button btn = (Button)v;
- for(int i=0;i<=2;i++)
- {
- for(int j=0;j<=2;j++)
- {
- if(a[i][j]==btn)
- {
- if(a[i][j].getText()==null)
- a[i][j].setText("1");
- if(Integer.parseInt(a[i][j].getText().toString())==9)
- {
- a[i][j].setText(" ");
- }
- else
- {
- int num=Integer.parseInt(a[i][j].getText().toString());
- num=num+1;
- String st= ""+num;
- a[i][j].setText(st);
- }
- }
- if(b[i][j]==btn)
- {
- if(b[i][j].getText()==null)
- b[i][j].setText("1");
- if(Integer.parseInt(b[i][j].getText().toString())==9)
- {
- b[i][j].setText(" ");
- }
- else
- {
- int num=Integer.parseInt(b[i][j].getText().toString());
- num=num+1;
- String st= ""+num;
- b[i][j].setText(st);
- }
- }
- if(c[i][j]==btn)
- {
- if(c[i][j].getText()==null)
- c[i][j].setText("1");
- if(Integer.parseInt(c[i][j].getText().toString())==9)
- {
- c[i][j].setText(" ");
- }
- else
- {
- int num=Integer.parseInt(c[i][j].getText().toString());
- num=num+1;
- String st= ""+num;
- c[i][j].setText(st);
- }
- }
- if(d[i][j]==btn)
- {
- if(d[i][j].getText()==null)
- d[i][j].setText("1");
- if(Integer.parseInt(d[i][j].getText().toString())==9)
- {
- d[i][j].setText(" ");
- }
- else
- {
- int num=Integer.parseInt(d[i][j].getText().toString());
- num=num+1;
- String st= ""+num;
- d[i][j].setText(st);
- }
- }
- if(e[i][j]==btn)
- {
- if(e[i][j].getText()==null)
- e[i][j].setText("1");
- if(Integer.parseInt(e[i][j].getText().toString())==9)
- {
- e[i][j].setText(" ");
- }
- else
- {
- int num=Integer.parseInt(e[i][j].getText().toString());
- num=num+1;
- String st= ""+num;
- e[i][j].setText(st);
- }
- }
- if(f[i][j]==btn)
- {
- if(f[i][j].getText()==null)
- f[i][j].setText("1");
- if(Integer.parseInt(f[i][j].getText().toString())==9)
- {
- f[i][j].setText(" ");
- }
- else
- {
- int num=Integer.parseInt(f[i][j].getText().toString());
- num=num+1;
- String st= ""+num;
- f[i][j].setText(st);
- }
- }
- if(g[i][j]==btn)
- {
- if(g[i][j].getText()==null)
- g[i][j].setText("1");
- if(Integer.parseInt(g[i][j].getText().toString())==9)
- {
- g[i][j].setText(" ");
- }
- else
- {
- int num=Integer.parseInt(g[i][j].getText().toString());
- num=num+1;
- String st= ""+num;
- g[i][j].setText(st);
- }
- }
- if(h[i][j]==btn)
- {
- if(h[i][j].getText()==null)
- h[i][j].setText("1");
- if(Integer.parseInt(h[i][j].getText().toString())==9)
- {
- h[i][j].setText(" ");
- }
- else
- {
- int num=Integer.parseInt(h[i][j].getText().toString());
- num=num+1;
- String st= ""+num;
- h[i][j].setText(st);
- }
- }
- if(I[i][j]==btn)
- {
- if(I[i][j].getText()==null)
- I[i][j].setText("1");
- if(Integer.parseInt(I[i][j].getText().toString())==9)
- {
- I[i][j].setText(" ");
- }
- else
- {
- int num=Integer.parseInt(I[i][j].getText().toString());
- num=num+1;
- String st= ""+num;
- I[i][j].setText(st);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment