Example #1
0
        public void PlayAFullGameOfHumanPlayers()
        {
            MockConsoleInput(
                Scissors,
                Paper,

                Paper,
                Paper,

                Scissors,
                Paper);

            var game = AGame.Build();

            game.Play();

            Assert.Equal(
                @"Player 1 input (P,R,S):Scissors
Player 2 input (P,R,S):Paper
Player1Wins
Player 1 input (P,R,S):Paper
Player 2 input (P,R,S):Paper
Draw
Player 1 input (P,R,S):Scissors
Player 2 input (P,R,S):Paper
Player1Wins

Final score after 3 turns:
Player1Wins!!
 - 2 times Player1Wins 
 - 0 times Player2Wins
 - 1 times Draw
", _output.ToString());
        }
Example #2
0
        public override Solution <TMove> Solve(AGame <TMove> game, ANode <TMove> finalState)
        {
            Solution <TMove> r = null;

            int count = this.GetStepCount(game);

            for (int k = 0; k < count; k++)
            {
                var partFinalState = this.BuildSolutionStep(game, finalState, k + 1);

                base.FilterNode = this.BuildFilterNode(game, finalState, k + 1, partFinalState);
                Solution <TMove> partial = base.Solve(game, partFinalState);

                System.Diagnostics.Debug.WriteLine($"Finished step {k + 1} of {count}");
                if (partial.Last != null)
                {
                    game.State = partial.Last;
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("Could not reach exact solution, is puzzle feasible?");
                    return(r);
                }

                r += partial;
            }

            return(r);
        }
Example #3
0
        public void PlayAFullGameOfTwoTacticalPlayers()
        {
            var randomMoves = new [] {
                Scissors,
                Paper
            };

            var game = AGame
                       .WithNumberOfTurns(2)
                       .WithPlayer1("tactical")
                       .WithPlayer2("tactical")
                       .WithRandomMoves(randomMoves)
                       .Build();

            game.Play();

            Assert.Equal(
                @"Player 1 (Tactical CPU):S
Scissors
Player 2 (Tactical CPU):P
Paper
Player1Wins
Player 1 (Tactical CPU):R
Rock
Player 2 (Tactical CPU):S
Scissors
Player1Wins

Final score after 2 turns:
Player1Wins!!
 - 2 times Player1Wins 
 - 0 times Player2Wins
 - 0 times Draw
", _output.ToString());
        }
Example #4
0
        public Task PostAsync(IMessageChannel chan, string text, AGame sender)
        {
            var quizzAudio = (IAudioGame)sender;

            quizzAudio.GetNewProcess();
            var process = Process.Start(new ProcessStartInfo
            {
                FileName               = "ffmpeg",
                Arguments              = $"-hide_banner -loglevel fatal -i - -af volume=0.2 -f s16le -ac 2 -ar 48000 pipe:1",
                UseShellExecute        = false,
                RedirectStandardOutput = true,
                RedirectStandardInput  = true
            });

            quizzAudio.SetCurrentProcess(process);
            _ = Task.Run(async() =>
            {
                var stream = await StaticObjects.HttpClient.GetStreamAsync(text);
                await stream.CopyToAsync(process.StandardInput.BaseStream);
                process.StandardInput.Close();
            });
            _ = Task.Run(async() =>
            {
                using Stream output = process.StandardOutput.BaseStream;
                try
                {
                    await output.CopyToAsync(quizzAudio.GetAudioOutStream());
                }
                catch (OperationCanceledException)
                {
                    return;
                }
            });
            return(Task.CompletedTask);
        }
Example #5
0
        public async Task PostAsync(IMessageChannel chan, string text, AGame _)
        {
            try
            {
                var result = await StaticObjects.HttpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, text));

                var length = int.Parse(result.Content.Headers.GetValues("content-length").ElementAt(0));
                if (length < 8000000)
                {
                    await chan.SendFileAsync((await StaticObjects.HttpClient.GetAsync(text)).Content.ReadAsStream(), "image" + Path.GetExtension(text));
                }
                else // Too big to be sent on Discord
                {
                    await chan.SendMessageAsync(text);
                }
            }
            catch (Discord.Net.HttpException dne)
            {
                if (dne.DiscordCode == DiscordErrorCode.MissingPermissions)
                {
                    throw new GameLost("Missing permissions to send files");
                }
                else
                {
                    throw;
                }
            }
        }
Example #6
0
        public void GameIsFinishedWithoutWinner_WhenAllPositionsAreTakenAndNoPlayerHasTakenWinLinePositions()
        {
            var game = AGame.WithoutWinner();

            var result = game.Outcome();

            result.Should().Be("egal");
        }
Example #7
0
        public void AskForNextMove_WhenGameIsNotFinished()
        {
            var game = AGame.WhichIsNotFinished();

            var result = game.Outcome();

            result.Should().Be("next");
        }
Example #8
0
        public void ExceptionShouldBeThrown_WhenPlayerMakeMoveToFilledPosition(string activePlayer)
        {
            var game = AGame.WithFilledPosition(2, "X");

            Action action = () => game.Move(activePlayer, 2);

            action.ShouldThrow <InvalidOperationException>();
        }
Example #9
0
        public void ExceptionShouldBeThrown_WhenSamePlayerTakeMoreThanOneConsecutiveMoves()
        {
            var game = AGame.WithFilledPosition(1, "X");

            Action action = () => game.Move("X", 2);

            action.ShouldThrow <ArgumentException>();
        }
Example #10
0
        private async Task CheckGame(AGame game)
        {
            Type       type = game.GetType();
            MethodInfo info = type.GetMethod("GetPostAsync", BindingFlags.Instance | BindingFlags.NonPublic);

            foreach (string s in await(Task <string[]>) info.Invoke(game, null))
            {
                Assert.True(await IsLinkValid(s), "Invalid URL " + s);
            }
        }
Example #11
0
        public void PlayerShouldWin_WhenMakeMovesOnOneOfDiagonalLinePositions(string winnerPlayer,
                                                                              string expectedWinner,
                                                                              params int[] diagonalWinPositions)
        {
            var game = AGame.WithWinnerPlayer(winnerPlayer, diagonalWinPositions);

            var winner = game.Outcome();

            winner.Should().Be(expectedWinner);
        }
Example #12
0
        public async Task PostAsync(IMessageChannel chan, string text, AGame _)
        {
            var result = await StaticObjects.HttpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, text));

            var length = int.Parse(result.Content.Headers.GetValues("content-length").ElementAt(0));

            if (length < 8000000)
            {
                await chan.SendFileAsync(await StaticObjects.HttpClient.GetStreamAsync(text), "image" + Path.GetExtension(text));
            }
            else // Too big to be sent on Discord
            {
                await chan.SendMessageAsync(text);
            }
        }
Example #13
0
        public void CalculateScore(string player1Move, string player2Move, Score expectedScore)
        {
            MockConsoleInput(
                player1Move,
                player2Move);

            var game = AGame
                       .WithNumberOfTurns(1)
                       .Build();

            game.Play();


            Assert.Contains(
                $@"Final score after 1 turns:
{expectedScore}!!", _output.ToString());
        }
Example #14
0
        public void PlayAFullGameWithTwoRandomCpuPlayers()
        {
            var randomMoves = new [] {
                Scissors,
                Paper,

                Paper,
                Paper,

                Rock,
                Scissors
            };

            var game = AGame
                       .WithPlayer1("random")
                       .WithPlayer2("random")
                       .WithRandomMoves(randomMoves)
                       .Build();

            game.Play();

            Assert.Equal(
                @"Player 1 (Random CPU):S
Scissors
Player 2 (Random CPU):P
Paper
Player1Wins
Player 1 (Random CPU):P
Paper
Player 2 (Random CPU):P
Paper
Draw
Player 1 (Random CPU):R
Rock
Player 2 (Random CPU):S
Scissors
Player1Wins

Final score after 3 turns:
Player1Wins!!
 - 2 times Player1Wins 
 - 0 times Player2Wins
 - 1 times Draw
", _output.ToString());
        }
Example #15
0
        public void IgnoreInvalidUserInputAndRequestItAgain()
        {
            MockConsoleInput(
                "Invalid",
                Scissors,
                Paper);

            var game = AGame
                       .WithNumberOfTurns(1)
                       .Build();

            game.Play();

            Assert.Contains(
                @"Player 1 input (P,R,S):Player 1 input (P,R,S):Scissors
Player 2 input (P,R,S):Paper
Player1Wins", _output.ToString());
        }
Example #16
0
        void UpdateMenuInfo()
        {
            AGame game = ROM.Instance.Game;

            codeLabel.Text    = game.Code;
            gameLabel.Text    = game.Name;
            creatorLabel.Text = game.Creator;

            tableNumerical.Maximum = game.SongTables.Length - 1;
            tableNumerical.Value   = 0;
            tableNumerical.Visible = game.SongTables.Length > 1;
            SetSongMaximum();
            PopulatePlaylists(game.Playlists);

            openMIDIToolStripMenuItem.Enabled = openASMToolStripMenuItem.Enabled =
                teToolStripMenuItem.Enabled   = vteToolStripMenuItem.Enabled = eSf2ToolStripMenuItem.Enabled = eASMToolStripMenuItem.Enabled = eMIDIToolStripMenuItem.Enabled =
                    songsComboBox.Enabled     = songNumerical.Enabled = playButton.Enabled = true;
        }
Example #17
0
        public void PlayAFullGameWithMixOfHumanAndRandomCpuPlayers()
        {
            MockConsoleInput(
                Scissors,
                Paper,
                Rock
                );
            var randomMoves = new [] {
                Paper,
                Paper,
                Scissors
            };

            var game = AGame
                       .WithPlayer1("human")
                       .WithPlayer2("random")
                       .WithRandomMoves(randomMoves)
                       .Build();

            game.Play();


            Assert.Equal(
                @"Player 1 input (P,R,S):Scissors
Player 2 (Random CPU):P
Paper
Player1Wins
Player 1 input (P,R,S):Paper
Player 2 (Random CPU):P
Paper
Draw
Player 1 input (P,R,S):Rock
Player 2 (Random CPU):S
Scissors
Player1Wins

Final score after 3 turns:
Player1Wins!!
 - 2 times Player1Wins 
 - 0 times Player2Wins
 - 1 times Draw
", _output.ToString());
        }
Example #18
0
        public virtual Solution <TMove> Solve(AGame <TMove> game, ANode <TMove> finalState)
        {
            this.graph = new Graph <TMove>(game.State);
            this.game  = game;

            // Le noeud passé en paramètre est supposé être le noeud initial
            var N = game.State;

            this.graph.Opened.Add(N);

            // tant que le noeud n'est pas terminal et que ouverts n'est pas vide
            while (this.graph.Opened.Count != 0 && !finalState.Equals(N))
            {
                // Le meilleur noeud des ouverts est supposé placé en tête de liste
                // On le place dans les fermés
                this.graph.Opened.Remove(N);
                this.graph.Closed.Add(N);

                // Il faut trouver les noeuds successeurs de N
                this.UpdateSuccessors(N, finalState);
                // Inutile de retrier car les insertions ont été faites en respectant l'ordre

                // On prend le meilleur, donc celui en position 0, pour continuer à explorer les états
                // A condition qu'il existe bien sûr
                if (this.graph.Opened.Count > 0)
                {
                    N = this.graph.Opened[0];
                }
                else
                {
                    N = null;
                }
            }

            this.graph.Finish(N);
            return(Solution <TMove> .BuildPathFrom(this.graph));
        }
Example #19
0
 protected override ANode <TaquinGame.Move> BuildSolutionStep(AGame <TaquinGame.Move> gameRef, ANode <TaquinGame.Move> targetState, int n)
 {
     this.filter = this.saved ?? this.Build(gameRef as TaquinGame, targetState as TaquinNode, 0);
     this.saved  = this.Build(gameRef as TaquinGame, targetState as TaquinNode, n);
     return(this.saved);
 }
Example #20
0
 protected override ANode <TaquinGame.Move> BuildFilterNode(AGame <TaquinGame.Move> gameRef, ANode <TaquinGame.Move> targetState, int n, ANode <TaquinGame.Move> currentSolutionStep)
 {
     return(this.filter);
 }
Example #21
0
        protected override int GetStepCount(AGame <TaquinGame.Move> game)
        {
            int gameSize = (game as TaquinGame).Size;

            return(2 * gameSize - 3);
        }
Example #22
0
 protected virtual ANode <TMove> BuildFilterNode(AGame <TMove> gameRef, ANode <TMove> targetState, int n, ANode <TMove> currentSolutionStep)
 {
     return(null);
 }
Example #23
0
 /// <summary>
 /// Génère la grille de l'étape <paramref name="n"/>.
 /// </summary>
 /// <param name="targetState">Etat final ciblé.</param>
 /// <param name="n">Numéro de l'étape.</param>
 /// <returns></returns>
 protected abstract ANode <TMove> BuildSolutionStep(AGame <TMove> gameRef, ANode <TMove> targetState, int n);
Example #24
0
 public async Task PostAsync(IMessageChannel chan, string text, AGame _)
 {
     await chan.SendMessageAsync(text);
 }
Example #25
0
 /// <summary>
 /// Nombre d'étapes soit le plus grand nombre qu'acceptera la fonction <see cref="BuildSolutionStep(ANode{TMove}, int)"/>
 /// </summary>
 /// <param name="game"></param>
 /// <returns></returns>
 protected abstract int GetStepCount(AGame <TMove> game);