Esempio n. 1
0
        private bool checkForwardDiagonal(CellState.cellState id, byte[] move)
        {
            //check only diagonal of last turn, looking top left to bottom right
            int min = move.Min();
            //define starting poit
            int row        = move[0] - min;
            int column     = move[1] - min;
            int numOfCells = fieldSize - column - row;

            if (numOfCells < goal) // check if diagonal is long enough
            {
                return(false);
            }
            int sameInRowCount = 0;

            for (int i = 0; i < numOfCells - (goal - 1); i++)
            {
                while (board[row, column] == id)
                {
                    i++;
                    row++;
                    column++;
                    sameInRowCount++;
                    if (sameInRowCount == goal)
                    {
                        return(true);
                    }
                }
                row++;
                column++;
                sameInRowCount = 0;
            }
            return(false);
        }
Esempio n. 2
0
 public MoveParameters(CellState.cellState playerID, int moveID, DateTime moveTime, DateTime wholeTime)
 {
     PlayerID  = playerID;
     MoveID    = moveID;
     MoveTime  = moveTime;
     WholeTime = wholeTime;
 }
Esempio n. 3
0
 private bool checkResult(CellState.cellState id, CellCoordinates move)
 {
     if (checkBackwardDiagonal(id, new byte[] { move.X, move.Y }) || checkForwardDiagonal(id, new byte[] { move.X, move.Y }) || checkHorizntal(id, move.X) || checkVertical(id, move.Y))
     {
         return(true);
     }
     return(false);
 }
Esempio n. 4
0
        public CellCoordinates AddTurn(IPlayer player, CellState.cellState id)
        {
            CellCoordinates newTurn = player.NextMove(board, goal, false, new TimeSpan(), 0);

            CellState.cellState currentValueOfCell = board[newTurn.X, newTurn.Y];

            if (currentValueOfCell != CellState.cellState.Empty)
            {
                Console.WriteLine("Cell {0}{1} is taken", newTurn.X, newTurn.Y);
                throw new CellNotEmptyException();
            }
            else
            {
                board[newTurn.X, newTurn.Y] = id;
                movesCount++;
                return(newTurn);
            }
        }
Esempio n. 5
0
        private bool checkHorizntal(CellState.cellState id, byte row)
        {
            //check only row of last turn
            int sameInRowCount = 0;

            for (int i = 0; i < fieldSize - (goal - 1); i++)
            {
                while (board[row, i] == id)
                {
                    i++;
                    sameInRowCount++;
                    if (sameInRowCount == goal)
                    {
                        return(true);
                    }
                }
                sameInRowCount = 0;
            }
            return(false);
        }
Esempio n. 6
0
        private bool checkVertical(CellState.cellState id, byte column)
        {
            //check only column of last turn
            int sameInRowCount = 0;

            for (int i = 0; i < fieldSize - (goal - 1); i++)
            {
                while (board[i, column] == id)
                {
                    i++;
                    sameInRowCount++;
                    if (sameInRowCount == goal)
                    {
                        return(true);
                    }
                }
                sameInRowCount = 0;
            }
            return(false);
        }
Esempio n. 7
0
        public void StartGame()
        {
            int currentPlayer = 0;

            CellState.cellState id = CellState.cellState.X;
            CellCoordinates     move;

            while (true)
            {
                try
                {
                    move = AddTurn(PlayersArray[currentPlayer], id);
                    PrintState();
                }
                catch (CellNotEmptyException)
                {
                    Console.WriteLine("This cell is already taken. Player {0} lost.", id);
                    return;
                }
                catch (ArgumentOutOfRangeException)
                {
                    Console.WriteLine("Turn is out of range of the field. Player {0} lost.", id);
                    return;
                }
                if (checkResult(id, move))
                {
                    Console.WriteLine("Game is over. Player {0} won.", id);
                    return;
                }
                else if (movesCount == fieldSize * fieldSize) //if number of moves equals to the number of cells - there is no more empty cells - it's a tie
                {
                    Console.WriteLine("It's a tie!");
                    return;
                }
                else
                {
                    currentPlayer = (currentPlayer == 0) ? 1 : 0; // change player
                    id            = id == CellState.cellState.X ? CellState.cellState.O : CellState.cellState.X;
                }
            }
        }
Esempio n. 8
0
        private bool checkBackwardDiagonal(CellState.cellState id, byte[] move)
        {
            //check only diagonal of last turn, looking bottom left to top right
            //define starting poit
            int row    = move[0];
            int column = move[1];

            while (row < fieldSize - 1 && column > 0)
            {
                row++;
                column--;
            }
            int numOfCells = row - column + 1;

            if (numOfCells < goal) // check if diagonal is long enough
            {
                return(false);
            }
            int sameInRowCount = 0;

            for (int i = 0; i < numOfCells - (goal - 1); i++)
            {
                while (board[row, column] == id)
                {
                    i++;
                    row--;
                    column++;
                    sameInRowCount++;
                    if (sameInRowCount == goal)
                    {
                        return(true);
                    }
                }
                row--;
                column++;
                sameInRowCount = 0;
            }
            return(false);
        }
        static void Main(string[] args)
        {
            CellState.cellState[,] playBoard;
            int    BoardSize = 15;
            byte   lineqty   = 5;
            Player player;
            Player hPlayer;

            playBoard = new CellState.cellState[BoardSize, BoardSize];
            player    = new Player();
            hPlayer   = new Player();
            CellCoordinates coords;

            while (true)
            {
                coords = player.NextMove(playBoard, lineqty, false, new TimeSpan(0), 0);
                playBoard[coords.X, coords.Y] = CellState.cellState.X;
                Console.WriteLine("Player {0} moved to {1}, {2}", CellState.cellState.X, coords.X, coords.Y);
                coords = hPlayer.NextMove(playBoard, lineqty, true, new TimeSpan(0), 0);
                playBoard[coords.X, coords.Y] = CellState.cellState.O;
                Console.WriteLine("Player {0} moved to {1}, {2}", CellState.cellState.O, coords.X, coords.Y);
            }
        }
Esempio n. 10
0
 private CellState.cellState[,] CreateBattleField(byte maxLengthFieldOfBattlefield)
 {
     CellState.cellState[,] battleField = new CellState.cellState[maxLengthFieldOfBattlefield, maxLengthFieldOfBattlefield];
     return(battleField);
 }
Esempio n. 11
0
 public void GetMoveParameters(CellState.cellState playerID, int moveID, DateTime moveTime, DateTime wholeTime)
 {
     PlayersMoves[(int)playerID].Push(new MoveParameters(playerID, moveID, moveTime, wholeTime));
     playersTables[(int)playerID].ItemsSource = PlayersMoves[(int)playerID];
     playersTables[(int)playerID].Items.Refresh();
 }