Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

private void InitializeComponent() { this.btnDone = new System.Windows.Forms.Button(); this.pnlBoard = new System.Windows.Forms.Panel(); this.btnQuit = new System.Windows.Forms.Button(); this.label8 = new System.Windows.Forms.Label(); this.btnLetsPlay = new System.Windows.Forms.Button(); this.btnOkPlayer2

private void InitializeComponent() { this.btnDone = new System.Windows.Forms.Button(); this.pnlBoard = new System.Windows.Forms.Panel(); this.btnQuit = new System.Windows.Forms.Button(); this.label8 = new System.Windows.Forms.Label(); this.btnLetsPlay = new System.Windows.Forms.Button(); this.btnOkPlayer2 = new System.Windows.Forms.Button(); this.btnOkPlayer1 = new System.Windows.Forms.Button(); this.txtTurn = new System.Windows.Forms.TextBox(); this.txtPlayer2 = new System.Windows.Forms.TextBox(); this.txtPlayer1 = new System.Windows.Forms.TextBox(); this.lblTurn = new System.Windows.Forms.Label(); this.lblReady = new System.Windows.Forms.Label(); this.lblSecondPlayer = new System.Windows.Forms.Label(); this.lblFirstPlayer = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.lblWelcome = new System.Windows.Forms.Label(); this.SuspendLayout(); // // btnDone // this.btnDone.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.btnDone.Location = new System.Drawing.Point(473, 432); this.btnDone.Name = "btnDone"; this.btnDone.Size = new System.Drawing.Size(82, 51); this.btnDone.TabIndex = 35; this.btnDone.Text = "I\'ve Completed My Turn"; this.btnDone.UseVisualStyleBackColor = true; this.btnDone.Visible = false; this.btnDone.Click += new System.EventHandler(this.btnDone_Click); // // pnlBoard // this.pnlBoard.Location = new System.Drawing.Point(11, 225); this.pnlBoard.Name = "pnlBoard"; this.pnlBoard.Size = new System.Drawing.Size(456, 258); this.pnlBoard.TabIndex = 34; this.pnlBoard.Visible = false; // // btnQuit // this.btnQuit.BackColor = System.Drawing.Color.Red; this.btnQuit.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.btnQuit.Location = new System.Drawing.Point(11, 489); this.btnQuit.Name = "btnQuit"; this.btnQuit.Size = new System.Drawing.Size(96, 26); this.btnQuit.TabIndex = 33; this.btnQuit.Text = "Let\'s Quit!"; this.btnQuit.UseVisualStyleBackColor = false; this.btnQuit.Click += new System.EventHandler(this.btnQuit_Click); // // label8 // this.label8.AutoSize = true; this.label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label8.ForeColor = System.Drawing.SystemColors.Highlight; this.label8.Location = new System.Drawing.Point(16, 207); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(494, 15); this.label8.TabIndex = 32; this.label8.Text = "Click on a Row button. Select any one or more marbles to take. Click Done."; this.label8.Visible = false; // // btnLetsPlay // this.btnLetsPlay.Location = new System.Drawing.Point(347, 151); this.btnLetsPlay.Name = "btnLetsPlay"; this.btnLetsPlay.Size = new System.Drawing.Size(75, 23); this.btnLetsPlay.TabIndex = 31; this.btnLetsPlay.Text = "Let\'s Play"; this.btnLetsPlay.UseVisualStyleBackColor = true; this.btnLetsPlay.Visible = false; this.btnLetsPlay.Click += new System.EventHandler(this.btnLetsPlay_Click); // // btnOkPlayer2 // this.btnOkPlayer2.BackColor = System.Drawing.Color.LimeGreen; this.btnOkPlayer2.Location = new System.Drawing.Point(347, 121); this.btnOkPlayer2.Name = "btnOkPlayer2"; this.btnOkPlayer2.Size = new System.Drawing.Size(47, 23); this.btnOkPlayer2.TabIndex = 30; this.btnOkPlayer2.Text = "OK"; this.btnOkPlayer2.UseVisualStyleBackColor = false; this.btnOkPlayer2.Visible = false; this.btnOkPlayer2.Click += new System.EventHandler(this.btnOkPlayer2_Click); // // btnOkPlayer1 // this.btnOkPlayer1.BackColor = System.Drawing.Color.LimeGreen; this.btnOkPlayer1.Location = new System.Drawing.Point(347, 92); this.btnOkPlayer1.Name = "btnOkPlayer1"; this.btnOkPlayer1.Size = new System.Drawing.Size(47, 23); this.btnOkPlayer1.TabIndex = 29; this.btnOkPlayer1.Text = "OK"; this.btnOkPlayer1.UseVisualStyleBackColor = false; this.btnOkPlayer1.Click += new System.EventHandler(this.btnOkPlayer1_Click); // // txtTurn // this.txtTurn.Location = new System.Drawing.Point(146, 177); this.txtTurn.Name = "txtTurn"; this.txtTurn.ReadOnly = true; this.txtTurn.Size = new System.Drawing.Size(100, 20); this.txtTurn.TabIndex = 28; this.txtTurn.Visible = false; // // txtPlayer2 // this.txtPlayer2.Location = new System.Drawing.Point(196, 121); this.txtPlayer2.Name = "txtPlayer2"; this.txtPlayer2.Size = new System.Drawing.Size(100, 20); this.txtPlayer2.TabIndex = 27; this.txtPlayer2.Visible = false; // // txtPlayer1 // this.txtPlayer1.Location = new System.Drawing.Point(196, 94); this.txtPlayer1.Name = "txtPlayer1"; this.txtPlayer1.Size = new System.Drawing.Size(100, 20); this.txtPlayer1.TabIndex = 26; // // lblTurn // this.lblTurn.AutoSize = true; this.lblTurn.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblTurn.Location = new System.Drawing.Point(16, 180); this.lblTurn.Name = "lblTurn"; this.lblTurn.Size = new System.Drawing.Size(108, 15); this.lblTurn.TabIndex = 25; this.lblTurn.Text = "It\'s Your Turn ..."; this.lblTurn.Visible = false; // // lblReady // this.lblReady.AutoSize = true; this.lblReady.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblReady.Location = new System.Drawing.Point(16, 154); this.lblReady.Name = "lblReady"; this.lblReady.Size = new System.Drawing.Size(299, 17); this.lblReady.TabIndex = 24; this.lblReady.Text = "When ready to play, click this button -->"; this.lblReady.Visible = false; // // lblSecondPlayer // this.lblSecondPlayer.AutoSize = true; this.lblSecondPlayer.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblSecondPlayer.ForeColor = System.Drawing.SystemColors.Highlight; this.lblSecondPlayer.Location = new System.Drawing.Point(16, 126); this.lblSecondPlayer.Name = "lblSecondPlayer"; this.lblSecondPlayer.Size = new System.Drawing.Size(163, 15); this.lblSecondPlayer.TabIndex = 23; this.lblSecondPlayer.Text = "Second Player Name -->"; this.lblSecondPlayer.Visible = false; // // lblFirstPlayer // this.lblFirstPlayer.AutoSize = true; this.lblFirstPlayer.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblFirstPlayer.ForeColor = System.Drawing.SystemColors.Highlight; this.lblFirstPlayer.Location = new System.Drawing.Point(16, 94); this.lblFirstPlayer.Name = "lblFirstPlayer"; this.lblFirstPlayer.Size = new System.Drawing.Size(143, 15); this.lblFirstPlayer.TabIndex = 22; this.lblFirstPlayer.Text = "First Player Name -->"; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(13, 71); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(271, 13); this.label3.TabIndex = 21; this.label3.Text = "The person forced to pick up the last marble is the loser."; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(16, 58); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(406, 13); this.label2.TabIndex = 20; this.label2.Text = "In each turn you must pick at least one marble and you can select from only one r" + "ow."; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(16, 45); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(399, 13); this.label1.TabIndex = 19; this.label1.Text = "Please enter both players names before beginning. Flip a coin to see who goes fir" + "st."; // // lblWelcome // this.lblWelcome.AutoSize = true; this.lblWelcome.Font = new System.Drawing.Font("Times New Roman", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblWelcome.Location = new System.Drawing.Point(12, 9); this.lblWelcome.Name = "lblWelcome"; this.lblWelcome.Size = new System.Drawing.Size(258, 23); this.lblWelcome.TabIndex = 18; this.lblWelcome.Text = "Welcome to the Game of Nim";

}

///////////////////////////////////////////////

class Player { private string hiddenName; private int hiddenWinsCount; private int hiddenMarblesCount; private int hiddenPlayOrder;

public Player(string name, int playOrder) { hiddenName = name; hiddenPlayOrder = playOrder; hiddenWinsCount = 0; hiddenMarblesCount = 0;

} // Read only properties public int MarblesCount { get { return hiddenMarblesCount; } } // end property Icon

public string Name { get { return hiddenName; } } // end property Name

public int Wins { get { return hiddenWinsCount; } } // end property Wins

// Increment number of wins for this player public void won() { hiddenWinsCount = hiddenWinsCount + 1; } // end isWin

// Update marbles count for this player (not used) public void updatePlayerMarblesCount( int thisCount) // IN: count of marbles just taken by this player { hiddenMarblesCount = hiddenMarblesCount + thisCount; } // end updatePlayerMarblesCount }

////////////////////////////////////////

class Board { const int marblesCountMax = 16; // Number of marbles on the Board int currentMarbleCount = marblesCountMax; int rowMax;

// This is the "internal board". First element is ignored. int[] rowMarbleCounts = { 1, 3, 5, 7 }; // Counts decreased until they reach 0

// private char[] internalBoardRep = new char[SIZE]; // The board representation itself

// Parameterized Constructor - resets the game and sets number of rows public Board(int rowCount) { rowMax = rowCount; // resetGame(); } // end New

// Updates key simulation information each time a player takes a turn // Returns true if marble counts for a row becomes empty public bool recordTurn (int rowID, // IN: represents ID of the row selected by the player int currentSelectionCount) // IN: represents number of marbles just selected in that row { rowMarbleCounts[rowID] = rowMarbleCounts[rowID] - currentSelectionCount; currentMarbleCount = currentMarbleCount - currentSelectionCount; if (rowMarbleCounts[rowID] == 0) return true; else return false; } // end recordTurn

// Get and return count of marbles left in row i (0 <= i <= 3) public int getRowCount(int i) { return rowMarbleCounts[i];

} // end getRowCount

public Boolean isWinner() { Boolean result = false; if (currentMarbleCount == 1) { result = true; } return result; } // end hasPlayerWon }

///////////////////////////////////////////

public partial class frmGameOfNim : Form { private List playersList = new List(2); private Player currentPlayer;

private const int maxRows = 4; private const int maxCols = 7; private Board internalBoardRep = new Board(maxRows);

int marbelsSelectedCount = 0; //Total number of marbles selected in current turn // int gameCount = 0; //Counts number of games player int rowID; //Row number of row just selected by current player

private string playerAName; private string playerBName; private Button[,] newButton = new Button[maxRows, maxCols]; private Button[] newRowButton = new Button[maxRows]; private int marblesSelectedCount; public frmGameOfNim() { InitializeComponent(); }

private void btnOkPlayer1_Click(object sender, EventArgs e) { if (txtPlayer1.Text.Trim() == string.Empty) { MessageBox.Show("Please enter a valid name!"); txtPlayer1.Focus(); return; } else { txtPlayer1.Enabled = false; btnOkPlayer1.Enabled = false; lblSecondPlayer.Visible = true; txtPlayer2.Visible = true; btnOkPlayer2.Visible = true; } }

private void btnOkPlayer2_Click(object sender, EventArgs e) { if (txtPlayer2.Text.Trim() == string.Empty) { MessageBox.Show("Please enter a valid name!"); txtPlayer2.Focus(); return; } else if (txtPlayer2.Text == txtPlayer1.Text) { MessageBox.Show("Sorry name has already been used enter a different name!"); txtPlayer2.Focus(); return; } else { //txtPlayer2.Text = playerBName; txtPlayer2.Enabled = false; btnOkPlayer2.Enabled = false; lblReady.Visible = true; btnLetsPlay.Visible = true; } }

private void btnLetsPlay_Click(object sender, EventArgs e) { lblTurn.Visible = true; txtTurn.Visible = true; pnlBoard.Visible = true; btnDone.Visible = true;

createBoard(); //Calls the baord class to draw the baord }

private void btnQuit_Click(object sender, EventArgs e) { this.Close(); }

private void createBoard() { // Dynamically Creates a 4x7 grid and associated row buttons pnlBoard.Visible = true;

Size size = new Size(40, 45); Point loc = new Point(0, 0); int padding = 10; int topMargin = 20;

// The board is treated like a maxRows x MaxCols array for (int row = 0; row < maxRows; row++) { // For each new row, insert at left the Row i Button loc.Y = (topMargin + 10) + row * (size.Height + padding); int rowBtnExtraLeftPadding = 15; newRowButton[row] = new System.Windows.Forms.Button(); newRowButton[row].Location = new Point(rowBtnExtraLeftPadding, loc.Y); newRowButton[row].Size = new Size(75, 23); newRowButton[row].Text = "Row " + (row + 1) + " -->"; newRowButton[row].Enabled = true; newRowButton[row].Visible = true; newRowButton[row].Name = "rowBtn" + row; newRowButton[row].Click += new EventHandler(RowButton_Click);

// Add row button to the form pnlBoard.Controls.Add(newRowButton[row]);

loc.Y = topMargin + row * (size.Height + padding); int extraLeftPadding = 100; for (int col = 0; col < maxCols; col++) { newButton[row, col] = new System.Windows.Forms.Button(); newButton[row, col].Location = new Point(extraLeftPadding + col * (size.Width + padding), loc.Y); newButton[row, col].Size = size; newButton[row, col].Text = ""; newButton[row, col].Enabled = false; newButton[row, col].Visible = true; newButton[row, col].Name = "btn" + row + col;

// Associates the same event handler with each of the buttons generated newButton[row, col].Click += new EventHandler(Button_Click);

// Add button to the form pnlBoard.Controls.Add(newButton[row, col]); } // end for col } // end for row

lblTurn.Visible = true; txtTurn.Visible = true; // Set up the board with the 16 marbles newButton[0, maxCols / 2].Text = "X"; for (int i = maxCols / 2 - 1; i <= maxCols / 2 + 1; i++) newButton[1, i].Text = "X"; for (int i = maxCols / 2 - 2; i <= maxCols / 2 + 2; i++) newButton[2, i].Text = "X"; for (int i = maxCols / 2 - 3; i <= maxCols / 2 + 3; i++) newButton[3, i].Text = "X";

} // end createBoard

private void RowButton_Click(object sender, EventArgs e) { rowID = convertCharToInt(((Button)sender).Name[6]); // MessageBox.Show("Row Button [" + (rowID+1) + "] has been selected!"); ((Button)sender).BackColor = System.Drawing.Color.Orange; for (int i = 0; i < maxRows; i++) newRowButton[i].Enabled = false; if (rowID == 0) newButton[rowID, maxCols / 2].Enabled = true; if (rowID == 1) for (int i = maxCols / 2 - 1; i <= maxCols / 2 + 1; i++) newButton[rowID, i].Enabled = true; if (rowID == 2) for (int i = maxCols / 2 - 2; i <= maxCols / 2 + 2; i++) newButton[rowID, i].Enabled = true; if (rowID == 3) for (int i = maxCols / 2 - 3; i <= maxCols / 2 + 3; i++) newButton[rowID, i].Enabled = true; btnDone.Enabled = true; } // end button clickhandler

private void Button_Click(object sender, EventArgs e) { marblesSelectedCount++; ((Button)sender).Visible = false; } // end button clickhandler

// Convert a charcter to its equivalent integer int convertCharToInt(Char c) { return ((int)(c) - (int)('0')); } // end convertCharToInt

private void btnDone_Click(object sender, EventArgs e) { newButton[0, maxCols / 2].Enabled = false; for (int i = maxCols / 2 - 1; i <= maxCols / 2 + 1; i++) newButton[1, i].Enabled = false; for (int i = maxCols / 2 - 2; i <= maxCols / 2 + 2; i++) newButton[2, i].Enabled = false; for (int i = maxCols / 2 - 3; i <= maxCols / 2 + 3; i++) newButton[3, i].Enabled = false;

for (int i = 0; i < maxRows; i++) { newRowButton[i].Enabled = true; } btnDone.Enabled = false; } }

I have gotten most of the stuff in the paranthesis to work but some of the other stuff works in bits and piece and cant seem to put it all together could you please help(this is a visual studio program) (This game is a nim game where a user inputs 2 names first they input first name and click OK which validates the name to see if it is and empty string if its empty throws an error if valid it unlocks player 2 name entry and ok button and does the same. Once both names are entered the lets play button is clicked.)

Once that is clicked the program should randomly select from the 2 players and enter the player that is going first once the player is selected they can select a row they would like to pick from and must pick atleast one X value before clicking i have completey my turn. Once that is clicked the program should subtract from the maximum value of 16, also check if the selected row has any left if there are none left the row should be disableled for the next player and system should display the next players turn. the process repeates until the player that selects the last X and presses i have completed my turn and declare him as the LOSER.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory Icdt 97 6th International Conference Delphi Greece January 8 10 1997 Proceedings Lncs 1186

Authors: Foto N. Afrati ,Phokion G. Kolaitis

1st Edition

3540622225, 978-3540622222

More Books

Students also viewed these Databases questions