示例#1
0
        }//PlayGame

        /// <summary>
        /// Turn functionality.
        /// If user turn, prompts for row, then column, then attempts to insert at chosen location.
        /// If AI turn, generates 2 random integers from 0 to 3 for row and column, then attempts to insert at that location.
        /// </summary>
        /// <param name="player">X or O to place</param>
        /// <param name="rng">Randum number generator. If null, it is user turn. If it exists, AI turn.</param>
        private async static Task TurnAsync(string player, TicTacToeServ.TicTacToeServClient client, Random rng = null)
        {
            int       row = -1, col = -1;
            bool      valid   = false;
            PlaceUnit toPlace = new PlaceUnit();

            toPlace.Unit = player;

            do                  //attempt to place token, repeat until valid location chosen
            {
                var board = await client.ShowBoardAsync(new Null { });

                Console.WriteLine(board.Board);
                if (rng == null) //if rng is null, we're processing player actions
                {
                    while (!valid)
                    {
                        Console.Write("Please enter the row you wish to place in: ");
                        valid = Int32.TryParse(Console.ReadLine(), out row);
                    }
                    valid = valid ^ valid;
                    while (!valid)
                    {
                        Console.Write("Please enter the column you wish to place in: ");
                        valid = Int32.TryParse(Console.ReadLine(), out col);
                    }
                    toPlace.Row = row;
                    toPlace.Col = col;
                }//if
                else            //if rng is not null, it's ai move
                {
                    row         = rng.Next(0, 3);
                    col         = rng.Next(0, 3);
                    toPlace.Row = row;
                    toPlace.Col = col;
                }//else

                var gamestate = await client.PlayTurnAsync(toPlace);

                valid = gamestate.Valid;
                if (!valid)
                {
                    if (rng != null)
                    {
                        Console.WriteLine("Invalid choice by AI. Rechoosing..)");
                    }
                    else
                    {
                        Console.WriteLine("Invalid choice. Please choose again.");
                    }
                }
            } while (!valid);
        } //Turn(Socket, TicTacToeBoard, string, Random)
示例#2
0
        }//main

        /// <summary>
        /// Actual game functionality
        /// </summary>
        /// <param name="Player">Either X or O depending on choice</param>
        private static async Task PlayGameAsync(string Player, TicTacToeServ.TicTacToeServClient client)
        {
            var GameState = await client.ShowBoardAsync(new Null { });

            var result = await client.CheckStateAsync(new Null { });

            Random rng = new Random();

            if (Player.ToLower() == "x")
            {
                result = await client.CheckStateAsync(new Null { });

                do          //Turn loop. Start with X (player), check for victory conditions, then continue to O and repeat.
                {
                    await TurnAsync(Player, client);

                    result = await client.CheckStateAsync(new Null { });

                    if (result.Result_ != "N")
                    {
                        break;
                    }

                    await TurnAsync("O", client, rng);

                    result = await client.CheckStateAsync(new Null { });

                    if (result.Result_ != "N")
                    {
                        break;
                    }
                } while (result.Result_ == "N");
            }//if player is X
            else                    //If the player chose to play O
            {
                result = await client.CheckStateAsync(new Null { });

                do
                {
                    await TurnAsync("X", client, rng);

                    result = await client.CheckStateAsync(new Null { });

                    if (result.Result_ != "N")
                    {
                        break;
                    }

                    await TurnAsync(Player, client);

                    result = await client.CheckStateAsync(new Null { });

                    if (result.Result_ != "N")
                    {
                        break;
                    }
                } while (result.Result_ == "N");
            }//else

            var board = await client.ShowBoardAsync(new Null { });

            Console.WriteLine(board.Board);

            // Generate and display winner messages
            switch (result.Result_)
            {
            case "X":
                Console.WriteLine("!!!!\t\t X wins the game\t\t!!!!");
                break;

            case "O":
                Console.WriteLine("!!!!\t\t O wins the game\t\t!!!!");
                break;

            case "D":
                Console.WriteLine("\t\t:( :( :(\tThe game is a draw.\t:( :( :(");
                break;
            }//switch

            await client.ResetAsync(new Null { });
        }//PlayGame