Human plays against the computer (use one-dimensional arrays) In this option you
ID: 3772247 • Letter: H
Question
Human plays against the computer (use one-dimensional arrays)
In this option you will develop a program in which a human plays against the computer.
1. Validate user input at every opportunity.
a. Do not allow number entries less than 0
b. Do not allow number entries greater than 8
c. Do not allow non-numeric entries
2. Do not use global variables in the development of the program
3. You may use global constants in the development of the program
4. Use one-dimensional arrays to keep track of the game:
a. Computer moves
b. Human moves
5. Use functions to pass arrays and implement other program requirements.
6. The program must be developed using functions so that the main() function consists mostly of function calls
7. Computer must be aggressive and take every opportunity to win the game if and when the human makes errors
8. The main() function must use a loop to keep the user in the program until he/she wants
Program Flow
1. At the start of the game, an initial "Splash" screen must be displayed which includes:
a. The game’s title
b. Your name
to quit.
2. Be sure to validate user entries; no white space, no numbers X, such that 0<=X<=8, no alpha characters.
3. The computer will choose a spot immediate
4. Don’t allow the human to select a square which is already in use (the human chose a position already in use)
5. A winner is detected when one occurs and a request to play again is made. Don’t forget to validate!
6. A tie is detected when one occurs.
7. Program ends when players don’t want to play anymore
Human plays against the computer (use one-dimensional arrays)
Explanation / Answer
import java.util.Scanner;
public class Human plays {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size_of_pile = (int) (Math.random() * 91) + 10;
int current_turn = (int) (Math.random() * 2);
int smart_or_stupid = (int) (Math.random() * 2);
while (size_of_pile > 0) {
System.out.println("Current number of marlbes in pile: " + size_of_pile);
int marbles_to_remove = 0;
if (current_turn == 0) {
if (smart_or_stupid == 1 || (size_of_pile == 1 || size_of_pile == 3
|| size_of_pile == 7 || size_of_pile == 15 || size_of_pile == 31
|| size_of_pile == 63)) {
marbles_to_remove = (int) (Math.random() * (size_of_pile / 2 + 1)) + 1;
}
else {
if (size_of_pile > 63) {
marbles_to_remove = size_of_pile - 63;
}
else if (size_of_pile > 31) {
marbles_to_remove = size_of_pile - 31;
}
else if (size_of_pile > 15) {
marbles_to_remove = size_of_pile - 15;
}
else if (size_of_pile > 7) {
marbles_to_remove = size_of_pile - 7;
}
else if (size_of_pile > 3) {
marbles_to_remove = size_of_pile - 3;
}
else {
marbles_to_remove = size_of_pile - 1;
}
}
System.out.println("Computer removes " + marbles_to_remove + " marble" + ((marbles_to_remove > 1)? "s": ""));
current_turn = 1;
}
else {
do {
System.out.println("How many marbles do you want to remove: ");
marbles_to_remove = input.nextInt();
} while ((marbles_to_remove != 1) && (marbles_to_remove <= 0 || marbles_to_remove > size_of_pile / 2));
current_turn = 0;
}
size_of_pile -= marbles_to_remove;
}
input.close();
if (current_turn == 0) {
System.out.println("Computer wins!");
}
else {
System.out.println("Human wins!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.