public void playerVSplayer(int x, int y) { Game.cell cell = new Game.cell(); if (Player.PlayerOneTurn == true) { cell = Game.cellSearch(x, y, p1); Player.playerOneCount++; if (Player.playerOneCount >= 4) { Player.winningPlayer(); if (Player.checkWinner == true) { MessageBox.Show(player1col + " is the winner"); dataGridView1.Enabled = false; p1count++; label2.Text = p1count.ToString(); return; } } Player.PlayerTwoTurn = true; Player.PlayerOneTurn = false; label5.ForeColor = p2; label5.Text = player2col + " Turn"; } else if (Player.PlayerTwoTurn == true) { cell = Game.cellSearch(x, y, p2); Player.playerTwoCount++; if (Player.playerTwoCount >= 4) { Player.winningPlayer(); if (Player.checkWinner == true) { MessageBox.Show(player2col + " is the winner"); dataGridView1.Enabled = false; p2count++; label4.Text = p2count.ToString(); return; } } Player.PlayerOneTurn = true; Player.PlayerTwoTurn = false; label5.ForeColor = p1; label5.Text = player1col + " Turn"; } }
public static Game.cell playerToWin() { cCell = new Game.cell(); Game.cell cY = new Game.cell(); for (int i = Game.boardCell.GetLength(0) - 1; i >= 0; i--) { for (int j = 0; j < Game.boardCell.GetLength(1); j++) { if (j <= 3) { if (Game.boardCell[i, j].cellColor == Form2.p2 && Game.boardCell[i, j + 2].cellColor == Form2.p2 && Game.boardCell[i, j + 3].cellColor == Form2.p2) { if (Game.boardCell[i, j + 1].cellColor == Color.White) { if (i < 5 && Game.boardCell[i + 1, j + 1].cellColor == Color.White) { break; } else { cY = Game.boardCell[i, j + 1]; } } } else if (Game.boardCell[i, j].cellColor == Form2.p2 && Game.boardCell[i, j + 1].cellColor == Form2.p2 && Game.boardCell[i, j + 3].cellColor == Form2.p2) { if (Game.boardCell[i, j + 2].cellColor == Color.White) { if (i < 5 && Game.boardCell[i + 1, j + 2].cellColor == Color.White) { break; } else { cY = Game.boardCell[i, j + 2]; } } } else if (Game.boardCell[i, j].cellColor == Form2.p1 && Game.boardCell[i, j + 2].cellColor == Form2.p1 && Game.boardCell[i, j + 3].cellColor == Form2.p1) { if (Game.boardCell[i, j + 1].cellColor == Color.White) { if (i < 5 && Game.boardCell[i + 1, j + 1].cellColor == Color.White) { break; } else { cCell = Game.boardCell[i, j + 1]; } } } else if (Game.boardCell[i, j].cellColor == Form2.p1 && Game.boardCell[i, j + 1].cellColor == Form2.p1 && Game.boardCell[i, j + 3].cellColor == Form2.p1) { if (Game.boardCell[i, j + 2].cellColor == Color.White) { if (i < 5 && Game.boardCell[i + 1, j + 2].cellColor == Color.White) { break; } else { cCell = Game.boardCell[i, j + 2]; } } } if (Game.boardCell[i, j].cellColor == Form2.p2 && Game.boardCell[i, j + 1].cellColor == Form2.p2 && Game.boardCell[i, j + 2].cellColor == Form2.p2) { if (Game.boardCell[i, j + 3].cellColor == Color.White) { if (i < 5 && Game.boardCell[i + 1, j + 3].cellColor == Color.White) { break; } else { cY = Game.boardCell[i, j + 3]; } } } else if (Game.boardCell[i, j].cellColor == Form2.p1 && Game.boardCell[i, j + 1].cellColor == Form2.p1 && Game.boardCell[i, j + 2].cellColor == Form2.p1) { if (Game.boardCell[i, j + 3].cellColor == Color.White) { if (i < 5 && Game.boardCell[i + 1, j + 3].cellColor == Color.White) { break; } else { cCell = Game.boardCell[i, j + 3]; } } } if (Game.boardCell[i, j + 1].cellColor == Form2.p2 && Game.boardCell[i, j + 2].cellColor == Form2.p2 && Game.boardCell[i, j + 3].cellColor == Form2.p2) { if (Game.boardCell[i, j].cellColor == Color.White) { if (i < 5 && Game.boardCell[i + 1, j].cellColor == Color.White) { break; } else { cY = Game.boardCell[i, j]; } } } else if (Game.boardCell[i, j + 1].cellColor == Form2.p1 && Game.boardCell[i, j + 2].cellColor == Form2.p1 && Game.boardCell[i, j + 3].cellColor == Form2.p1) { if (Game.boardCell[i, j].cellColor == Color.White) { if (i < 5 && Game.boardCell[i + 1, j].cellColor == Color.White) { break; } else { cY = Game.boardCell[i, j]; } } } } if (i >= 3) { if (Game.boardCell[i, j].cellColor == Form2.p2 && Game.boardCell[i - 1, j].cellColor == Form2.p2 && Game.boardCell[i - 2, j].cellColor == Form2.p2) { if (Game.boardCell[i - 3, j].cellColor == Color.White && i > 0) { cY = Game.boardCell[i - 3, j]; } } else if (Game.boardCell[i, j].cellColor == Form2.p1 && Game.boardCell[i - 1, j].cellColor == Form2.p1 && Game.boardCell[i - 2, j].cellColor == Form2.p1) { if (Game.boardCell[i - 3, j].cellColor == Color.White) { cCell = Game.boardCell[i - 3, j]; } } } if (j <= 3 && i >= 3) { if (Game.boardCell[i, j].cellColor == Form2.p2 && Game.boardCell[i - 1, j + 1].cellColor == Form2.p2 && Game.boardCell[i - 2, j + 2].cellColor == Form2.p2) { if (Game.boardCell[i - 3, j + 3].cellColor == Color.White && i > 0) { if (Game.boardCell[i - 2, j + 3].cellColor == Color.White) { break; } else { cY = Game.boardCell[i - 3, j + 3]; } } } else if (Game.boardCell[i, j].cellColor == Form2.p1 && Game.boardCell[i - 1, j + 1].cellColor == Form2.p1 && Game.boardCell[i - 2, j + 2].cellColor == Form2.p1) { if (Game.boardCell[i - 3, j + 3].cellColor == Color.White) { if (Game.boardCell[i - 2, j + 3].cellColor == Color.White) { break; } else { cCell = Game.boardCell[i - 3, j + 3]; } } } } if (j >= 3 && i >= 3) { if (Game.boardCell[i, j].cellColor == Form2.p2 && Game.boardCell[i - 1, j - 1].cellColor == Form2.p2 && Game.boardCell[i - 2, j - 2].cellColor == Form2.p2) { if (Game.boardCell[i - 3, j - 3].cellColor == Color.White) { if (Game.boardCell[i - 2, j - 3].cellColor == Color.White) { break; } else { cY = Game.boardCell[i - 3, j - 3]; } } } else if (Game.boardCell[i, j].cellColor == Form2.p1 && Game.boardCell[i - 1, j - 1].cellColor == Form2.p1 && Game.boardCell[i - 2, j - 2].cellColor == Form2.p1) { if (Game.boardCell[i - 3, j - 3].cellColor == Color.White) { if (Game.boardCell[i - 2, j - 3].cellColor == Color.White) { break; } else { cCell = Game.boardCell[i - 3, j - 3]; } } } } if (j <= 3 && i < 3) { if (Game.boardCell[i, j].cellColor == Form2.p2 && Game.boardCell[i + 1, j + 1].cellColor == Form2.p2 && Game.boardCell[i + 2, j + 2].cellColor == Form2.p2) { if (Game.boardCell[i + 3, j + 3].cellColor == Color.White) { if (Game.boardCell[i + 4, j + 3].cellColor == Color.White) { break; } else { cY = Game.boardCell[i + 3, j + 3]; } } } else if (Game.boardCell[i, j].cellColor == Form2.p1 && Game.boardCell[i + 1, j + 1].cellColor == Form2.p1 && Game.boardCell[i + 2, j + 2].cellColor == Form2.p1) { if (Game.boardCell[i + 3, j + 3].cellColor == Color.White) { if (Game.boardCell[i + 4, j + 3].cellColor == Color.White) { break; } else { cY = Game.boardCell[i + 3, j + 3]; } } } } if (j >= 3 && i < 3) { if (Game.boardCell[i, j].cellColor == Form2.p2 && Game.boardCell[i + 1, j - 1].cellColor == Form2.p2 && Game.boardCell[i + 2, j - 2].cellColor == Form2.p2) { if (Game.boardCell[i + 3, j - 3].cellColor == Color.White) { if (i + 4 == 5) { cY = Game.boardCell[i + 3, j - 3]; } else if (Game.boardCell[i + 4, j - 3].cellColor == Color.White) { break; } } } else if (Game.boardCell[i, j].cellColor == Form2.p1 && Game.boardCell[i + 1, j - 1].cellColor == Form2.p1 && Game.boardCell[i + 2, j - 2].cellColor == Form2.p1) { if (Game.boardCell[i + 3, j - 3].cellColor == Color.White) { if (i + 4 == 5) { cY = Game.boardCell[i + 3, j - 3]; } else if (Game.boardCell[i + 4, j - 3].cellColor == Color.White) { break; } } } } } } if (cY.cellPosition.Y != 0 || cY.cellPosition.X != 0) { return(cY); } else { return(cCell); } }
public static void minimax() { Game.cell cell = new Game.cell(); List <int> totalscores = new List <int>(); List <Point> scorespoints = new List <Point>(); int maxindex = 0; Point finalpos = new Point(0, 0); cell = Player.playerToWin(); if (cell.cellPosition.X != 0 || cell.cellPosition.Y != 0) { for (int i = Game.boardCell.GetLength(0) - 1; i >= 0; i--) { for (int j = 0; j < Game.boardCell.GetLength(1); j++) { if (cell.cellPosition == Game.boardCell[i, j].cellPosition) { cell.cellColor = Color.Yellow; Game.boardCell[i, j].cellColor = cell.cellColor; Form2.rowN = i; Form2.colN = j; } } } } else { for (int j = 5; j >= 0; j--) { for (int i = 0; i < 7; i++) { if (Game.boardCell[j, i].cellColor == Color.White) { if (j < 5 && Game.boardCell[j + 1, i].cellColor == Color.White) { break; } else { totalscores.Add(calculateScore((Game.boardCell[j, i]), i, j)); scorespoints.Add(Game.boardCell[j, i].cellPosition); } } } } maxindex = totalscores.IndexOf(totalscores.Max()); Form2.score = totalscores.Max(); finalpos = scorespoints[maxindex]; cell.cellPosition = finalpos; for (int i = Game.boardCell.GetLength(0) - 1; i >= 0; i--) { for (int j = 0; j < Game.boardCell.GetLength(1); j++) { if (cell.cellPosition == Game.boardCell[i, j].cellPosition) { cell.cellColor = Color.Yellow; Game.boardCell[i, j].cellColor = cell.cellColor; Form2.colN = j; Form2.rowN = i; } } } } }
public static int calculateScore(Game.cell c1, int columns, int rows) { int Count = 0; int maxScores = 0; int minX = Math.Max(Game.boardCell.GetLowerBound(1), columns - 3); int maxX = Math.Min(Game.boardCell.GetUpperBound(1), columns + 3); int maxY = Math.Max(Game.boardCell.GetLowerBound(0), rows - 3); int minY = Math.Min(Game.boardCell.GetUpperBound(0), rows + 3); List <int> scores = new List <int>(); //Right side for (int i = columns + 1; i <= maxX; i++) { if (Game.boardCell[rows, i].cellColor == Color.Yellow) { Count++; } else if (Game.boardCell[rows, i].cellColor != Color.White) { Count--; } else { break; } } scores.Add(Count); Count = 0; //Left Side for (int j = columns - 1; j >= minX; j--) { if (Game.boardCell[rows, j].cellColor == Color.Yellow) { Count++; } else if (Game.boardCell[rows, j].cellColor != Color.White) { Count--; } else { break; } } scores.Add(Count); Count = 0; //Down side for (int i = rows + 1; i <= minY; i++) { if (Game.boardCell[i, columns].cellColor == Color.Yellow) { Count++; } else if (Game.boardCell[i, columns].cellColor != Color.White) { Count--; } else { break; } } scores.Add(Count); Count = 0; //right-up diagonal int y = rows - 1; for (int i = columns + 1; i <= maxX; i++) { if (y < 0) { break; } if (Game.boardCell[y, i].cellColor == Color.Yellow) { Count++; } else if (Game.boardCell[y, i].cellColor != Color.White) { Count--; } else { break; } y--; } scores.Add(Count); Count = 0; //Left down digonal y = rows + 1; for (int i = columns - 1; i >= minX; i--) { if (y > 5) { break; } if (Game.boardCell[y, i].cellColor == Color.Yellow) { Count++; } else if (Game.boardCell[y, i].cellColor != Color.White) { Count--; } else { break; } y++; } scores.Add(Count); Count = 0; //Right down digonal y = rows + 1; for (int i = columns + 1; i <= maxX; i++) { if (y > 5) { break; } if (Game.boardCell[y, i].cellColor == Color.Yellow) { Count++; } else if (Game.boardCell[y, i].cellColor != Color.White) { Count--; } else { break; } y++; } scores.Add(Count); Count = 0; //Left up digonal y = rows - 1; for (int i = columns - 1; i >= minX; i--) { if (y < 0) { break; } if (Game.boardCell[y, i].cellColor == Color.Yellow) { Count++; } else if (Game.boardCell[y, i].cellColor != Color.White) { Count--; } else { break; } y--; } scores.Add(Count); Count = 0; maxScores = scores.Max(); return(maxScores); }