public bool HasWon(int i, int j, GameForm.Player player) { // check horizontal if (table[i, 0] == player && table[i, 1] == player && table[i, 2] == player) { return(true); } // check vertical if (table[0, j] == player && table[1, j] == player && table[2, j] == player) { return(true); } // check main diaognal if (table[0, 0] == player && table[1, 1] == player && table[2, 2] == player) { return(true); } // check secondary diagonal if (table[0, 2] == player && table[1, 1] == player && table[2, 0] == player) { return(true); } return(false); }
public void CheckWin(int row, int column, GameForm.Player player) { if (HasWon(row, column, player)) { if (player == Player.X) { MessageBox.Show("The RED player won!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { MessageBox.Show("The BLUE player won!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { tables[i, j].GetTableImage().Controls.Clear(); } } GameContainer.Visible = false; MenuContainer.Visible = true; } else { bool allWon = true; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (!tables[i, j].IsWon() && tables[i, j].GetCapacity() > 0) { allWon = false; break; } } } if (allWon) { MessageBox.Show("No one won - DRAFT!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { tables[i, j].GetTableImage().Controls.Clear(); } } GameContainer.Visible = false; MenuContainer.Visible = true; } } }
private void OnClick(object sender, EventArgs e) { if (!Focused) { return; } if (IsWon()) { return; } MouseEventArgs mouseEventArgs = (MouseEventArgs)e; Point mouseCoordinates = mouseEventArgs.Location; int row = GetRow(mouseCoordinates); int column = GetColumn(mouseCoordinates); if (table[row, column] == GameForm.Player.NONE) { table[row, column] = GameForm.GetCurrentPlayerTurn(); PictureBox icon = new PictureBox(); icon.Size = new Size(TableImage.Size.Width / 3 - 8, TableImage.Size.Width / 3 - 8); icon.Location = new Point(column * (TableImage.Size.Width / 3) + 4, row * (TableImage.Size.Height / 3) + 4); icon.SizeMode = PictureBoxSizeMode.StretchImage; if (GameForm.GetCurrentPlayerTurn() == GameForm.Player.X) { icon.Image = UltimateTicTacToe.Properties.Resources.x; } else { icon.Image = UltimateTicTacToe.Properties.Resources.o; } this.TableImage.Controls.Add(icon); if (HasWon(row, column, GameForm.GetCurrentPlayerTurn())) { this.WonBy = GameForm.GetCurrentPlayerTurn(); PictureBox bigIcon = new PictureBox(); bigIcon.Size = new Size((int)(TableImage.Size.Width / 1.2f), (int)(TableImage.Size.Width / 1.2f)); int position = (int)(TableImage.Size.Width / 2 - bigIcon.Size.Width / 2); bigIcon.Location = new Point(position, position); bigIcon.SizeMode = PictureBoxSizeMode.StretchImage; if (GameForm.GetCurrentPlayerTurn() == GameForm.Player.X) { bigIcon.Image = UltimateTicTacToe.Properties.Resources.x; } else { bigIcon.Image = UltimateTicTacToe.Properties.Resources.o; } this.TableImage.Controls.Add(bigIcon); bigIcon.BringToFront(); } capacity--; GameForm.CheckWin(TableRow, TableColumn, GameForm.GetCurrentPlayerTurn()); GameForm.SetFocusedTable(row, column); GameForm.SwitchTurn(); } }