//----------------------------------------------------------------------// internal bool ReturnRandomTileFromList(out Board.Tile io_Tile, Random i_Random) { io_Tile = null; bool isTileFound = false; if (this.m_RememberFlips.Count != 0) { io_Tile = m_RememberFlips[i_Random.Next(0, m_RememberFlips.Count)]; } while (this.m_RememberFlips.Count > 1 && io_Tile.IsOpen) { this.m_RememberFlips.Remove(io_Tile); io_Tile = m_RememberFlips[i_Random.Next(0, m_RememberFlips.Count)]; } if (this.m_RememberFlips.Count != 0 && io_Tile.IsOpen) { this.m_RememberFlips.Remove(io_Tile); } else if (io_Tile != null) { isTileFound = true; } return(isTileFound); }
//----------------------------------------------------------------------// internal bool IsMatchingFlip(Board.Tile i_FirstTile, Board.Tile i_SecondTile) { bool isMatchingSet = false; bool openedFirstTile = false; bool openedSecondTile = false; if (!i_FirstTile.IsOpen) { i_FirstTile.OpenTile(); openedFirstTile = true; } if (!i_SecondTile.IsOpen) { i_SecondTile.OpenTile(); openedSecondTile = true; } isMatchingSet = i_FirstTile.ContentOfTile == i_SecondTile.ContentOfTile; if (openedFirstTile) { i_FirstTile.CloseTile(); } if (openedSecondTile) { i_SecondTile.CloseTile(); } return(isMatchingSet); }
//----------------------------------------------------------------------// internal void MakeAIMove(UserInterface i_UI, out Board.Tile o_FirstTile, out Board.Tile o_SecondTile, Player i_Player) { bool foundMatchInList = findAMatchInList(out o_FirstTile, out o_SecondTile, i_UI.Game); if (!foundMatchInList) { o_SecondTile = this.ReturnARandomTileFromBoard(i_UI.Game); while (o_SecondTile == o_FirstTile) { o_SecondTile = this.ReturnARandomTileFromBoard(i_UI.Game); } if (!this.m_RememberFlips.Contains(o_SecondTile) && !i_UI.Game.IsMatchingFlip(o_FirstTile, o_SecondTile)) { this.m_RememberFlips.Add(o_SecondTile); } else if (i_UI.Game.IsMatchingFlip(o_FirstTile, o_SecondTile)) { this.m_RememberFlips.Remove(o_FirstTile); } } else { this.m_RememberFlips.Remove(o_FirstTile); this.m_RememberFlips.Remove(o_SecondTile); } this.OpenAndPrintBoard(o_FirstTile, o_SecondTile, i_UI, i_Player); }
//----------------------------------------------------------------------// internal void OpenAndPrintBoard(Board.Tile i_FirstTile, Board.Tile i_SecondTile, UserInterface i_UI, Player i_Player) { i_FirstTile.OpenTile(); i_UI.ClearScreenShowBoard(i_Player); System.Threading.Thread.Sleep(2000); i_SecondTile.OpenTile(); i_UI.ClearScreenShowBoard(i_Player); }
//----------------------------------------------------------------------// internal bool findAMatchingSet(Board.Tile i_FirstTile, out Board.Tile o_SecondTile, GuessingGame i_Game) { o_SecondTile = null; bool isMatchFound = false; foreach (Board.Tile currentTile in this.m_RememberFlips) { if (i_Game.IsMatchingFlip(i_FirstTile, currentTile)) { if (currentTile != i_FirstTile) { o_SecondTile = currentTile; isMatchFound = true; break; } } } return(isMatchFound); }
//----------------------------------------------------------------------// private bool findAMatchInList(out Board.Tile o_FirstTile, out Board.Tile o_SecondTile, GuessingGame i_Game) { bool isFoundMatch = false; o_SecondTile = o_FirstTile = null; foreach (Board.Tile currentTile in this.m_RememberFlips) { if (!currentTile.IsOpen) { isFoundMatch = this.findAMatchingSet(currentTile, out o_SecondTile, i_Game); if (isFoundMatch) { o_FirstTile = currentTile; break; } } } if (!isFoundMatch) { o_FirstTile = this.ReturnARandomTileFromBoard(i_Game); if (!this.m_RememberFlips.Contains(o_FirstTile)) { this.m_RememberFlips.Add(o_FirstTile); if (findAMatchingSet(o_FirstTile, out o_SecondTile, i_Game)) { isFoundMatch = true; } } } return(isFoundMatch); }
//----------------------------------------------------------------------// internal bool CheckForValidGameMove(string i_UserMove, out Board.Tile io_TileToFlip) { bool isValidMove = false, validRows, validColumns; int rowFromInput = 0; int colFromInput = 0; io_TileToFlip = null; if (i_UserMove.Length == 2 && char.IsUpper(i_UserMove[0]) && char.IsDigit(i_UserMove[1])) { colFromInput = Convert.ToInt32(i_UserMove[0] - 'A'); rowFromInput = Convert.ToInt32(i_UserMove[1] - '0'); validRows = (rowFromInput <= this.Board.RowBorder && rowFromInput >= 0); validColumns = (colFromInput <= this.Board.ColumnBorder && colFromInput >= 0); isValidMove = (validRows && validColumns); } if (isValidMove) { io_TileToFlip = this.Board[rowFromInput - 1, colFromInput]; } return(isValidMove); }
//----------------------------------------------------------------------// private void userInputMove(out Board.Tile io_Tile, Player i_Player) { string userMoveInput = Console.ReadLine(); this.m_Game.DidUserQuit(userMoveInput); while (!this.m_Game.CheckForValidGameMove(userMoveInput, out io_Tile) || (io_Tile != null && io_Tile.IsOpen == true)) { if ((io_Tile != null && io_Tile.IsOpen == true)) { Console.Write("Tile already open, please re-enter another tile to flip: "); } else { Console.Write("Invalid tile, please re-enter another tile to flip: "); } userMoveInput = Console.ReadLine(); this.m_Game.DidUserQuit(userMoveInput); } io_Tile.OpenTile(); this.ClearScreenShowBoard(i_Player); }