// Constructor creates a new Player receiving a name from ibxInputBox, access to the dice window // through dwDiceWin and a number of players as parameters public Player(InputBox ibxInputBox, DiceWindow dwDiceWin, int iPlayers = 1) { InitializeComponent(); bLurgit = false; // Hides "Games Won" label because it makes no sense in this case if (iPlayers == 1) { lblSession.Visible = false; } // Instantiate a new game session sesPlayerSession = new Session(); // Initialise counters _iNumPlayers++; iPlayerNumber = _iNumPlayers; iRollCounter = 0; iRoundCounter = 1; _iGameCounter = 1; // Initialise dice window dwDice = dwDiceWin; // Instantiate the first game gGame = new Game(); // Instantiate first round rouRound = new Round(1); // Dice have no reason to be user lockable at this point dwDice.disableAllDice(); // Set the properties of the player windows Top = dwDiceWin.Top; if (_iNumPlayers == 1) { cPlayerColour = Color.LightBlue; Left = dwDiceWin.Left - 50 - Width; } else { cPlayerColour = Color.LightPink; Left = dwDiceWin.Right + 50; Enabled = false; } BackColor = cPlayerColour; // Set the title of the player windows if (ibxInputBox.sName == null) { sPlayerName = "Computer"; btnRoll.Visible = false; } else { sPlayerName = ibxInputBox.sName; } Text = sPlayerName; }
// This event handler method occurs when the Roll button is clicked private void btnRoll_Click(object sender, EventArgs e) { // Clear the player window for a new game if (iRoundCounter == 0) { lbxGame.Items.Clear(); lbxRound.Items.Clear(); lblTotal.Text = "Game Total: 0"; iRoundCounter = 1; } // Increment counters if (iRollCounter < 3) { iRollCounter++; } else { iRollCounter = 1; iRoundCounter++; rouRound = new Round(iRoundCounter); lbxRound.Items.Clear(); } // Instantiate a new dice roll Roll rDiceRoll = new Roll(iRoundCounter); rDiceRoll.iRollNumber = iRollCounter; // Roll the dice btnRoll.Enabled = false; dwDice.roll(this); btnRoll.Enabled = true; // Enable the dice for user selection if (dwDice.dieDice[0].bInactive) { dwDice.enableDieOne(); } if (dwDice.dieDice[1].bInactive) { dwDice.enableDieTwo(); } if (dwDice.dieDice[2].bInactive) { dwDice.enableDieThree(); } // Store the dice roll rDiceRoll.iDie1 = dwDice.dieDice[0].iFace; rDiceRoll.iDie2 = dwDice.dieDice[1].iFace; rDiceRoll.iDie3 = dwDice.dieDice[2].iFace; // Check for a Lurgit condition bLurgit = false; if (!dwDice.dieDice[0].bLocked && !dwDice.dieDice[1].bLocked && !dwDice.dieDice[2].bLocked) { bLurgit = rDiceRoll.checkLurgit(); if (bLurgit) { MessageBox.Show(sPlayerName + " got a Lurgit! Whoopdee Dooo!", "Lurgit", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); iRollCounter = 3; } } // Check for a sequence condition bSequence = rDiceRoll.checkSequence(); // Add the roll to the player's round and display in the roll list rouRound.setRoll(rDiceRoll, iRollCounter); lbxRound.Items.Add(rDiceRoll.ToString()); // Prevent the player from keeping the sequence sequenceLock(rDiceRoll, bSequence); if (!bLurgit && sPlayerName == "Computer") { chooseDice(); } // End the round if (iRollCounter >= 3) { dwDice.disableAllDice(); // Add round to the round list and game scores lbxGame.Items.Add(rouRound.ToString()); gGame.setRound(iRoundCounter, rouRound); // Display running points total lblTotal.Text = "Game Total: " + gGame.iGameScore; // Switch player windows for two player if (_iNumPlayers > 1) { if (iRoundCounter < 6) { if (sPlayerName == "Computer") { MessageBox.Show("Player's turn!", "Turn Over"); } else { MessageBox.Show("Player's turn finished!", "Turn Over"); } } // Disable this window Enabled = false; List <Form> lstPlayers = new List <Form>(); foreach (Form frmForm in Application.OpenForms) { if (frmForm.Name == "Player") { lstPlayers.Add(frmForm); } } // Cycle open forms to find the other player form foreach (Form frmPlayer in lstPlayers) { if (frmPlayer != this) { frmPlayer.Enabled = true; } } } // Check for end of game if (iRoundCounter < 6) { if (_iNumPlayers == 1) { MessageBox.Show("Player's turn finished!", "Turn Over"); } // Player cannot choose dice dwDice.disableAllDice(); // If a Lurgit occurred, move on to the next round if (bLurgit) { iRollCounter = 0; iRoundCounter++; rouRound = new Round(iRoundCounter); lbxRound.Items.Clear(); } } else { // Set the game number and the high score gGame.iGameNumber = _iGameCounter; dwDice.iHighScore = gGame.iGameScore; // After the last roll choose to play again or quit if (_iNumPlayers == 1 || iPlayerNumber == 2) { // Add game data to the list of games in the session sesPlayerSession.lstGameList.Add(gGame); String sWinner = ""; // Determine which player was the winner if (iPlayerNumber == 2) { sWinner = selectWinner(); } _iGameCounter++; // Show message box with options DialogResult drResult = MessageBox.Show(sWinner + "Another game?", "Play Again", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); // Act on user selection if (drResult == DialogResult.Yes) { // Reset player two window resetPlayerWindow(); if (_iNumPlayers == 2) { // Disable this window Enabled = false; // Return to player one window foreach (Form frmForm in Application.OpenForms) { if (frmForm.Name.Equals("Player") && frmForm != this) { frmForm.Enabled = true; } } } } else { dwDice.Close(); } } else { //Game.iP1Score = gGame.iGameScore; if (Game.iP1Score > Game.iP2Score) { sesPlayerSession.iGamesWon++; lblSession.Text = "Games Won: " + sesPlayerSession.iGamesWon; } //sesPlayerSession.lstGameList.Add(gGame); dwDice.disableAllDice(); resetPlayerWindow(); } } } }