Exemplo n.º 1
0
        public bool IsWinner(Board board, bool first)
        {
            int DIM     = board.GetDIM();
            string mark = (first) ? "X" : "O";

            for (int i=0; i<DIM; i++) {
                if (board.CheckRow(mark, i)) {
                    UpdateWinnerInfo(LineType.ROW, i, first);
                    return true;
                }

                if (board.CheckCol(mark, i)) {
                    UpdateWinnerInfo(LineType.COL, i, first);
                    return true;
                }
            }

            if (board.CheckDia(mark)) {
                UpdateWinnerInfo(LineType.DIA, 1, first);
                return true;
            }

            if (board.CheckAntiDia(mark)) {
                UpdateWinnerInfo(LineType.DIA, 2, first);
                return true;
            }

            return false;
        }
        public int[] Algorithm(Board board, bool tempFirst, int alpha, int beta, int depth)
        {
            int[] pos = new int[2] {-1, -1};
            int score = -100;

            List<int[]> empties = board.GetEmpties();
            bool exit = false;

            for (int i=0; i<empties.Count && !exit; i++) {
                int[] tempPos = empties[i];
                bool winner   = PlaceMarkAndCheckWinner(board, tempPos, tempFirst);

                int tempScore = 0;
                if (!winner && tempBoard.GetEmpties().Count > 0 && depth > 0) {
                    int[] tempResult = Algorithm(tempBoard, !tempFirst, -beta, -alpha, depth - 1);
                    tempScore        = -tempResult[2];
                }

                else
                    tempScore = Heuristics(winner);

                if (tempScore > score) {
                    pos   = tempPos;
                    score = tempScore;
                }

                if (tempScore > alpha)
                    alpha = tempScore;

                if (alpha >= beta)
                    exit = true;
            }

            return new[] {pos[0], pos[1], score};
        }
Exemplo n.º 3
0
        public int[] Algorithm(Board board, bool tempFirst)
        {
            int[] pos = new int[2] { -1, -1 };
            int score = (tempFirst == first) ? -100 : 100;

            List<int[]> empties = board.GetEmpties();

            for (int i=0; i<empties.Count; i++) {
                int[] tempPos = empties[i];
                bool winner   = PlaceMarkAndCheckWinner(board, tempPos, tempFirst);

                int tempScore = 0;
                if (!winner && tempBoard.GetEmpties().Count > 0) {
                    int[] tempResult = Algorithm(tempBoard, !tempFirst);
                    tempScore        = tempResult[2];
                }

                else
                    tempScore = Heuristics(winner, tempFirst);

                if ((tempFirst == first && tempScore > score) ||
                    (tempFirst != first && tempScore < score)) {
                    pos   = tempPos;
                    score = tempScore;
                }
            }

            return new[] { pos[0], pos[1], score };
        }
Exemplo n.º 4
0
        public int[] CalculateCell(Board board, bool player)
        {
            first = player;

            int[] result = Algorithm(board, first);
            return new[] { result[0], result[1] };
        }
        public int[] CalculateCell(Board board, bool player)
        {
            int alpha = -10;
            int beta  =  10;
            int depth =   8;

            int[] result = Algorithm(board, player, alpha, beta, depth);
            return new[] {result[0], result[1]};
        }
        public void DisplayBoard(Board board)
        {
            Console.Clear();
            Console.WriteLine("Player1: X");
            Console.WriteLine("Player2: O");
            Console.WriteLine("");
            Console.WriteLine("|{0}{1}{2}|", ConvertToSign(board.Cells[0].Value), ConvertToSign(board.Cells[1].Value), ConvertToSign(board.Cells[2].Value));
            Console.WriteLine("|{0}{1}{2}|", ConvertToSign(board.Cells[3].Value), ConvertToSign(board.Cells[4].Value), ConvertToSign(board.Cells[5].Value));
            Console.WriteLine("|{0}{1}{2}|", ConvertToSign(board.Cells[6].Value), ConvertToSign(board.Cells[7].Value), ConvertToSign(board.Cells[8].Value));

            Thread.Sleep(1000);
        }
Exemplo n.º 7
0
        private int[] Expected()
        {
            string[,] s = new string[DIM, DIM];
            if (DIM == 3) {
                s = new string[,] { {"O", "X", "O" },
                                    { "", "X", "X" },
                                    { "", "O",  "" } };
            }

            else if (DIM == 4) {
                s = new string[,] { {"O", "X", "O", "X" },
                                    { "", "X", "X", "X" },
                                    {"O", "O",  "", "O" },
                                    {"O", "X", "O", "X" } };
            }

            board = new Board(DIM);
            board.SetBoard(s);
            int[] expected = new[] { 1, 0 };

            return expected;
        }
Exemplo n.º 8
0
 public void InitializeBoard()
 {
     Board = new Board();
 }
		private void InitialiseBoards() {
			int windowSize = 700;
			int cellSize   = 140;

			this.Width  = windowSize + (DIM - 3) * cellSize;
			this.Height = windowSize + (DIM - 3) * cellSize;

			boardGrid.Children.Clear();

			boardGrid.Rows    = DIM;
			boardGrid.Columns = DIM;
			boardGrid.Height  = DIM*cellSize;
			boardGrid.Width   = DIM*cellSize;

			for (int i=0; i<DIM*DIM; i++) {
				TextBlock tb = new TextBlock();
				tb.Width     = cellSize;
				tb.Height    = cellSize;
				tb.Style     = (Style) Resources["Cells"] as Style;

				boardGrid.Children.Add(tb);
			}

			board = new Board(DIM);
		}