Esempio n. 1
0
        public object Clone()
        {
            TicTacToeState newState = new TicTacToeState();

            newState.player = player;
            newState.x      = x;
            newState.y      = y;

            for (int i = 0; i < BOARDSIZE; i++)
            {
                for (int j = 0; j < BOARDSIZE; j++)
                {
                    newState.board[i, j] = board[i, j];
                }
            }
            return(newState);
        }
Esempio n. 2
0
 /// <summary>
 /// Called when the client successfully registers with the server. Retrieves the ongoing game data.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 void proxy_RegisterCompleted(object sender, RegisterCompletedEventArgs e)
 {
     if (e.Error != null)
     {
         state = TicTacToeState.ServiceNotAvailable;
         return;
     }
     if (e.Result)
     {
         try
         {
             proxy.GetGameStateAsync(playerID);
         }
         catch (Exception)
         {
             state = TicTacToeState.ServiceNotAvailable;
         }
     }
 }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var game = new TicTacToeState();

            while (game.Actions.Any())
            {
                Console.WriteLine($"CurrentPlayer: {game.CurrentPlayer}");
                Console.WriteLine(game);
                Console.WriteLine(SEPARATOR);

                var position = -1;
                while (position < 0)
                {
                    Console.WriteLine("Choose a free space: (0-8)");

                    var input = Console.ReadKey();
                    int.TryParse(input.KeyChar.ToString(), out position);
                }

                Console.WriteLine();

                game.ApplyAction(new TicTacToeAction(position, TicTacToePlayer.X));
                var computer = MonteCarloTreeSearch.GetTopActions(game, 50000).ToList();
                Console.WriteLine(SEPARATOR);
                if (computer.Count > 0)
                {
                    Console.WriteLine("Computer's ranked plays:");
                    foreach (var a in computer)
                    {
                        Console.WriteLine($"\t{a.Action}\t{a.NumWins}/{a.NumRuns} ({a.NumWins / a.NumRuns})");
                    }
                    game.ApplyAction(computer[0].Action);
                }

                position = -1;
            }

            Console.WriteLine(SEPARATOR);
            Console.WriteLine(game.ToString());
            Console.WriteLine("Game Over");
            Console.ReadKey();
        }
Esempio n. 4
0
        /// <summary>
        /// Occurs when the "send move" button clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void sendMoveButton_Click(object sender, EventArgs e)
        {
            Guid guid = Guid.Empty;

            if (state == TicTacToeState.AITurn)
            {
                guid = aiID;
            }
            else
            {
                guid = playerID;
            }
            try
            {
                proxy.GameStepAsync(guid, currentMove.X, currentMove.Y, currentMove.Player);

                state = TicTacToeState.WaitingForService;
            }
            catch (Exception)
            {
                state = TicTacToeState.ServiceNotAvailable;
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Method for recursively expand a graphnode.
        /// </summary>
        /// <param name="node">the nood to expand</param>
        /// <param name="depth">how deep we are in the subtree, in our recursive call</param>
        private void Expand(GraphNode node, int depth)
        {
            if (depth >= maxDepth || node.currentState.isGoal() != 0)
            {
                node.weight = node.currentState.Heuristics;
            }
            else
            {
                double maxWeight = double.MinValue; // the max weight of the current child nodes.

                foreach (PutOperator cuurOperator in PutOperator.operatorList)
                {
                    {
                        if (cuurOperator.Precondition(node.currentState))
                        {
                            TicTacToeState newState = cuurOperator.Apply(node.currentState);
                            GraphNode      newNode  = new GraphNode(newState, cuurOperator, node, 0);
                            Expand(newNode, depth + 1);
                            if (newNode.currentState.player != node.currentState.player)
                            {
                                newNode.weight = -newNode.weight;
                            }
                            if (newNode.weight > maxWeight)
                            {
                                maxWeight = newNode.weight;
                                if (depth == 0)
                                {
                                    proposedNode = newNode;
                                }
                            }
                        }
                    }
                }
                node.weight = maxWeight;
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Perform logic based on the current game state.
        /// </summary>
        /// <param name="gameTime">Time since this method was last called.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to be closed using the back button
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }


            // Reads a gesture if one is available
            Rectangle?    touchRect = null;
            GestureSample gs        = new GestureSample();

            while (TouchPanel.IsGestureAvailable && canUserInput)
            {
                gs        = TouchPanel.ReadGesture();
                touchRect = new Rectangle((int)gs.Position.X - 1, (int)gs.Position.Y - 1, 2, 2);
            }

            switch (state)
            {
            case TicTacToeState.GameInitialize:
                if (random.Next(0, 10) > 5)
                {
                    state = TicTacToeState.PlayerTurn;
                }
                else
                {
                    state = TicTacToeState.AITurn;
                }
                break;

            case TicTacToeState.ServiceNotAvailable:
                text = ServiceNotAvailableText;
                break;

            case TicTacToeState.WaitingForService:

                if (currentMove.Player == ConstData.XString)
                {
                    text = SendingMoveText;
                }
                else
                {
                    text = SendingAIMoveText;
                }
                break;

            case TicTacToeState.PlayerTurn:
                // If a tap was performed, try to perform a corresponding move
                if (touchRect.HasValue)
                {
                    HandlePickMoveInput(touchRect.Value);
                    if (currentMove != null)
                    {
                        sendMoveButton.HandleInput(gs);
                    }
                }
                break;

            case TicTacToeState.AITurn:
                text = "Waiting for AI Player...";
                AIPlay();
                break;

            case TicTacToeState.GameOver:
                // Checks if one of the two buttons available at this state have been clicked
                newGameButton.HandleInput(gs);
                exitButtonButton.HandleInput(gs);
                break;

            default:
                break;
            }

            base.Update(gameTime);
        }
Esempio n. 7
0
 public TicTacToeViewModelItem()
 {
     _state = TicTacToeState.None;
 }
Esempio n. 8
0
 private void OnGameFinished(TicTacToeState winner)
 {
     MessageBox.Show($"{winner} has won the game! Play again?", "Game over", MessageBoxButton.OK, MessageBoxImage.Question);
     _vm.Reset();
 }