Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Game 5 - Tic Tac Toe class - Write a fifth game program that can play Tic Tac To

ID: 3765057 • Letter: G

Question

Game 5 - Tic Tac Toe class - Write a fifth game program that can play Tic Tac Toe. This game displays the lines of the Tic Tac Toe game and prompts the player to choose a move. The move is recorded on the screen and the computer picks his move from those that are left. The player then picks his next move. The program should allow only legal moves. The program should indicate when there is a winner and tell who won. The player is then asked if he would like to play again. A sample output is shown in the attached file.

Game 6 - due date FiveDigitNumber class - Write a sixth game program using the random number generator to generate a five-digit number. The player guesses the number. With each guess the computer returns two numbers, the first is the number of digits that are in the proper position and the second is the sum of the correct digits. Allow the client to repeat the game as many times as she wishes. Guarantee the numbers entered are 5 digit numbers. A sample output is shown in the attached file.

Explanation / Answer

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class NumberGameMain
{
public static void main(String args[])
{
String correctNumber = "";
boolean correctlyGuessed = false;
boolean gameOver = false;
while (!gameOver)
{
Random rand = new Random();
correctNumber = Integer.toString(rand.nextInt(100000));
while (!correctlyGuessed)
{
System.out.println("Please guess the 5-digit number ");
String guess = readFromCmdLine();
int numberOfCorrectDigits = checkCorrectDigits(correctNumber, guess);
if (numberOfCorrectDigits == 5)
{
System.out.println("You guessed correctly. Congratulations");
correctlyGuessed = true;
}
else
{
System.out.println("You have found " + numberOfCorrectDigits +" digits so far with a sum of " + sumCorrectDigits(correctNumber, guess) + ".");
}
}
System.out.println("Would you like to play the game again? (yes/no)");
String response = readFromCmdLine();
if (response.equals("no"))
{
System.out.println("Thanks for playing.");
gameOver = true;
}
else
{
correctlyGuessed = false;
}
}
}
private static int checkCorrectDigits(String number, String guess)
{
int correctDigits = 0;
for (int i = 0; i < 5; i++)
{
if (number.charAt(i) == guess.charAt(i))
correctDigits++;
}
return correctDigits;
}
private static int sumCorrectDigits(String number, String guess)
{
int total = 0;
for (int i = 0; i < 5; i++)
{
if (number.charAt(i) == guess.charAt(i))
{
total += Integer.parseInt(Character.toString(number.charAt(i)));
}
}
return total;
}
private static String readFromCmdLine()
{
InputStreamReader cin = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(cin);
String input = "";
try
{
input = in.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return input;
}
}