public void updateWorldState(int position) { ColorSquare sel = get(position); sel.toggleSelected(); //TODO game logic }
//col = x; row= = y //takes in 2 colorsquares, returns true if they are in the same Y row public bool sameY(ColorSquare square1, ColorSquare square2) { bool result = false; if (square1.yLoc == square2.yLoc) { result = true; } return(result); }
//takes 2 ColorSquare objects. will randomize all elements in between private void randomizeBetween(ColorSquare square1, ColorSquare square2) { if (sameX(square1, square2)) //same Column { if (square1.yLoc > square2.yLoc) //1 further down the grid than 2 { int diff = square1.yLoc - square2.yLoc; //-1 to avoid outofbounds int start = square1.yLoc; for (int i = 0; i < diff; i++) { cntr.get(square1.xLoc, start--).randomizeColor(); score++; } } if (square1.yLoc < square2.yLoc) //1 higher up the grid than 2 { int diff = square2.yLoc - square1.yLoc; //-1 to avoid outofbounds int start = square2.yLoc; for (int i = 0; i < diff; i++) { cntr.get(square2.xLoc, start--).randomizeColor(); score++; } } } if (sameY(square1, square2)) //same row { if (square1.xLoc > square2.xLoc) //1 further down the grid than 2 { int diff = square1.xLoc - square2.xLoc; //-1 to avoid outofbounds int start = square1.xLoc; for (int i = 0; i < diff; i++) { cntr.get(start--, square1.yLoc).randomizeColor(); score++; } } if (square1.xLoc < square2.xLoc) //1 higher up the grid than 2 { int diff = square2.xLoc - square1.xLoc; //-1 to avoid outofbounds int start = square2.xLoc; for (int i = 0; i < diff; i++) { cntr.get(start--, square2.yLoc).randomizeColor(); score++; } } } }
//Doesnt work properly yet private bool isSelected(ColorSquare square) { bool isSelect = false; for (int i = 0; i < selectedSquares.Length; i++) { if (selectedSquares[i] != null && selectedSquares[i].xLoc == square.xLoc && selectedSquares[i].yLoc == square.xLoc) { scoreBox.Text = "Already Selected!"; isSelect = true; } } return(isSelect); }
public void loadBoard() { int[] scolors = { Resource.Drawable.Blue_selected, Resource.Drawable.Green_selected, Resource.Drawable.Red_selected, Resource.Drawable.Yellow_selected }; Random rng = new Random(); for (int row = 0; row < board.GetLength(0); row++) { for (int col = 0; col < board.GetLength(1); col++) { int n = rng.Next(colors.Length); int c = colors[n]; int cs = scolors[n]; board[row, col] = new ColorSquare(c, cs, col, row, n); } //col = x; row= = y } }
// create a new ImageView for each item referenced by the Adapter public override View GetView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(context); imageView.LayoutParameters = new AbsListView.LayoutParams(100, 100); imageView.SetScaleType(ImageView.ScaleType.CenterCrop); //ensures images stay square imageView.SetPadding(1, 1, 1, 1); //padding around, but will stretch to fill the whole screen } else { imageView = (ImageView)convertView; } ColorSquare sel = gameCntr.get(position); imageView.SetImageResource(sel.showColor()); return(imageView); }
private void Gridview_ItemClick(object sender, AdapterView.ItemClickEventArgs e) { gameCntr.updateWorldState(e.Position); gridAdapter.NotifyDataSetChanged(); ColorSquare selectedSquare = gameCntr.get(e.Position); string[] stringCompPatt = new string[3]; //only used when matching 3 selected. converts values to strings for comparison //score and match processing if (count < 3) { int selNum = selectedSquare.colorNum; selectedValues[count] = selNum; selectedSquares[count] = selectedSquare; count++; /*if (!selectedSquare.selected) * { * * }*/ if (count == 3) //AND add row checking logic { //if three selected total, process score //create a string list to compare from the selected values for (int i = 0; i < selectedValues.Length; i++) { switch (selectedValues[i]) { case 0: stringCompPatt[i] = "Blue"; break; case 1: stringCompPatt[i] = "Green"; break; case 2: stringCompPatt[i] = "Red"; break; case 3: stringCompPatt[i] = "Yellow"; break; } } //if the selected three match the pattern given, process score and adjust board as needed if (stringCompPatt[0] == patternArray[0] && stringCompPatt[1] == patternArray[1] && stringCompPatt[2] == patternArray[2] && gameCntr.processMatch(selectedSquares)) { if (gameCntr.sound == true) { MatchSound.Start(); } matchBox.Text = "MATCH!"; scoreBox.Text = "Score: " + gameCntr.score; //reset all selected values AND inbetween squares (to do) selectedSquares = new ColorSquare[3]; selectedValues = new int[3]; count = 0; updatePattern(); } else {//the match wasn't corrent if (gameCntr.sound == true) { failMatchSound.Start(); } gameCntr.deToggleAll(); matchBox.Text = "No match, try again!"; } count = 0; } } else { //will never happen } }