I don't truly understand how OOP works and It's stopping me from creating good programs! Below I have a program that will check to see if there are 3 tiles from a gridview in a row.
Here's a screenshot of the game.
Basically you're not supposed to get 3 of the same colours in a row! But at the moment I've been adding in the basic functionality. So eventually Im going to add in methods to restore the gameboard to grey, start a timer to see how quickly they can fill the board up without getting 3 in a row, etc. So this is why Im really wanting to understand how else I could write this part of my program!
At the moment it's all in the one class (game.java
) which I don't think is very OO. The Items()
class will just set the tile to a different colour!
package com.example.assignment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Game extends Activity {
GridView gridView;
Item[] gridArray = new Item[16];
ImageAdapter iAdapter;
/*
* Here is the amount of collumns in the app - This is used to get the y and
* x position
*/
int columncount = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
// 4X4 array
for (int i = 0; i < 16; i++) {
gridArray[i] = new Item(R.drawable.grey, "grey", i);
}
GridView gridview = (GridView) findViewById(R.id.gridview);
iAdapter = new ImageAdapter(this, gridArray);
gridview.setAdapter(iAdapter);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
/*
* Here is where we get the clicked position Simply divide the
* position by the columncount (9) to get the x and do the same
* for the y but instead change the divide to a modulus which
* will get the remainder note: use this for the assignment
*/
int x = position / columncount;
int y = position % columncount;
int c = gridArray[position].nextColor();
// Run the checkWinner method that will check if
checkWinner();
((ImageView) v).setImageResource(c);
/*
* If you want to display the collumns/rows not based on a 0
* index then use this toast
* Toast.makeText(getApplicationContext(),(x+1) + " " +
* (y+1),Toast.LENGTH_SHORT).show(); All this does
*/
Toast.makeText(getApplicationContext(), x + " " + y,
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void checkWinner() {
// Check for horizontal loss
for (int i = 0; i < 16; i += 4) {
// Check for white
if ((gridArray[i].getRDC() == R.drawable.white
&& gridArray[i + 1].getRDC() == R.drawable.white && gridArray[i + 2]
.getRDC() == R.drawable.white)
|| (gridArray[i + 1].getRDC() == R.drawable.white
&& gridArray[i + 2].getRDC() == R.drawable.white && gridArray[i + 3]
.getRDC() == R.drawable.white)) {
Toast.makeText(getApplicationContext(),
"THREE WHITE IN A ROW HORIZONTALLY", Toast.LENGTH_SHORT)
.show();
Log.d("MyApp", "3 in row horizontally WHITE");
}
// Check for red
if ((gridArray[i].getRDC() == R.drawable.red
&& gridArray[i + 1].getRDC() == R.drawable.red && gridArray[i + 2]
.getRDC() == R.drawable.red)
|| (gridArray[i + 1].getRDC() == R.drawable.white
&& gridArray[i + 2].getRDC() == R.drawable.red && gridArray[i + 3]
.getRDC() == R.drawable.red)) {
Toast.makeText(getApplicationContext(),
"THREE RED IN A ROW HORIZONTALLY", Toast.LENGTH_SHORT)
.show();
Log.d("MyApp", "3 in row horizontally RED");
}
}
// Check for vertical loss
for (int i = 0; i <= 7; i++) {
/*
* This for loop is looping through each column and checking if 3 drawables are in a row
*/
//Check for white loss
if (gridArray[i].getRDC() == R.drawable.white
&& gridArray[i + 4].getRDC() == R.drawable.white
&& gridArray[i + 8].getRDC() == R.drawable.white) {
Toast.makeText(getApplicationContext(),
"THREE WHITE IN A ROW VERTICALLY", Toast.LENGTH_SHORT).show();
Log.d("MyApp", "3 in row vertically WHITE");
}
//Check for red loss
if (gridArray[i].getRDC() == R.drawable.red
&& gridArray[i + 4].getRDC() == R.drawable.red
&& gridArray[i + 8].getRDC() == R.drawable.red) {
Toast.makeText(getApplicationContext(),
"THREE RED IN A ROW VERTICALLY", Toast.LENGTH_SHORT).show();
Log.d("MyApp", "3 in row vertically RED");
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent i;
switch (item.getItemId()) {
case R.id.homeMenuItem:
i = new Intent(this, Main.class);
startActivity(i);
return true;
case R.id.settingMenuItem:
i = new Intent(this, Setting.class);
startActivity(i);
return true;
case R.id.highscoreMenuItem:
i = new Intent(this, Highscores.class);
startActivity(i);
return true;
case R.id.gamesLabel:
i = new Intent(this, Game.class);
startActivity(i);
return true;
case R.id.helpLabel:
i = new Intent(this, help.class);
startActivity(i);
return true;
}
return false;
}
}
package com.example.assignment;
I'm guessing that this is homework. \$\endgroup\$