public string RoundWinner() { StringBuilder res = new StringBuilder(); string envNewLine = Environment.NewLine; if (m_LastRoundWinner == ePlayerNum.Empty) { //// A tie. res.Append("A tie" + envNewLine); LastRoundWinner = ePlayerNum.Empty; } else if (m_LastRoundWinner == ePlayerNum.Player1) { //// Player 1 won. res.Append("Player 1 Won" + envNewLine); LastRoundWinner = ePlayerNum.Player1; } else { //// Player 2 won. AKA player 2 OR computer. res.Append("Player 2 Won" + envNewLine); LastRoundWinner = ePlayerNum.Player2; } return(res.ToString()); }
public bool CheckForAWin(int i_ColumnNum, ePlayerNum i_PlayerNum, bool i_Update) { bool res = false; res = m_Game.CheckForAWin(i_ColumnNum, i_PlayerNum, i_Update); return(res); }
private void insertSelection(int i_Col, ePlayerNum i_PlayerNum) { // If you can click on it - you can enter a chip. this.InsertChip(new Chip(i_PlayerNum), i_Col, true); updateButtonsAvailability(); if (this.CheckForAWin(i_Col, i_PlayerNum, true)) { // Player i_PlayerNum WON ! increasePointsByOne(i_PlayerNum); // If we have a winner, we need to publish it. winnerMessageBox(i_PlayerNum); this.m_GameManager.LastRoundWinner = i_PlayerNum; } else { // Player i_PlayerNum didn't win. if (this.IsBoardFull()) { this.aTieMessageBox(); } } updateButtonsAvailability(); }
public GameManager(int i_NumOfRows, int i_NumOfCols) { m_GameBoard = new Board(i_NumOfRows, i_NumOfCols); m_Player1Points = 0; m_Player2Points = 0; m_LastRoundWinner = ePlayerNum.Empty; }
private void clearBoardOption(ePlayerNum i_PlayerNum) { System.Console.WriteLine(k_UserClearBoard); if (getAnswerYesOrNot(k_UserClearBoard)) { this.clearBoard(); } }
public void IncreaseOpponentPointsByOne(ePlayerNum i_PlayerNum) { if (i_PlayerNum == ePlayerNum.Player1) { m_Player2Points++; } else { m_Player1Points++; } }
private void increasePointsByOne(ePlayerNum i_PlayerNum) { if (i_PlayerNum == ePlayerNum.Player1) { this.labelPlayer1WinsCounter.Text = this.m_GameManager.IncreasePointsByOne(i_PlayerNum).ToString(); } else { this.labelPlayer2WinsCounter.Text = this.m_GameManager.IncreasePointsByOne(i_PlayerNum).ToString(); } }
private void runTwoPlayersAux(ePlayerNum i_PlayerNum) { getNextTurn(i_PlayerNum); if (this.IsBoardFull()) { this.PrintGameStatus(); exitMenu(i_PlayerNum); this.clearBoard(); } }
// Winner Message Box private void winnerMessageBox(ePlayerNum i_PlayerNum) { string playerName = i_PlayerNum == ePlayerNum.Player1 ? labelPlayer1Score.Text : labelPlayer2Score.Text; MessageBox.Show( @"Congratulation, " + playerName + @", has won this round", @"Congratulation !", MessageBoxButtons.OK); this.additionalMessageBox(); }
private void getNextTurn(ePlayerNum i_PlayerNum) { int colToBeInserted = 0; int numOfCols = m_Game.GetNumOfCols(); string tempLine = string.Empty; Screen.Clear(); PrintBoard(); Console.WriteLine(i_PlayerNum + k_ChooseColumn); //// A loop that keeps running until valid column number or 'Q' - k_ExitKey. for (tempLine = Console.ReadLine(); (!int.TryParse(tempLine, out colToBeInserted) && tempLine.CompareTo(k_ExitKey) != 0) || (int.TryParse(tempLine, out colToBeInserted) && !ValidateColumn(--colToBeInserted)); tempLine = Console.ReadLine()) { //// supposed to run until a valid num or 'Q' will be entered. (k_ExitKey). Console.WriteLine(k_IllegalValue + k_ChooseColumn); } if (tempLine.CompareTo(k_ExitKey) == 0) { m_Retire = true; this.PrintGameStatus(); exitMenu(i_PlayerNum); m_Retire = false; } else { //// only if a legal column....(have to a legal one at this point..^^^^) InsertChip(new Chip(i_PlayerNum), colToBeInserted); Screen.Clear(); PrintBoard(); if (CheckForAWin(colToBeInserted, i_PlayerNum, true)) { //// The last move was a winner ! /// Add updates points to game manager & remove it from the exit menu method. this.m_Game.IncreasePointsByOne(i_PlayerNum); this.m_Game.LastRoundWinner = i_PlayerNum; this.PrintGameStatus(); exitMenu(i_PlayerNum); this.clearBoard(); } else { ////Tie - because if the other player has won - it should dealt in his turn. } } }
//// checks winning conditions after adding a chip to pos [i_Row,i_Col] private bool isPartOfASequence(int i_Row, int i_Col, ePlayerNum i_ePlayerNum) { bool isPartOfSeq = false; if (checkRowSeq(i_Row, i_Col, i_ePlayerNum) || checkColumnSeq(i_Row, i_Col, i_ePlayerNum) || checkDiagonalSeq(i_Row, i_Col, i_ePlayerNum)) { isPartOfSeq = true; } return(isPartOfSeq); }
// Checks if the board position is filled by the passed player enum. private bool checkPosPerPlayer(int i_Row, int i_Col, ePlayerNum i_ePlayerNum = ePlayerNum.Empty) { bool res = false; if ((i_Row >= 0 && i_Row < m_Board.GetLength(0)) && (i_Col >= 0 && i_Col < m_Board.GetLength(1))) { if (m_Board[i_Row, i_Col] != null && m_Board[i_Row, i_Col].PlayerNum == i_ePlayerNum) { res = true; } } return(res); }
public int IncreasePointsByOne(ePlayerNum i_PlayerNum) { int updatedPoints = 0; if (i_PlayerNum == ePlayerNum.Player1) { m_Player1Points++; updatedPoints = this.m_Player1Points; } else { m_Player2Points++; updatedPoints = this.m_Player2Points; } return(updatedPoints); }
// This method will mange the control flow after one of the players entered 'Q'. private void exitMenu(ePlayerNum i_PlayerNum) { System.Console.WriteLine(k_UserExitProgram); this.m_ExitProgram = getAnswerYesOrNot(k_UserExitProgram); if (!m_ExitProgram) { if (m_Retire) { clearBoardOption(i_PlayerNum); } } else { //// yes - 'i_PlayerNum' want to quit !! this.m_ExitProgram = true; } }
// Checks whether a diagonal sequence exists with the new added chip private bool checkDiagonalSeq(int i_Row, int i_Col, ePlayerNum i_ePlayerNum) { bool isPartOfSeq = false; int chipCounterForwardDiag = 1; int chipCounterBackwardDiag = 1; chipCounterForwardDiag += checkDiagonalSeqAux(i_Row, i_Col, 0, '+', '+', i_ePlayerNum) + checkDiagonalSeqAux(i_Row, i_Col, 0, '-', '-', i_ePlayerNum); chipCounterBackwardDiag += checkDiagonalSeqAux(i_Row, i_Col, 0, '+', '-', i_ePlayerNum) + checkDiagonalSeqAux(i_Row, i_Col, 0, '-', '+', i_ePlayerNum); if (chipCounterForwardDiag == 4 || chipCounterBackwardDiag == 4) { isPartOfSeq = true; } return(isPartOfSeq); }
// Checks whether a row sequence exists with the new added chip private bool checkRowSeq(int i_Row, int i_Col, ePlayerNum i_ePlayerNum) { bool isPartOfSeq = false; int chipCounter = 1; int runningColIndex = i_Col + 1; // Explore the right side of the row while (runningColIndex < m_Board.GetLength(1) && chipCounter < 4) { if (checkPosPerPlayer(i_Row, runningColIndex, i_ePlayerNum)) { chipCounter++; runningColIndex++; } else { break; } } runningColIndex = i_Col - 1; // Explore the left side of the row while (runningColIndex >= 0 && chipCounter < 4) { if (checkPosPerPlayer(i_Row, runningColIndex, i_ePlayerNum)) { chipCounter++; runningColIndex--; } else { break; } } // $G$ CSS-999 (-3) instead of using numbers in a if statement use constants if (chipCounter == 4) { isPartOfSeq = true; } return(isPartOfSeq); }
// Checks whether a column sequence exists with the new added chip private bool checkColumnSeq(int i_Row, int i_Col, ePlayerNum i_ePlayerNum) { bool isPartOfSeq = false; int chipCounter = 1; int runningRowIndex = i_Row + 1; // Explore the upper column for a sequnce while (runningRowIndex < m_Board.GetLength(0) && chipCounter < 4) { if (checkPosPerPlayer(runningRowIndex, i_Col, i_ePlayerNum)) { chipCounter++; runningRowIndex++; } else { break; } } runningRowIndex = i_Row - 1; // Explore the bottom column for a sequnce while (runningRowIndex >= 0 && chipCounter < 4) { if (checkPosPerPlayer(runningRowIndex, i_Col, i_ePlayerNum)) { chipCounter++; runningRowIndex--; } else { break; } } if (chipCounter == 4) { isPartOfSeq = true; } return(isPartOfSeq); }
// This function return a column number that wins the game for a player(player1 or computer - as given) - if exists. // Otherwise ,return (-1). private int winnerColumn(GameManager io_Game, ePlayerNum i_PlayerNum) { int colToBeInserted = -1; int numOfCols = io_Game.GetNumOfCols(); for (int col = 0; io_Game.ValidateColumn(col); col++) { if (io_Game.InsertChip(new Chip(i_PlayerNum), col)) { if (io_Game.CheckForAWin(col, i_PlayerNum, false)) { colToBeInserted = col; } io_Game.RemoveChip(col); } } return(colToBeInserted); }
public Chip(ePlayerNum i_PlayerNum) { m_PlayerNum = i_PlayerNum; if (i_PlayerNum == ePlayerNum.Player1) { m_Representation = eRepresentation.Player1; } else if (i_PlayerNum == ePlayerNum.Player2) { m_Representation = eRepresentation.Player2; } else if (i_PlayerNum == ePlayerNum.Computer) { m_Representation = eRepresentation.Computer; } else { m_Representation = eRepresentation.Empty; } }
// This method check if the last move was a 4 chip flash. // It also updates the 'm_LastRoundWinner' attribute. // If i_Update , true - updates the winner. else - not update.(For AI purpose). public bool CheckForAWin(int i_ColumnNum, ePlayerNum i_PlayerNum, bool i_Update) { bool isASeqExists = false; if (i_Update) { this.LastRoundWinner = ePlayerNum.Empty; } isASeqExists = m_GameBoard.IsPartOfASequence(i_ColumnNum, i_PlayerNum); if (isASeqExists) { if (i_Update) { this.LastRoundWinner = i_PlayerNum; } } return(isASeqExists); }
public bool IsPartOfASequence(int i_ColumNum, ePlayerNum i_PlayerNum) { bool isPartOfSeq = false; int numOfRows = m_Board.GetLength(0); if (IsColNumInRange(i_ColumNum)) { int row = 0; for (; row < numOfRows - 1 && m_Board[row, i_ColumNum] == null; row++) { //// Find the first non-null object in the column } if (row <= numOfRows - 1) { isPartOfSeq = isPartOfASequence(row, i_ColumNum, m_Board[row, i_ColumNum].PlayerNum); } } return(isPartOfSeq); }
internal void ButtonImageChange(int i_Row, int i_Col, ePlayerNum i_PlayerNum) { Bitmap blueChip = Properties.Resources.bluePiece; Bitmap redChip = Properties.Resources.RedPiece; // Dont check if avialable, because if not - should'nt get here, because cant be choosen. if (IsColNumInRange(i_Col) && IsRowNumInRange(i_Row)) { if (i_PlayerNum == ePlayerNum.Empty) { ButtonImageRemovel(i_Row, i_Col); } else if (i_PlayerNum == ePlayerNum.Player1) { // Blue Chip. chipMovment(i_Row, i_Col, blueChip); } else { // Red Chip. chipMovment(i_Row, i_Col, redChip); } } }
// The game will mange by this method. // buttonClicked or OnButtonClicked ? - it self generated.. private void buttonClicked(object sender, EventArgs e) { int col; Button b = sender as Button; // Check it before all process - more general way.. int.TryParse(b.Name.Substring(b.Name.Length - 1), out col); if (this.m_NumOfPlayers == 1) { // If one player : // The method will get the first click from player1, and then from the computer. // (need to disable all controls - while computer turn) // All control will Enabled after it. // (and this function will be activate again..) insertSelection(col, ePlayerNum.Player1); this.Enabled = false; this.Refresh(); this.computerTurn(); this.Enabled = true; this.Refresh(); } else { // If two players : // The method will use a 'ePlayer m_Player' field (Attribute), that after each click will be changed on & off. // In this way each click will be a click of a different player. insertSelection(col, this.m_Player); // Change the player on & off.. this.m_Player = this.m_Player == ePlayerNum.Player1 ? ePlayerNum.Player2 : ePlayerNum.Player1; } }
public Chip() { m_PlayerNum = ePlayerNum.Empty; m_Representation = eRepresentation.Empty; }
// Auxilery function for checking both possible diagonal sequences private int checkDiagonalSeqAux(int i_Row, int i_Col, int i_ChipCounter, char i_RowIncDir, char i_ColIncDir, ePlayerNum i_ePlayerNum) { int runningColIndex = i_ColIncDir == '+' ? i_Col + 1 : i_Col - 1; int runningRowIndex = i_RowIncDir == '+' ? i_Row + 1 : i_Row - 1; while (IsColNumInRange(runningColIndex) && IsRowNumInRange(runningRowIndex) && i_ChipCounter < 4) { if (checkPosPerPlayer(runningRowIndex, runningColIndex, i_ePlayerNum)) { i_ChipCounter++; runningColIndex = i_ColIncDir == '+' ? runningColIndex + 1 : runningColIndex - 1; runningRowIndex = i_RowIncDir == '+' ? runningRowIndex + 1 : runningRowIndex - 1; } else { break; } } return(i_ChipCounter); }
public Chip(ePlayerNum i_PlayerNum, eRepresentation i_Representation) { m_PlayerNum = i_PlayerNum; m_Representation = i_Representation; }