Code in Java Using Arrays ( input should end with 0 to terminate the program)
Bulls and Cows is an old code-breaking mind or paper and pencil game for two or more players, predating the similar commercially marketed board game Mastermind. The game is usually played with 4 digits, but can also be played with 3 or any other number of digits.
The players must try to guess the secret number created by the codemaker. On a sheet of paper, each players write their guess number. The digits must be all different. If the matching digits are on their right positions, they are ‘bulls’, if on different positions, they are ‘cows’. Example:
Secret number: 4 2 7 1
Player’s guess: 1 2 3 4
Then the answer will be 1 bull and 2 cows (The bull is ‘2’ and the cows is ‘1’ and ‘4’).
Write a program to play 4-digits “Bulls and Cows” game.
Input
The first line of input is a positive integer N (1 ≤ N ≤ 100) which indicates the number of input with N-1 players in the test case. The following line contains 4 positif integers represent 4-digit secret number and the next N-1 lines are the players’ guess number. Input is terminated by 0.
Output
For each test case, output a line in the format "Player #x:" where x is the case number (starting from 1). The following n lines are the answer for each player, as shown in the sample output.
Sample Input
6 2 4 3 1 7 5 4 3 3 2 0 1 2 3 4 1 6 5 0 7 3 4 1 2 0
Sample Output
Player #1: 0 bulls 2 cows Player #2: 1 bulls 2 cows Player #3: 2 bulls 2 cows Player #4: 0 bulls 0 cows Player #5: 1 bulls 3 cows
code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //take input from
console
String s =sc.nextLine(); //take no of players
String secNum = sc.nextLine(); // take secret
key
secNum = secNum.replaceAll(" ", ""); // remove all
whtespaces
int noPlayers = Integer.parseInt(s); //convert string
s to no of players
int i =1; // count no of players
while(i<noPlayers){ // if i less than no of players
than loop do because last player is just 0
int cows=0; //intiliaze cows to 0
int bulls=0; //intiliaze bulls to 0
String playerNum = sc.nextLine(); // take player num from each
player
playerNum = playerNum.replaceAll(" ", ""); // remove all
whtespaces
for(int k=0;k <playerNum.length(); k++){ // loop each number
Character by Character
if( (secNum.charAt(k) == playerNum.charAt(k)) ){ // check paticular
position num matches
bulls = bulls + 1;
}else if(playerNum.contains(Character.toString(secNum.charAt(k)))){
//check num contains in secret key
cows = cows + 1;
}
}
System.out.println("Player #"+i+": "+bulls+" bulls "+cows+" cows");
//print reulst of player
i++;
}
}
}
Screenshot:



Code in Java Using Arrays ( input should end with 0 to terminate the program) Bulls...
Code with Java using arrays and Scanner only ( input should end with 0 to terminate the program) Everyone loves to be celebrated on their birthdays. Birthday celebration can encourage positive social interaction among co-workers, foster friendship among classmates or even strengthen bond between families. Birthday graph can be display in many forms. It can a creative drawing consists of cupcakes, balloons, candles with names, or it can be in the form of simple bar chart to indicate the birthday...
python question
Question 1 Write a Python program to play two games. The program should be menu driven and should use different functions to play each game. Call the file containing your program fun games.py. Your program should include the following functions: • function guess The Number which implements the number guessing game. This game is played against the computer and outputs the result of the game (win/lose) as well as number of guesses the user made to during the...
Please do it by C++ programming! By completing this project, you will demonstrate your understanding of: 1) Arithmetic operations (addition, subtraction, multiplication, division, modulus) 2) Conditional statements (If, If-Else, If-ElseIf-Else, Conditional Operator, Switch) 3) Precondition, postcondition, and indexing loops (Do-While, While, For) 4) Standard text file operations (open, close, read, write) 5) Modularizing code by breaking into functions (including functions with input parameters, and ones that return a specific data type) There is a number guessing game similar to MasterMind...
lab5 is to rewrite lab1 by oop
the code down below is the code of lab1
Lab 5 Rewrite the program in Lab 1 in object-oriented programming. You need to analyze the functionalities in Lab 1. You may define any number of classes if necessary. For example, you can consider in this way: one object for the game procedure, the other object for the player. In this way, you should define a protocol so that the game object has minimal...
How to play Bulls and Cows between you (the host) and your computer (the player) When completed, your program allows you to host the game (i.e., you create a secret word and score every guess) and it makes guesses until it wins. During the development or testing of your program, you may set a condition (e.g., the total count of guesses is already 10) to allow your program to request to reveal the secret word and quit the game. In...
Python Algorithm Coding
There are N paper with three letters written on each paper.
Characters are 0 to 9 digits or *.You can create a continuous
number by attaching the paper in a proper order. Print out the
maximum number of digits sum of a sequence of numbers that can be
made when N paper is given.
For example, suppose you have a piece of paper with a lettering
as shown below.
In this case, the consecutive numbers of strings...
Java code
INNER PRODUCT 4E Input Standard input Output Standard output Topic Array & Array Processing! Problem Description The inner product (also known as the dot product or scalar product) of the elements of set A and the elements of set B of size is defined as the sum of the products of corresponding terms. For example, given set A as the array (2, 3, 4, 5, 6, 7, 8, 9; and set B as 6,5, 4, 3, 2, 7,...
USE JAVA
Problem 1: Guess a Number You are writing a program for a number guessing game. For playing "Guess a Number", the program will generate a number for player to guess from within a range specified by the player. The program will take as input the lowest number in the range and the highest number in the range from the player, and then a series of guesses of the form: <n,>n, = n, where n is the number guessed....
This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...
Code in JAVA.................... Guess the Number In this activity, you will write a REST server to facilitate playing a number guessing game known as "Bulls and Cows". In each game, a 4-digit number is generated where every digit is different. For each round, the user guesses a number and is told the exact and partial digit matches. An exact match occurs when the user guesses the correct digit in the correct position. A partial match occurs when the user guesses...