///<summary> ///a emthod to check for a winner /// </summary> /// <return>true if a winner has been identified</return> public bool EvaluateBoard() { //loop through all 8 winning combinations //remember: _winningCombinations is a ollection of collections of tic tac toecell foreach (var combination in _winningCombinations) { var firstCell = combination[0]; if ((firstCell.CellOwner != CellOwners.Computer) && (firstCell.CellOwner != CellOwners.Human)) { continue; } if ((firstCell.CellOwner != combination[1].CellOwner) || (firstCell.CellOwner != combination[2].CellOwner)) { continue; } Winner = firstCell.CellOwner; return(true); } return(false); }
public void AssignCellOwner(int CellRow, int CellCol, CellOwners CellOwner) { if (_ticTacToeCells.Count == 0) { return; } var targetCell = _ticTacToeCells.FirstOrDefault(tttc => tttc.RowID == CellRow && tttc.ColID == CellCol); if (targetCell == null) { return; } targetCell.CellOwner = CellOwner; System.Diagnostics.Debug.Print($"AssignCellOwner: {CellRow},{CellCol} = {CellOwner.ToString()}"); // unit #9 // these will be the arguments used when the event is fired var eventArgs = new CellOwnerChangedArgs(CellRow, CellCol, CellOwner); //bubble the event up to the parent ( ONLY if the parent is listening ) CellOwnerChanged?.Invoke(this, eventArgs); }
public void AssignCellOwner(int CellRow, int CellCol, CellOwners CellOwner) { if (_ticTacToeCells.Count == 0) { return; } if (Winner == CellOwners.Computer || Winner == CellOwners.Human) { return; } var targetCell = _ticTacToeCells .FirstOrDefault(tttc => tttc.RowID == CellRow && tttc.ColID == CellCol); if (targetCell == null) { return; } targetCell.CellOwner = CellOwner; Debug.WriteLine($"Method: AssignCellOwner {CellRow}-{CellCol} {CellOwner}"); // unit #9 // these will be the arguments used when the event is fired var eventArgs = new CellOwnerChangedArgs(CellRow, CellCol, CellOwner); //bubble the event up to the parent ( ONLY if the parent is listening ) CellOwnerChanged?.Invoke(this, eventArgs); }
public CellOwnerChangedArgs(int rowID, int colID, CellOwners cellOwner) { RowID = rowID; ColID = colID; CellOwner = cellOwner; Debug.WriteLine($"Method: CellOwnerChangedArgs {RowID}-{ColID} {CellOwner}"); }
public void Verify_Assignment_Of_CellOwner(CellOwners assignedCellOwner) { // assign var ticTacToeCell = new TicTacToeCell(); // act and assert ticTacToeCell.CellOwner = assignedCellOwner; ticTacToeCell.CellOwner.ShouldBe(assignedCellOwner); }
public void Verify_Assignment_Of_CellOwner(CellOwners attemptedAssignment, CellOwners expectedResult) { // assign var ticTacToeCell = new TicTacToeCell(); // action ticTacToeCell.CellOwner = attemptedAssignment; // assert ticTacToeCell.CellOwner.ShouldBe(expectedResult); }
/* * ProfReynolds1205 * this is a rather advanced solution but you will learn if you study it. * * public void AutoPlayComputer() * { * bool SearchForPlayerReadyToWin(CellOwners checkingCellOwner) * { * foreach (var combination in _winningCombinations) * foreach (var targetCell in combination.Where(tttc => tttc.CellOwner == CellOwners.Open)) * { * if (combination * .Count(tttc => * tttc != targetCell && * tttc.CellOwner != checkingCellOwner) > 0) * break; * * AssignCellOwner(targetCell.RowID, targetCell.ColID, CellOwners.Computer); * return true; * } * * return false; * } * * if (_ticTacToeCells.Count == 0) return; * * if (Winner == CellOwners.Computer || Winner == CellOwners.Human) return; * * * if (SearchForPlayerReadyToWin(CellOwners.Computer)) return; * if (SearchForPlayerReadyToWin(CellOwners.Human)) return; * * var winningCell = _goodNextMove * .FirstOrDefault(tttc => tttc.CellOwner == CellOwners.Open); * if (winningCell != null) * AssignCellOwner(winningCell.RowID, winningCell.ColID, CellOwners.Computer); * } * */ public bool CheckForWinner() { foreach (var combination in _winningCombinations) { var firstCell = combination[0]; if ((firstCell.CellOwner != CellOwners.Computer) && (firstCell.CellOwner != CellOwners.Human)) { continue; } if (combination.Count == 5) { if ((firstCell.CellOwner != combination[1].CellOwner) || (firstCell.CellOwner != combination[2].CellOwner) || (firstCell.CellOwner != combination[3].CellOwner) || (firstCell.CellOwner != combination[4].CellOwner)) { continue; } } else { if ((firstCell.CellOwner != combination[1].CellOwner) || (firstCell.CellOwner != combination[2].CellOwner) || (firstCell.CellOwner != combination[3].CellOwner)) { continue; } } Winner = firstCell.CellOwner; Debug.WriteLine($"Method: CheckForWinnder {Winner}"); return(true); } /* * ProfReynolds1205 * The above is complicated due to the combinations being 4 or 5 * replace the above with this * * if (combination.Any(tttc => tttc.CellOwner != firstCell.CellOwner)) * continue; * * Winner = firstCell.CellOwner; * */ Debug.WriteLine($"Method: CheckForWinnder {Winner}"); return(false); }
//public void AutoPlayComputer() //{ // if (_ticTacToeCells.Count == 0) return; // if (Winner == CellOwners.Computer || Winner == CellOwners.Human) return; // // checking if the computer is the winner // foreach (var combination in _winningCombinations) // { // if (combination[0].CellOwner == CellOwners.Open) // { // if ((combination[1].CellOwner == CellOwners.Computer) && // (combination[2].CellOwner == CellOwners.Computer)) // { // AssignCellOwner(combination[0].RowID, combination[0].ColID, CellOwners.Computer); // return; // } // } // if (combination[1].CellOwner == CellOwners.Open) // { // if ((combination[0].CellOwner == CellOwners.Computer) && // (combination[2].CellOwner == CellOwners.Computer)) // { // AssignCellOwner(combination[1].RowID, combination[1].ColID, CellOwners.Computer); // return; // } // } // if (combination[2].CellOwner == CellOwners.Open) // { // if ((combination[0].CellOwner == CellOwners.Computer) && // (combination[1].CellOwner == CellOwners.Computer)) // { // AssignCellOwner(combination[2].RowID, combination[2].ColID, CellOwners.Computer); // return; // } // } // } // // checking if the human is the winner // foreach (var combination in _winningCombinations) // { // if (combination[0].CellOwner == CellOwners.Open) // { // if ((combination[1].CellOwner == CellOwners.Human) && // (combination[2].CellOwner == CellOwners.Human)) // { // AssignCellOwner(combination[0].RowID, combination[0].ColID, CellOwners.Computer); // return; // } // } // if (combination[1].CellOwner == CellOwners.Open) // { // if ((combination[0].CellOwner == CellOwners.Human) && // (combination[2].CellOwner == CellOwners.Human)) // { // AssignCellOwner(combination[1].RowID, combination[1].ColID, CellOwners.Computer); // return; // } // } // if (combination[2].CellOwner == CellOwners.Open) // { // if ((combination[0].CellOwner == CellOwners.Human) && // (combination[1].CellOwner == CellOwners.Human)) // { // AssignCellOwner(combination[2].RowID, combination[2].ColID, CellOwners.Computer); // return; // } // } // } // foreach (var targetCell in _goodNextMove) // { // if (targetCell.CellOwner == CellOwners.Open) // { // AssignCellOwner(targetCell.RowID, targetCell.ColID, CellOwners.Computer); // return; // } // } //} public bool CheckForWinner() { // 2021 add this: if (_ticTacToeCells.Count == 0) { return(false); } if (Winner == CellOwners.Computer || Winner == CellOwners.Human) { return(true); } foreach (var combination in _winningCombinations) { // combination is a collection of 3 TicTacToe cell references var firstCell = combination[0]; if ((firstCell.CellOwner != CellOwners.Computer) && (firstCell.CellOwner != CellOwners.Human)) { continue; } var winnerFound = true; foreach (var item in combination) { if (firstCell.CellOwner != item.CellOwner) { winnerFound = false; } } if (winnerFound) { Winner = firstCell.CellOwner; Debug.WriteLine($"Method: CheckForWinner {Winner}"); return(true); } } Debug.WriteLine($"Method: CheckForWinner {Winner}"); return(false); }
public void AssignCellOwner(int CellRow, int CellCol, CellOwners CellOwner) { if (_ticTacToeCells.Count == 0) { return; } var targetCell = _ticTacToeCells .FirstOrDefault(tttc => tttc.RowID == CellRow && tttc.ColID == CellCol); if (targetCell == null) { return; } targetCell.CellOwner = CellOwner; }
// notice - no constructor. I dont think one is needed public void ResetGrid() { _ticTacToeCells.Clear(); // resets the collection to empty Winner = CellOwners.Open; // create the 9 cells for (var rowNo = 0; rowNo < 3; rowNo++) { for (var colNo = 0; colNo < 3; colNo++) { _ticTacToeCells.Add(new TicTacToeCell { RowID = rowNo, ColID = colNo }); } } }
/// <summary> /// Method called when a play is made in-game /// </summary> /// <param name="rowNo">Row number of the selected square</param> /// <param name="colNo">Column number of the selected square</param> /// <param name="squareOwner">Owner of the square (human, computer, open, error)</param> public void PlaySquare(int rowNo, int colNo, CellOwners squareOwner) { var targetSquare = _ticTacToeCells .FirstOrDefault(ttts => ttts.rowID == rowNo && ttts.colID == colNo); // there may be bad input, accounts for unknown unknowns and is safe programming practice if (targetSquare != null) { targetSquare.XorY = squareOwner; // new event argument with a row, column, and XorY property var eventArgs = new CellAssignmentArgs(targetSquare.rowID, targetSquare.colID, targetSquare.XorY); // assigns the new arguments to the selected square, if the cell selected isnt null if (this.CellAssignment != null) { CellAssignment(this, eventArgs); } } }
///<summary> ///a method to place the marker for the human (or computer). /// </summary> public void PlaceMarker(int rowID, int colID, CellOwners cellOwner) { var targetCell = _ticTacToeCells .FirstOrDefault(tttc => tttc.RowID == rowID && tttc.ColID == colID); // if the targetCell is not null (meaning it was located), then // change the owner as specified if (targetCell != null) { if (targetCell.CellOwner != CellOwners.Open) { return; } //these will be arguments used when the event is fired targetCell.CellOwner = cellOwner; var eventArgs = new MarkerChangedArgs(rowID, colID, cellOwner); //bubble the event up to the parent ( Only if the parent is listening ) this.MarkerChanged?.Invoke(this, eventArgs); } }
public void AssignCellOwner(int CellRow, int CellCol, CellOwners CellOwner) { if (_ticTacToeCells.Count == 0) { return; } var targetCell = _ticTacToeCells .FirstOrDefault(tttc => tttc.RowID == CellRow & tttc.ColID == CellCol); if (targetCell == null) { return; } targetCell.CellOwner = CellOwner; Debug.WriteLine($"Method: AssignCellOwner {CellRow}-{CellCol} --> {CellOwner}"); var eventArgs = new CellOwnerChangedArgs(CellRow, CellCol, CellOwner); CellOwnerChanged?.Invoke(this, eventArgs); }
/// <summary> /// Method to identify the winner of the game /// </summary> /// <returns>String with the name of the game winner (playerName, or "Computer")</returns> public string IdentifyWinner() { // _scanMatrix is a Collection<Collection<TicTacToeCell>> foreach (var scanpattern in _scanMatrix) { // scanpattern is a Collection<TicTacToeCell>, consists of five cells in a row - cells to be checked var theseFiveOwners = new Collection <CellOwners>(); foreach (var scancell in scanpattern) { theseFiveOwners.Add(scancell.XorY); } // now check the cellOwners - are they ALL human? var winnerHuman = theseFiveOwners.All(cell => ((cell == CellOwners.Human) || (cell == CellOwners.WildCard))); if (winnerHuman) { Winner = CellOwners.Human; } // now check the cellOwners - are they ALL computer? var winnerComputer = theseFiveOwners.All(cell => ((cell == CellOwners.Computer) || (cell == CellOwners.WildCard))); if (winnerComputer) { Winner = CellOwners.Computer; } } switch (Winner) { case CellOwners.Human: return(PlayerName); case CellOwners.Computer: return("Computer"); default: return(string.Empty); } }
/// <summary> /// Reads through an array of combinations to check for a a winning combination /// </summary> /// <returns></returns> //public bool CheckForWinner() //{ // #region Human // ////first row // //if ((IdentifyCellOwner(0, 0) == CellOwners.Human) && // // (IdentifyCellOwner(0, 1) == CellOwners.Human) && // // (IdentifyCellOwner(0, 2) == CellOwners.Human)) // //{ // // Winner = CellOwners.Human; // // return true; // //} // ////second row // //if ((IdentifyCellOwner(1, 0) == CellOwners.Human) && // // (IdentifyCellOwner(1, 1) == CellOwners.Human) && // // (IdentifyCellOwner(1, 2) == CellOwners.Human)) // //{ // // Winner = CellOwners.Human; // // return true; // //} // ////third row // //if ((IdentifyCellOwner(2, 0) == CellOwners.Human) && // // (IdentifyCellOwner(2, 1) == CellOwners.Human) && // // (IdentifyCellOwner(2, 2) == CellOwners.Human)) // //{ // // Winner = CellOwners.Human; // // return true; // //} // ////first column // //if ((IdentifyCellOwner(0, 0) == CellOwners.Human) && // // (IdentifyCellOwner(1, 0) == CellOwners.Human) && // // (IdentifyCellOwner(2, 0) == CellOwners.Human)) // //{ // // Winner = CellOwners.Human; // // return true; // //} // ////second column // //if ((IdentifyCellOwner(0, 1) == CellOwners.Human) && // // (IdentifyCellOwner(1, 1) == CellOwners.Human) && // // (IdentifyCellOwner(2, 1) == CellOwners.Human)) // //{ // // Winner = CellOwners.Human; // // return true; // //} // ////third column // //if ((IdentifyCellOwner(0, 2) == CellOwners.Human) && // // (IdentifyCellOwner(1, 2) == CellOwners.Human) && // // (IdentifyCellOwner(2, 2) == CellOwners.Human)) // //{ // // Winner = CellOwners.Human; // // return true; // //} // ////first diagonal // //if ((IdentifyCellOwner(0, 0) == CellOwners.Human) && // // (IdentifyCellOwner(1, 1) == CellOwners.Human) && // // (IdentifyCellOwner(2, 2) == CellOwners.Human)) // //{ // // Winner = CellOwners.Human; // // return true; // //} // ////second diagonal // //if ((IdentifyCellOwner(0, 2) == CellOwners.Human) && // // (IdentifyCellOwner(1, 1) == CellOwners.Human) && // // (IdentifyCellOwner(2, 0) == CellOwners.Human)) // //{ // // Winner = CellOwners.Human; // // return true; // //} // #endregion // #region Computer // ////first row // //if ((IdentifyCellOwner(0, 0) == CellOwners.Computer) && // // (IdentifyCellOwner(0, 1) == CellOwners.Computer) && // // (IdentifyCellOwner(0, 2) == CellOwners.Computer)) // //{ // // Winner = CellOwners.Computer; // // return true; // //} // ////second row // //if ((IdentifyCellOwner(1, 0) == CellOwners.Computer) && // // (IdentifyCellOwner(1, 1) == CellOwners.Computer) && // // (IdentifyCellOwner(1, 2) == CellOwners.Computer)) // //{ // // Winner = CellOwners.Computer; // // return true; // //} // ////third row // //if ((IdentifyCellOwner(2, 0) == CellOwners.Computer) && // // (IdentifyCellOwner(2, 1) == CellOwners.Computer) && // // (IdentifyCellOwner(2, 2) == CellOwners.Computer)) // //{ // // Winner = CellOwners.Computer; // // return true; // //} // ////first column // //if ((IdentifyCellOwner(0, 0) == CellOwners.Computer) && // // (IdentifyCellOwner(1, 0) == CellOwners.Computer) && // // (IdentifyCellOwner(2, 0) == CellOwners.Computer)) // //{ // // Winner = CellOwners.Computer; // // return true; // //} // ////second column // //if ((IdentifyCellOwner(0, 1) == CellOwners.Computer) && // // (IdentifyCellOwner(1, 1) == CellOwners.Computer) && // // (IdentifyCellOwner(2, 1) == CellOwners.Computer)) // //{ // // Winner = CellOwners.Computer; // // return true; // //} // ////third column // //if ((IdentifyCellOwner(0, 2) == CellOwners.Computer) && // // (IdentifyCellOwner(1, 2) == CellOwners.Computer) && // // (IdentifyCellOwner(2, 2) == CellOwners.Computer)) // //{ // // Winner = CellOwners.Computer; // // return true; // //} // ////first diagonal // //if ((IdentifyCellOwner(0, 0) == CellOwners.Computer) && // // (IdentifyCellOwner(1, 1) == CellOwners.Computer) && // // (IdentifyCellOwner(2, 2) == CellOwners.Computer)) // //{ // // Winner = CellOwners.Computer; // // return true; // //} // ////second diagonal // //if ((IdentifyCellOwner(0, 2) == CellOwners.Computer) && // // (IdentifyCellOwner(1, 1) == CellOwners.Computer) && // // (IdentifyCellOwner(2, 0) == CellOwners.Computer)) // //{ // // Winner = CellOwners.Computer; // // return true; // //} // #endregion // if (_winningCombinations != null) // { // foreach (var combination in _winningCombinations) // { // var firstCell = combination[0]; // if ((firstCell.CellOwner != CellOwners.Computer) && // (firstCell.CellOwner != CellOwners.Human)) continue; // bool noMatch = false; // for (int i = 1; i < combination.Count; i++) // { // if (firstCell.CellOwner != combination[i].CellOwner) // { // noMatch = true; // break; // } // } // if (noMatch == true) continue; // //if ((firstCell.CellOwner != combination[1].CellOwner) || // //(firstCell.CellOwner != combination[2].CellOwner)) continue; // Winner = firstCell.CellOwner; // return true; // } // } // return false; //} public bool CheckForWinner() { if (_ticTacToeCells.Count == 0) { return(false); } if (Winner == CellOwners.Computer || Winner == CellOwners.Human) { return(true); } foreach (var combination in _winningCombinations) { CellOwners testOwner; testOwner = CellOwners.Computer; if (!combination.Any(tttc => tttc.CellOwner != testOwner)) { Winner = testOwner; return(true); } testOwner = CellOwners.Human; if (!combination.Any(tttc => tttc.CellOwner != testOwner)) { Winner = testOwner; return(true); } } ; Debug.WriteLine($"Method: CheckForWinner {Winner}"); return(false); }
public bool CheckForWinner() { // first row if ((IdentifyCellOwner(0, 0) == CellOwners.Human) && (IdentifyCellOwner(0, 1) == CellOwners.Human) && (IdentifyCellOwner(0, 2) == CellOwners.Human)) { Winner = CellOwners.Human; return(true); } // 2nd row if ((IdentifyCellOwner(1, 0) == CellOwners.Human) && (IdentifyCellOwner(1, 1) == CellOwners.Human) && (IdentifyCellOwner(1, 2) == CellOwners.Human)) { Winner = CellOwners.Human; return(true); } // 3rd row if ((IdentifyCellOwner(2, 0) == CellOwners.Human) && (IdentifyCellOwner(2, 1) == CellOwners.Human) && (IdentifyCellOwner(2, 2) == CellOwners.Human)) { Winner = CellOwners.Human; return(true); } // 1st col if ((IdentifyCellOwner(0, 0) == CellOwners.Human) && (IdentifyCellOwner(1, 0) == CellOwners.Human) && (IdentifyCellOwner(2, 0) == CellOwners.Human)) { Winner = CellOwners.Human; return(true); } // 2nd col if ((IdentifyCellOwner(0, 1) == CellOwners.Human) && (IdentifyCellOwner(1, 1) == CellOwners.Human) && (IdentifyCellOwner(2, 1) == CellOwners.Human)) { Winner = CellOwners.Human; return(true); } // 3rd col if ((IdentifyCellOwner(0, 2) == CellOwners.Human) && (IdentifyCellOwner(1, 2) == CellOwners.Human) && (IdentifyCellOwner(2, 2) == CellOwners.Human)) { Winner = CellOwners.Human; return(true); } // 1st diag if ((IdentifyCellOwner(0, 0) == CellOwners.Human) && (IdentifyCellOwner(1, 1) == CellOwners.Human) && (IdentifyCellOwner(2, 2) == CellOwners.Human)) { Winner = CellOwners.Human; return(true); } // 2nd diag if ((IdentifyCellOwner(0, 2) == CellOwners.Human) && (IdentifyCellOwner(1, 1) == CellOwners.Human) && (IdentifyCellOwner(2, 0) == CellOwners.Human)) { Winner = CellOwners.Human; return(true); } // first row if ((IdentifyCellOwner(0, 0) == CellOwners.Computer) && (IdentifyCellOwner(0, 1) == CellOwners.Computer) && (IdentifyCellOwner(0, 2) == CellOwners.Computer)) { Winner = CellOwners.Computer; return(true); } // 2nd row if ((IdentifyCellOwner(1, 0) == CellOwners.Computer) && (IdentifyCellOwner(1, 1) == CellOwners.Computer) && (IdentifyCellOwner(1, 2) == CellOwners.Computer)) { Winner = CellOwners.Computer; return(true); } // 3rd row if ((IdentifyCellOwner(2, 0) == CellOwners.Computer) && (IdentifyCellOwner(2, 1) == CellOwners.Computer) && (IdentifyCellOwner(2, 2) == CellOwners.Computer)) { Winner = CellOwners.Computer; return(true); } // 1st col if ((IdentifyCellOwner(0, 0) == CellOwners.Computer) && (IdentifyCellOwner(1, 0) == CellOwners.Computer) && (IdentifyCellOwner(2, 0) == CellOwners.Computer)) { Winner = CellOwners.Computer; return(true); } // 2nd col if ((IdentifyCellOwner(0, 1) == CellOwners.Computer) && (IdentifyCellOwner(1, 1) == CellOwners.Computer) && (IdentifyCellOwner(2, 1) == CellOwners.Computer)) { Winner = CellOwners.Computer; return(true); } // 3rd col if ((IdentifyCellOwner(0, 2) == CellOwners.Computer) && (IdentifyCellOwner(1, 2) == CellOwners.Computer) && (IdentifyCellOwner(2, 2) == CellOwners.Computer)) { Winner = CellOwners.Computer; return(true); } // 1st diag if ((IdentifyCellOwner(0, 0) == CellOwners.Computer) && (IdentifyCellOwner(1, 1) == CellOwners.Computer) && (IdentifyCellOwner(2, 2) == CellOwners.Computer)) { Winner = CellOwners.Computer; return(true); } // 2nd diag if ((IdentifyCellOwner(0, 2) == CellOwners.Computer) && (IdentifyCellOwner(1, 1) == CellOwners.Computer) && (IdentifyCellOwner(2, 0) == CellOwners.Computer)) { Winner = CellOwners.Computer; return(true); } return(false); }
public void SetCellOwner(int CellRow, int CellCol, CellOwners CellOwner) { throw new NotImplementedException(); }
/// <summary> /// Generates a new board for play /// Public so that it can be modified outside of local scope /// Used when a new board/game is requested /// </summary> public void ResetGrid() { // clear the current board for a new game _ticTacToeCells.Clear(); _scanMatrix.Clear(); // 25 "spaces" are assigned the values of rowNo and colNo simulating a 5 x 5 board for (var rowNo = 0; rowNo < 5; rowNo++) { for (var colNo = 0; colNo < 5; colNo++) { // sets the "free" space if (rowNo == 2 && colNo == 2) { _ticTacToeCells.Add(new TicTacToeCells { rowID = rowNo, colID = colNo, // Wild card denotes the free space XorY = CellOwners.WildCard }); } // sets the playing spaces else { _ticTacToeCells.Add(new TicTacToeCells { rowID = rowNo, colID = colNo, // XorY property is determined in the game, not initially XorY = CellOwners.Open }); } } } #region // Adding the three collections of 5 rows to the _scanMatrix for (var rowNo = 0; rowNo < 5; rowNo++) { // scanMatrixSet will hold columns 0, 1, and 2 for a given row var scanMatrixSet = new Collection <TicTacToeCells>(); for (int colNo = 0; colNo < 5; colNo++) { scanMatrixSet.Add(_ticTacToeCells.First(ttts => ttts.rowID == rowNo && ttts.colID == colNo)); } _scanMatrix.Add(scanMatrixSet); // just added a row of cells to the scan matrix } // Adding the columns to the _scanMatrix for (var colNo = 0; colNo < 5; colNo++) { var scanMatrixSet = new Collection <TicTacToeCells>(); for (int rowNo = 0; rowNo < 5; rowNo++) { scanMatrixSet.Add(_ticTacToeCells.First(ttts => ttts.rowID == rowNo && ttts.colID == colNo)); } _scanMatrix.Add(scanMatrixSet); } // Adding the first diagonal to the _scanMatrix _scanMatrix.Add(new Collection <TicTacToeCells> { _ticTacToeCells.First(ttts => ttts.rowID == 0 && ttts.colID == 0), _ticTacToeCells.First(ttts => ttts.rowID == 1 && ttts.colID == 1), _ticTacToeCells.First(ttts => ttts.rowID == 2 && ttts.colID == 2), _ticTacToeCells.First(ttts => ttts.rowID == 3 && ttts.colID == 3), _ticTacToeCells.First(ttts => ttts.rowID == 4 && ttts.colID == 4) }); // Adding the second diagonal to the _scanMatrix _scanMatrix.Add(new Collection <TicTacToeCells> { _ticTacToeCells.First(ttts => ttts.rowID == 4 && ttts.colID == 0), _ticTacToeCells.First(ttts => ttts.rowID == 3 && ttts.colID == 1), _ticTacToeCells.First(ttts => ttts.rowID == 2 && ttts.colID == 2), _ticTacToeCells.First(ttts => ttts.rowID == 1 && ttts.colID == 3), _ticTacToeCells.First(ttts => ttts.rowID == 0 && ttts.colID == 4), }); #endregion // Reset winner as open, since there is no winner at this point yet Winner = CellOwners.Open; IdentifyWinner(); }
public bool CheckForWinner() { /* * All of these repititious checks are replaced by ... * * // first row * if ((IdentifyCellOwner(0, 0) == CellOwners.Human) && * (IdentifyCellOwner(0, 1) == CellOwners.Human) && * (IdentifyCellOwner(0, 2) == CellOwners.Human)) * { * Winner = CellOwners.Human; * return true; * } * * // 2nd row * if ((IdentifyCellOwner(1, 0) == CellOwners.Human) && * (IdentifyCellOwner(1, 1) == CellOwners.Human) && * (IdentifyCellOwner(1, 2) == CellOwners.Human)) * { * Winner = CellOwners.Human; * return true; * } * * // 3rd row * if ((IdentifyCellOwner(2, 0) == CellOwners.Human) && * (IdentifyCellOwner(2, 1) == CellOwners.Human) && * (IdentifyCellOwner(2, 2) == CellOwners.Human)) * { * Winner = CellOwners.Human; * return true; * } * * // 1st col * if ((IdentifyCellOwner(0, 0) == CellOwners.Human) && * (IdentifyCellOwner(1, 0) == CellOwners.Human) && * (IdentifyCellOwner(2, 0) == CellOwners.Human)) * { * Winner = CellOwners.Human; * return true; * } * * // 2nd col * if ((IdentifyCellOwner(0, 1) == CellOwners.Human) && * (IdentifyCellOwner(1, 1) == CellOwners.Human) && * (IdentifyCellOwner(2, 1) == CellOwners.Human)) * { * Winner = CellOwners.Human; * return true; * } * * // 3rd col * if ((IdentifyCellOwner(0, 2) == CellOwners.Human) && * (IdentifyCellOwner(1, 2) == CellOwners.Human) && * (IdentifyCellOwner(2, 2) == CellOwners.Human)) * { * Winner = CellOwners.Human; * return true; * } * * // 1st diag * if ((IdentifyCellOwner(0, 0) == CellOwners.Human) && * (IdentifyCellOwner(1, 1) == CellOwners.Human) && * (IdentifyCellOwner(2, 2) == CellOwners.Human)) * { * Winner = CellOwners.Human; * return true; * } * * // 2nd diag * if ((IdentifyCellOwner(0, 2) == CellOwners.Human) && * (IdentifyCellOwner(1, 1) == CellOwners.Human) && * (IdentifyCellOwner(2, 0) == CellOwners.Human)) * { * Winner = CellOwners.Human; * return true; * } * * * * * * // first row * if ((IdentifyCellOwner(0, 0) == CellOwners.Computer) && * (IdentifyCellOwner(0, 1) == CellOwners.Computer) && * (IdentifyCellOwner(0, 2) == CellOwners.Computer)) * { * Winner = CellOwners.Computer; * return true; * } * * // 2nd row * if ((IdentifyCellOwner(1, 0) == CellOwners.Computer) && * (IdentifyCellOwner(1, 1) == CellOwners.Computer) && * (IdentifyCellOwner(1, 2) == CellOwners.Computer)) * { * Winner = CellOwners.Computer; * return true; * } * * // 3rd row * if ((IdentifyCellOwner(2, 0) == CellOwners.Computer) && * (IdentifyCellOwner(2, 1) == CellOwners.Computer) && * (IdentifyCellOwner(2, 2) == CellOwners.Computer)) * { * Winner = CellOwners.Computer; * return true; * } * * // 1st col * if ((IdentifyCellOwner(0, 0) == CellOwners.Computer) && * (IdentifyCellOwner(1, 0) == CellOwners.Computer) && * (IdentifyCellOwner(2, 0) == CellOwners.Computer)) * { * Winner = CellOwners.Computer; * return true; * } * * // 2nd col * if ((IdentifyCellOwner(0, 1) == CellOwners.Computer) && * (IdentifyCellOwner(1, 1) == CellOwners.Computer) && * (IdentifyCellOwner(2, 1) == CellOwners.Computer)) * { * Winner = CellOwners.Computer; * return true; * } * * // 3rd col * if ((IdentifyCellOwner(0, 2) == CellOwners.Computer) && * (IdentifyCellOwner(1, 2) == CellOwners.Computer) && * (IdentifyCellOwner(2, 2) == CellOwners.Computer)) * { * Winner = CellOwners.Computer; * return true; * } * * // 1st diag * if ((IdentifyCellOwner(0, 0) == CellOwners.Computer) && * (IdentifyCellOwner(1, 1) == CellOwners.Computer) && * (IdentifyCellOwner(2, 2) == CellOwners.Computer)) * { * Winner = CellOwners.Computer; * return true; * } * * // 2nd diag * if ((IdentifyCellOwner(0, 2) == CellOwners.Computer) && * (IdentifyCellOwner(1, 1) == CellOwners.Computer) && * (IdentifyCellOwner(2, 0) == CellOwners.Computer)) * { * Winner = CellOwners.Computer; * return true; * } * */ // unit #8 foreach (var combination in _winningCombinations) { // combination is a collection of 3 TicTacToe cell references var firstCell = combination[0]; if ((firstCell.CellOwner != CellOwners.Computer) && (firstCell.CellOwner != CellOwners.Human)) { continue; } if ((firstCell.CellOwner != combination[1].CellOwner) || (firstCell.CellOwner != combination[2].CellOwner)) { continue; } Winner = firstCell.CellOwner; return(true); } return(false); }
public bool CheckForWinner() { #region Human ////first row //if ((IdentifyCellOwner(0, 0) == CellOwners.Human) && // (IdentifyCellOwner(0, 1) == CellOwners.Human) && // (IdentifyCellOwner(0, 2) == CellOwners.Human)) //{ // Winner = CellOwners.Human; // return true; //} ////second row //if ((IdentifyCellOwner(1, 0) == CellOwners.Human) && // (IdentifyCellOwner(1, 1) == CellOwners.Human) && // (IdentifyCellOwner(1, 2) == CellOwners.Human)) //{ // Winner = CellOwners.Human; // return true; //} ////third row //if ((IdentifyCellOwner(2, 0) == CellOwners.Human) && // (IdentifyCellOwner(2, 1) == CellOwners.Human) && // (IdentifyCellOwner(2, 2) == CellOwners.Human)) //{ // Winner = CellOwners.Human; // return true; //} ////first column //if ((IdentifyCellOwner(0, 0) == CellOwners.Human) && // (IdentifyCellOwner(1, 0) == CellOwners.Human) && // (IdentifyCellOwner(2, 0) == CellOwners.Human)) //{ // Winner = CellOwners.Human; // return true; //} ////second column //if ((IdentifyCellOwner(0, 1) == CellOwners.Human) && // (IdentifyCellOwner(1, 1) == CellOwners.Human) && // (IdentifyCellOwner(2, 1) == CellOwners.Human)) //{ // Winner = CellOwners.Human; // return true; //} ////third column //if ((IdentifyCellOwner(0, 2) == CellOwners.Human) && // (IdentifyCellOwner(1, 2) == CellOwners.Human) && // (IdentifyCellOwner(2, 2) == CellOwners.Human)) //{ // Winner = CellOwners.Human; // return true; //} ////first diagonal //if ((IdentifyCellOwner(0, 0) == CellOwners.Human) && // (IdentifyCellOwner(1, 1) == CellOwners.Human) && // (IdentifyCellOwner(2, 2) == CellOwners.Human)) //{ // Winner = CellOwners.Human; // return true; //} ////second diagonal //if ((IdentifyCellOwner(0, 2) == CellOwners.Human) && // (IdentifyCellOwner(1, 1) == CellOwners.Human) && // (IdentifyCellOwner(2, 0) == CellOwners.Human)) //{ // Winner = CellOwners.Human; // return true; //} #endregion #region Computer ////first row //if ((IdentifyCellOwner(0, 0) == CellOwners.Computer) && // (IdentifyCellOwner(0, 1) == CellOwners.Computer) && // (IdentifyCellOwner(0, 2) == CellOwners.Computer)) //{ // Winner = CellOwners.Computer; // return true; //} ////second row //if ((IdentifyCellOwner(1, 0) == CellOwners.Computer) && // (IdentifyCellOwner(1, 1) == CellOwners.Computer) && // (IdentifyCellOwner(1, 2) == CellOwners.Computer)) //{ // Winner = CellOwners.Computer; // return true; //} ////third row //if ((IdentifyCellOwner(2, 0) == CellOwners.Computer) && // (IdentifyCellOwner(2, 1) == CellOwners.Computer) && // (IdentifyCellOwner(2, 2) == CellOwners.Computer)) //{ // Winner = CellOwners.Computer; // return true; //} ////first column //if ((IdentifyCellOwner(0, 0) == CellOwners.Computer) && // (IdentifyCellOwner(1, 0) == CellOwners.Computer) && // (IdentifyCellOwner(2, 0) == CellOwners.Computer)) //{ // Winner = CellOwners.Computer; // return true; //} ////second column //if ((IdentifyCellOwner(0, 1) == CellOwners.Computer) && // (IdentifyCellOwner(1, 1) == CellOwners.Computer) && // (IdentifyCellOwner(2, 1) == CellOwners.Computer)) //{ // Winner = CellOwners.Computer; // return true; //} ////third column //if ((IdentifyCellOwner(0, 2) == CellOwners.Computer) && // (IdentifyCellOwner(1, 2) == CellOwners.Computer) && // (IdentifyCellOwner(2, 2) == CellOwners.Computer)) //{ // Winner = CellOwners.Computer; // return true; //} ////first diagonal //if ((IdentifyCellOwner(0, 0) == CellOwners.Computer) && // (IdentifyCellOwner(1, 1) == CellOwners.Computer) && // (IdentifyCellOwner(2, 2) == CellOwners.Computer)) //{ // Winner = CellOwners.Computer; // return true; //} ////second diagonal //if ((IdentifyCellOwner(0, 2) == CellOwners.Computer) && // (IdentifyCellOwner(1, 1) == CellOwners.Computer) && // (IdentifyCellOwner(2, 0) == CellOwners.Computer)) //{ // Winner = CellOwners.Computer; // return true; //} #endregion if (_winningCombinations != null) { foreach (var combination in _winningCombinations) { var firstCell = combination[0]; if ((firstCell.CellOwner != CellOwners.Computer) && (firstCell.CellOwner != CellOwners.Human)) { continue; } if (combination[2].RowID == 2 && combination[2].ColID == 2) { if ((firstCell.CellOwner != combination[1].CellOwner) || //(firstCell.CellOwner != combination[2].CellOwner) || (firstCell.CellOwner != combination[3].CellOwner) || (firstCell.CellOwner != combination[4].CellOwner)) { continue; } } else { if ((firstCell.CellOwner != combination[1].CellOwner) || (firstCell.CellOwner != combination[2].CellOwner) || (firstCell.CellOwner != combination[3].CellOwner) || (firstCell.CellOwner != combination[4].CellOwner)) { continue; } } Winner = firstCell.CellOwner; return(true); } } return(false); }
// accepts info from the player's choice (row, column, Human/Computer) public CellAssignmentArgs(int rowID, int colID, CellOwners squareOwner) { this.RowId = rowID; this.ColId = colID; this.SquareOwner = squareOwner; }
public MarkerChangedArgs(int rowID, int colID, CellOwners cellOwner) { RowID = rowID; ColID = colID; CellOwner = cellOwner; }
// notice - no constructor. I dont think one is needed public void ResetGrid() { _ticTacToeCells.Clear(); // resets the collection to empty Winner = CellOwners.Open; // create the 9 cells for (var rowNo = 0; rowNo < 3; rowNo++) { for (var colNo = 0; colNo < 3; colNo++) { _ticTacToeCells.Add(new TicTacToeCell { RowID = rowNo, ColID = colNo }); } } // Unit #8 // // initializes the _goodNextMove collection with the preferred moves _goodNextMove = new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), }; // Unit #8 _winningCombinations = new Collection <Collection <TicTacToeCell> >() { new Collection <TicTacToeCell>() // loading the first row { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2) }, new Collection <TicTacToeCell>() // loading the second row { _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2) }, new Collection <TicTacToeCell>() // loading the first column { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2) }, new Collection <TicTacToeCell>() // loading the first diagonal { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2) }, new Collection <TicTacToeCell>() // loading final diagonal { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0) }, }; }
///<summary> ///a medthod to reset the in-memory grid /// </summary> public void ResetGrid() { _winningCombinations = null; _goodNextMove = null; _ticTacToeCells.Clear(); // resets the collection to empty Winner = CellOwners.Open; for (var rowNo = 0; rowNo < 3; rowNo++) { for (var colNo = 0; colNo < 3; colNo++) { // this will create 9 cells - rows 0-2 and col 0-2 var ticTacToeCell = new TicTacToeCell { RowID = rowNo, ColID = colNo }; _ticTacToeCells.Add(ticTacToeCell); } } _goodNextMove = new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1) }; _winningCombinations = new Collection <Collection <TicTacToeCell> >() { new Collection <TicTacToeCell>() // loading the first row { { // reference to these objects is in the collection - not the new ones! _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), }, new Collection <TicTacToeCell>()// loading the second row { _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), }, new Collection <TicTacToeCell>()// loading the third row { _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2), }, new Collection <TicTacToeCell>()// loading the first column { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), }, new Collection <TicTacToeCell>()// loading the second column { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), }, new Collection <TicTacToeCell>()// loading the third column { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2), }, new Collection <TicTacToeCell>()// loading the first diagonal { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2), }, new Collection <TicTacToeCell>()// loading the first diagonal { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), } }; }
/* * ProfReynolds * I moved the CellOwnerChangedArgs event to the bottom - personal preference - not wrong, just easier for me to follow */ public void ResetGrid() { _ticTacToeCells.Clear(); // resets the collection to empty Winner = CellOwners.Open; // create the 9 cells for (var rowNo = 0; rowNo < 5; rowNo++) { for (var colNo = 0; colNo < 5; colNo++) { _ticTacToeCells.Add(new TicTacToeCell { RowID = rowNo, ColID = colNo }); } } _goodNextMove = new Collection <TicTacToeCell>() { //4 corners _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 4), //middle boxes on sides _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 4), }; _winningCombinations = new Collection <Collection <TicTacToeCell> >() { new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 0) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 1) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 2) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 3) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 0) } }; Debug.WriteLine("\nMethod: Reset()"); }
/// <summary> /// student to provide comments explaining this method: /// --- what does it do -> Generates a new board for play /// --- why is it public -> So that it can be modified outside of local scope /// --- when is it executed (from where is it invoked) -> when a new board is requested /// </summary> public void ResetGrid() { // clear the current board for a new game _ticTacToeCells.Clear(); _scanMatrix.Clear(); for (var rowNo = 0; rowNo < 5; rowNo++) { for (var colNo = 0; colNo < 5; colNo++) { // 25 "spaces" are assigned the values of rowNo and colNo simulating a 5 x 5 board if ((rowNo == 2) && (colNo == 2)) { _ticTacToeCells.Add(new TicTacToeCells { rowID = rowNo, colID = colNo, // Sets the free space XorY = CellOwners.WildCard }); } else { _ticTacToeCells.Add(new TicTacToeCells { rowID = rowNo, colID = colNo, // XorY property is open to begin the game XorY = CellOwners.Open }); } } } #region - this region contains advanced code for the _scanMatrix. the student is expected to review it, but not modify it // Adding the three collections of 3 rows to the _scanMatrix for (var rowNo = 0; rowNo < 5; rowNo++) { // scanMatrixSet will hold columns 0, 1, and 2 for a given row var scanMatrixSet = new Collection <TicTacToeCells>(); for (int colNo = 0; colNo < 5; colNo++) { scanMatrixSet.Add(_ticTacToeCells.First(ttts => ttts.rowID == rowNo && ttts.colID == colNo)); } _scanMatrix.Add(scanMatrixSet); // just added a row of cells to the scan matrix } // Adding the columns to the _scanMatrix for (var colNo = 0; colNo < 5; colNo++) { var scanMatrixSet = new Collection <TicTacToeCells>(); for (int rowNo = 0; rowNo < 5; rowNo++) { scanMatrixSet.Add(_ticTacToeCells.First(ttts => ttts.rowID == rowNo && ttts.colID == colNo)); } _scanMatrix.Add(scanMatrixSet); } // Adding the first diagonal to the _scanMatrix _scanMatrix.Add(new Collection <TicTacToeCells> { _ticTacToeCells.First(ttts => ttts.rowID == 0 && ttts.colID == 0), _ticTacToeCells.First(ttts => ttts.rowID == 1 && ttts.colID == 1), _ticTacToeCells.First(ttts => ttts.rowID == 2 && ttts.colID == 2), _ticTacToeCells.First(ttts => ttts.rowID == 3 && ttts.colID == 3), _ticTacToeCells.First(ttts => ttts.rowID == 4 && ttts.colID == 4), }); // Adding the second diagonal to the _scanMatrix _scanMatrix.Add(new Collection <TicTacToeCells> { _ticTacToeCells.First(ttts => ttts.rowID == 4 && ttts.colID == 0), _ticTacToeCells.First(ttts => ttts.rowID == 3 && ttts.colID == 1), _ticTacToeCells.First(ttts => ttts.rowID == 2 && ttts.colID == 2), _ticTacToeCells.First(ttts => ttts.rowID == 1 && ttts.colID == 3), _ticTacToeCells.First(ttts => ttts.rowID == 0 && ttts.colID == 4) }); #endregion // it will be the winner's turn when the winning move is made Winner = CellOwners.Open; }
// notice - no constructor. I dont think one is needed public void ResetGrid() { _ticTacToeCells.Clear(); // resets the collection to empty Winner = CellOwners.Open; // create the 9 cells for (var rowNo = 0; rowNo < 5; rowNo++) { for (var colNo = 0; colNo < 5; colNo++) { _ticTacToeCells.Add(new TicTacToeCell { RowID = rowNo, ColID = colNo }); } } _goodNextMove = new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), }; _winningCombinations = new Collection <Collection <TicTacToeCell> >() { #region 5x5 Rows //new Collection<TicTacToeCell>() // loading the first row //{ // // the reference to these objects is in the collection - not new ones! // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==0), // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==1), // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==2), // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==3), // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==4) //}, //new Collection<TicTacToeCell>() // loading the second row //{ // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==0), // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==1), // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==2), // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==3), // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==4) //}, //new Collection<TicTacToeCell>() // loading the third row //{ // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==0), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==1), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==2), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==3), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==4) //}, //new Collection<TicTacToeCell>() // loading the fourth row //{ // _ticTacToeCells.First(tttc => tttc.RowID==3 && tttc.ColID==0), // _ticTacToeCells.First(tttc => tttc.RowID==3 && tttc.ColID==1), // _ticTacToeCells.First(tttc => tttc.RowID==3 && tttc.ColID==2), // _ticTacToeCells.First(tttc => tttc.RowID==3 && tttc.ColID==3), // _ticTacToeCells.First(tttc => tttc.RowID==3 && tttc.ColID==4) //}, //new Collection<TicTacToeCell>() // loading the fifth row //{ // _ticTacToeCells.First(tttc => tttc.RowID==4 && tttc.ColID==0), // _ticTacToeCells.First(tttc => tttc.RowID==4 && tttc.ColID==1), // _ticTacToeCells.First(tttc => tttc.RowID==4 && tttc.ColID==2), // _ticTacToeCells.First(tttc => tttc.RowID==4 && tttc.ColID==3), // _ticTacToeCells.First(tttc => tttc.RowID==4 && tttc.ColID==4) //}, #endregion //First Diagonal new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 4) }, //Second Diagonal new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 4) }, #region 5x5 Columns //new Collection<TicTacToeCell>() // loading the first row //{ // // the reference to these objects is in the collection - not new ones! // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==0), // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==0), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==0), // _ticTacToeCells.First(tttc => tttc.RowID==3 && tttc.ColID==0), // _ticTacToeCells.First(tttc => tttc.RowID==4 && tttc.ColID==0) //}, //new Collection<TicTacToeCell>() // loading the second row //{ // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==1), // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==1), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==1), // _ticTacToeCells.First(tttc => tttc.RowID==3 && tttc.ColID==1), // _ticTacToeCells.First(tttc => tttc.RowID==4 && tttc.ColID==1) //}, //new Collection<TicTacToeCell>() // loading the third row //{ // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==2), // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==2), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==2), // _ticTacToeCells.First(tttc => tttc.RowID==3 && tttc.ColID==2), // _ticTacToeCells.First(tttc => tttc.RowID==4 && tttc.ColID==2) //}, //new Collection<TicTacToeCell>() // loading the fourth row //{ // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==3), // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==3), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==3), // _ticTacToeCells.First(tttc => tttc.RowID==3 && tttc.ColID==3), // _ticTacToeCells.First(tttc => tttc.RowID==4 && tttc.ColID==3) //}, //new Collection<TicTacToeCell>() // loading the fifth row //{ // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==4), // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==4), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==4), // _ticTacToeCells.First(tttc => tttc.RowID==3 && tttc.ColID==4), // _ticTacToeCells.First(tttc => tttc.RowID==4 && tttc.ColID==4) //}, #endregion #region 3x3 //new Collection<TicTacToeCell>() //{ // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==0), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==1), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==2) //}, //new Collection<TicTacToeCell>() // loading the first column //{ // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==0), // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==0), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==0) //}, //new Collection<TicTacToeCell>() // loading the second column //{ // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==1), // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==1), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==1) //}, //new Collection<TicTacToeCell>() // loading the third column //{ // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==2), // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==2), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==2) //}, //new Collection<TicTacToeCell>() // loading beginning diagonal //{ // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==2), // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==1), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==0) //}, //new Collection<TicTacToeCell>() // loading final diagonal //{ // _ticTacToeCells.First(tttc => tttc.RowID==0 && tttc.ColID==0), // _ticTacToeCells.First(tttc => tttc.RowID==1 && tttc.ColID==1), // _ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==2) //}, #endregion }; for (int row = 0; row < 5; row++) { var combo = new Collection <TicTacToeCell>(); for (int col = 0; col < 5; col++) { combo.Add(_ticTacToeCells.First(tttc => tttc.RowID == row && tttc.ColID == col)); } _winningCombinations.Add(combo); } for (int col = 0; col < 5; col++) { var combo = new Collection <TicTacToeCell>(); for (int row = 0; row < 5; row++) { combo.Add(_ticTacToeCells.First(tttc => tttc.RowID == row && tttc.ColID == col)); } _winningCombinations.Add(combo); } }
// notice - no constructor. I dont think one is needed public void ResetGrid() { _ticTacToeCells.Clear(); // resets the collection to empty Winner = CellOwners.Open; // create the 9 cells for (var rowNo = 0; rowNo < 5; rowNo++) { for (var colNo = 0; colNo < 5; colNo++) { if (rowNo != 2 || colNo != 2) { _ticTacToeCells.Add(new TicTacToeCell { RowID = rowNo, ColID = colNo }); } } } // Unit #8 // // initializes the _goodNextMove collection with the preferred moves _goodNextMove = new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 3), }; // Unit #8 _winningCombinations = new Collection <Collection <TicTacToeCell> >() { new Collection <TicTacToeCell>() // loading row #0 { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() // loading row #1 { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() // loading row #2 { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() // loading row #3 { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() // loading row #4 { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() // loading column #0 { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 0) }, new Collection <TicTacToeCell>() // loading column #1 { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 1) }, new Collection <TicTacToeCell>() // loading column #2 { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 2) }, new Collection <TicTacToeCell>() // loading column #3 { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 3) }, new Collection <TicTacToeCell>() // loading column #4 { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() // 1st diagonal { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() // 2nd diagopnal { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 0) }, }; Debug.WriteLine("\nMethod: Reset()"); }
// notice - no constructor. I dont think one is needed /// <summary> /// Resets the grid for the first game or a new game /// </summary> public void ResetGrid() { _ticTacToeCells.Clear(); // resets the collection to empty Winner = CellOwners.Open; for (var rowNo = 0; rowNo < GridSize; rowNo++) { for (var colNo = 0; colNo < GridSize; colNo++) { if (GridSize == 3 || !(rowNo == 2 && colNo == 2)) { _ticTacToeCells.Add(new TicTacToeCell { RowID = rowNo, ColID = colNo }); } } } if (GridSize == 3) { _goodNextMove = new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), }; } if (GridSize == 5) { _goodNextMove = new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 3), }; } if (GridSize == 3) { _winningCombinations = new Collection <Collection <TicTacToeCell> >() { new Collection <TicTacToeCell>() // loading the first row { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2) }, new Collection <TicTacToeCell>() // loading the second row { _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2) }, new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2) }, new Collection <TicTacToeCell>() // loading the first column { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0) }, new Collection <TicTacToeCell>() // loading the second column { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1) }, new Collection <TicTacToeCell>() // loading the third column { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2) }, new Collection <TicTacToeCell>() // loading beginning diagonal { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0) }, new Collection <TicTacToeCell>() // loading final diagonal { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 2) }, }; } else if (GridSize == 5) { _winningCombinations = new Collection <Collection <TicTacToeCell> >() { #region 5x5 Rows new Collection <TicTacToeCell>() // loading the first row { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() // loading the second row { _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() // loading the third row { _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), //_ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==2), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() // loading the fourth row { _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 4) }, new Collection <TicTacToeCell>() // loading the fifth row { _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 4) }, #endregion #region 5x5 Columns new Collection <TicTacToeCell>() // loading the first column { // the reference to these objects is in the collection - not new ones! _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 0) }, new Collection <TicTacToeCell>() // loading the second column { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 1) }, new Collection <TicTacToeCell>() // loading the third column { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 2), //_ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==2), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 2), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 2) }, new Collection <TicTacToeCell>() // loading the fourth column { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 3) }, new Collection <TicTacToeCell>() // loading the fifth column { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 2 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 4) }, #endregion //first Diagonal new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 0), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 1), //_ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==2), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 3), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 4) }, //Second Diagonal new Collection <TicTacToeCell>() { _ticTacToeCells.First(tttc => tttc.RowID == 0 && tttc.ColID == 4), _ticTacToeCells.First(tttc => tttc.RowID == 1 && tttc.ColID == 3), //_ticTacToeCells.First(tttc => tttc.RowID==2 && tttc.ColID==2), _ticTacToeCells.First(tttc => tttc.RowID == 3 && tttc.ColID == 1), _ticTacToeCells.First(tttc => tttc.RowID == 4 && tttc.ColID == 0) }, }; #region for loop testing //for (int row = 0; row < 5; row++) //{ // var combo = new Collection<TicTacToeCell>(); // for (int col = 0; col < 5; col++) // { // if (row != 2 && col != 2) // { // combo.Add(_ticTacToeCells.First(tttc => tttc.RowID == row && tttc.ColID == col)); // } // } // _winningCombinations.Add(combo); //} //for (int col = 0; col < 5; col++) //{ // var combo = new Collection<TicTacToeCell>(); // for (int row = 0; row < 5; row++) // { // if (row != 2 && col != 2) // { // combo.Add(_ticTacToeCells.First(tttc => tttc.RowID == row && tttc.ColID == col)); // } // } // _winningCombinations.Add(combo); //} #endregion //ignore } }