Question

In: Computer Science

Write a program that allows 2 players to play the marble game using combinations of loop...

Write a program that allows 2 players to play the marble game using combinations of loop and conditional statements. In the marble game, you ask the user to enter how many marbles to start with. Then, the game begins. The first player must take 1, 2 or 3 marbles. Then the second player goes and must take 1, 2 or 3 marbles. The winner is the player who takes the last marble. Allow two users to play this game and print out the winner (player #1 or player#2). Assume that both players enter valid inputs (1, 2 or 3, and they never try to take more marbles than there are in the pile.)

Solutions

Expert Solution

def pickMarbles(numMarbles, player):
        print('Player', player, ', How many marbles you want to pick out of', numMarbles)
        x = int(input('Enter marbles to be picked: '))

        while(x < 1 or x > 3 or x > numMarbles):
                print('Invalid input. Try again.')
                x = int(input('Enter marbles to be picked: '))
        print()
        return x

numMarbles = int(input('Enter number of marbles: '))
turn = 2 # to start with first person.

while(numMarbles > 0):
        turn = 1 if turn == 2 else 2
        numMarbles -= pickMarbles(numMarbles, turn)

print('Player', turn, 'won!')
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

I am using C++ Write a program that allows two players to play a game of...
I am using C++ Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row...
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
For this assignment you will write a Java program using a loop that will play a...
For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment. The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the...
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the...
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the computer multiple times. This program assesses your knowledge of decision structures, repetition structures, and functions. Requirements: 1.Define a function named as play_game. This function receives two parameters representing the player’s and computer’s choices, and it returns an integer representing the game result from the player’s perspective. Specifically, it returns 1 if the player wins and 0 if the player does not win. Hint: you...
Write a program with at least 2 functions that play the game of “guess the number”...
Write a program with at least 2 functions that play the game of “guess the number” as follows: Your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000. The program then displays the following: I have a number between 1 and 1000. Can you guess my number? Please type in your first guess. The player then types the first guess. The program then responds with one of the following: 1.      ...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes....
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed (such as #include <stdio.h> etc). Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects...
USING JAVA... We want to play a simple card game with 2 players option. The deck...
USING JAVA... We want to play a simple card game with 2 players option. The deck of cards contains 52 cards with 13 cards each in the 4 suits: clubs, diamonds, hearts and spades. Each player begins with 26 cards and one of the players starts the game by putting their first card on the table. Players take turns by putting the top card from their hand, until the current card placed on the table matches the suit of the...
Design and implement a Python program which will allow two players to play the game of...
Design and implement a Python program which will allow two players to play the game of Tic-Tac-Toe in a 4x4 grid! X | O | X | O -------------- O | O | X | O -------------- X | X | O | X -------------- X | X | O | X The rules for this game is the same as the classic, 3x3, game – Each cell can hold one of the following three strings: "X", "O", or "...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT