Ejemplo n.º 1
0
        private void ReadPlayerData(string filePath)
        {
            if (File.Exists(filePath))
            {
                StreamReader  reader = new StreamReader(filePath);
                List <string> lines  = ListOfLines(filePath);

                for (int lineCtr = 0; lineCtr < lines.Count; lineCtr++)
                {
                    MoveVector2 combination = ReturnCombination(lines[lineCtr]);

                    if (!combination.Equals(MoveVector2.EmptyVector))
                    {
                        playerMoveHistory.Add(combination, ReturnMoves(lines[lineCtr + 2]));
                    }
                }

                reader.Close();

                playerAiMoveCombination = playerMoveHistory.ReturnKeys()[0];
                //Skipping the study state and switching to predicting player moves
                states["Study"]            = false;
                states["Make Predictions"] = true;
            }
        }
Ejemplo n.º 2
0
        private MoveVector2 ReturnCombination(string line)
        {
            MoveVector2 combination = MoveVector2.EmptyVector;

            if (line.Length > 0)
            {
                if (line[0] == '(')
                {
                    string playerMoveName          = "";
                    Move   playerMove              = new Move();
                    bool   playerMoveInputRecieved = false;
                    string aiMoveName              = "";
                    Move   aiMove = new Move();

                    for (int a = 1; a < line.Length; a++)
                    {
                        if (line[a] != ',' && !playerMoveInputRecieved)
                        {
                            playerMoveName += line[a];
                        }
                        else
                        {
                            playerMoveInputRecieved = true;
                            if (line[a + 1] != ')')
                            {
                                aiMoveName += line[a + 1];
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    switch (playerMoveName.ToUpper())
                    {
                    case "ROCK": playerMove = Move.Rock; break;

                    case "PAPER": playerMove = Move.Paper; break;

                    case "SCISSOR": playerMove = Move.Scissor; break;
                    }

                    switch (aiMoveName.ToUpper())
                    {
                    case "ROCK": aiMove = Move.Rock; break;

                    case "PAPER": aiMove = Move.Paper; break;

                    case "SCISSOR": aiMove = Move.Scissor; break;
                    }

                    combination = new MoveVector2(playerMove, aiMove);
                }
            }

            return(combination);
        }
Ejemplo n.º 3
0
        public bool Equals(MoveVector2 vec1)
        {
            if (Move.MoveEquals(vec1.xComponent, xComponent) && Move.MoveEquals(vec1.yComponent, yComponent))
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 4
0
        private bool ListContainsCombination(List <MoveVector2> list, MoveVector2 combination)
        {
            foreach (MoveVector2 vec in list)
            {
                if (combination.Equals(vec))
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 5
0
        public void RecieveRoundResults(Move playerMove)
        {
            if (playerAiMoveCombination != null)
            {
                List <MoveVector2> keys = playerMoveHistory.ReturnKeys();

                foreach (MoveVector2 combination in keys) //Adding the move to the player's move history
                {
                    if (combination.Equals(playerAiMoveCombination))
                    {
                        playerMoveHistory[combination].Add(playerMove);
                    }
                }

                playerAiMoveCombination = new MoveVector2(playerMove, aiMove); //Updating the combination vector
                bool combinationAlreadyExists = false;

                foreach (MoveVector2 combination in keys) //Checking whether the combination already exists in the moves list
                {
                    if (combination.Equals(playerAiMoveCombination))
                    {
                        combinationAlreadyExists = true;
                        break;
                    }
                }

                if (!combinationAlreadyExists) //Adding the combination to the list, as it does not exist in the list
                {
                    playerMoveHistory.Add(playerAiMoveCombination, new List <Move>());
                }
            }
            else
            {
                playerAiMoveCombination = new MoveVector2(playerMove, aiMove);
                playerMoveHistory.Add(playerAiMoveCombination, new List <Move>());
            }
        }
Ejemplo n.º 6
0
        public void SavePlayerData()
        {
            string currentFileName = @"Past Player Data\" + playerName.ToUpper() + ".txt";

            if (!Directory.Exists(@"Past Player Data")) //Creating a new folder if it doesnt already exist
            {
                Directory.CreateDirectory(@"Past Player Data");
            }

            if (File.Exists(currentFileName)) //Checking whether the players file exists
            {
                StreamReader       reader       = new StreamReader(currentFileName);
                List <MoveVector2> combinations = playerMoveHistory.ReturnKeys();
                int lineCount = 0;

                //Updating the existing combinations
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    lineCount++;
                    MoveVector2 combination = ReturnCombination(line);

                    //Updating the existing combinations
                    if (!combination.Equals(MoveVector2.EmptyVector))
                    {
                        reader.Close();
                        if (ListContainsCombination(combinations, combination))
                        {
                            List <string> lines        = ListOfLines(currentFileName);
                            string        lineContents = "";

                            for (int a = 0; a < lines.Count; a++)
                            {
                                if (line == lines[a])
                                {
                                    lineContents = ReadFromLine(currentFileName, a + 2);

                                    foreach (MoveVector2 existingCombinations in combinations)
                                    {
                                        if (combination.Equals(existingCombinations))
                                        {
                                            List <Move> moves = playerMoveHistory[existingCombinations];

                                            foreach (Move move in moves)
                                            {
                                                lineContents += move.MoveName;
                                            }
                                        }
                                    }
                                }
                            }

                            WriteOnLine(currentFileName, lineCount + 2, lineContents);
                            break;
                        }
                    }
                }

                reader.Close();


                //Adding new combinations to the file
                List <string> linesInFile = ListOfLines(currentFileName);
                foreach (MoveVector2 combination in combinations)
                {
                    bool combinationExists = false;

                    for (int a = 0; a < linesInFile.Count; a++)
                    {
                        if (combination.Equals(ReturnCombination(linesInFile[a])))
                        {
                            combinationExists = false;
                            break;
                        }
                    }

                    if (!combinationExists)
                    {
                        string textCombination = "(" + combination.XComponent.MoveName + "," + combination.YComponent.MoveName + ")";
                        WriteOnLine(currentFileName, linesInFile.Count + 2, textCombination);
                        linesInFile = ListOfLines(currentFileName); //Updating the list of lines
                    }
                }
            }
            else
            {
                //Creating a new file and writing the players information to it
                StreamWriter       writer = new StreamWriter(currentFileName);
                List <MoveVector2> keys   = playerMoveHistory.ReturnKeys();

                foreach (MoveVector2 combination in keys)
                {
                    writer.WriteLine("");
                    writer.WriteLine("(" + combination.XComponent.MoveName + "," + combination.YComponent.MoveName + ")");
                    writer.WriteLine("/");

                    for (int a = 0; a < playerMoveHistory[combination].Count; a++)
                    {
                        writer.Write(playerMoveHistory[combination].ElementAt(a).MoveName + " ");
                    }

                    writer.WriteLine("");
                }

                writer.Close();
            }
        }