public bool TryAddDiscToLocation(int i_X, int i_Y, Disc.eColors i_Color) { List <eDirections> directionsToFlip = new List <eDirections>(); bool leagalMove = isCellEmpty(i_X, i_Y) && isMoveLeagle(i_X, i_Y, i_Color, directionsToFlip); if (leagalMove) { BoardMatrix[i_Y, i_X] = new Disc(i_Color, new Point(i_X, i_Y)); flipRelevantDiscs(BoardMatrix[i_Y, i_X], directionsToFlip); } return(leagalMove); }
public bool TryAddDiscToLocation(int i_X, int i_Y, Disc.eColors i_Color) { List <eDirections> directionsToFlip = new List <eDirections>(); bool leagalMove = isCellEmpty(i_X, i_Y) && isMoveLeagle(i_X, i_Y, i_Color, directionsToFlip); if (leagalMove) { BoardMatrix[i_Y, i_X] = new Disc(i_Color, new Point(i_X, i_Y)); if (!r_isCreatedFromAI) { OnSet(new Point(i_X, i_Y), (GameEngien.ePlayers)i_Color); } flipRelevantDiscs(BoardMatrix[i_Y, i_X], directionsToFlip); } return(leagalMove); }
private bool isMoveLeagle(int i_X, int i_Y, Disc.eColors i_Color, List <eDirections> io_DirectionsToFlip) { int numOfOptions = 0; List <eDirections> OponentLocation = inWhatCellsNextToDiscLocationThereIsOponentDiscs(i_X, i_Y, i_Color); if (OponentLocation.Count != 0) { foreach (eDirections diraction in OponentLocation) { if (scanForAnotherDiscInDirection(diraction, i_X, i_Y, i_Color)) { numOfOptions++; io_DirectionsToFlip.Add(diraction); } } } return(numOfOptions != 0); }
private bool scanForAnotherDiscInDirection(eDirections i_Direction, int i_X, int i_Y, Disc.eColors i_Color) { bool IsMoveLeagle = false; switch (i_Direction) { case eDirections.Up: { int j = 1; Disc CurrentDisc = r_BoardMatrix[i_Y - j, i_X]; while (CurrentDisc != null && i_Y - j >= 0) { if (CurrentDisc.Color == i_Color) { IsMoveLeagle = true; } j++; if (i_Y - j >= 0) { CurrentDisc = r_BoardMatrix[i_Y - j, i_X]; } } break; } case eDirections.Down: { int j = 1; Disc CurrentDisc = r_BoardMatrix[i_Y + j, i_X]; while (CurrentDisc != null && i_Y + j < m_Size) { if (CurrentDisc.Color == i_Color) { IsMoveLeagle = true; } j++; if (i_Y + j < m_Size) { CurrentDisc = r_BoardMatrix[i_Y + j, i_X]; } } break; } case eDirections.Left: { int j = 1; Disc CurrentDisc = r_BoardMatrix[i_Y, i_X - j]; while (CurrentDisc != null && i_X - j >= 0) { if (CurrentDisc.Color == i_Color) { IsMoveLeagle = true; } j++; if (i_X - j >= 0) { CurrentDisc = r_BoardMatrix[i_Y, i_X - j]; } } break; } case eDirections.Right: { int j = 1; Disc CurrentDisc = r_BoardMatrix[i_Y, i_X + j]; while (CurrentDisc != null && i_X + j < m_Size) { if (CurrentDisc.Color == i_Color) { IsMoveLeagle = true; } j++; if (i_X + j < m_Size) { CurrentDisc = r_BoardMatrix[i_Y, i_X + j]; } } break; } case eDirections.UpLeft: { int j = 1; Disc CurrentDisc = r_BoardMatrix[i_Y - j, i_X - j]; while (CurrentDisc != null && i_Y - j >= 0 && i_X - j >= 0) { if (CurrentDisc.Color == i_Color) { IsMoveLeagle = true; } j++; if (i_Y - j >= 0 && i_X - j >= 0) { CurrentDisc = r_BoardMatrix[i_Y - j, i_X - j]; } } break; } case eDirections.UpRight: { int j = 1; Disc CurrentDisc = r_BoardMatrix[i_Y - j, i_X + j]; while (CurrentDisc != null && i_Y - j >= 0 && i_X + j < m_Size) { if (CurrentDisc.Color == i_Color) { IsMoveLeagle = true; } j++; if (i_Y - j >= 0 && i_X + j < m_Size) { CurrentDisc = r_BoardMatrix[i_Y - j, i_X + j]; } } break; } case eDirections.DownLeft: { int j = 1; Disc CurrentDisc = r_BoardMatrix[i_Y + j, i_X - j]; while (CurrentDisc != null && i_Y + j < m_Size && i_X - j >= 0) { if (CurrentDisc.Color == i_Color) { IsMoveLeagle = true; } j++; if (i_Y + j < m_Size && i_X - j >= 0) { CurrentDisc = r_BoardMatrix[i_Y + j, i_X - j]; } } break; } case eDirections.DownRight: { int j = 1; Disc CurrentDisc = r_BoardMatrix[i_Y + j, i_X + j]; while (CurrentDisc != null && i_Y + j < m_Size && i_X + j < m_Size) { if (CurrentDisc.Color == i_Color) { IsMoveLeagle = true; } j++; if (i_Y + j < m_Size && i_X + j < m_Size) { CurrentDisc = r_BoardMatrix[i_Y + j, i_X + j]; } } break; } } return(IsMoveLeagle); }
private List <eDirections> inWhatCellsNextToDiscLocationThereIsOponentDiscs(int i_X, int i_Y, Disc.eColors i_Color) { List <eDirections> DiractionList = new List <eDirections>(); if ((i_Y + 1 < m_Size && r_BoardMatrix[i_Y + 1, i_X] != null) && (r_BoardMatrix[i_Y + 1, i_X].Color != i_Color)) { DiractionList.Add(eDirections.Down); } if ((i_Y - 1 >= 0 && r_BoardMatrix[i_Y - 1, i_X] != null) && (r_BoardMatrix[i_Y - 1, i_X].Color != i_Color)) { DiractionList.Add(eDirections.Up); } if (i_X + 1 < m_Size && r_BoardMatrix[i_Y, i_X + 1] != null && r_BoardMatrix[i_Y, i_X + 1].Color != i_Color) { DiractionList.Add(eDirections.Right); } if (i_X - 1 >= 0 && r_BoardMatrix[i_Y, i_X - 1] != null && r_BoardMatrix[i_Y, i_X - 1].Color != i_Color) { DiractionList.Add(eDirections.Left); } if (i_X + 1 < m_Size && i_Y + 1 < m_Size && r_BoardMatrix[i_Y + 1, i_X + 1] != null && r_BoardMatrix[i_Y + 1, i_X + 1].Color != i_Color) { DiractionList.Add(eDirections.DownRight); } if (i_Y + 1 < m_Size && i_X - 1 >= 0 && r_BoardMatrix[i_Y + 1, i_X - 1] != null && r_BoardMatrix[i_Y + 1, i_X - 1].Color != i_Color) { DiractionList.Add(eDirections.DownLeft); } if (i_Y - 1 >= 0 && i_X + 1 < m_Size && r_BoardMatrix[i_Y - 1, i_X + 1] != null && r_BoardMatrix[i_Y - 1, i_X + 1].Color != i_Color) { DiractionList.Add(eDirections.UpRight); } if (i_Y - 1 >= 0 && i_X - 1 >= 0 && r_BoardMatrix[i_Y - 1, i_X - 1] != null && r_BoardMatrix[i_Y - 1, i_X - 1].Color != i_Color) { DiractionList.Add(eDirections.UpLeft); } return(DiractionList); }