From 8bef3007887dc635c08fb8123a739f30d3b41fd5 Mon Sep 17 00:00:00 2001 From: nivos1 Date: Thu, 10 Oct 2024 00:18:10 +0300 Subject: [PATCH] Hi there mad a little progress here the game now runs, has some restrictions for the user(as for not able to press staff before clicking on the start button),as for now you can steal the opponent walls, the winer is decided by tic tac toe rules --- SquareGameApp/frmSquare.Designer.cs | 38 +++ SquareGameApp/frmSquare.cs | 443 +++++++++++++++++++++++++++- 2 files changed, 479 insertions(+), 2 deletions(-) diff --git a/SquareGameApp/frmSquare.Designer.cs b/SquareGameApp/frmSquare.Designer.cs index efddb66..be96cc7 100644 --- a/SquareGameApp/frmSquare.Designer.cs +++ b/SquareGameApp/frmSquare.Designer.cs @@ -220,6 +220,7 @@ private void InitializeComponent() tblGrid.RowStyles.Add(new RowStyle(SizeType.Percent, 20.0000019F)); tblGrid.Size = new Size(701, 704); tblGrid.TabIndex = 0; + tblGrid.Paint += tblGrid_Paint; // // lblDot1 // @@ -241,6 +242,8 @@ private void InitializeComponent() lblLine1.Name = "lblLine1"; lblLine1.Size = new Size(116, 8); lblLine1.TabIndex = 1; + lblLine1.Tag = "row"; + lblLine1.Click += row1_Click; // // lblDot2 // @@ -262,6 +265,7 @@ private void InitializeComponent() lblLine2.Name = "lblLine2"; lblLine2.Size = new Size(116, 8); lblLine2.TabIndex = 3; + lblLine2.Tag = "row"; // // lblDot3 // @@ -294,6 +298,7 @@ private void InitializeComponent() lblLine3.Name = "lblLine3"; lblLine3.Size = new Size(116, 8); lblLine3.TabIndex = 5; + lblLine3.Tag = "row"; // // lblLine4 // @@ -303,6 +308,7 @@ private void InitializeComponent() lblLine4.Name = "lblLine4"; lblLine4.Size = new Size(8, 116); lblLine4.TabIndex = 7; + lblLine4.Tag = "column"; // // lblLine5 // @@ -312,6 +318,7 @@ private void InitializeComponent() lblLine5.Name = "lblLine5"; lblLine5.Size = new Size(8, 116); lblLine5.TabIndex = 8; + lblLine5.Tag = "column"; // // lblLine6 // @@ -321,6 +328,7 @@ private void InitializeComponent() lblLine6.Name = "lblLine6"; lblLine6.Size = new Size(8, 116); lblLine6.TabIndex = 9; + lblLine6.Tag = "column"; // // lblLine7 // @@ -330,6 +338,7 @@ private void InitializeComponent() lblLine7.Name = "lblLine7"; lblLine7.Size = new Size(8, 116); lblLine7.TabIndex = 10; + lblLine7.Tag = "column"; // // lblDot5 // @@ -351,6 +360,7 @@ private void InitializeComponent() lblLine8.Name = "lblLine8"; lblLine8.Size = new Size(116, 8); lblLine8.TabIndex = 12; + lblLine8.Tag = "row"; // // lblDot6 // @@ -372,6 +382,7 @@ private void InitializeComponent() lblLine9.Name = "lblLine9"; lblLine9.Size = new Size(116, 8); lblLine9.TabIndex = 14; + lblLine9.Tag = "row"; // // lblDot7 // @@ -403,6 +414,7 @@ private void InitializeComponent() lblLine11.Name = "lblLine11"; lblLine11.Size = new Size(8, 116); lblLine11.TabIndex = 18; + lblLine11.Tag = "column"; // // lblLine12 // @@ -412,6 +424,7 @@ private void InitializeComponent() lblLine12.Name = "lblLine12"; lblLine12.Size = new Size(8, 116); lblLine12.TabIndex = 19; + lblLine12.Tag = "column"; // // lblLine13 // @@ -421,6 +434,7 @@ private void InitializeComponent() lblLine13.Name = "lblLine13"; lblLine13.Size = new Size(8, 116); lblLine13.TabIndex = 19; + lblLine13.Tag = "column"; // // lblLine14 // @@ -430,6 +444,7 @@ private void InitializeComponent() lblLine14.Name = "lblLine14"; lblLine14.Size = new Size(8, 116); lblLine14.TabIndex = 20; + lblLine14.Tag = "column"; // // lblDot9 // @@ -451,6 +466,8 @@ private void InitializeComponent() lblLine15.Name = "lblLine15"; lblLine15.Size = new Size(116, 8); lblLine15.TabIndex = 22; + lblLine15.Tag = "row"; + lblLine15.Click += row7_Click; // // lblDot10 // @@ -472,6 +489,8 @@ private void InitializeComponent() lblLine16.Name = "lblLine16"; lblLine16.Size = new Size(116, 8); lblLine16.TabIndex = 24; + lblLine16.Tag = "row"; + lblLine16.Click += row8_Click; // // lblDot11 // @@ -493,6 +512,7 @@ private void InitializeComponent() lblLine17.Name = "lblLine17"; lblLine17.Size = new Size(116, 8); lblLine17.TabIndex = 26; + lblLine17.Tag = "row"; // // lblDot12 // @@ -513,6 +533,7 @@ private void InitializeComponent() lblLine18.Name = "lblLine18"; lblLine18.Size = new Size(8, 116); lblLine18.TabIndex = 28; + lblLine18.Tag = "column"; // // lblLine19 // @@ -522,6 +543,7 @@ private void InitializeComponent() lblLine19.Name = "lblLine19"; lblLine19.Size = new Size(8, 116); lblLine19.TabIndex = 29; + lblLine19.Tag = "column"; // // lblLine20 // @@ -531,6 +553,7 @@ private void InitializeComponent() lblLine20.Name = "lblLine20"; lblLine20.Size = new Size(8, 116); lblLine20.TabIndex = 30; + lblLine20.Tag = "column"; // // lblLine21 // @@ -540,6 +563,7 @@ private void InitializeComponent() lblLine21.Name = "lblLine21"; lblLine21.Size = new Size(8, 116); lblLine21.TabIndex = 31; + lblLine21.Tag = "column"; // // lblDot13 // @@ -561,6 +585,8 @@ private void InitializeComponent() lblLine22.Name = "lblLine22"; lblLine22.Size = new Size(116, 8); lblLine22.TabIndex = 33; + lblLine22.Tag = "row"; + lblLine22.Click += row10_Click; // // lblDot14 // @@ -582,6 +608,7 @@ private void InitializeComponent() lblLine23.Name = "lblLine23"; lblLine23.Size = new Size(116, 8); lblLine23.TabIndex = 35; + lblLine23.Tag = "row"; // // lblDot15 // @@ -603,6 +630,7 @@ private void InitializeComponent() lblLine24.Name = "lblLine24"; lblLine24.Size = new Size(116, 8); lblLine24.TabIndex = 37; + lblLine24.Tag = "row"; // // lblDot16 // @@ -624,11 +652,13 @@ private void InitializeComponent() lblLine10.Name = "lblLine10"; lblLine10.Size = new Size(116, 8); lblLine10.TabIndex = 16; + lblLine10.Tag = "row"; // // lblBoxFill1 // lblBoxFill1.AutoSize = true; lblBoxFill1.Dock = DockStyle.Fill; + lblBoxFill1.Font = new Font("Ravie", 45F, FontStyle.Regular, GraphicsUnit.Point, 0); lblBoxFill1.Location = new Point(149, 146); lblBoxFill1.Name = "lblBoxFill1"; lblBoxFill1.Size = new Size(110, 116); @@ -639,6 +669,7 @@ private void InitializeComponent() // lblBoxFill2.AutoSize = true; lblBoxFill2.Dock = DockStyle.Fill; + lblBoxFill2.Font = new Font("Ravie", 45F, FontStyle.Regular, GraphicsUnit.Point, 0); lblBoxFill2.Location = new Point(295, 146); lblBoxFill2.Name = "lblBoxFill2"; lblBoxFill2.Size = new Size(110, 116); @@ -649,6 +680,7 @@ private void InitializeComponent() // lblBoxFill3.AutoSize = true; lblBoxFill3.Dock = DockStyle.Fill; + lblBoxFill3.Font = new Font("Ravie", 45F, FontStyle.Regular, GraphicsUnit.Point, 0); lblBoxFill3.Location = new Point(441, 146); lblBoxFill3.Name = "lblBoxFill3"; lblBoxFill3.Size = new Size(110, 116); @@ -659,6 +691,7 @@ private void InitializeComponent() // lblBoxFill4.AutoSize = true; lblBoxFill4.Dock = DockStyle.Fill; + lblBoxFill4.Font = new Font("Ravie", 45F, FontStyle.Regular, GraphicsUnit.Point, 0); lblBoxFill4.Location = new Point(149, 292); lblBoxFill4.Name = "lblBoxFill4"; lblBoxFill4.Size = new Size(110, 116); @@ -669,6 +702,7 @@ private void InitializeComponent() // lblBoxFill5.AutoSize = true; lblBoxFill5.Dock = DockStyle.Fill; + lblBoxFill5.Font = new Font("Ravie", 45F, FontStyle.Regular, GraphicsUnit.Point, 0); lblBoxFill5.Location = new Point(295, 292); lblBoxFill5.Name = "lblBoxFill5"; lblBoxFill5.Size = new Size(110, 116); @@ -679,6 +713,7 @@ private void InitializeComponent() // lblBoxFill6.AutoSize = true; lblBoxFill6.Dock = DockStyle.Fill; + lblBoxFill6.Font = new Font("Ravie", 45F, FontStyle.Regular, GraphicsUnit.Point, 0); lblBoxFill6.Location = new Point(441, 292); lblBoxFill6.Name = "lblBoxFill6"; lblBoxFill6.Size = new Size(110, 116); @@ -689,6 +724,7 @@ private void InitializeComponent() // lblBoxFill7.AutoSize = true; lblBoxFill7.Dock = DockStyle.Fill; + lblBoxFill7.Font = new Font("Ravie", 45F, FontStyle.Regular, GraphicsUnit.Point, 0); lblBoxFill7.Location = new Point(149, 438); lblBoxFill7.Name = "lblBoxFill7"; lblBoxFill7.Size = new Size(110, 116); @@ -699,6 +735,7 @@ private void InitializeComponent() // lblBoxFill8.AutoSize = true; lblBoxFill8.Dock = DockStyle.Fill; + lblBoxFill8.Font = new Font("Ravie", 45F, FontStyle.Regular, GraphicsUnit.Point, 0); lblBoxFill8.Location = new Point(295, 438); lblBoxFill8.Name = "lblBoxFill8"; lblBoxFill8.Size = new Size(110, 116); @@ -709,6 +746,7 @@ private void InitializeComponent() // lblBoxFill9.AutoSize = true; lblBoxFill9.Dock = DockStyle.Fill; + lblBoxFill9.Font = new Font("Ravie", 45F, FontStyle.Regular, GraphicsUnit.Point, 0); lblBoxFill9.Location = new Point(441, 438); lblBoxFill9.Name = "lblBoxFill9"; lblBoxFill9.Size = new Size(110, 116); diff --git a/SquareGameApp/frmSquare.cs b/SquareGameApp/frmSquare.cs index 7f49761..b541ae3 100644 --- a/SquareGameApp/frmSquare.cs +++ b/SquareGameApp/frmSquare.cs @@ -1,7 +1,11 @@ +using System.Xml.Linq; + namespace SquareGameApp { public partial class frmSquare : Form { + private int counter = 0; + private bool gameStarted = false; //game started or not enum TurnEnum { X, O }; TurnEnum currentturn = TurnEnum.X; @@ -13,12 +17,14 @@ enum GameStatusEnum { NotStarted, Playing, Winner } public frmSquare() { + InitializeComponent(); - lstlines = new() { lblLine1, lblLine2, lblLine3, lblLine4, lblLine5, lblLine6, lblLine7, lblLine8, lblLine9, lblLine10, lblLine11, lblLine12, lblLine13, lblLine14, lblLine15, lblLine16, lblLine17, lblLine18, lblLine19, lblLine20, lblLine21, lblLine22, lblLine23, lblLine24}; + lstlines = new() { lblLine1, lblLine2, lblLine3, lblLine4, lblLine5, lblLine6, lblLine7, lblLine8, lblLine9, lblLine10, lblLine11, lblLine12, lblLine13, lblLine14, lblLine15, lblLine16, lblLine17, lblLine18, lblLine19, lblLine20, lblLine21, lblLine22, lblLine23, lblLine24 }; lstlines.ForEach(lbl => lbl.Click += LineClick_Click); btnStart.Click += BtnStart_Click; + lstcompleteboxes = new() { new(){lblLine1, lblLine4, lblLine5, lblLine8 }, @@ -31,16 +37,449 @@ public frmSquare() new(){lblLine16, lblLine19, lblLine20, lblLine23 }, new(){lblLine17, lblLine20, lblLine21, lblLine24 } }; + lblStatus.Text = "Game not started yet"; // Update the status label if you have one } private void BtnStart_Click(object? sender, EventArgs e) { - + counter++; + ResetBoard(); + gameStarted = true; //setting the game on + + UpdateTurnDisplay(); + } + private void ResetBoard() + { + string newLabelName; + Label newLabel; + string newBoxlName; + Label newBox; + for (int i = 1; i <= 24; i++)//reseting the lines + { + newLabelName = $"lblLine{i}"; + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + newLabel.BackColor = System.Drawing.Color.Gainsboro; + } + for (int i = 1; i <= 9; i++)// reseting the boxes + { + newBoxlName = $"lblBoxFill{i}"; + newBox = this.Controls.Find(newBoxlName, true).FirstOrDefault() as Label; + newBox.BackColor = System.Drawing.Color.White; + newBox.Text = ""; + } + // Optionally, reset the game state + currentturn = TurnEnum.X; // Reset to the starting player + gameStarted = false; // Reset the game status } + private void tblGrid_Paint(object? sender, EventArgs e) + { + } private void LineClick_Click(object? sender, EventArgs e) + { + bool row = false; + if (gameStarted == false) // if the game is not on you cant play + { + if(counter==0) + MessageBox.Show("Please start the game first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); + else + MessageBox.Show("Please start a new game", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + else + { + Label clickedLine = sender as Label; + if (currentturn == TurnEnum.X) + { + clickedLine.BackColor = System.Drawing.Color.Blue; // Set color of the line to Blue for X + } + else // currentTurn is O + { + clickedLine.BackColor = System.Drawing.Color.Red; // Set color of the line to Red for O + } + if (clickedLine.Tag.ToString() == "row")//did the user just clicked a line in a row or a coll + row = true; + else + row = false; + CheckSquare(row, int.Parse(clickedLine.Name.Replace("lblLine", "")), clickedLine);// doing a check if the square is finished, and giving us the number + if (checkWin()) + { + gameStarted = false; + } + else + { + ToggleTurn(); + } + } + } + private void ToggleTurn() + { + // Toggle the turn between X and O + currentturn = currentturn == TurnEnum.X ? TurnEnum.O : TurnEnum.X; + UpdateTurnDisplay(); // Update the TextBox with the new turn + } + private void UpdateTurnDisplay() + { + lblStatus.Text = $"{currentturn} Turn";// Set the TextBox to "X" or "O" + // Change the text color based on the current turn + if (currentturn == TurnEnum.X) + { + lblStatus.ForeColor = System.Drawing.Color.Blue; // Set color to Blue for X + } + else // currentTurn is O + { + lblStatus.ForeColor = System.Drawing.Color.Red; // Set color to Red for O + } + } + private void CheckSquare(bool RowOrNot, int lineNumber, Label clickedLine) + { + if (RowOrNot) + { + checkup(lineNumber, clickedLine); + checkdown(lineNumber, clickedLine); + } + else + { + checkRight(lineNumber, clickedLine); + checkLeft(lineNumber, clickedLine); + } + } + private int RedOrNot(Label newlabel) //checking which counter to add + { + if (newlabel == null)// for null and estetics + return 2; + else if (newlabel.BackColor == Color.Red) + return 1; + else if (newlabel.BackColor == Color.Blue) + return 0; + else return 3; + } + private void checkup(int lineNumber, Label clickedLine) //chechking if the box above is filled + { + string newLabelName; + Label newLabel; + int redCounter = 0, blueCounter = 0; + newLabelName = $"lblLine{lineNumber - 3}"; //checking the left wall + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + newLabelName = $"lblLine{lineNumber - 4}"; //checking the right wall + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + newLabelName = $"lblLine{lineNumber -7}"; //checking the upper wall + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (newLabel != null) + { + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + } + newLabelName = $"lblLine{lineNumber}"; //checking the clicked line + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + if (redCounter + blueCounter == 4)//all the box is filled + { + string newBoxName=""; + Label newBox = null; + Label newBoxLabel = null; + if (lineNumber <= 10 && lineNumber >= 8) + { + newBoxName = $"lblBoxFill{lineNumber - 7}"; // Adjusted for box naming convention + newBox = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + } + else if (lineNumber <= 17 && lineNumber >= 15) + { + newBoxName = $"lblBoxFill{lineNumber - 11}"; // Adjusted for box naming convention + newBox = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + } + else if (lineNumber <= 24 && lineNumber >= 22) + { + newBoxName = $"lblBoxFill{lineNumber - 15}"; // Adjusted for box naming convention + newBox = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + } + newBoxLabel = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + if (redCounter == blueCounter) + newBoxLabel.Text = ""; + else if (redCounter > blueCounter) + { + newBoxLabel.Text = "O Turn"; + newBoxLabel.ForeColor = Color.Red; // Set text color to red for O + } + else + { + newBoxLabel.Text = "X Turn"; + newBoxLabel.ForeColor = Color.Blue; // Set text color to red for O + } + } + } + private void checkdown(int lineNumber, Label clickedLine) //chechking if the box under is filled + { + string newLabelName; + Label newLabel; + int redCounter = 0, blueCounter = 0; + newLabelName = $"lblLine{lineNumber + 3}"; //checking the left wall + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + newLabelName = $"lblLine{lineNumber + 4}"; //checking the right wall + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + newLabelName = $"lblLine{lineNumber + 7}"; //checking the upper wall + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (newLabel != null) + { + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + } + newLabelName = $"lblLine{lineNumber}"; //checking the clicked line + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + if (redCounter + blueCounter == 4)//all the box is filled + { + string newBoxName=""; + Label newBox = null; + Label newBoxLabel = null; + if (lineNumber <= 3 && lineNumber >= 1) + { + newBoxName = $"lblBoxFill{lineNumber}"; // Adjusted for box naming convention + newBox = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + } + else if (lineNumber <= 10 && lineNumber >= 8) + { + newBoxName = $"lblBoxFill{lineNumber - 4}"; // Adjusted for box naming convention + newBox = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + } + else if (lineNumber <= 17 && lineNumber >= 15) + { + newBoxName = $"lblBoxFill{lineNumber - 8}"; // Adjusted for box naming convention + newBox = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + } + newBoxLabel = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + if (redCounter == blueCounter) + newBoxLabel.Text = ""; + else if (redCounter > blueCounter) + { + newBoxLabel.Text = "O Turn"; + newBoxLabel.ForeColor = Color.Red; // Set text color to red for O + } + else + { + newBoxLabel.Text = "X Turn"; + newBoxLabel.ForeColor = Color.Blue; // Set text color to red for O + } + } + } + private void checkLeft(int lineNumber, Label clickedLine) //chechking if the box to the left is filled + { + string newLabelName; + Label newLabel; + int redCounter = 0, blueCounter = 0; + newLabelName = $"lblLine{lineNumber -4 }"; //checking the upper wall + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + newLabelName = $"lblLine{lineNumber + 3}"; //checking the bellow wall + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + newLabelName = $"lblLine{lineNumber - 1}"; //checking the upper wall + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (lineNumber!=4&&lineNumber!=11&&lineNumber!=18)//not going back to rows + { + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + } + newLabelName = $"lblLine{lineNumber}"; //checking the clicked line + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + if (redCounter + blueCounter == 4)//all the box is filled + { + string newBoxName=""; + Label newBox = null; + Label newBoxLabel = null; + if (lineNumber <= 7 && lineNumber >= 5) + { + newBoxName = $"lblBoxFill{lineNumber - 4}"; // Adjusted for box naming convention + newBox = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + } + else if (lineNumber <= 14 && lineNumber >= 12) + { + newBoxName = $"lblBoxFill{lineNumber - 8}"; // Adjusted for box naming convention + newBox = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + } + else if (lineNumber <= 21 && lineNumber >= 19) + { + newBoxName = $"lblBoxFill{lineNumber - 12}"; // Adjusted for box naming convention + newBox = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + } + newBoxLabel = this.Controls.Find(newBoxName,true).FirstOrDefault()as Label; + if (redCounter == blueCounter) + newBoxLabel.Text = ""; + else if (redCounter > blueCounter) + { + newBoxLabel.Text = "O Turn"; + newBoxLabel.ForeColor = Color.Red; // Set text color to red for O + } + else + { + newBoxLabel.Text = "X Turn"; + newBoxLabel.ForeColor = Color.Blue; // Set text color to red for O + } + } + } + private void checkRight(int lineNumber, Label clickedLine) //chechking if the box to the right is filled + { + string newLabelName; + Label newLabel; + int redCounter = 0, blueCounter = 0; + newLabelName = $"lblLine{lineNumber - 3}"; //checking the upper wall + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + newLabelName = $"lblLine{lineNumber + 4}"; //checking the bellow wall + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + newLabelName = $"lblLine{lineNumber + 1}"; //checking the upper wall + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (lineNumber != 7 && lineNumber != 14 && lineNumber != 21)//not going back to rows + { + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + } + newLabelName = $"lblLine{lineNumber}"; //checking the clicked line + newLabel = this.Controls.Find(newLabelName, true).FirstOrDefault() as Label; + if (RedOrNot(newLabel) == 1) + redCounter++; + else if (RedOrNot(newLabel) == 0) + blueCounter++; + if (redCounter + blueCounter == 4)//all the box is filled + { + string newBoxName=""; + Label newBox = null; + Label newBoxLabel = null; + if (lineNumber <= 6 && lineNumber >= 4) + { + newBoxName = $"lblBoxFill{lineNumber - 3}"; // Adjusted for box naming convention + newBox = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + } + else if (lineNumber <= 13 && lineNumber >= 11) + { + newBoxName = $"lblBoxFill{lineNumber - 7}"; // Adjusted for box naming convention + newBox = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + } + else if (lineNumber <= 20 && lineNumber >= 18) + { + newBoxName = $"lblBoxFill{lineNumber - 11}"; // Adjusted for box naming convention + newBox = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + } + newBoxLabel = this.Controls.Find(newBoxName, true).FirstOrDefault() as Label; + if (redCounter == blueCounter) + newBoxLabel.Text = ""; + else if (redCounter > blueCounter) + { + newBoxLabel.Text = "O Turn"; + newBoxLabel.ForeColor = Color.Red; // Set text color to red for O + } + else + { + newBoxLabel.Text = "X Turn"; + newBoxLabel.ForeColor = Color.Blue; // Set text color to red for O + } + } + } + private bool checkWin() { + int[][] winningCombinations = new int[][] + { + new int[] { 1, 2, 3 }, // Row 1 + new int[] { 4, 5, 6 }, // Row 2 + new int[] { 7, 8, 9 }, // Row 3 + new int[] { 1, 4, 7 }, // Column 1 + new int[] { 2, 5, 8 }, // Column 2 + new int[] { 3, 6, 9 }, // Column 3 + new int[] { 1, 5, 9 }, // Diagonal \ + new int[] { 3, 5, 7 } // Diagonal / + }; + + // Check each winning combination + foreach (var combination in winningCombinations) + { + // Get the values in the boxes for the current combination + string value1 = GetBoxValue(combination[0]); + string value2 = GetBoxValue(combination[1]); + string value3 = GetBoxValue(combination[2]); + + // Check if all three values are the same and not empty + if (value1 == value2 && value2 == value3 && !string.IsNullOrEmpty(value1)) + { + if (value1=="X Turn") + MessageBox.Show($"X wins!", "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Information); + else + MessageBox.Show($"O wins!", "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Information); + return true; // There is a winner + } + } + return false; + } + private string GetBoxValue(int boxNumber) + { + string boxName = $"lblBoxFill{boxNumber}"; + Label boxLabel = this.Controls.Find(boxName, true).FirstOrDefault() as Label; + + return boxLabel?.Text; // Return the text or null if not found + } + private void row1_Click(object sender, EventArgs e) + { + + } + + private void row7_Click(object sender, EventArgs e) + { + + } + + private void row8_Click(object sender, EventArgs e) + { + + } + + private void row10_Click(object sender, EventArgs e) { } } } +