/// <summary> /// Colour the cell to peg colour. /// </summary> /// <param name="peg"><c>Peg</c> that is placed in cell.</param> public void SetPeg(Peg peg) { //set GUI cell colour switch (peg.Colour) { case PegColours.Aqua: Colour = Color.Aqua; break; case PegColours.Black: Colour = Color.Black; break; case PegColours.None: Colour = Color.Empty; break; case PegColours.Blue: Colour = Color.Blue; break; case PegColours.Brown: Colour = Color.Brown; break; case PegColours.Green: Colour = Color.Green; break; case PegColours.Orange: Colour = Color.Orange; break; case PegColours.Purple: Colour = Color.Purple; break; case PegColours.Red: Colour = Color.Red; break; case PegColours.White: Colour = Color.White; break; case PegColours.Yellow: Colour = Color.Yellow; break; } IsEmpty = false; }
/// <summary> /// Checks and returns how many pegs were correctly guessed. /// Finishes active row. /// </summary> /// <returns>How many pegs were correctly guessed.</returns> public void CheckGuess() { Row rw = GetActiveRow(); //Get active row to add checking pegs //int count = 0; //how many correct pegs we got //the logic //pegsTop and pegsBot all have -1 //but when there is a black or white peg, it will switch the corrosponding array item to 1 //this will make sure that we skip pegs that are already accounted for => if they are correct then skip. int[] pegsTop = {-1, -1, -1, -1}; int[] pegsBot = {-1, -1, -1, -1}; var blackPegs = 0; //how many black pegs? var whitePegs = 0; //how many white pegs? //loop through the user guessed row to check for black pegs for (var i = 0; i < 4; i++) { if (GuessedPegs[i].Colour != CorrectPegs[i].Colour) { continue; //if the guess is not right, continue } //TODO: Add black peg display. Peg peg = new Peg(0); //create a new peg to be added to the checking box to the current row rw.CheckingPegs.Add(peg); //add to checking box of current row //count++; //debug - how many black pegs blackPegs++; //how many black pegs? pegsTop[i] = 1; //set the arrays to 1 => skip next loop pegsBot[i] = 1; } //loop through the user guessed row to check for white pegs for (var i = 0; i < 4; i++) { //white pegs = correct colour but wrong position. //therefore we need to check one user guessed peg to ALL of the correct pegs. for (var j = 0; j < 4; j++) { //if i == j, we missed it in the black peg checking... //if pegsTop == 1 or pegsBot == 1 then we have already checked them in the black checking if ((i == j) || (pegsTop[i] == 1) || (pegsBot[j] == 1)) { continue; } //if user guessed peg colour is not equal to correct peg colour, then continue to next if (GuessedPegs[i].Colour != CorrectPegs[j].Colour) { continue; } //TODO: Add white peg display Peg peg = new Peg(7); // new peg with white colour rw.CheckingPegs.Add(peg); // add to checking box of current row pegsTop[i] = 1; //set to 1 so we don't check it again pegsBot[j] = 1; //set to 1 so we don't check it again whitePegs++; //add to whitePegs break; } } UpdateRow(rw); UserHasWon = FinishRow(); }
/// <summary> /// Populates the game list with pegs. /// </summary> private void GetRandomPegs() { Random rnd = new Random(); //init a black peg to differ out all black, white and none colours. Peg peg = new Peg(0); //black peg init for (var i = 0; i < 4; i++) { //if we get a random peg that is still black, white or none - loop until we get a different color. while (peg.Colour == PegColours.Black || peg.Colour == PegColours.None || peg.Colour == PegColours.White) { peg = new Peg(rnd.Next(0, 11)); CorrectPegs[i] = peg; } peg = new Peg(0); } //MessageBox.Show(CorrectPegs[0].Colour + " " + CorrectPegs[1].Colour + " " + // CorrectPegs[2].Colour + " " + CorrectPegs[3].Colour); }
/// <summary> /// Place user <c>peg</c>. /// </summary> /// <param name="position">Which <c>cell</c>? Spanning from 0 to 3, left to right.</param> /// <param name="peg"><c>Peg</c> which is placed by <c>user</c>.</param> public bool PlacePeg(int position, Peg peg) { foreach (Row rw in _rowArray.Where(rw => rw.Active && rw.Cells[position].IsEmpty)) { rw.Cells[position].SetPeg(peg); GuessedPegs[position] = peg; } foreach (Row rw in _rowArray.Where(rw => rw.Active)) { var check = false; for (var i = 0; i < 4; i++) { if (rw.Cells[i].IsEmpty) { check = false; break; } check = true; } if (check) { HowManyRows--; CheckGuess(); return true; } } return false; }
/// <summary> /// Get the <c>Peg</c> user wants to place. /// </summary> /// <param name="sender">Which peg button was pressed.</param> /// <param name="e"></param> private void PegClicked(object sender, EventArgs e) { Button color = sender as Button; switch (color?.Name) { case "btnRed": PegToPlace = new Peg(1); break; case "btnGreen": PegToPlace = new Peg(2); break; case "btnYellow": PegToPlace = new Peg(3); break; case "btnBlue": PegToPlace = new Peg(4); break; case "btnOrange": PegToPlace = new Peg(5); break; case "btnBrown": PegToPlace = new Peg(6); break; case "btnPurple": PegToPlace = new Peg(8); break; case "btnAqua": PegToPlace = new Peg(9); break; } }