Example #1
0
        private void buttonCreateGame_Click(object sender, EventArgs e)
        {
            string startWord = textBoxStartWord.Text.ToUpper();

            if (!wordBase.Contains(startWord))
            {
                MessageBox.Show("Такого слова нет в базе!");
                return;
            }
            int n = playerTypes.Length;
            int m = startWord.Length;

            if (m % 2 == 0)
            {
                MessageBox.Show("Нельзя выбирать слова с чётным количеством букв.");
                return;
            }
            if (!(m * (m - 1) % n == 0))
            {
                MessageBox.Show("С таким словом у игроков будет неодинаковое количество ходов.");
                return;
            }
            List <string> realPlayerNames = new List <string>();
            List <Player> players         = new List <Player>();

            GamingForm gamingForm = new GamingForm(users);

            gamingForm.Owner = this.Owner;

            for (int i = 0; i < playerTypes.Length; ++i)
            {
                if (playerTypes[i].SelectedIndex == 0)
                {
                    string realPlayerName = playerNames[i].Text;
                    if (realPlayerName == "")
                    {
                        MessageBox.Show("Вы не ввели имя игрока!");
                        return;
                    }

                    if (!realPlayerNames.Contains(realPlayerName))
                    {
                        realPlayerNames.Add(realPlayerName);
                    }
                    else
                    {
                        MessageBox.Show("Вы ввели одно или несколько одинаковых имён реальных игроков");
                        return;
                    }

                    players.Add(new Player(new HumanStrategy(gamingForm),
                                           realPlayerName,
                                           playerColors[i],
                                           0));
                }
                else
                {
                    switch (playerTypes[i].SelectedIndex)
                    {
                    case 3:
                    {
                        players.Add(new Player(new ComputerStrategy(StrategyStrength.Hard, gamingForm),
                                               "Сильный ИИ " + computerPlayerNames[i],
                                               playerColors[i],
                                               0));
                    }
                    break;

                    case 2:
                    {
                        players.Add(new Player(new ComputerStrategy(StrategyStrength.Medium, gamingForm),
                                               "Средний ИИ " + computerPlayerNames[i],
                                               playerColors[i],
                                               0));
                    }
                    break;

                    case 1:
                    {
                        players.Add(new Player(new ComputerStrategy(StrategyStrength.Easy, gamingForm),
                                               "Слабый ИИ " + computerPlayerNames[i],
                                               playerColors[i],
                                               0));
                    }
                    break;
                    }
                }
            }
            foreach (string realPlayerName in realPlayerNames)
            {
                if (!users.ContainsKey(realPlayerName))
                {
                    users.Add(realPlayerName, defaultRating);
                }
            }
            // contruct "Game" object somewhere there
            Rules rules = new Rules();

            rules.AllowDiagonal      = checkBoxAllowDiagonal.Checked;
            rules.AllowIntersections = checkBoxAllowIntersections.Checked;
            if ((int)numericUpDownTimeLimit.Value != 0)
            {
                rules.HasTimeLimit = true;
                rules.TimeLimit    = (int)numericUpDownTimeLimit.Value;
            }
            rules.AllowRepeats = checkBoxAllowRepeats.Checked;
            Game game = new Game(startWord, players, rules, wordBase, gamingForm);

            gamingForm.Game = game;
            gamingForm.Show();
            this.Close();
        }
Example #2
0
        private void processPlayer(Player player, int currentIndex)
        {
            timeIsUp = false;
            bool correctEndTurn = false;
            Move move           = new Move();

            //time = new TimeSpan(0, Rules.TimeLimit, 0);
            time = new TimeSpan(0, Rules.TimeLimit, 0);
            gamingForm.updateTimer(time);
            MessageBox.Show("Ходит игрок " + player.Name);
            gamingForm.updatePlayersScore(Players, currentIndex);

            do
            {
                if (timeIsUp == false)
                {
                    gamingForm.updateForm(State, Rules);
                    player.Strategy.move(State, ref move, Rules);
                }
                else
                {
                    move.Action = ActionType.PassTurn;
                }
                switch (move.Action)
                {
                case ActionType.EnterLetter:
                {
                    int x = move.X, y = move.Y;
                    int width = State.Field.GetLength(0);
                    if (x < 0 || x >= width || y < 0 || y >= width)
                    {
                        // indices out of range
                        break;
                    }
                    move.Letter = Char.ToUpper(move.Letter);
                    if (!isCapitalRussianLetter(move.Letter))
                    {
                        // incorrect character
                        break;
                    }
                    if (State.NewX != -1 && State.NewY != -1)
                    {
                        // user already entered new letter
                        break;
                    }
                    State.NewX        = x;
                    State.NewY        = y;
                    State.Field[y, x] = move.Letter;
                }
                break;

                case ActionType.SelectLetter:
                {
                    int  wordLength = State.X.Count;
                    int  x = move.X, y = move.Y;
                    bool isCorrect = true;
                    int  width     = State.Field.GetLength(0);
                    if (x < 0 || x >= width || y < 0 || y >= width)
                    {
                        // indices out of range
                        break;
                    }
                    char nullLetter = '\0';
                    if (State.Field[y, x] == nullLetter)
                    {        //user must select only letters;
                        break;
                    }
                    if (wordLength == 0)
                    {
                        State.X.Add(x);
                        State.Y.Add(y);
                        break;
                    }
                    if (!Rules.AllowIntersections)
                    {
                        for (int i = 0; i < wordLength; i++)
                        {
                            if (State.X[i] == x && State.Y[i] == y)
                            {
                                //new coordinate intersects with the word
                                isCorrect = false;
                                break;
                            }
                        }
                        if (!isCorrect)
                        {
                            break;
                        }
                    }
                    if (Rules.isNeighbours(x, y, State.X[wordLength - 1], State.Y[wordLength - 1]))
                    {
                        State.X.Add(x);
                        State.Y.Add(y);
                    }
                    else
                    {
                        break;
                    }
                }
                break;

                case ActionType.EndTurn:
                {
                    bool   foundLetter = false;
                    string word        = "";
                    //check if the word contains the newly enter letter
                    if (State.NewX == -1 && State.NewY == -1)
                    {
                        MessageBox.Show("Введите букву");
                        break;
                    }

                    if (State.X.Count == 0 && State.Y.Count == 0)
                    {
                        MessageBox.Show("Выберите слово");
                        break;
                    }

                    for (int i = 0; i < State.X.Count; i++)
                    {
                        if (State.X[i] == State.NewX && State.Y[i] == State.NewY)
                        {
                            foundLetter = true;
                            break;
                        }
                    }

                    if (!foundLetter)
                    {
                        MessageBox.Show("Слово не содержит поставленную букву");
                        resetTurn();
                        break;
                    }

                    for (int i = 0; i < State.X.Count; i++)
                    {
                        int x = State.X[i];
                        int y = State.Y[i];
                        word += State.Field[y, x];
                    }

                    if (!wordBase.Contains(word))
                    {
                        MessageBox.Show(String.Format("База не содержит слова - {0}", word));
                        resetTurn();
                        break;
                    }

                    if (State.ProhibitedWords.Contains(word))
                    {
                        MessageBox.Show("Cлово " + word + " уже было");
                        resetTurn();
                        break;
                    }

                    State.ProhibitedWords.Add(word);

                    player.Score += word.Length;

                    State.NewX = -1;
                    State.NewY = -1;
                    State.X.Clear();
                    State.Y.Clear();

                    ListViewItem item = new ListViewItem(word);
                    item.ForeColor = player.PlayerColor;
                    gamingForm.addNewWord(item);
                    consequtiveTurnPasses = 0;
                    correctEndTurn        = true;
                    return;
                }
                break;

                case ActionType.Reset:
                {
                    resetTurn();
                }
                break;

                case ActionType.PassTurn:
                {
                    consequtiveTurnPasses++;
                    resetTurn();
                    return;
                }
                break;
                }
            }while (!((move.Action == ActionType.EndTurn && correctEndTurn == true) || move.Action == ActionType.PassTurn));
            // if the user chose non-existing word, we should ask him for another word instead of exitting
            // the very last turn will not be shown otherwise
            gamingForm.updatePlayersScore(Players, currentIndex);
        }