static void Main(string[] args) { XO.PrintTheme(); ConsoleKeyInfo keyInfo = Console.ReadKey(true); //while (keyInfo.Key != ConsoleKey.Escape) //{ // keyInfo = Console.ReadKey(true); // if (keyInfo.Key == ConsoleKey.UpArrow) // { // Console.SetCursorPosition(Console.CursorLeft , Console.CursorTop - 1); // } // else if (keyInfo.Key == ConsoleKey.DownArrow) // { // Console.SetCursorPosition(Console.CursorLeft , Console.CursorTop + 1); // } // else if (keyInfo.Key == ConsoleKey.LeftArrow) // { // Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop); // } // else if (keyInfo.Key == ConsoleKey.RightArrow) // { // Console.SetCursorPosition(Console.CursorLeft +1 , Console.CursorTop); // } //} while (keyInfo.Key != ConsoleKey.Escape) { XO.PrintX(keyInfo); XO.PrintO(); keyInfo = Console.ReadKey(true); } }
public void placingpiece(int positionX, int positionY) { XO thispiece = turn(); if (positionX < 0) { myIllegalMoveHandler(); return; } else if (positionY < 0) { myIllegalMoveHandler(); return; } else if (positionX > 2) { myIllegalMoveHandler(); return; } else if (positionY > 2) { myIllegalMoveHandler(); return; } else if (board[positionX, positionY] != XO.Empty) { myIllegalMoveHandler(); return; } if (this.NextMove == XO.X) { this.NextMove = XO.O; } else if (this.NextMove == XO.O) { this.NextMove = XO.X; } board[positionX, positionY] = thispiece; }
public WhoWon state() { for (int i = 0; i < board.GetLength(0); i++) { bool rowEqual = true; XO firstInRow = board[i, 0]; bool colEqual = true; XO firstINCol = board[0, i]; for (int j = 0; j < board.GetLength(1); j++) { rowEqual = rowEqual && board[i, j] == firstInRow; colEqual = colEqual && board[j, i] == firstINCol; } if (rowEqual == true) { if (firstInRow == XO.X) { return(WhoWon.XWins); } if (firstInRow == XO.O) { return(WhoWon.OWins); } } if (colEqual == true) { if (firstINCol == XO.X) { return(WhoWon.XWins); } if (firstINCol == XO.O) { return(WhoWon.OWins); } } } return(WhoWon.StillPlaying); }
private bool checkHorizontal(int x, int y, ref List <Point> LineWin) { XO team = _side.Current.Symbol; int jStart = 0, jFinish = _board.Width - 1; int kToWin; //число подряд идущих одинаковых знаков int jTempCoords; int j; //не столбец а счётчик развилки //поиск победы по горизонтали kToWin = 1; //увеличиваем счётчик на и проверяем элементы по бокам //если элемент вышел а рамки массива или не пренадлежит к текущему знаку( Х О ) //тогда мы перестаём идти в эту сторону //цикл работает пока мы можем идти в какую нибудь и сторон bool Right, Left; Right = Left = true; LineWin.Add(new Point(x, y)); j = 1; while (Right || Left) { if (Right) { jTempCoords = y + j; if (jTempCoords <= jFinish && _board[x, jTempCoords] == team) { kToWin++; LineWin.Add(new Point(x, jTempCoords)); } else { Right = false; } if (kToWin == _seriesWin) { break; } } if (Left) { jTempCoords = y - j; if (jTempCoords >= jStart && _board[x, jTempCoords] == team) { kToWin++; LineWin.Add(new Point(x, jTempCoords)); } else { Left = false; } if (kToWin == _seriesWin) { break; } } j++; } if (kToWin != _seriesWin) { LineWin.Clear(); } return(kToWin == _seriesWin); }
private bool checkSecondDiagonal(int x, int y, ref List <Point> LineWin) { XO team = _side.Current.Symbol; int jStart = 0, jFinish = _board.Width - 1; int iStart = 0, iFinish = _board.Height - 1; int kToWin; //число подряд идущих одинаковых знаков int jTempCoords, iTempCoords; int j; //не столбец а счётчик развилки //поиск победы по побочной диагонали kToWin = 1; iStart = 0; iFinish = _board.Height - 1; jStart = _board.Width - 1; jFinish = 0; LineWin.Add(new Point(x, y)); bool UpRight, DownLeft; UpRight = DownLeft = true; j = 1; while (UpRight || DownLeft) { if (UpRight) { iTempCoords = x - j; jTempCoords = y + j; if ((iTempCoords >= iStart && jTempCoords <= jStart) && _board[iTempCoords, jTempCoords] == team) { kToWin++; LineWin.Add(new Point(iTempCoords, jTempCoords)); } else { UpRight = false; } if (kToWin == _seriesWin) { break; } } if (DownLeft) { iTempCoords = x + j; jTempCoords = y - j; if ((iTempCoords <= iFinish && jTempCoords >= jFinish) && _board[iTempCoords, jTempCoords] == team) { kToWin++; LineWin.Add(new Point(iTempCoords, jTempCoords)); } else { DownLeft = false; } if (kToWin == _seriesWin) { break; } } j++; } if (kToWin != _seriesWin) { LineWin.Clear(); } return(kToWin == _seriesWin); }
private bool checkVertical(int x, int y, ref List <Point> LineWin) { XO team = _side.Current.Symbol; int iStart = 0, iFinish = _board.Height - 1; int kToWin; //число подряд идущих одинаковых знаков int iTempCoords; int j; //не столбец а счётчик развилки //поиск победы по вертикали kToWin = 1; //увеличиваем счётчик на и проверяем элементы по бокам //если элемент вышел а рамки массива или не пренадлежит к текущему знаку( Х О ) //тогда мы перестаём идти в эту сторону //цикл работает пока мы можем идти в какую нибудь и сторон LineWin.Add(new Point(x, y)); bool Up, Down; Up = Down = true; j = 1; while (Down || Up) { if (Down) { iTempCoords = x + j; if (iTempCoords <= iFinish && _board[iTempCoords, y] == team) { kToWin++; LineWin.Add(new Point(iTempCoords, y)); } else { Down = false; } if (kToWin == _seriesWin) { break; } } if (Up) { iTempCoords = x - j; if (iTempCoords >= iStart && _board[iTempCoords, y] == team) { kToWin++; LineWin.Add(new Point(iTempCoords, y)); } else { Up = false; } if (kToWin == _seriesWin) { break; } } j++; } if (kToWin != _seriesWin) { LineWin.Clear(); } return(kToWin == _seriesWin); }