Метод не возвращает случайное число из другого метода - PullRequest
0 голосов
/ 02 февраля 2020

Трудно было найти правильное название для этой проблемы. Раньше у меня была небольшая проблема с этой игрой в кости, и я решил ее, спасибо ребятам из Stackoverflow.

Моя игра сейчас готова на 99%, но почему-то основная программа не может вернуть номера игральных костей. Эта игра предназначена, чтобы сломаться, когда у игрока есть три победы, и это делает его победителем. Вот почему есть раунд.

Консоль должна выглядеть следующим образом:

Имя игрока 1:

Имя игрока 2:

Раунд 1

"Игрок 1 имя ": 5 + 4 = 9

" Имя игрока 2 ": 1 + 2 = 3

Раунд 2 и так далее

И когда игрок получает три победы, это победитель. У меня также есть два других класса, но проблема в игре в кости, поэтому я не прикрепляю другие здесь.

Класс игры

using System;
using System.Collections.Generic;
using System.Text;
using static System.Console;

namespace Dicegame
{
    static class Game
    {
        static int winline = 3;

        static int round = 1;

        public static void Aja()
        {
            Player p1 = new Player
            (
                Helpers.Syote.Merkkijono("Name of player 1: ")
            );
            Player p2 = new Player
            (
                Helpers.Syote.Merkkijono("Name of player 2: ")
            );

            Dice dice1 = new Dice();
            Dice dice2 = new Dice();


            for (int i = 1; p1.Points < winline | p1.Points < winline; i++)
            {
                int p1throw1 = dice1.Throw(), p1throw2 = dice2.Throw(), p1total = p1throw1 + p1throw2,
                    p2throw1 = dice1.Throw(), p2throw2 = dice2.Throw(), p2total = p2throw1 + p2throw2;

                round++;
            }
        }
    }
}

Класс Dice

using System;
using System.Collections.Generic;
using System.Text;
using static System.Random;

namespace Dicegame
{
    class Dice
    {
        static Random randomnumber = new Random();

        public int Count { get; set; }
        public int ThrowCount { get; set; }

        public Dice()
        {
            ThrowCount = 0;
        }




        public int Throw()
        {

            int Count = randomnumber.Next(1, 7);
            ThrowCount++;

            return Count;
        }

    }
}

Ответы [ 2 ]

1 голос
/ 02 февраля 2020

Вы просто никогда не реализуете точки P1 или P2.
Также вы используете a для l oop, где простое время l oop будет работать лучше:

while(p1.Points < winline || p2.Points < winline)
{
    p1.AddPoints(dice1.Throw() + dice2.Throw());
    p2.AddPoints(dice1.Throw() + dice2.Throw());
    round++;
}
0 голосов
/ 23 марта 2020

Я выкладываю свое решение этого, я надеюсь, что это поможет кому-то в будущем. Прокомментировал все, чтобы было легко понять и новичкам.

do 
    {
        if (p1.Points == WINLINE || p2.Points == WINLINE)
        {
            break; // Break this loop when either one reach WINLINE points (3)
        }
        else
        {
                // Get random numbers for each throw between 1-6 and sum them together
            int p1throw1 = dice1.Throw(), p1throw2 = dice2.Throw(), p1total = p1throw1 + p1throw2,

                // Get random numbers for each throw between 1-6 and sum them together
                p2throw1 = dice1.Throw(), p2throw2 = dice2.Throw(), p2total = p2throw1 + p2throw2; 

            // Write round number which is how many times dice is thrown. Split with 2, because same dice is throwed two times in one round
            WriteLine($"Round {dice1.ThrowCount / 2}");

            // Write given player names and points of each throw and after all sum of those points
            WriteLine($"{p1.Name}: {p1throw1} + {p1throw2} = {p1total}");
            WriteLine($"{p2.Name}: {p2throw1} + {p2throw2} = {p2total}");

            // If player 1 get more points than player 2, add one point for player 1 and reset player 2 points
            if (p1total > p2total)
            {
                p1.Points++;
                p2.Points = 0;
            }

            // If player 2 get more points than player 1, add one point for player 2 and reset player 1 points
            else if (p1total < p2total)
            {
                p2.Points++;
                p1.Points = 0;
            }
                else // If something else (same points for both) do nothing
            {

        }
    }
}
while (true);

// When do loop broke print results of the game
if (p1.Points == WINLINE) // Print this if player 1 wins
{
    WriteLine($"Winner of the game is {p1.Name} and total rounds: {dice1.ThrowCount / 2}."); 
}
else if (p2.Points == WINLINE) // Print this if player 2 wins
{
    WriteLine($"Winner of the game is {p2.Name} and total rounds: {dice1.ThrowCount / 2}");

}
...