private void BoardButton_Click(object sender, EventArgs e) { BoardButton pointerToButton = (sender as BoardButton); if (isThereAnotherBlue() == true && m_EatInARow == false) { BoardButton pressedButton = getPressedButton(); if (pressedButton.CheckEatingMove(pointerToButton)) { makeEatingMove(pointerToButton, pressedButton, getEatenButton(pointerToButton, pressedButton)); } else if (pressedButton.CheckSingleMove(pointerToButton)) { makeSingleMove(pointerToButton, getPressedButton()); } else if (pointerToButton.ButtonStatus == BoardButton.eButtonStatus.Blue) { pointerToButton.BackColor = Color.White; pointerToButton.ButtonStatus = BoardButton.eButtonStatus.White; } else if (pointerToButton.MyTurn != Logics.eTurn.Empty && (pointerToButton.ButtonStatus == BoardButton.eButtonStatus.White) && pointerToButton.MyTurn == m_GameRules.Turn) { cleanOtherBlue(pointerToButton); pointerToButton.BackColor = Color.Blue; pointerToButton.ButtonStatus = BoardButton.eButtonStatus.Blue; } if (pointerToButton.MyTurn == Logics.eTurn.PlayerOne) { m_LabelWhoTurn.Text = string.Format("{0} Turn's ", m_PlayerTwoName); } else { m_LabelWhoTurn.Text = string.Format("{0} Turn's ", m_PlayerOneName); } } else if (m_EatInARow) { BoardButton pressedButton = getPressedButton(); if (pressedButton.CheckEatingMove(pointerToButton)) { makeEatingMove(pointerToButton, pressedButton, getEatenButton(pointerToButton, pressedButton)); } } else { if (pointerToButton.MyTurn == m_GameRules.Turn) { if (pointerToButton.ButtonStatus == BoardButton.eButtonStatus.White && pointerToButton.MyTurn != Logics.eTurn.Empty) { pointerToButton.BackColor = Color.Blue; pointerToButton.ButtonStatus = BoardButton.eButtonStatus.Blue; } } } }
public void CheckAllOptions(BoardButton i_destination, BoardButton i_eaten) { if (m_Logics.IsSingleMovePossible(i_destination, this)) { m_OptionalMoves.Add(i_destination); } if (m_Logics.IsEatingMovePossible(i_destination, this, i_eaten)) { m_EatingPossibilities.Add(i_destination); } }
}//מחשב מחדש את כל אופציות המהלכים private bool isOtherEatPossible(BoardButton i_Button) { bool check = false; if (i_Button.EatingPossibilities.Count > 0) { check = true; } return(check); }//בודק אופציות אכילה ברצף
private void setBoardSize() { for (int i = 0; i < m_BoardSize; i++) { for (int j = 0; j < m_BoardSize; j++) { BoardButton boardButton = new BoardButton(); boardButton.Size = new Size(40, 40); boardButton.Location = new Point(boardButton.Width * (j + 1), (i + 1) * (boardButton.Height)); if ((j + i) % 2 == 0) { boardButton.BackColor = Color.White; boardButton.ButtonStatus = BoardButton.eButtonStatus.White; if (i < (m_BoardSize / 2) - 1) { boardButton.Text = "O"; boardButton.MyTurn = Logics.eTurn.PlayerTwo; m_PlayerTwoScore++; } else if (i > (m_BoardSize / 2)) { boardButton.Text = "X"; boardButton.MyTurn = Logics.eTurn.PlayerOne; m_PlayerOneScore++; } else { boardButton.Text = ""; boardButton.MyTurn = Logics.eTurn.Empty; } } else { boardButton.BackColor = Color.Black; boardButton.ButtonStatus = BoardButton.eButtonStatus.Grey; boardButton.MyTurn = Logics.eTurn.Empty; } boardButton.BoardPlaceX = i; boardButton.BoardPlaceY = j; boardButton.Click += BoardButton_Click; this.Controls.Add(boardButton); } } refreshMoves(); }
public bool CheckSingleMove(BoardButton i_destination) { bool check = false; foreach (BoardButton button in m_OptionalMoves) { if (button == i_destination) { check = true; } } return(check); }
public bool CheckEatingMove(BoardButton i_destination) { bool check = false; foreach (BoardButton button in m_EatingPossibilities) { if (button == i_destination) { check = true; } } return(check); }
}//בודק חוקיות של ניסיון אכילה private eTurn getOpositeTurn(BoardButton i_Button) { eTurn opossiteTurn = new eTurn(); if (i_Button.MyTurn == eTurn.PlayerOne) { opossiteTurn = eTurn.PlayerTwo; } else if (i_Button.MyTurn == eTurn.PlayerTwo) { opossiteTurn = eTurn.PlayerOne; } return(opossiteTurn); }
}//בודק חוקיות של מהלך רגיל public bool IsEatingMovePossible(BoardButton i_Destination, BoardButton i_Source, BoardButton i_EatenSoldiar) { bool check = true; if (i_EatenSoldiar == null) { check = false; } else { //MessageBox.Show(string.Format("{0}, {1}, {2}, {6}\n {3},{4},{5},", Math.Abs(i_destination.BoardPlaceY - i_source.BoardPlaceY), i_destination.MyTurn.ToString(), i_eatenSoldiar.MyTurn.ToString(), i_source.ToString(), i_eatenSoldiar.ToString(), i_destination.ToString(), m_Turn.ToString())); if ((Math.Abs(i_Destination.BoardPlaceY - i_Source.BoardPlaceY) == 2) && i_Destination.MyTurn == eTurn.Empty && i_EatenSoldiar.MyTurn == getOpositeTurn(i_Source)) { //MessageBox.Show(string.Format(i_source.ToString() + i_eatenSoldiar.ToString() + i_destination.ToString() + " After")); if (i_Source.Text == "O") { if ((i_Destination.BoardPlaceX - i_Source.BoardPlaceX) != 2) { check = false; } } else if (i_Source.Text == "X") { if ((i_Destination.BoardPlaceX - i_Source.BoardPlaceX) != -2) { check = false; } } else if (i_Source.Text == "K" || i_Source.Text == "U") { if (Math.Abs(i_Destination.BoardPlaceX - i_Source.BoardPlaceX) != 2) { check = false; } } } else { check = false; } } return(check); }//בודק חוקיות של ניסיון אכילה
}//מבצע צעד רגיל private void cleanOtherBlue(BoardButton i_Button) { BoardButton button = null; foreach (object obj in this.Controls) { if (obj is BoardButton) { button = (obj as BoardButton); if (button != i_Button && button.ButtonStatus == BoardButton.eButtonStatus.Blue) { button.ButtonStatus = BoardButton.eButtonStatus.White; button.BackColor = Color.White; } } } }//מאפס את הלוח מכפתוים לחוצים
}//מחזיר את הריבוע שפוטנציאל לאכילה private void setOptionalMove(BoardButton i_Button) { BoardButton pointerToButon = null; foreach (object obj in this.Controls) { if (obj is BoardButton) { pointerToButon = obj as BoardButton; if (pointerToButon.MyTurn == Logics.eTurn.Empty && pointerToButon.ButtonStatus != BoardButton.eButtonStatus.Grey) { i_Button.CheckAllOptions(pointerToButon, getEatenButton(pointerToButon, i_Button)); } } } }//מאתחל את כל אופציות המהלכים
private void makeEatingMove(BoardButton i_Destination, BoardButton i_Source, BoardButton i_Eaten) { i_Destination.Text = i_Source.Text; i_Destination.MyTurn = m_GameRules.Turn; i_Source.Text = ""; i_Source.BackColor = Color.White; i_Source.ButtonStatus = BoardButton.eButtonStatus.White; i_Source.MyTurn = Logics.eTurn.Empty; i_Eaten.Text = ""; i_Eaten.BackColor = Color.White; i_Eaten.MyTurn = Logics.eTurn.Empty; if (m_GameRules.BecomeKing(i_Destination, m_BoardSize)) { ; } updateScore(); refreshMoves(); if (isOtherEatPossible(i_Destination)) { i_Destination.BackColor = Color.Blue; i_Destination.ButtonStatus = BoardButton.eButtonStatus.Blue; m_EatInARow = true; } else { m_EatInARow = false; m_GameRules.SwitchTurn(); } this.m_LabelWhoTurn.Text = string.Format("'{0}' Turn's now.", m_GameRules.Turn.ToString()); if (m_IsComputerMove == true && m_GameRules.Turn == Logics.eTurn.PlayerTwo) { ComputerMove(); refreshMoves(); } }//מבצע צעד אכילה
}//בודק האם קיימים כפתורים לחוצים private BoardButton getPressedButton() { BoardButton button = null; BoardButton pointerToButton = null; foreach (object obj in this.Controls) { if (obj is BoardButton) { button = (obj as BoardButton); if (button.ButtonStatus == BoardButton.eButtonStatus.Blue) { pointerToButton = button; } } } return(pointerToButton); }//מחזיר את הריבוע הלחוץ
}//מתודה שמחליפה תור בין שחקן לשחקן public bool BecomeKing(BoardButton i_Button, int i_Size) { m_BecomeKing = false; if (i_Button.Text == "X" && i_Button.BoardPlaceX == 0) { i_Button.Text = "K"; i_Button.MyTurn = eTurn.PlayerOne; m_BecomeKing = true; } else if (i_Button.Text == "O" && i_Button.BoardPlaceX == i_Size - 1) { i_Button.Text = "U"; i_Button.MyTurn = eTurn.PlayerTwo; m_BecomeKing = true; } return(m_BecomeKing); }//מתודה שהופכת חייל למלך
}//מאפס את הלוח מכפתוים לחוצים private bool isThereAnotherBlue() { bool anotherBlue = false; BoardButton button = null; foreach (object obj in this.Controls) { if (obj is BoardButton) { button = (obj as BoardButton); if (button.ButtonStatus == BoardButton.eButtonStatus.Blue) { anotherBlue = true; break; } } } return(anotherBlue); }//בודק האם קיימים כפתורים לחוצים
}//מבצע צעד אכילה private void makeSingleMove(BoardButton i_Destination, BoardButton i_Source) { i_Destination.Text = i_Source.Text; i_Destination.MyTurn = m_GameRules.Turn; i_Source.Text = ""; i_Source.ButtonStatus = BoardButton.eButtonStatus.White; i_Source.BackColor = Color.White; i_Source.MyTurn = Logics.eTurn.Empty; m_GameRules.BecomeKing(i_Destination, m_BoardSize); refreshMoves(); m_GameRules.SwitchTurn(); this.m_LabelWhoTurn.Text = string.Format("'{0}' Turn's now.", m_GameRules.Turn.ToString()); if (m_IsComputerMove == true && m_GameRules.Turn == Logics.eTurn.PlayerTwo) { ComputerMove(); refreshMoves(); } }//מבצע צעד רגיל
public bool IsSingleMovePossible(BoardButton i_Destination, BoardButton i_Source) { bool check = true; if (i_Destination.MyTurn != eTurn.Empty || (i_Destination.ButtonStatus == BoardButton.eButtonStatus.Grey) || Math.Abs(i_Destination.BoardPlaceY - i_Source.BoardPlaceY) != 1) { check = false; } else { if (i_Source.Text == "X") { if ((i_Destination.BoardPlaceX - i_Source.BoardPlaceX) != -1) { check = false; } } else if (i_Source.Text == "O") { if ((i_Destination.BoardPlaceX - i_Source.BoardPlaceX) != 1) { check = false; } } else if (i_Source.Text == "K" || i_Source.Text == "U") { if (Math.Abs(i_Destination.BoardPlaceX - i_Source.BoardPlaceX) != 1) { check = false; } } } return(check); }//בודק חוקיות של מהלך רגיל
public void ComputerMove() { listMove move = new listMove(); Random rnd = new Random(); int randomIndex; BoardButton button = null; BoardButton pointerToButton = null; foreach (object obj in this.Controls) { if (obj is BoardButton) { button = (obj as BoardButton); if (button.MyTurn == Logics.eTurn.PlayerTwo) { move.Button = button; if (move.Button.EatingPossibilities.Count > 0) { r_VaildEatingnMove.Add(move); } else if (move.Button.OptionalMoves.Count > 0) { r_Vaild.Add(move); } } } } if (m_EatInARow) { button = getPressedButton(); if (button.EatingPossibilities.Count == 1) { pointerToButton = button.EatingPossibilities[0]; } else if (button.EatingPossibilities.Count > 1) { randomIndex = rnd.Next(button.EatingPossibilities.Count - 1); pointerToButton = button.EatingPossibilities[randomIndex]; } pointerToButton.PerformClick(); r_VaildEatingnMove.Clear(); } else if (r_VaildEatingnMove.Count > 0) { randomIndex = rnd.Next(r_VaildEatingnMove.Count - 1); button = r_VaildEatingnMove[randomIndex].Button; button.PerformClick(); if (button.EatingPossibilities.Count == 1) { pointerToButton = button.EatingPossibilities[0]; } else if (button.EatingPossibilities.Count > 1) { randomIndex = rnd.Next(button.EatingPossibilities.Count - 1); pointerToButton = button.EatingPossibilities[randomIndex]; } pointerToButton.PerformClick(); r_VaildEatingnMove.Clear(); } else if (r_Vaild.Count > 0) { randomIndex = rnd.Next(r_Vaild.Count - 1); button = r_Vaild[randomIndex].Button; button.PerformClick(); if (button.EatingPossibilities.Count > 0) { randomIndex = rnd.Next(button.EatingPossibilities.Count - 1); pointerToButton = button.EatingPossibilities[randomIndex]; } else if (button.OptionalMoves.Count > 0) { randomIndex = rnd.Next(button.OptionalMoves.Count - 1); pointerToButton = button.OptionalMoves[randomIndex]; } pointerToButton.PerformClick(); r_Vaild.Clear(); } else { this.OnClosed(e); } }//מהלכי מחשב
}//מחזיר את הריבוע הלחוץ private BoardButton getEatenButton(BoardButton i_Destination, BoardButton i_Source) { BoardButton button = null; BoardButton pointerToButton = null; if (i_Source.Text == "O") { foreach (object obj in this.Controls) { if (obj is BoardButton) { button = (obj as BoardButton); if (i_Destination.BoardPlaceY > i_Source.BoardPlaceY) { if ((button.BoardPlaceY == i_Source.BoardPlaceY + 1 && button.BoardPlaceY == i_Destination.BoardPlaceY - 1) && (button.BoardPlaceX == i_Source.BoardPlaceX + 1 && button.BoardPlaceX == i_Destination.BoardPlaceX - 1)) { pointerToButton = button; } } else if (i_Destination.BoardPlaceY < i_Source.BoardPlaceY) { if ((button.BoardPlaceY == i_Source.BoardPlaceY - 1 && button.BoardPlaceY == i_Destination.BoardPlaceY + 1) && (button.BoardPlaceX == i_Source.BoardPlaceX + 1 && button.BoardPlaceX == i_Destination.BoardPlaceX - 1)) { pointerToButton = button; } } } } } else if (i_Source.Text == "X") { foreach (object obj in this.Controls) { if (obj is BoardButton) { button = (obj as BoardButton); if (i_Destination.BoardPlaceY > i_Source.BoardPlaceY) { if ((button.BoardPlaceY == i_Source.BoardPlaceY + 1 && button.BoardPlaceY == i_Destination.BoardPlaceY - 1) && (button.BoardPlaceX == i_Source.BoardPlaceX - 1 && button.BoardPlaceX == i_Destination.BoardPlaceX + 1)) { pointerToButton = button; } } else if (i_Destination.BoardPlaceY < i_Source.BoardPlaceY) { if ((button.BoardPlaceY == i_Source.BoardPlaceY - 1 && button.BoardPlaceY == i_Destination.BoardPlaceY + 1) && (button.BoardPlaceX == i_Source.BoardPlaceX - 1 && button.BoardPlaceX == i_Destination.BoardPlaceX + 1)) { pointerToButton = button; } } } } } else if (i_Source.Text == "K" || i_Source.Text == "U") { foreach (object obj in this.Controls) { if (obj is BoardButton) { button = (obj as BoardButton); if (i_Destination.BoardPlaceX > i_Source.BoardPlaceX) { if (i_Destination.BoardPlaceY > i_Source.BoardPlaceY) { if ((button.BoardPlaceY == i_Source.BoardPlaceY + 1 && button.BoardPlaceY == i_Destination.BoardPlaceY - 1) && (button.BoardPlaceX == i_Source.BoardPlaceX + 1 && button.BoardPlaceX == i_Destination.BoardPlaceX - 1)) { pointerToButton = button; } } else if (i_Destination.BoardPlaceY < i_Source.BoardPlaceY) { if ((button.BoardPlaceY == i_Source.BoardPlaceY - 1 && button.BoardPlaceY == i_Destination.BoardPlaceY + 1) && (button.BoardPlaceX == i_Source.BoardPlaceX + 1 && button.BoardPlaceX == i_Destination.BoardPlaceX - 1)) { pointerToButton = button; } } } else { if (i_Destination.BoardPlaceY > i_Source.BoardPlaceY) { if ((button.BoardPlaceY == i_Source.BoardPlaceY + 1 && button.BoardPlaceY == i_Destination.BoardPlaceY - 1) && (button.BoardPlaceX == i_Source.BoardPlaceX - 1 && button.BoardPlaceX == i_Destination.BoardPlaceX + 1)) { pointerToButton = button; } } else if (i_Destination.BoardPlaceY < i_Source.BoardPlaceY) { if ((button.BoardPlaceY == i_Source.BoardPlaceY - 1 && button.BoardPlaceY == i_Destination.BoardPlaceY + 1) && (button.BoardPlaceX == i_Source.BoardPlaceX - 1 && button.BoardPlaceX == i_Destination.BoardPlaceX + 1)) { pointerToButton = button; } } } } } } return(pointerToButton); }//מחזיר את הריבוע שפוטנציאל לאכילה