/// <summary> /// Performs the actual check for the above method. /// </summary> /// <param name="row"></param> /// <param name="col"></param> /// <param name="rowDirection"></param> /// <param name="colDirection"></param> /// <param name="color"></param> /// <returns></returns> private bool Check(int row, char col, int rowDirection, int colDirection, Color color) { bool both = false, track = true; for (int i = 0; i <= 4; i++) { if (0 < row && row <= ColumnSize && col >= 'A' && col <= 'G') { DoubleLinkedListCell <GamePiece> x = FindCell(col.ToString() + row); if (x == null) { if (both) { track = false; break; } else { rowDirection = rowDirection * -1; colDirection = colDirection * -1; both = true; i = 0; } } else if (x.Data.PieceColor != color) { if (both) { track = false; break; } else { rowDirection = rowDirection * -1; colDirection = colDirection * -1; both = true; i = 0; } } } else { if (both) { track = false; break; } else { rowDirection = rowDirection * -1; colDirection = colDirection * -1; both = true; i = 0; } } row += rowDirection; col = (char)(col + colDirection); } return(track); }
} //END OF Column /// <summary> /// Finds the column the user is working in /// </summary> /// <param name="columId"></param> public void FindColumn(string columId) { while (columId != _columns.Id) { if (_columns.Next != null) { _columns = _columns.Next; } //END OF IF STATEMENT else { break; } } //END OF WHILE LOOP while (columId != _columns.Id) { if (_columns.Prev != null) { _columns = _columns.Prev; } //END OF IF STATEMENT else { break; } } //END OF WHILE LOOP } //END OF FindColum
/// <summary> /// Checks for four continous pieces in any direction. /// </summary> /// <param name="cell"></param> /// <returns></returns> public bool CheckWin(DoubleLinkedListCell <GamePiece> cell) { int row = cell.Data.Row; char col = cell.Data.Column; if (Check(row, col, -1, -1, cell.Data.PieceColor)) { return(true); } if (Check(row, col, -1, 1, cell.Data.PieceColor)) { return(true); } if (Check(row, col, 0, 1, cell.Data.PieceColor)) { return(true); } if (Check(row, col, 1, 0, cell.Data.PieceColor)) { return(true); } else { return(false); } }
} //END OF CheckWin /// <summary> /// This is a helper function to the CheckWin function /// </summary> /// <param name="row">Start row</param> /// <param name="col">Start col</param> /// <param name="RowDirection">Used to traverse the game board</param> /// <param name="colDirection">Used to traverse the game board</param> /// <param name="color">Color of the "Winner"</param> /// <returns>A boolean stating if the Color has won</returns> private bool Check(int row, char col, int RowDirection, int colDirection, Color color) { bool Conditionals = false, ReverseLookUp = true; for (int i = 0; i <= 4; i++) { if (0 < row && row <= ColumnSize && col >= 'A' && col <= 'G') { DoubleLinkedListCell <GamePiece> temp = FindCell((col.ToString() + row).ToString()); if (temp == null) { if (Conditionals) { ReverseLookUp = false; break; } //END OF IF STATEMENT else { RowDirection = RowDirection * -1; colDirection = colDirection * -1; Conditionals = true; i = 0; } //END OF ELSE } //END OF IF STATEMENT else if (temp.Data.PieceColor != color) { if (Conditionals) { ReverseLookUp = false; break; } //END OF IF STATEMENT else { RowDirection = RowDirection * -1; colDirection = colDirection * -1; Conditionals = true; i = 0; } //END OF ELSE } //END OF ELSE IF } //END OF IF STATEMENT else { if (Conditionals) { ReverseLookUp = false; break; } //END OF IF STATEMENT else { RowDirection = RowDirection * -1; colDirection = colDirection * -1; Conditionals = true; i = 0; } //END OF ELSE STATEMENT } //END OF ELSE STATEMENT row += RowDirection; col = (char)(col + colDirection); } //END OF FOR LOOP return(ReverseLookUp); } //END OF Check
/// <summary> /// Finds a particular cell. /// </summary> /// <param name="id"></param> /// <returns></returns> public DoubleLinkedListCell <GamePiece> FindCell(string id) { FindColumn(id[0].ToString()); DoubleLinkedListCell <GamePiece> cell = _column.Data; while (cell != null) { if (cell.Id == id) { return(cell); } else { cell = cell.Prev; } } return(null); }
} //END OF FindColum /// <summary> /// Finds the cell using the string id /// </summary> /// <param name="id"></param> /// <returns>A cell containing Data within the ID</returns> private DoubleLinkedListCell <GamePiece> FindCell(string id) { FindColumn(id[0].ToString()); DoubleLinkedListCell <GamePiece> temp = _columns.Data; //temp cell to manipulate while (temp != null) { if (temp.Id == id) { return(temp); } //END OF IF else { temp = temp.Prev; } //END OF ELSE } //END OF WHILE LOOP return(null); } //END OF FindCell
}//end FindCell /// <summary> /// Method FindColumn finds the column of a gamepiece /// </summary> /// <param name="columnID"></param> public void FindColumn(string columnID) { char column = columnID[0]; if (column.CompareTo('G') > 0) { _column = null; return; } char colLetter = columnID[0]; DoubleLinkedListCell <DoubleLinkedListCell <GamePiece> > tempCell = _columns; while (tempCell.Id[0] != colLetter) { tempCell = tempCell.Next; } _column = tempCell; } //end FindColumn
}//end CheckWin /// <summary> /// Findcell finds the cell which we drop a gamepiece into /// </summary> /// <param name="id"></param> /// <returns></returns> private DoubleLinkedListCell <GamePiece> FindCell(string id) { FindColumn(id); if (_column == null || _column.Data == null) { return(null); } DoubleLinkedListCell <GamePiece> tempCell = _column.Data; while (tempCell.Id[1] != id[1]) { tempCell = tempCell.Next; if (tempCell == null) { return(null); } } return(tempCell); }//end FindCell
/// <summary> /// Initalizes the FlowDirections and puts buttons and labels in /// them /// </summary> public ConnectFour() { InitializeComponent(); for (int i = 0; i < 7; i++) { Button uxbutton = new Button(); uxbutton.Text = ((char)('A' + i)).ToString(); uxbutton.Name = ((char)('A' + i)).ToString(); uxbutton.Width = 45; uxbutton.Height = 20; uxbutton.Margin = new System.Windows.Forms.Padding(5); uxbutton.Click += Button_Click; uxPlaceButtonContainer.Controls.Add(uxbutton); if (_game.Column == null) { _game.Column = new DoubleLinkedListCell <DoubleLinkedListCell <GamePiece> >(uxbutton.Name); } else { while (_game.Column.Next != null) { _game.Column = _game.Column.Next; } DoubleLinkedListCell <DoubleLinkedListCell <GamePiece> > temp = new DoubleLinkedListCell <DoubleLinkedListCell <GamePiece> >(uxbutton.Name); temp.Next = null; temp.Prev = _game.Column; _game.Column.Next = temp; } for (int j = Game.ColumnSize; j > 0; j--) { Label uxlabel = new Label(); uxlabel.Width = 45; uxlabel.Height = 45; uxlabel.Margin = new System.Windows.Forms.Padding(5); uxlabel.BackColor = Color.White; uxlabel.Name = (((char)('A' + i)).ToString() + j.ToString()); uxBoardContainer.Controls.Add(uxlabel); } //END OF INNER FOR LOOP } //END OF OUTTER FOR LOOP uxTurnLabel.BackColor = Color.Red; uxTurnLabel.ForeColor = Color.Black; uxTurnLabel.Text = "Red"; } //END OF ConnectFour METHOD
/// <summary> /// Public connectfour builds the buttons /// </summary> public ConnectFour() { DoubleLinkedListCell <DoubleLinkedListCell <GamePiece> > cell = null; InitializeComponent(); uxTurnLabel.BackColor = Color.Red; uxTurnLabel.Text = "Red"; for (char i = 'A'; i < 'H'; i++) { Button b = new Button(); b.Text = i.ToString(); b.Width = 45; b.Height = 20; b.Margin = new System.Windows.Forms.Padding(5); b.Click += new System.EventHandler(uxPlaceButtonClick); uxPlaceButtonC.Controls.Add(b); DoubleLinkedListCell <DoubleLinkedListCell <GamePiece> > tempCell = new DoubleLinkedListCell <DoubleLinkedListCell <GamePiece> >(b.Text); if (cell != null) { cell.Next = tempCell; tempCell.Prev = cell; cell = cell.Next; } else { cell = tempCell; _game.Columns = tempCell; } //inner loop for (int size = Game.ColumnSize; size > 0; size--) { Label l = new Label(); l.Width = 45; l.Height = 45; l.Margin = new System.Windows.Forms.Padding(5); l.BackColor = Color.White; l.Text = ""; l.Name = b.Text + size; uxBoardContainer.Controls.Add(l); } } }
/// <summary> /// Sets _column to the cell cooresponding with the column ID /// </summary> /// <param name="columnid"></param> public void FindColumn(string columnid) { bool right = true; while (columnid != _column.Id) { if (_column.Next == null) { right = false; } if (right) { _column = _column.Next; } else { _column = _column.Prev; } } }
} //end Check /// <summary> /// Checkwin is the method which will state if there is a winner or not /// </summary> /// <param name="cell"></param> /// <returns></returns> public bool CheckWin(DoubleLinkedListCell <GamePiece> cell) { bool won = false; if (Check(cell.Data.Row, cell.Data.Column, 0, -1, cell.Data.PieceColor)) { won = true; } if (Check(cell.Data.Row, cell.Data.Column, 0, 1, cell.Data.PieceColor)) { won = true; } if (Check(cell.Data.Row, cell.Data.Column, 1, -1, cell.Data.PieceColor)) { won = true; } if (Check(cell.Data.Row, cell.Data.Column, 1, 0, cell.Data.PieceColor)) { won = true; } if (Check(cell.Data.Row, cell.Data.Column, 1, 1, cell.Data.PieceColor)) { won = true; } if (Check(cell.Data.Row, cell.Data.Column, -1, -1, cell.Data.PieceColor)) { won = true; } if (Check(cell.Data.Row, cell.Data.Column, -1, 0, cell.Data.PieceColor)) { won = true; } if (Check(cell.Data.Row, cell.Data.Column, -1, 1, cell.Data.PieceColor)) { won = true; } return(won); }//end CheckWin
/// <summary> /// Initializes the Connect Four interface. /// </summary> public ConnectFour() { InitializeComponent(); uxTurnLabel.BackColor = Color.Red; uxTurnLabel.Text = "Red"; for (int i = 'A'; i < 'A' + 7; i++) { DoubleLinkedListCell <DoubleLinkedListCell <GamePiece> > c = new DoubleLinkedListCell <DoubleLinkedListCell <GamePiece> >(((char)i).ToString()); if (_game.Column != null) { _game.Column.Next = c; } c.Prev = _game.Column; _game.Column = c; Button temp = new Button(); temp.Name = ((char)i).ToString(); temp.Text = ((char)i).ToString(); temp.Width = 45; temp.Height = 20; temp.Margin = new Padding(5); uxPlaceButtonContainer.Controls.Add(temp); temp.Click += new EventHandler(uxPlaceButtonClick); for (int j = Game.ColumnSize; j > 0; j--) { Label slot = new Label(); slot.Width = 45; slot.Height = 45; slot.Margin = new Padding(5); slot.BackColor = Color.White; slot.Name = ((char)i).ToString() + j; uxBoardContainer.Controls.Add(slot); } } }
} //END OF FindCell /// <summary> /// This function checks to see if the given cell was placed in a spot that connected four /// game pieces of the same color in a row /// </summary> /// <param name="cell">Holds the cell ID of the piece just placed</param> /// <returns>A boolean holding whether the player won or not</returns> public bool CheckWin(DoubleLinkedListCell <GamePiece> cell) { if (Check(cell.Data.Row, cell.Data.Column, 0, 1, cell.Data.PieceColor)) { return(true); } //END OF IF STATEMENT else if (Check(cell.Data.Row, cell.Data.Column, 1, 0, cell.Data.PieceColor)) { return(true); } //END OF IF STATEMENT else if (Check(cell.Data.Row, cell.Data.Column, -1, 1, cell.Data.PieceColor)) { return(true); } //END OF IF STATEMENT else if (Check(cell.Data.Row, cell.Data.Column, 1, 1, cell.Data.PieceColor)) { return(true); } //END OF IF STATEMENT else { return(false); } //END OF ELSE } //END OF CheckWin
} //END OF ConnectFour METHOD /// <summary> /// This event handler takes care of placing a new /// game piece on the board in the corresponding column /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Click(object sender, EventArgs e) { Button button = (Button)sender; string buttonHit = button.Text; _game.FindColumn(buttonHit); Color color = new Color(); if (uxTurnLabel.BackColor == Color.Red) { color = Color.Red; uxTurnLabel.BackColor = Color.Black; uxTurnLabel.ForeColor = Color.White; uxTurnLabel.Text = "Black"; } //END OF IF else { color = Color.Black; uxTurnLabel.BackColor = Color.Red; uxTurnLabel.ForeColor = Color.Black; uxTurnLabel.Text = "Red"; } //END OF ELSE if (_game.Column.Data == null) { _game.Column.Data = new DoubleLinkedListCell <GamePiece>(buttonHit + 1); _game.Column.Data.Data = new GamePiece(color, 1, Convert.ToChar(buttonHit)); SetColor(buttonHit + "1", color); } else { GamePiece add = new GamePiece(color, _game.Column.Data.Data.Row + 1, Convert.ToChar(buttonHit)); DoubleLinkedListCell <GamePiece> newPiece = new DoubleLinkedListCell <GamePiece>((buttonHit + (_game.Column.Data.Data.Row + 1)).ToString()); newPiece.Data = add; newPiece.Next = null; newPiece.Prev = _game.Column.Data; _game.Column.Data = newPiece; SetColor(buttonHit + add.Row.ToString(), color); if (_game.Column.Data.Data.Row == 6) { button.Enabled = false; _counter++; } //END OF IF } //END OF ELSE if ((_game.CheckWin(_game.Column.Data))) { MessageBox.Show(color.ToString() + " is the winner"); try { Environment.Exit(0); } //END OF TRY catch { } //END OF CATCH } //END OF IF if (_counter == 7) { MessageBox.Show("It is a Draw"); try { Environment.Exit(0); } //END OF TRY catch { } //END OF CATCH } //END OF IF } //END OF uxButton_Click
/// <summary> /// Handles the button click of the top row of buttons /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void uxPlaceButtonClick(object sender, EventArgs e) { Color color; string temp = ((Button)sender).Text; _game.FindColumn(temp); DoubleLinkedListCell <GamePiece> cell = _game.Column.Data; if (_game.Turn == Game.PlayersTurn.Red) { color = Color.Red; _game.Turn = Game.PlayersTurn.Black; uxTurnLabel.BackColor = Color.Black; uxTurnLabel.ForeColor = Color.White; uxTurnLabel.Text = "Black"; } else { color = Color.Black; _game.Turn = Game.PlayersTurn.Red; uxTurnLabel.BackColor = Color.Red; uxTurnLabel.ForeColor = Color.Black; uxTurnLabel.Text = "Red"; } if (_game.Column.Data == null) { _game.Column.Data = new DoubleLinkedListCell <GamePiece>(temp + 1); _game.Column.Data.Data = new GamePiece(color, 1, Convert.ToChar(temp)); SetColor(_game.Column.Data.Id, _game.Column.Data.Data.PieceColor); } else { int r = _game.Column.Data.Data.Row + 1; GamePiece gp = new GamePiece(color, r, Convert.ToChar(temp)); DoubleLinkedListCell <GamePiece> TempCell = new DoubleLinkedListCell <GamePiece>(temp + r); TempCell.Data = gp; TempCell.Prev = _game.Column.Data; _game.Column.Data.Next = TempCell; _game.Column.Data = TempCell; SetColor(_game.Column.Data.Id, _game.Column.Data.Data.PieceColor); if (r == Game.ColumnSize) { ((Button)sender).Enabled = false; _count++; if (_count == 7) { MessageBox.Show("Draw!"); Environment.Exit(0); } } } if (_game.CheckWin(_game.Column.Data)) { if (_game.Turn == Game.PlayersTurn.Black) { MessageBox.Show("Red has won!"); Environment.Exit(0); } else if ((_game.Turn == Game.PlayersTurn.Red)) { MessageBox.Show("Black has won!"); Environment.Exit(0); } } }
/// <summary> /// uxPlaceButtonClick places the buttons places the buttons which holds the cell /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxPlaceButtonClick(object sender, EventArgs e) { string button = ((Button)sender).Text; _game.FindColumn(button); Color c = _game.PlayerColors[(int)_game.Turn]; DoubleLinkedListCell <DoubleLinkedListCell <GamePiece> > head = _game.Column; DoubleLinkedListCell <GamePiece> temp = head.Data; int count = 1; if (temp == null) { temp = new DoubleLinkedListCell <GamePiece>(button + "1"); temp.Data = new GamePiece(c, 1, button[0]); head.Data = temp; //draw } else { while (temp.Next != null) { temp = temp.Next; count++; } temp.Next = new DoubleLinkedListCell <GamePiece>(button + (count + 1).ToString()); temp.Next.Data = new GamePiece(c, count + 1, button[0]); temp = temp.Next; count++; } SetColor(button + (count).ToString(), c); if (count == Game.ColumnSize) { ((Button)sender).Enabled = false; _disCount++; } if (_game.CheckWin(temp)) { MessageBox.Show(_game.Turn.ToString() + " Wins!"); Environment.Exit(0); } else if (_disCount == 7) { MessageBox.Show("Draw!"); Environment.Exit(0); } if (_game.Turn == Game.PlayersTurn.Red) { _game.Turn = Game.PlayersTurn.Black; uxTurnLabel.BackColor = Color.Black; uxTurnLabel.ForeColor = Color.White; uxTurnLabel.Text = "Black"; } else { _game.Turn = Game.PlayersTurn.Red; uxTurnLabel.BackColor = Color.Red; uxTurnLabel.ForeColor = Color.Black; uxTurnLabel.Text = "Red"; } }