A random collection of Celtic knot elements appear. You rotate them to see if you can make a knot. The app recognises when you do. You can alter the colours of the different knots.
app/manifests/AndroidManifest.xml - to force orientation
app/res/drawable - pictures
There are 6 x 15 image files (15 per colour). The colours are "black", "blue", "green", "red", "white" and "yellow" and the files are, e.g., "black0" (background) to "black14". 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
CelticChaosTap on tile to rotate. How many knots can you make?newchange stylesmallmediumlarge" ""Knot incomplete ""% of area covered"
app/res/layout/activity_main.xml - screen format
app/java/com.(the project name)/MainActivity - main code for app
package com.edkins.celticchaos;
import ...
import java.util.Random;
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 scwidth = 10;
public static int scdepth = 10;
public static int st = (scwidth - width) / 2;
public static int fin = st + width - 1;
// tiles - for setting up knot and colurs
public static int [] tile = new int [scwidth * scdepth];
public static int [] colour = new int [scwidth * scdepth];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
width = 4;
newKnot();
GridView style = (GridView) findViewById(R.id.paletteView);
style.setNumColumns(scwidth);
style.setAdapter(new ImagePalette(this));
style.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView> parent, View v,
int position, long id) {
processPaletteClick(position);
}
});
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);
}
// Main process - tap to rotate pattern plus check if knot completed
public void processTileClick(int position) {
if (tile[position]==0) {return;}
if (colour[position]!=styleType) {
colour[position]=styleType;
}
else {
if (tile [position] == 4) {tile[position] = 1;}
else if (tile [position] == 8) {tile[position] = 5;}
else if (tile [position] == 10) {tile[position] = 9;}
else if (tile [position] == 14) {tile[position] = 11;}
else {tile[position] ++;}
}
redo();
checkTiles();
}
// check if knots OK
public void checkTiles() {
int ok=1;
int i; int j;
int knots=0;
for (i=0; i<(scwidth * scdepth); i++) {
int type = tile [i];
if (colour[i] > 0) {
knots++;
if (nJoin[type]==1) {
j = i - scwidth;
if (colour[j] != colour[i]) {ok = 0;}
else {if (sJoin[tile [j]]==0) {ok = 0;}}
}
if (eJoin[type]==1) {
j = i + 1;
if (colour[j] != colour[i]) {ok = 0;}
else if (wJoin[tile [j]]==0) {ok = 0;}
}
if (sJoin[type]==1) {
j = i + scwidth;
if (colour[j] != colour[i]) {ok = 0;}
else if (nJoin[tile [j]]==0) {ok = 0;}
}
if (wJoin[type]==1) {
j = i - 1;
if (colour[j] != colour[i]) {ok = 0;}
else if (eJoin[tile [j]]==0) {ok = 0;}
}
}
}
TextView knotMessage = (TextView) findViewById(R.id.finishMessage);
if (ok==0) {knotMessage.setText(R.string.notOK);}
else {
int percent = (100 * knots) / (width * width);
String mess = percent+getString(R.string.isOK);
knotMessage.setText(mess);
}
}
// next game - set tiles within knot area to random
public void newKnot() {
TextView finish = (TextView) findViewById(R.id.finishMessage);
finish.setText(R.string.space);
st = (scwidth - width) / 2;
fin = st + width - 1;
for (int i=0; i<(scwidth * scdepth); i++) {
colour[i] = 0;
int x = i % scwidth;
int y = i / scwidth;
if (xfin || yfin) {tile[i] = 0;}
else if ((x==st || x==fin ) && (y==st || y==fin)) {tile[i] = 1 + randomNum.nextInt(8);}
else {tile[i] = 1 + randomNum.nextInt(14);}
}
}
// buttons, etc
// next knot
public void doNew(View view) {
newKnot();
redo();
}
//change colour
public void processPaletteClick(int position) {
styleType = position;
redo();
}
// change size of knot
public void clickSmall(View view) {width = 4; newKnot(); redo();}
public void clickMedium(View view) {width = 6; newKnot(); redo();}
public void clickLarge(View view) {width = 8; newKnot(); redo();}
// redo screen
public void redo() {
GridView matrix = (GridView) findViewById(R.id.tileView);
matrix.setAdapter(new ImageTile(this));
}
// Different colours of knot
public static Integer[] black = {
R.drawable.black0, R.drawable.black1, R.drawable.black2, R.drawable.black3,
R.drawable.black4, R.drawable.black5, R.drawable.black6, R.drawable.black7,
R.drawable.black8, R.drawable.black9, R.drawable.black10, R.drawable.black11,
R.drawable.black12, R.drawable.black13, R.drawable.black14
};
public static Integer[] red = {
R.drawable.red0, R.drawable.red1, R.drawable.red2, R.drawable.red3,
R.drawable.red4, R.drawable.red5, R.drawable.red6, R.drawable.red7,
R.drawable.red8, R.drawable.red9, R.drawable.red10, R.drawable.red11,
R.drawable.red12, R.drawable.red13, R.drawable.red14
};
public static Integer[] blue = {
R.drawable.blue0, R.drawable.blue1, R.drawable.blue2, R.drawable.blue3,
R.drawable.blue4, R.drawable.blue5, R.drawable.blue6, R.drawable.blue7,
R.drawable.blue8, R.drawable.blue9, R.drawable.blue10, R.drawable.blue11,
R.drawable.blue12, R.drawable.blue13, R.drawable.blue14
};
public static Integer[] yellow = {
R.drawable.yellow0, R.drawable.yellow1, R.drawable.yellow2, R.drawable.yellow3,
R.drawable.yellow4, R.drawable.yellow5, R.drawable.yellow6, R.drawable.yellow7,
R.drawable.yellow8, R.drawable.yellow9, R.drawable.yellow10, R.drawable.yellow11,
R.drawable.yellow12, R.drawable.yellow13, R.drawable.yellow14
};
public static Integer[] green = {
R.drawable.green0, R.drawable.green1, R.drawable.green2, R.drawable.green3,
R.drawable.green4, R.drawable.green5, R.drawable.green6, R.drawable.green7,
R.drawable.green8, R.drawable.green9, R.drawable.green10, R.drawable.green11,
R.drawable.green12, R.drawable.green13, R.drawable.green14
};
public static Integer[] white = {
R.drawable.white0, R.drawable.white1, R.drawable.white2, R.drawable.white3,
R.drawable.white4, R.drawable.white5, R.drawable.white6, R.drawable.white7,
R.drawable.white8, R.drawable.white9, R.drawable.white10, R.drawable.white11,
R.drawable.white12, R.drawable.white13, R.drawable.white14
};
// where joins are
public static Integer[] nJoin = {0,0,0,1,0,0,0,1,1,0,1,0,1,1,1};
public static Integer[] eJoin = {0,0,0,0,1,1,0,0,1,1,0,1,0,1,1};
public static Integer[] sJoin = {0,1,0,0,0,1,1,0,0,0,1,1,1,0,1};
public static Integer[] wJoin = {0,0,1,0,0,0,1,1,0,1,0,1,1,1,0};
}
app/java/com.(the project name)/ImagePalette - code for palette gridview - a new file, see above
package com.edkins.celticchaos;
import ...
public class ImagePalette extends BaseAdapter {
private AppCompatActivity mContext;
public ImagePalette(AppCompatActivity c) {mContext = c;}
public int getCount() {return MainActivity.styleNum;}
public Object getItem(int position) {return null;}
public long getItemId(int position) {return 0;}
// create new ImageView for palette
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
GridView palette = (GridView) mContext.findViewById(R.id.tileView);
int w = palette.getWidth() / 11;
imageView.setLayoutParams(new GridView.LayoutParams(w, w));
}
else {
imageView = (ImageView) convertView;
}
// initialise the view element (to one of the colours)
if (position == 0) {imageView.setImageResource(R.drawable.black9);}
else if (position == 1) {imageView.setImageResource(R.drawable.red9);}
else if (position == 2) {imageView.setImageResource(R.drawable.blue9);}
else if (position == 3) {imageView.setImageResource(R.drawable.yellow9);}
else if (position == 4) {imageView.setImageResource(R.drawable.green9);}
else if (position == 5) {imageView.setImageResource(R.drawable.white9);}
else {imageView.setImageResource(R.drawable.space);}
return imageView;
}
}
app/java/com.(the project name)/ImageTile - code for tile gridview - a new file, see above
package com.edkins.celticchaos;
import ...
public class ImageTile extends BaseAdapter {
private AppCompatActivity mContext;
public ImageTile(AppCompatActivity c) {mContext = c;}
public int getCount() {return MainActivity.scwidth * MainActivity.scdepth;}
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() / MainActivity.scwidth;
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];
int col = MainActivity.colour[position];
if (col == 0) {imageView.setImageResource(MainActivity.black[sub]);}
else if (col == 1) {imageView.setImageResource(MainActivity.red[sub]);}
else if (col == 2) {imageView.setImageResource(MainActivity.blue[sub]);}
else if (col == 3) {imageView.setImageResource(MainActivity.yellow[sub]);}
else if (col == 4) {imageView.setImageResource(MainActivity.green[sub]);}
else if (col == 5) {imageView.setImageResource(MainActivity.white[sub]);}
return imageView;
}
}
Index to my other websites