C++ ..code change .Look at the code bellow and try to change a little bit. The i
ID: 3567786 • Letter: C
Question
C++ ..code change .Look at the code bellow and try to change a little bit. The intention is to have two players. Player A and Player B. Give a change to choose Player A what is your name, and Player B, what is your name. Player A will flip a coin and player B must guess head or tail.... Delete the sequence of three. I only need either head or tail. If you think to design a different code than this go ahead...but I also need to keep track of the score, just like the end of this code.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdlib.h>
#define SEQLEN 3
int getseq(char *s)
{
int r = 0;
int i = 1 << (SEQLEN - 1);
while (*s && i) {
switch (*s++) {
case 'H':
case 'h':
r |= i;
break;
case 'T':
case 't':
/* 0 indicates tails, this is 0, so do nothing */
break;
default:
return -1;
}
i >>= 1;
}
return r;
}
void printseq(int seq)
{
int i;
for (i = SEQLEN - 1; i >= 0; --i)
printf("%c", seq & (1 << i) ? 'h' : 't');
}
int getuser(void)
{
int user;
char s[SEQLEN + 1];
printf("Enter your sequence of %d (h/t): ", SEQLEN);
while (1) {
/* This needs to be manually changed if SEQLEN is changed */
if (scanf("%3s", s) != 1) exit(1);
if ((user = getseq(s)) != -1) return user;
printf("Please enter only h/t characters: ");
}
}
int getai(int user)
{
int ai;
printf("Computers sequence of %d is: ", SEQLEN);
/* The ai's perfect choice will only be perfect for SEQLEN == 3 */
if (user == -1)
ai = rand() & (1 << SEQLEN) - 1;
else
ai = (user >> 1) | ((~user << 1) & (1 << SEQLEN - 1));
printseq(ai);
printf(" ");
return ai;
}
int rungame(int user, int ai)
{
/* Generate first SEQLEN flips. We only need to store the last SEQLEN
* tosses at any one time. */
int last3 = rand() & (1 << SEQLEN) - 1;
printf("Tossed sequence: ");
printseq(last3);
while (1) {
if (user == last3) {
printf(" User wins! ");
return 1;
}
if (ai == last3) {
printf(" Ai wins! ");
return 0;
}
last3 = ((last3 << 1) & (1 << SEQLEN) - 2) | (rand() & 1);
printf("%c", last3 & 1 ? 'h' : 't');
}
}
int main(void)
{
srand(time(NULL));
int playerwins = 0;
int totalgames = 0;
/* Just use ctrl-c for exit */
while (1) {
int user = -1;
int ai = -1;
printf(" ");
if (rand() & 1) {
ai = getai(user);
user = getuser();
}
else {
user = getuser();
ai = getai(user);
}
playerwins += rungame(user, ai);
totalgames++;
printf("You have won %d out of %d games ", playerwins, totalgames);
printf("================================= ");
}
return 0;
}
Explanation / Answer
Program code:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdlib.h>
#include <string>
#define SEQLEN 3
using namespace std;
//main function
int main()
{
//declare the variables
char tos;//to store the tossed value
//toss[] to store the characters h or t
char toss[]={'h', 't'};
//to maintain the winning
int win1=0, win2=0;
//to store the players name
string player1, player2;
//accept input from the player
cout<<"Enter player 1 name: ";
cin>>player1;
cout<<endl;
cout<<"Enter player 2 name: ";
cin>>player2;
cout<<endl;
//to play the game for 3 times
int count=0;
//to store the guess toss
char guess;
//loop upto three games
while(count<3)
{
//player 1 is tossing the coins
cout<<" Player 1 throws the toss. ";
tos=toss[rand()%2];//generating random toss
//player2 guesses the number
cout<<"Player 2 guess the toss: ";
cin>>guess;
cout<<"Tossed coin is: "<<tos;
cout<<" Gussed toss is: "<<guess<<endl;
//check whether who wins the toss
if(guess==tos)
{
cout<<player2<<" wins! ";
win2++;//increment when player2 wins
}
else
{
cout<<player1<< " wins! ";
win1++;//increment when player1 wins
}
count++;
}
//final results
cout<<" The final winner is: ";
if(win1>win2)
{
cout<<player1<<"!"<<endl;
cout<<player1<<" wins the matches "<<win1<<" out of "<<count;
}
else
{
cout<<player2<<"!"<<endl;
cout<<player2<<" wins the matches "<<win2<<" out of "<<count;
}
}
------------------------------------------------------------------------------------------------------------------------
Sample output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.