static void TakeTurn(ITicTacToe g)
        {
            PlayerMark whoturn = g.GetWhoTurn();
            bool       placed  = false;

            while (!placed)
            {
                bool gotX = false;
                bool gotY = false;
                int  x, y;
                x = y = 0;
                Console.WriteLine("Entering Player Move For: {0}", GetMark(whoturn));
                while (!gotX)
                {
                    Console.WriteLine("Enter Column:");
                    string lineIn = Console.ReadLine();
                    gotX = int.TryParse(lineIn, out x);
                }
                while (!gotY)
                {
                    Console.WriteLine("Enter Row:");
                    string lineIn = Console.ReadLine();
                    gotY = int.TryParse(lineIn, out y);
                }
                placed = g.TryPlaceMarkAt(whoturn, x, y);
                if (!placed)
                {
                    Console.WriteLine("Invalid Move!");
                }
            }
        }
Example #2
0
 public PLAYER(string Name, ImageSource Image, PlayerMark Mark, ClickType Click)
 {
     this.Name  = Name;
     this.Image = Image;
     this.Mark  = Mark;
     this.Click = Click;
 }
    // Previously tried solution with collision detection on all separate clients for all players(instead of events).
    // But the result was that some tile marking got skipped if the clients skipped walking over them because of a bad connection.
    // This way we can be sure all tiles are getting marked.
    public void SetTileMarker(InGameMazeTile tile, PlayerCharacter player)
    {
        if (GameRules.GamePlayerType == GamePlayerType.SinglePlayer ||
            GameRules.GamePlayerType == GamePlayerType.SplitScreenMultiplayer)
        {
            player.LastTile = tile;

            MazeTilePath mazeTilePath = (MazeTilePath)tile.GetBackgrounds().FirstOrDefault(background => background is MazeTilePath);
            if (mazeTilePath == null)
            {
                return;
            }

            PlayerMark playerMark = new PlayerMark(mazeTilePath.ConnectionScore);

            HandlePlayerMarkerSprite(tile, player.PlayerNumber, playerMark);
            HandlePlayerTileMarkerEnds(tile);
            HandleNumberOfUnmarkedTiles();

            tile.ResetPlayerMarkEndsRenderer();

            tile.TriggerTransformations();
        }
        else
        {
            PlayerMarksTileEvent playerMarksTileEvent = new PlayerMarksTileEvent();
            playerMarksTileEvent.SendPlayerMarksTileEvent(tile.GridLocation, player);
        }
    }
        public void TestPlayerOrder()
        {
            ITicTacToe g = new Game();
            PlayerMark previousPlayer = PlayerMark.O;
            PlayerMark expectedPlayer = PlayerMark.X;
            PlayerMark tempPlayer     = PlayerMark._;
            int        moves_made     = 0;

            foreach (point p in failmoves)
            {
                // check player is who we expect:
                Assert.AreEqual <PlayerMark>(expectedPlayer, g.GetWhoTurn());



                // make a move guaranteed to make a stalemate (so we get 9 moves in)
                g.TryPlaceMarkAt(expectedPlayer, p.x, p.y);

                ++moves_made;

                // alternate who we expect:
                tempPlayer     = expectedPlayer;
                expectedPlayer = previousPlayer;
                previousPlayer = tempPlayer;
            }
        }
        public Point2D GetTurn(IBoard board, PlayerMark currentPlayer)
        {
            if (board.Width != _Width || board.Height != _Height)
            {
                throw new InvalidOperationException();
            }

            return(GetAction(board));
        }
Example #6
0
        public Point2D GetTurn(IBoard gameBoard, PlayerMark currentPlayer)
        {
            PlayerMark playerMark = currentPlayer;
            Board      board      = gameBoard.GetCopy();

            AIRunner aiRunner = new AIRunner(board, playerMark);

            return(aiRunner.GetBest(1.0, board.Width * board.Height).Point);
        }
 void TestMovesExpectWinner(ITicTacToe g, point[] moves, PlayerMark winner)
 {
     foreach (point p in moves)
     {
         g.TryPlaceMarkAt(g.GetWhoTurn(), p.x, p.y);
     }
     Assert.IsTrue(g.IsGameOver());
     Assert.AreEqual <PlayerMark>(winner, g.GetWinner());
 }
Example #8
0
        public static void DoTurn(Game game, int depth)
        {
            PlayerMark playerMark = game.CurrentPlayer;

            Board board = game.Board.GetCopy();

            AIRunner      ai    = new AIRunner(board, playerMark);
            PointWithMark point = ai.GetBest(1.0, depth);

            game.DoTurn(point.Point.X, point.Point.Y);
        }
        static void PrintBoard(ITicTacToe g)
        {
            PlayerMark whoturn = g.GetWhoTurn();

            Console.WriteLine("Player {0}'s Turn", GetMark(whoturn));
            Console.WriteLine("Board:");
            Console.WriteLine("   0 | 1 | 2 ");
            for (int row = 0; row < 3; ++row)
            {
                Console.WriteLine("{3}: {0} | {1} | {2} ",
                                  GetMark(g.GetMarkAt(0, row)), GetMark(g.GetMarkAt(1, row)), GetMark(g.GetMarkAt(2, row)), row);
            }
        }
Example #10
0
 private void ProcessCoords(PlayerMark playerMark, ref int counter, ref bool inRow, int x, int y)
 {
     if (inRow && x >= 0 && y >= 0 && x < Width && y < Height)
     {
         if (_Cells[x, y].IsMarked(playerMark))
         {
             counter++;
         }
         else
         {
             inRow = false;
         }
     }
 }
        public TicTacToeAi(double alpha, double gamma, int width, int height, int countInRow, PlayerMark playerMark)
        {
            _Alpha      = alpha;
            _Gamma      = gamma;
            _Width      = width;
            _Height     = height;
            _PlayerMark = playerMark;
            _CountInRow = countInRow;

            int stateCount  = (int)Math.Pow(GetCellStateCount(), _Width * _Height);
            int actionCount = _Width * _Height;

            _StateActionTable = new double[stateCount, actionCount];
        }
 private void HandlePlayerMarkerSprite(MazeTile tile, PlayerNumber playerNumber, PlayerMark playerMark)
 {
     if (playerNumber == PlayerNumber.Player1)
     {
         playerMark.SetOwner(PlayerMarkOwner.Player1);
         tile.PlayerMarkRenderer.sprite = MazeSpriteManager.Instance.Player1TileMarker[playerMark.ConnectionScore - 1];
         tile.PlayerMark = playerMark;
     }
     else
     {
         playerMark.SetOwner(PlayerMarkOwner.Player2);
         tile.PlayerMarkRenderer.sprite = MazeSpriteManager.Instance.Player2TileMarker[playerMark.ConnectionScore - 1];
         tile.PlayerMark = playerMark;
     }
 }
Example #13
0
        private void DrawCell(Graphics g, PlayerMark playerMark, Rectangle rectangle)
        {
            switch (playerMark)
            {
            case PlayerMark.Cross:
                g.DrawLine(Pens.Red, new Point(rectangle.Left, rectangle.Top), new Point(rectangle.Right, rectangle.Bottom));
                g.DrawLine(Pens.Red, new Point(rectangle.Right, rectangle.Top), new Point(rectangle.Left, rectangle.Bottom));
                break;

            case PlayerMark.Nought:
                g.DrawEllipse(Pens.Blue, rectangle);
                break;

            default:
                throw new NotSupportedException();
            }
        }
Example #14
0
    private void CountTileMarkerScores()
    {
        Dictionary <PlayerNumber, int> tempPlayerScores = new Dictionary <PlayerNumber, int>();

        List <InGameMazeTile> markedTiles = new List <InGameMazeTile>();

        for (int i = 0; i < MazeLevelGameplayManager.Instance.Level.Tiles.Count; i++)
        {
            InGameMazeTile tile = MazeLevelGameplayManager.Instance.Level.Tiles[i] as InGameMazeTile;
            if (tile.PlayerMark != null)
            {
                markedTiles.Add(tile);
            }
        }

        int playerMarkScorePlayer1 = 0;
        int playerMarkScorePlayer2 = 0;

        for (int i = 0; i < markedTiles.Count; i++)
        {
            PlayerMark playerMark = markedTiles[i].PlayerMark;

            if (playerMark.Owner == PlayerMarkOwner.Player1)
            {
                playerMarkScorePlayer1 += MarkedTileValue;
            }
            else if (playerMark.Owner == PlayerMarkOwner.Player2)
            {
                playerMarkScorePlayer2 += MarkedTileValue;
            }
        }

        tempPlayerScores.Add(PlayerNumber.Player1, playerMarkScorePlayer1);
        if (PlayerMazeScores.ContainsKey(PlayerNumber.Player2))
        {
            tempPlayerScores.Add(PlayerNumber.Player2, playerMarkScorePlayer2);
        }

        foreach (KeyValuePair <PlayerNumber, int> item in tempPlayerScores)
        {
            PlayerMazeScore p = PlayerMazeScores[item.Key];
            p.TileMarkScore            = item.Value;
            PlayerMazeScores[item.Key] = p;
            Logger.Log(Logger.Score, $"Tile marker scores: {item.Key.ToString()} has {PlayerMazeScores[item.Key].TileMarkScore} points.");
        }
    }
        public static IDictionary <PlayerMark, ITicTacToeAi> Teach(int countInRow, int boardWidth, int boardHeight, int gameCount, double alpha, double gamma, Action <int, int> progressNotifier = null)
        {
            IDictionary <PlayerMark, ITicTacToeAi> ticTacToeAiMap = new Dictionary <PlayerMark, ITicTacToeAi>();

            foreach (PlayerMark playerMark in Enum.GetValues(typeof(PlayerMark)))
            {
                ticTacToeAiMap.Add(playerMark, new TicTacToeAi(alpha, gamma, boardWidth, boardHeight, countInRow, playerMark));
            }

            //   ticTacToeAiMap.Add(PlayerMark.Nought, new TicTacToeAi(alpha, gamma, boardWidth, boardHeight, countInRow, PlayerMark.Nought));
            //   ticTacToeAiMap.Add(PlayerMark.Cross, new AI());

            for (int i = 0; i < gameCount; i++)
            {
                progressNotifier?.Invoke(i, gameCount);

                Game game = new Game(countInRow, boardWidth, boardHeight, default(PlayerMark));

                while (!game.Completed)
                {
                    Point2D point = GetTurn(ticTacToeAiMap, game);
                    game.DoTurn(point.X, point.Y);
                }

                // Console.WriteLine("Winner: {0}", game.Winner?.ToString() ?? "Tie");

                PlayerMark?winner = game.Winner;
                double     reward = winner.HasValue ? 1.0 : 0.0;

                Board      board = game.Board.GetCopy();
                PlayerMark?currentPlayerNullable = board.LastPlayerMark;

                do
                {
                    Point2D point2D = board.LastPoint;
                    board.Undo();
                    PlayerMark currentPlayer = currentPlayerNullable.Value;
                    double     playerReward  = winner == currentPlayer ? reward : -reward;
                    ticTacToeAiMap[currentPlayer].Backpropagate(board, point2D, playerReward);
                    currentPlayerNullable = board.LastPlayerMark;
                } while (currentPlayerNullable.HasValue);
            }

            return(ticTacToeAiMap);
        }
Example #16
0
    //***********************************

    void Start()
    {
        playerMark = GetComponentInChildren <PlayerMark>();

        Controller = GetComponent <PlayerController>();

        Money = new Money(500);

        Items = new List <ITradeable>();

        Messages = new List <Message>();

        /*for(int i=0; i<10; i++) {
         *
         *      Messages.Add (new Message("Title " + i, "Description " + i));
         * }*/

        GameLogic.Instance.RegisterPlayer(this);
    }
Example #17
0
        private void MainForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            Point p = e.Location;
            int   x = BoardWidth * p.X / ClientSize.Width;
            int   y = BoardHeight * p.Y / ClientSize.Height;

            if (!_Game.DoTurn(x, y))
            {
                return;
            }

            Invalidate();

            if (!_Game.Completed)
            {
                Point2D point = _Map[_Game.CurrentPlayer].GetTurn(_Game.Board, _Game.CurrentPlayer);
                _Game.DoTurn(point);
                Invalidate();

                if (!_Game.Completed)
                {
                    return;
                }
            }

            if (_Game.Winner.HasValue)
            {
                PlayerMark playerMark = _Game.Winner.Value;
                MessageBox.Show(string.Format("Player {0} win", playerMark), "Win/Lose", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Tie", "Tie", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            NewGame();
        }
Example #18
0
        private bool IsWin(PlayerMark playerMark)
        {
            //横
            for (int y = 0; y < 3; y++)
            {
                if (_boardStates[0, y] == playerMark &&
                    _boardStates[1, y] == playerMark &&
                    _boardStates[2, y] == playerMark)
                {
                    return(true);
                }
            }

            //縦
            for (int x = 0; x < 3; x++)
            {
                if (_boardStates[x, 0] == playerMark &&
                    _boardStates[x, 1] == playerMark &&
                    _boardStates[x, 2] == playerMark)
                {
                    return(true);
                }
            }

            //斜め
            if (_boardStates[0, 0] == playerMark &&
                _boardStates[1, 1] == playerMark &&
                _boardStates[2, 2] == playerMark)
            {
                return(true);
            }

            if (_boardStates[2, 0] == playerMark &&
                _boardStates[1, 1] == playerMark &&
                _boardStates[0, 2] == playerMark)
            {
                return(true);
            }

            return(false);
        }
Example #19
0
        private double GetWeight(double weight, int depth)
        {
            if (_Board.HasInRow(_PlayerMark))
            {
                return(weight);
            }

            if (!_Board.HasEmpty() || depth == 0)
            {
                return(0.0);
            }

            PlayerMark oldPlayerMark = _PlayerMark;

            _PlayerMark = _PlayerMark.GetNext();
            PointWithMark pointWithMark = GetBest(weight / 2.0, depth - 1);

            _PlayerMark = oldPlayerMark;

            return(-pointWithMark.Mark);
        }
Example #20
0
        private void Draw(Bitmap bitmap)
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.Clear(Color.White);

                int cellWidth  = bitmap.Width / _Game.BoardWidth;
                int cellHeight = bitmap.Height / _Game.BoardHeight;

                for (int i = 1; i < _Game.BoardWidth; i++)
                {
                    int x = cellWidth * i;
                    g.DrawLine(Pens.Gray, x, 0, x, bitmap.Height);
                }

                for (int j = 1; j < _Game.BoardHeight; j++)
                {
                    int y = cellHeight * j;
                    g.DrawLine(Pens.Gray, 0, y, bitmap.Width, y);
                }

                for (int i = 0; i < _Game.BoardWidth; i++)
                {
                    for (int j = 0; j < _Game.BoardHeight; j++)
                    {
                        Cell cell = _Game[i, j];

                        if (cell.Empty)
                        {
                            continue;
                        }

                        PlayerMark playerMark = cell.PlayerMark;

                        DrawCell(g, playerMark, new Rectangle(cellWidth * i, cellHeight * j, cellWidth, cellHeight));
                    }
                }
            }
        }
        public void TestMoveCommit()
        {
            ITicTacToe g = new Game();
            PlayerMark previousPlayer = PlayerMark.O;
            PlayerMark expectedPlayer = PlayerMark.X;
            PlayerMark tempPlayer     = PlayerMark._;
            int        moves_made     = 0;

            foreach (point p in failmoves)
            {
                // make sure move is accepted
                Assert.IsTrue(g.TryPlaceMarkAt(expectedPlayer, p.x, p.y));

                // make sure move is committed
                Assert.AreEqual <PlayerMark>(expectedPlayer, g.GetMarkAt(p.x, p.y));
                ++moves_made;

                // alternate who we expect:
                tempPlayer     = expectedPlayer;
                expectedPlayer = previousPlayer;
                previousPlayer = tempPlayer;
            }
        }
        public void TestToStalemate()
        {
            ITicTacToe g = new Game();
            PlayerMark previousPlayer = PlayerMark.O;
            PlayerMark expectedPlayer = PlayerMark.X;
            PlayerMark tempPlayer     = PlayerMark._;
            int        moves_made     = 0;

            foreach (point p in failmoves)
            {
                // check player is who we expect:
                Assert.AreEqual <PlayerMark>(expectedPlayer, g.GetWhoTurn());



                // make a move guaranteed to make a stalemate (so we get 9 moves in)
                Assert.IsTrue(g.TryPlaceMarkAt(expectedPlayer, p.x, p.y));

                // make sure move is committed
                Assert.AreEqual <PlayerMark>(expectedPlayer, g.GetMarkAt(p.x, p.y));
                ++moves_made;

                // alternate who we expect:
                tempPlayer     = expectedPlayer;
                expectedPlayer = previousPlayer;
                previousPlayer = tempPlayer;
            }

            // should be unnecessary. might as well though.
            Assert.AreEqual <int>(9, moves_made);

            // game should now be over
            Assert.IsTrue(g.IsGameOver());

            // game winner should be no one
            Assert.AreEqual <PlayerMark>(PlayerMark._, g.GetWinner());
        }
Example #23
0
        public bool Mark(int x, int y, PlayerMark playerMark)
        {
            if (!_Cells[x, y].Empty)
            {
                return(false);
            }

            BoardState boardState = _Stack[_StackPointer++];

            CopyBoardState(_CurrentBoardState, boardState);
            boardState.Point = new Point2D(x, y);
            _EmptyCount--;
            _Cells[x, y] = Cell.Get(playerMark);

            int index = (int)playerMark;
            int count = _CurrentBoardState.MaxCountInRowPerMark[index];

            if (count >= CountInRow)
            {
                return(true);
            }

            int  primaryDiagonal = 1;
            bool primaryDiagonalInRowBackward   = true;
            bool primaryDiagonalInRowForward    = true;
            int  secondaryDiagonal              = 1;
            bool secondaryDiagonalInRowBackward = true;
            bool secondaryDiagonalInRowForward  = true;
            int  horizontal = 1;
            bool horizontalInRowBackward = true;
            bool horizontalInRowForward  = true;
            int  vertical = 1;
            bool verticalInRowBackward = true;
            bool verticalInRowForward  = true;

            for (int i = 1; i < CountInRow; i++)
            {
                int pdx = x + i;
                int pdy = y + i;
                int ndx = x - i;
                int ndy = y - i;

                ProcessCoords(playerMark, ref primaryDiagonal, ref primaryDiagonalInRowBackward, ndx, ndy);
                ProcessCoords(playerMark, ref primaryDiagonal, ref primaryDiagonalInRowForward, pdx, pdy);

                ProcessCoords(playerMark, ref secondaryDiagonal, ref secondaryDiagonalInRowBackward, ndx, pdy);
                ProcessCoords(playerMark, ref secondaryDiagonal, ref secondaryDiagonalInRowForward, pdx, ndy);

                ProcessCoords(playerMark, ref horizontal, ref horizontalInRowBackward, ndx, y);
                ProcessCoords(playerMark, ref horizontal, ref horizontalInRowForward, pdx, y);


                ProcessCoords(playerMark, ref vertical, ref verticalInRowBackward, x, ndy);
                ProcessCoords(playerMark, ref vertical, ref verticalInRowForward, x, pdy);
            }

            int candidateCount = Math.Max(Math.Max(horizontal, vertical), Math.Max(primaryDiagonal, secondaryDiagonal));

            _CurrentBoardState.MaxCountInRowPerMark[index] = Math.Max(candidateCount, count);

            return(true);
        }
Example #24
0
 static string GetMark(PlayerMark p)
 {
     return(System.Enum.GetName(typeof(PlayerMark), p));
 }
Example #25
0
 public Game(int countInRow, int width, int height, PlayerMark currentPlayer)
 {
     _Board        = new Board(width, height, countInRow);
     Board         = new ReadOnlyBoard(_Board);
     CurrentPlayer = currentPlayer;
 }
Example #26
0
 public static Cell Get(PlayerMark playerMark)
 {
     return(_Cells[playerMark]);
 }
Example #27
0
 private Cell()
 {
     Empty      = true;
     PlayerMark = default(PlayerMark);
 }
Example #28
0
 private Cell(PlayerMark playerMark)
 {
     Empty      = false;
     PlayerMark = playerMark;
 }
Example #29
0
 public bool IsMarked(PlayerMark playerMark)
 {
     return(!Empty && playerMark == PlayerMark);
 }
Example #30
0
 public AIRunner(Board board, PlayerMark playerMark)
 {
     _Board      = board;
     _PlayerMark = playerMark;
 }