answersLogoWhite

0


Best Answer

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 > 4 humanChoice < 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); } }

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a matchstick program to be played between a computer and the user but the computer must win the game?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Computer Science

How many different genres are there of computer games?

There is no formal definition of game genres for computer games because the data cannot be compiled. Many modern computer games do not need physical software installation to be sought out and played.


What types of computer poker game are there?

Texas Hold'em, World Series of Poker, Five Card Draw, and Video Poker are all popular computer poker games. All of these games can be played free online.


What are the benefits of using computer aided design?

CAD allows for testing designs and ideas before you build them. For example, I am in a robotics club within my school, and we always draw pieces of the robot in CAD before we start building. By doing this we can see whether any of the parts will conflict, where excess stress is placed on the machine, and others, depending on the program used. I like to use Autodesk Inventor Pro, but I have also played around with SolidWorks and PTC Creo


Can PC games be played on anything besides a computer?

This depends on the game. Games created with cross-platform code, like Java or Flash, can run on any supporting device. Otherwise, multiple versions of a game have to made to work on different platforms or operating systems.


Does Movie Star Planet slow down computers?

Yes it certainly does. my daughter plays it on my computer and I notice it is next to impossible to use Internet after she has been on it. my wife's computer ( an apple) has actually crashed its hard disk but it was of no use anyway since moviestarplanet was played on it. really it was of no use before but msp definitely made it even more annoyingly slow. a good friend of mine has similar problems since his childeren play msp.

Related questions

The CD that played in the playstation can br played in the computer?

No not without an emulator program and PS1 bios


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");'''}''''''''''''''''''''''''


What 1960's song is played for the Target store commercial?

Pictures of Matchstick Men by Status Quo.


What is the difference between a DVD and a data DVD?

In short, Data DVD only can be played in computer, and DVD can be played on both DVD play and computer.


Who is Freddie in iCarly?

Fredward Benson otherwise known as Freddie plays carlys friend in the program he is a computer whizz and is played by nathan kress :)


Who played the little match stick girl in the 1987 film?

First of all it is the Match Girl not matchstick girl, secondly it was a cartoon. A rarity of rarities, a tragedy cartoon.


What is the name of the song played in the Payless Endless Summer Commercial 2011?

I just found it. It's called "Pictures of Matchstick Men" by Status Quo. Hope that's the one you meant!


What movie and television projects has Frank Remley been in?

Frank Remley has: Performed in "The Story of Molly X" in 1949. Played himself in "The Jack Benny Program" in 1950. Played Himself - Guitarist in "The Jack Benny Program" in 1950. Played Guitar Player in "The Jack Benny Program" in 1950. Played Remley the Guitarist in "The Jack Benny Program" in 1950. Played Frankie the Guitarist in "The Jack Benny Program" in 1950. Played Frankie Remley in "The Jack Benny Program" in 1950. Played Himself - Musician in "The Jack Benny Program" in 1950. Played Remley in "The Jack Benny Program" in 1950. Played Orchestra Leader in "My Blue Heaven" in 1950. Played Frankie in "Shower of Stars" in 1954.


What movie and television projects has Jacob Devik been in?

Jacob Devik has: Played Theatre Elite in "Smash" in 2012. Played Matchstick in "Super Knocked Up" in 2012. Played News Crew in "Hits" in 2014. Played Bar Patron in "Hobo Heyseus" in 2014. Played Disco Patron in "The Normal Heart" in 2014.


What movie and television projects has Wayne Songer been in?

Wayne Songer has: Played himself in "The Jack Benny Program" in 1950. Played Wayne in "The Jack Benny Program" in 1950. Played Himself - Clarinetist in "The Jack Benny Program" in 1950. Played Wayne the Clarinetist in "The Jack Benny Program" in 1950. Played Clarinet Player in "The Jack Benny Program" in 1950. Played Wayne in "Shower of Stars" in 1954.


What movie and television projects has Marco Kyris been in?

Marco Kyris has: Played Convict Guard in "Hangfire" in 1991. Played Karl in "Sweating Bullets" in 1991. Played Extra in "Trapped in Paradise" in 1994. Played Scuzzy Friend in "Mrs. Winterbourne" in 1996. Played Pizza Boy in "Matchstick Men" in 2003. Played Marco in "Dimension" in 2007.


Can a computer game be played on the ps2?

No the PS2 or any other game console does not play computer games they are played on a computer