app/manifests/AndroidManifest.xml - to force orientation
app/res/drawable - pictures
There are 6 x 16 image files (17 per style). The styles are "close", "loose", "stone", "simple", "roman" and "border" and the files are, e.g., "close0" (background) to "close16". They give the various parts of the knot. They are loaded into an arrays defined in in app/java/com.(the project name)/MainActivity and used in app/java/com.(the project name)/ImagePalette and app/java/com.(the project name)/ImageTile. The number of styles (6) is defined in app/java/com.(the project name)/MainActivity.
app/res/mipmap/ic_launcher.png/ - icon for launching app
app/res/values/strings.xml - strings
CelticKnotGameMake knot by tapping on tile to rotatenewchange stylesmallmediumlarge" ""Knot is complete! Click on New."
app/res/layout/activity_main.xml - screen format
app/java/com.(the project name)/MainActivity - main code for app
package com.edkins.celticknotgame;
import ...
public class MainActivity extends AppCompatActivity {
// style of knot and number of styles
public static int styleType = 1;
public static int styleNum = 6;
public static Random randomNum = new Random();
public static int width = 4;
public static int complete = 0;
// tiles - for setting up tessera
public static int [] tile = new int [100];
public static int [] ok = new int [100];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newKnot();
GridView matrix = (GridView) findViewById(R.id.tileView);
matrix.setAdapter(new ImageTile(this));
matrix.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView> parent, View v, int position, long id) {
processTileClick(position);
}
});
RadioButton size = (RadioButton) findViewById(R.id.sizeSmall);
size.setChecked(true);
width = 4;
}
// Main process - tap to rotate pattern plus check if knot completed
public void processTileClick(int position) {
if (complete==1) {return;}
if (tile[position] < 2 || tile[position]== 16) {return;}
if (tile [position] == 5) {tile[position] = 2;}
else if (tile [position] == 9) {tile[position] = 6;}
else if (tile [position] == 11) {tile[position] = 10;}
else if (tile [position] == 15) {tile[position] = 12;}
else {tile[position] ++;}
redo();
complete = 1;
for (int i=0; i<100; i++) {
if (ok [i] != tile[i]) {complete = i;}
}
if (complete == 1) {hooray();}
}
// next game
public void newKnot() {
complete = 0;
TextView finish = (TextView) findViewById(R.id.finishMessage);
finish.setText(R.string.space);
setTilesToOne();
makeKnot();
shuffle();
}
//set tiles within knot area to one (non-zero) - leave 20% zero
public void setTilesToOne() {
for (int i=0; i<100; i++) {tile[i] = 0;}
int st = 5 - width / 2; int fin = st + width - 1;
for (int x=st; x<=fin; x++) {
for (int y=st; y<=fin; y++) {
if (randomNum.nextInt(5)>0) {tile[y * 10 + x] = 1;}
}
}
}
// make the tiles into a proper knot
public void makeKnot() {
for (int x=0; x<10; x++) {
for (int y=0; y<10; y++) {
int position = y*10 + x;
int n = checkNext(x, y-1);
int s = checkNext(x, y+1);
int w = checkNext(x-1, y);
int e = checkNext(x+1, y);
if (tile[position]>0) {
if (n == 0 && e == 0 && s == 0 && w == 0) {tile[position] = 1;}
else if (n == 0 && e == 0 && s == 1 && w == 0) {tile[position] = 2;}
else if (n == 0 && e == 0 && s == 0 && w == 1) {tile[position] = 3;}
else if (n == 1 && e == 0 && s == 0 && w == 0) {tile[position] = 4;}
else if (n == 0 && e == 1 && s == 0 && w == 0) {tile[position] = 5;}
else if (n == 0 && e == 1 && s == 1 && w == 0) {tile[position] = 6;}
else if (n == 0 && e == 0 && s == 1 && w == 1) {tile[position] = 7;}
else if (n == 1 && e == 0 && s == 0 && w == 1) {tile[position] = 8;}
else if (n == 1 && e == 1 && s == 0 && w == 0) {tile[position] = 9;}
else if (n == 0 && e == 1 && s == 0 && w == 1) {tile[position] = 10;}
else if (n == 1 && e == 0 && s == 1 && w == 0) {tile[position] = 11;}
else if (n == 0 && e == 1 && s == 1 && w == 1) {tile[position] = 12;}
else if (n == 1 && e == 0 && s == 1 && w == 1) {tile[position] = 13;}
else if (n == 1 && e == 1 && s == 0 && w == 1) {tile[position] = 14;}
else if (n == 1 && e == 1 && s == 1 && w == 0) {tile[position] = 15;}
else {tile[position] = 16;}
}
}
}
// make copy for checking is OK
for (int i=0; i<100; i++) {
if (tile[i]<2) {tile[i] = 0;}
ok [i] = tile[i];
}
}
// check next door tiles to see if they are part of the knot
public int checkNext(int x, int y) {
if (x<0 || x>9 || y<0 || y>9) {return 0;}
int position = y*10 + x;
if (tile [position]==0) {return 0;} else {return 1;}
}
// shuffle the tiles
public void shuffle() {
for (int x=0; x<10; x++) {
for (int y=0; y<10; y++) {
int position = y * 10 + x;
if (tile[position]<2) {tile[position] = 0;}
else if (tile[position] < 6) {tile[position] = 2 + randomNum.nextInt(4);}
else if (tile[position] < 10) {tile[position] = 6 + randomNum.nextInt(4);}
else if (tile[position] < 12) {tile[position] = 10 + randomNum.nextInt(2);}
else if (tile[position] < 15) {tile[position] = 12 + randomNum.nextInt(4);}
}
}
}
// what happens when knot is complete
public void hooray() {
TextView finish = (TextView) findViewById(R.id.finishMessage);
finish.setText(R.string.finished);
int st = 3 - width / 2; int fin = st + width +3;
if (width==8) {st = 0; fin = 9;}
for (int x=(st+1); x
app/java/com.(the project name)/ImageTile - code for tile gridview - a new file, see above
package com.edkins.celticknotgame;
import ...
public class ImageTile extends BaseAdapter {
private AppCompatActivity mContext;
public ImageTile(AppCompatActivity c) {mContext = c;}
public int getCount() {return 100;}
public Object getItem(int position) {return null;}
public long getItemId(int position) {return 0;}
// create new ImageView for tile
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
GridView tile = (GridView) mContext.findViewById(R.id.tileView);
int w = tile.getWidth() / 10;
imageView.setLayoutParams(new GridView.LayoutParams(w, w));
} else {
imageView = (ImageView) convertView;
}
// initialise the view element (to whatever is in array tile[])
int sub = MainActivity.tile[position];
if (MainActivity.styleType == 0) {
imageView.setImageResource(MainActivity.close[sub]);
}
else if (MainActivity.styleType == 1) {
imageView.setImageResource(MainActivity.loose[sub]);
}
else if (MainActivity.styleType == 2) {
imageView.setImageResource(MainActivity.stone[sub]);
}
else if (MainActivity.styleType == 3) {
imageView.setImageResource(MainActivity.simple[sub]);
}
else if (MainActivity.styleType == 4) {
imageView.setImageResource(MainActivity.roman[sub]);
}
else if (MainActivity.styleType == 5) {
imageView.setImageResource(MainActivity.border[sub]);
}
return imageView;
}
}
Index to my other websites