#I think I am doing rng wrong

15 messages · Page 1 of 1 (latest)

barren valve
#

Hello everyone!
I have been working on making a portfolio of projects as an exercise to improve my skills with cpp.
I coded this simple rock paper scissors games this morning.
The code runs fine, its just that the rng seems to be completely of, with the player always losing.
I think that I am either doing rng wrong, or have an error in my logic that I have not spotted.
Thanks in advance,
I2P

#include<random>
#include<iostream>

void round()
{
int selection;
std::cin>>selection;

std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type>dist3(1,3);

bool win=false;
int opponent=dist3(rng);

if(selection==opponent)
{
    std::cout<<"tie!"<<std::endl;
}
else
{
    switch(selection)
    {
        case 1:
            if(opponent==3)
            {
                std::cout<<"you win!"<<std::endl;
                win=true;
            }

        case 2:
            if(opponent==1)
            {
                std::cout<<"you win!"<<std::endl;
                win=true;
            }

        case 3:
            if(opponent==2)
            {
                std::cout<<"you win!"<<std::endl;
                win=true;
            }
    }
    if(!win)
    {
        std::cout<<"you lose!"<<std::endl;
    }
}

}

int main()
{
while(true)
{
round();
}
return 0;
}

velvet hollowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

velvet hollowBOT
#

Hello everyone!
I have been working on making a portfolio of projects as an exercise to improve my skills with cpp.
I coded this simple rock paper scissors games this morning.
The code runs fine, its just that the rng seems to be completely of, with the player always losing.
I think that I am either doing rng wrong, or have an error in my logic that I have not spotted.
Thanks in advance,
I2P

#include <iostream>
#include <random>

void round() {
  int selection;
  std::cin >> selection;

  std::random_device dev;
  std::mt19937 rng(dev());
  std::uniform_int_distribution<std::mt19937::result_type> dist3(1, 3);

  bool win = false;
  int opponent = dist3(rng);

  if (selection == opponent) {
    std::cout << "tie!" << std::endl;
  } else {
    switch (selection) {
      case 1:
        if (opponent == 3) {
          std::cout << "you win!" << std::endl;
          win = true;
        }

      case 2:
        if (opponent == 1) {
          std::cout << "you win!" << std::endl;
          win = true;
        }

      case 3:
        if (opponent == 2) {
          std::cout << "you win!" << std::endl;
          win = true;
        }
    }
    if (!win) {
      std::cout << "you lose!" << std::endl;
    }
  }
}

int main() {
  while (true) {
    round();
  }
  return 0;
}
I2PTHEGREAT
calm vine
#

, with the player always losing.

barren valve
#

Sorry my mistake, meant to say never losing

velvet hollowBOT
barren valve
#

So is it just rng being weird or did I make a mistake in my code?

calm vine
#

wait

#

how did i even miss that

#

you not use break anywhere

#

xDDD

wanton lance
# barren valve Hello everyone! I have been working on making a portfolio of projects as an exer...

The error is in the switch statement. You need to break; at the end of each switch statement, like this:

        switch(selection)
        {
            case 1:
                if(opponent==3)
                {
                    std::cout<<"you win!"<<std::endl;
                    win=true;
                }
                break;
            case 2:
                if(opponent==1)
                {
                    std::cout<<"you win!"<<std::endl;
                    win=true;
                }
                break;
            case 3:
                if(opponent==2)
                {
                    std::cout<<"you win!"<<std::endl;
                    win=true;
                }
                break;
          }

Otherwise, when a case statement matches, all case statements after that will execute as well. For example if the user choice a value of 1, it will set win to true if the opponent has value 1, 2 or 3 (aka, all cases).

barren valve
#

!solved

velvet hollowBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity