Home > Back-end >  Novice c programming for help request in class
Novice c programming for help request in class

Time:11-30

Rock, scissors, cloth: to design a game, let the user and the computer to play "stone, cutting knife, cloth" game, requirements: (1) the number of statistics program can play as well as the result of every time, that is who won, also is flat; (2) the computer play the most simple way is random algorithm, please try to consider can set up an analysis of the user card rule algorithm, for greater than 50% chance to win the user's computer,

CodePudding user response:

After the game more, can analyze the user's preferences, such as like what

CodePudding user response:

Players play probability simple judgment, if a careful analysis of the player's behavior, rule, is in trouble,

#include
#include

//rock-paper-scissors
Enum
{
ACTION_MIN,
ACTION_ROCK,
ACTION_SCISSORS,
ACTION_PAPER,
ACTION_MAX,
};

The static char * s_lpActionName [ACTION_MAX]=
{
""," stone ", "scissors", "cloth,"
};

Void RockScissorsPaper (void)
{
Int nRockCount=0;
Int nScissorCount=0;
Int nPaperCount=0;
Int nTotalGame=0;

Int nDrawGame=0;
Int nPlayerWin=0;
Int nComputerWin=0;

Srand (GetTickCount ()/2);

Int nPlayerAction=0;
Int nComputerAction=0;
While (true)
{
If (nTotalGame & lt;
=6){
NComputerAction=rand () % 3 + 1;
}
The else
{
Int nRandom=rand () % nTotalGame;

If (nRandom & lt; NRockCount)
NComputerAction=ACTION_PAPER;
Else if (nRandom & lt; NRockCount + nScissorCount)
NComputerAction=ACTION_ROCK;
The else
NComputerAction=ACTION_SCISSORS;
}

Rock printf (" (1 -, 2 - scissors, 3 - cloth] please enter: ");
If (scanf_s (" % d ", & amp; NPlayerAction)!
=1)break;

If (nPlayerAction & lt;=ACTION_MIN | | ACTION_MAX & lt;=nPlayerAction)
break;

If (nPlayerAction==ACTION_ROCK)
+ + nRockCount;
Else if (nPlayerAction==ACTION_SCISSORS)
+ + nScissorCount;
The else
+ + nPaperCount;

+ + nTotalGame;


Char const * lpResult=NULL;
If (nPlayerAction==nComputerAction)
{
+ + nDrawGame;
LpResult="draw";
}
Else if (nPlayerAction % 3 + 1==nComputerAction)
{
+ + nPlayerWin;
LpResult="players victory";
}
The else
{
+ + nComputerWin;
LpResult="computer victory";
}

VS printf (" % s % s: % s \ r \ n ", s_lpActionName [nPlayerAction], s_lpActionName [nComputerAction], lpResult);
}

Printf (" game over! \r\n");
Printf (" statistics: \ r \ n ");

Printf (" the game number: % d times, winning players: % d times, computer victory: % d times, draw: % d \ r \ n ", nTotalGame, nPlayerWin, nComputerWin, nDrawGame);

Float fRock=0;
Float fScissor=0;
Float fPaper=0;
If (nTotalGame!=0)
{
FRock nRockCount * 100.0=f/nTotalGame;
FScissor nScissorCount * 100.0=f/nTotalGame;
FPaper nPaperCount * 100.0=f/nTotalGame;
}

Printf (" players out of the stone probability: % 2 f % %, players with scissors probability: %, % % 2 f, the probability of players out of the cloth: %, 2 f % % \ r \ n ", fRock, fScissor, fPaper);
Printf (" computer cloth probability: % 2 f % %, computer stone probability: %, % % 2 f, the player with scissors probability: %, 2 f % % \ r \ n ", fRock, fScissor, fPaper);
}

Int main ()
{
RockScissorsPaper ();

system("pause");

return 0;
}

CodePudding user response:

According to probability, the probability of every time the user out of the stone scissors cloth is the same, is a third
But according to the characteristics of the people;
People tend to the less out before their own; So add a statistics; Computer beat the probability is quite high,

Of course if you meet a fool, always out of the same type, the above algorithm is useless,

Can be combined with multiple algorithm,,

CodePudding user response:

refer to the original poster weixin_49042810 response:
stone, scissors, cloth: to design a game, let the user and the computer to play "stone, cutting knife, cloth" game, requirements: (1) the number of statistics program can play as well as the result of every time, that is who won, also is flat; (2) the computer play the most simple way is random algorithm, please try to consider can set up an analysis of the user card rule algorithm, for greater than 50% chance to win the user's computer,

Problems of the building Lord smack of artificial intelligence

CodePudding user response:

https://blog.csdn.net/wyyy2088511/article/details/107717570 code scissors stone cloth
  • Related