public Othello ApplyMove(ulong location)
        {
            var nextState = new Othello
            {
                player1Pieces      = player1Pieces,
                player2Pieces      = player2Pieces,
                skips              = skips,
                lastMove           = location,
                LastPlayersTurn    = CurrentPlayersTurn,
                CurrentPlayersTurn = CurrentPlayersTurn == PlayerId.Player1 ? PlayerId.Player2 : PlayerId.Player1,
            };

            if (location == skipMoveLocation)
            {
                nextState.skips += 1;
                return(nextState);
            }

            nextState.skips = 0;
            if (CurrentPlayersTurn == PlayerId.Player1)
            {
                ulong captures = Captures(player1Pieces, player2Pieces, location);
                nextState.player1Pieces |= captures | location;
                nextState.player2Pieces &= ~captures;
            }
            else
            {
                ulong captures = Captures(player2Pieces, player1Pieces, location);
                nextState.player2Pieces |= captures | location;
                nextState.player1Pieces &= ~captures;
            }
            return(nextState);
        }
Beispiel #2
0
        public void TestEndCaptures()
        {
            //  0  1  2  3  4  5  6  7
            //  8  9 10 11 12 13 14 15
            // 16 17 18 19 20 21 22 23
            // 24 25 26  X  O 29 30 31
            // 32 33 34  O  X 37 38 39
            // 40 41 42 43 44 45 46 47
            // 48 49 50 51 52 53 54 55
            // 56 57 58 59 60 61 62 63

            var state = new Othello();

            state.player1Pieces = ulong.MaxValue & ~(1ul << 57);
            state.player2Pieces = 1ul << 57;
            var successors = state.ExpandSuccessors();

            if (successors.Length != 1)
            {
                throw new Exception("should only be 1 successor");
            }
            var next = (Othello)successors[0];

            if (next.player1Pieces != state.player1Pieces || next.player2Pieces != state.player2Pieces)
            {
                throw new Exception("pieces are not the same");
            }
            if (next.skips != (state.skips + 1))
            {
                throw new Exception("skips should be 1");
            }
            if (next.IsTerminal(out PlayerId _))
            {
                throw new Exception("should have to skip more before terminal");
            }

            state      = next;
            successors = state.ExpandSuccessors();
            if (successors.Length != 1)
            {
                throw new Exception("should only be 1 successor");
            }
            next = (Othello)successors[0];
            if (next.player1Pieces != state.player1Pieces || next.player2Pieces != state.player2Pieces)
            {
                throw new Exception("pieces are not the same 2");
            }
            if (next.skips != (state.skips + 1))
            {
                throw new Exception("skips should be 2");
            }
            if (!next.IsTerminal(out PlayerId _))
            {
                throw new Exception("should be terminal");
            }
        }
        public static void GetBoardRepresentation(Othello state, System.IO.TextWriter writer)
        {
            Dictionary <PlayerId, string> rep = new Dictionary <PlayerId, string>()
            {
                { PlayerId.None, " " },
                { PlayerId.Player1, " X" },
                { PlayerId.Player2, " O" },
            };

            Console.ForegroundColor = ConsoleColor.White;
            var board = state.GetBoard();

            for (int i = 0; i < 8; i++)
            {
                var player = board[i * 8];
                for (int j = 0; j < 8; j++)
                {
                    player = board[i * 8 + j];
                    if (j > 0)
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        writer.Write("|");
                    }
                    if (player == PlayerId.None)
                    {
                        if (state.IsValidMove(1ul << (i * 8 + j)))
                        {
                            Console.ForegroundColor = ConsoleColor.DarkGray;
                            writer.Write((i * 8 + j).ToString().PadLeft(2));
                        }
                        else
                        {
                            writer.Write("  ");
                        }
                    }
                    else
                    {
                        var cellPlayer = board[i * 8 + j];
                        Console.ForegroundColor = cellPlayer == PlayerId.Player1 ? ConsoleColor.Red : ConsoleColor.Blue;
                        writer.Write(rep[cellPlayer]);
                    }
                }
                writer.WriteLine();
                if (i < 7)
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    writer.WriteLine("--+--+--+--+--+--+--+--");
                }
            }
            Console.ResetColor();
        }
Beispiel #4
0
        public void TestCaptures()
        {
            //  0  1  2  3  4  5  6  7
            //  8  9 10 11 12 13 14 15
            // 16 17 18 19 20 21 22 23
            // 24 25 26  X  O 29 30 31
            // 32 33 34  O  X 37 38 39
            // 40 41 42 43 44 45 46 47
            // 48 49 50 51 52 53 54 55
            // 56 57 58 59 60 61 62 63

            const ulong noCaptures = 0ul;

            var state    = new Othello();
            var captures = state.Captures(state.player1Pieces, state.player2Pieces, 1ul << 19);

            if (captures != noCaptures)
            {
                throw new Exception("expected no captures, got " + string.Join(", ", GetLocations(captures)));
            }
        }