answersLogoWhite

0


Best Answer

The following example demonstrates a simple implementation using 15 matches where you take turns removing 1, 2 or 3 matches. The player who removes the last match loses.

In order to ensure the computer always wins, the computer must always play first. In the example, the player always starts but unless the player knows the secret, the computer will always win.

#include<iostream>

#include<conio.h>

unsigned ask(unsigned max=3)

{

int choice = 0;

do

{

choice = getch() - '0';

if (choice<0 choice>max)

choice=0;

else

{

std::cout<<choice<<'\n'<<std::endl;

break;

}

} while (!choice);

return ((unsigned)choice);

}

bool yn()

{

int choice = 0;

bool result = false;

while(1)

{

choice = getch();

if (choice=='y' choice=='Y')

{

result = true;

break;

}

if (choice=='n' choice=='N')

{

result = false;

break;

}

}

std::cout<<(char)choice<<'\n'<<std::endl;

return (result);

}

int main()

{

unsigned start = 15;

unsigned matches = start;

bool player = true; // set to false so computer always starts

unsigned take = 0;

while (1)

{

std::cout<<"There "<<(matches==1?"is":"are")<<" "<<matches<<" match"<<(matches==1?"":"es")<<" remaining.\n"<<std::endl;

if( matches <= 1 )

{

if ((player && matches == 1) (!player && !matches))

std::cout<<"You lose!\n"<<std::endl;

else

std::cout<<"You win!\n"<<std::endl;

std::cout<<"Would you like to play again? ";

if (!yn())

break;

else

{

matches = start;

continue;

}

}

if( player ) // player's turn

{

std::cout<<"Would you like to take 1";

if( matches>2 )

{

std::cout<<", 2 or 3 matches? ";

take = ask( 3 );

}

else

{

std::cout<<"or 2 matches? ";

take = ask( 2 );

}

}

else // computer's turn

{

take = (matches-1)%4;

if( !take )

take = rand()%3+1;

std::cout<<"I will take "<<take<<" matches.\n"<<std::endl;

}

matches -= take;

player = !player; // toggle player

}

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program for a matchstick game being played between the computer and a user What if your program should ensure that the computer always wins using c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

A computer always does exactly what you program it to?

Correct


What is difference between client server and web server?

client site program always execute on client computer while server site program execute on sever computer


Write a matchstick program to be played between a computer and the user but the computer must win the game?

The Matchstick program below uses the following rules: - Starts with 21 matchsticks - each player must remove 1 - 4 matchsticks - The player who takes the last stick loses. The algorithm is this: To always win you must: - Let your opponent go first. - on your turn, take enough sticks that your take plus your opponent's take equal 5 sticks. This program is written in Java, and uses the above algorithm: import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.InputStream; public class Matchstick { static int sticksLeft = 21; public static void main(String[] args) { String input; int humanChoice; int computerChoice; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Welcome To Matchsticks\n\n" + "The Rules:\n" + " - There are 21 Matchsticks\n" + " - Each player takes 1, 2, 3, or 4 matchsticks on their turn\n" + " - The person who takes the last matchstick loses the game\n\n" + "The computer has graciously let you pick first"); do { try { System.out.print("How many sticks do you take: "); System.out.flush(); input = in.readLine(); humanChoice = Integer.parseInt(input); if (humanChoice &gt; 4 humanChoice &lt; 1) { System.out.println("Please choose a number between 1 and 4\n"); } else { sticksLeft = sticksLeft - humanChoice; System.out.println("You chose to remove " + humanChoice + " Matchsticks. There are " + sticksLeft + " Matchsticks Left"); computerChoice = 5 - humanChoice; sticksLeft = sticksLeft - computerChoice; System.out.println("I chose to remove " + computerChoice + " Matchsticks. There's " + sticksLeft + " Matchstick(s) Left\n"); } } catch (Exception e) { System.out.println("Please enter a numeric value\n"); } } while (sticksLeft != 1); System.out.println("You take the last Matchstick, The computer wins!"); System.exit(0); } }


What computer program did Ada Lovelace invent?

She was tutored by Mary Somerville in science and she knew that she wanted to invent a computer program so her mother always told her to follow her dreams.


Write a program for a matchstick game being played between the computer and a user What if your program should ensure that the computer always wins Rules for the game are as follows?

-There are 21 matchsticks.-Whoever is forced to pick up the last matchstick loses the game.So the last one is special, so it's all about how to get rid of 20 matches in pairs of turns.-The computer asks the player to pick 1, 2, 3 or 4 matchsticks.So if we reduce the total by 5 each round, the sequence will go21 16 11 6 1In effect, whatever number the user picks (n), the computer picks 5-n'''#include'''main()'''{''''''int matchsticks=21, user, computer;printf("Do not enter Invalid Numbers.\nNumbers above 4 are invalid.");printf("\nIf you do so, the computer automatically wins.");'''while (matchsticks>=1)'''{'''printf("\nNumber of matchsticks available right now is %d.", matchsticks);printf("\n\nYour Turn...\n\n\n");printf("\nPick up the matchstick(s)-- (1-4): ");scanf ("%d", &user);'''if (user>4)'''{'''printf("Invalid Selection");'''break;'''}'''computer=5-user;printf("\nComputer's Turn..\n" );printf("\nComputer chooses:%d", computer);matchsticks=matchsticks-user-computer;'''continue;'''if(matchsticks==1)'''break;'''}'''matchsticks--;printf("\nComputer Wins");'''}''''''''''''''''''''''''


Where can people get gimp tutorials?

Gimp is a computer art design program in which showing depth and an almost 3D scene is possible. The program is not too much more difficult than the Paint program that come on every PC computer. The differences include the possibilities you can create, the schema of the edges of the program being its own measurement tool to always allow you to know comparison depth and relativity between items in the pictures.


Which version of Microsoft office is the best for a new computer user?

A new computer should always run the latest of any program. That being said, a new computer should obviously run Windows Office 2013. This newer office program will ensure they get the most out of their new computer.


How can you delete a program that wont delete?

One should not delete a program,programs can never be deleted they should always be uninstalled.Deleting a program can never help because if you delete a program u will not see it there but it is used by the computer or system.When you uninstall a program it is totally removed from the computer and is never used again unless u install it.


What is a good program to use to defragment your Windows computer rather than the inbuilt Windows defragmenter?

I own a computer repair company and have always used System Mechanic.


What do you do if you get a virus on your computer?

install AVG free edition and allow it to run a system scan to remove it. it is a good idea to always have a virus removing program on your computer, Just install AVG free and leave it on your computer.


What is the deference between apple computer and others computer?

Apple has the combination between hardware and software, this makes the computer faster and easier to use. Also the mac OS is an original they are always ahead of the game.


Do you use the add and remove program application to unistall a program?

Always use Add Remove or the application's uninstaller program to perform an uninstall. By doing so, it permits the computer to completely remove all parts of the program/application, and revise the Registry by itself.