Question

In: Computer Science

The past units have provided you with a solid foundation for java programming. You have tirelessly...

The past units have provided you with a solid foundation for java programming. You have tirelessly worked through tough assignments, all the while thinking:

“I wish I could just program what I want!”
Well, today is your lucky day.

It is time to apply all your problem-solving skills to something that you are interested in, and will find room to be creative with.

Would anyone please explain it to me briefly what am I supposed to be doing in this final assignment or project?

The world is filled with possible computer programs, the opportunities really are endless. Anyone of your favorite board or card games would make an interesting project. Perhaps you are more interested in serious programming, creating a database with fast searching and sorting abilities. This is your chance to define the problem, think of something relevant to your interests that you think you can tackle; these are just a few ideas:

Game Card (solitaire BlackJack... etc)
Small Business software.
Personal finance/budget system.
Interactive game.
A new java object that will be useful for future programs.
You will make 3 Submissions, each representing the Software Development Lifecycle. Be sure to read each Submission description carefully, and take your time working on the different parts of your assignment. As always you will be marked for creativity, style, functionality and following guidelines.

Each Submission is worth 5 marks for a total of 15% of your final grade.

Solutions

Expert Solution

You are asked to make a software and are given some options of which "an interactive game" is one. And so, here is the code for a Tic Tac Toe game in Java.

TicTacToe.java

public class TicTacToe {

final private char[][] board;
private int turnNumber;
private boolean isGameWon;
private String player1Name, player2Name;

// default constructor
public TicTacToe() {
// instantiating the board
board = new char[3][3];
for (char[] row : board) {
Arrays.fill(row, ' ');
}
turnNumber = 0;
isGameWon = false;
}

public String getPlayer1Name() {
return player1Name;
}

public void setPlayer1Name(String player1Name) {
this.player1Name = player1Name;
}

public String getPlayer2Name() {
return player2Name;
}

public void setPlayer2Name(String player2Name) {
this.player2Name = player2Name;
}

public int getTurnNumber() {
return turnNumber;
}

public boolean getIsGameWon() {
return isGameWon;
}

// method that allows player to make a move
public void makeAMove(int row, int column) {
if (isPlayLegal(row, column)) {
if (turnNumber % 2 == 0) {
board[row][column] = 'X';
} else {
board[row][column] = 'O';
}
turnNumber++;
}
}

// method checking whether a play by a player is legal
public boolean isPlayLegal(int row, int column) {
boolean validPlay = true;
if (row < 0 || column < 0 || row > 2 || column > 2) {
System.out.println("ERROR: Invalid spot attempt! Please try again!");
validPlay = false;
} else if (!(board[row][column] == ' ')) {
System.out.println("ERROR: Spot already occupied! Please try again!");
validPlay = false;
}
return validPlay;
}

// method giving the conditions of winning a game
public void checkIfGameWon() {
if (turnNumber >= 5) {
for (int i = 0; i < board.length; i++) {
if (board[0][i] != ' ' && board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
isGameWon = true;
}
if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
isGameWon = true;
}
}
}
}

// method that displays the final result: which player has won the game or if a game is a Tie
public void displayFinalStatusOfGame() {
if (isGameWon) {
if (turnNumber % 2 != 0) {
System.out.println("Player " + getPlayer1Name() + " is the winner!\n------------------------------\n\n");
} else {
System.out.println("Player " + getPlayer1Name() + " is the winner!\n------------------------------\n\n");
}
} else {
System.out.println("Sorry, the game is a Tie!\n------------------------------\n\n");
}
}

// method that gives a visual representation of the board every time a move is made
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();

for (int row = 0; row < board.length; row++) {
for (int column = 0; column < board[row].length; column++) {
switch (column) {
case 0:
stringBuilder.append(" ").append(board[row][column]).append(" | ");
break;

case 1:
stringBuilder.append(board[row][column]).append(" | ");
break;

case 2:
stringBuilder.append(board[row][column]);
break;
}
}
if (row < board.length - 1) {
stringBuilder.append("\n-----------\n");
}
}
return (stringBuilder.toString() + "\n"); // prints an extra new line after printing the board
}
}

TicTcToeDriver.java (Main class)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TicTacToeDriver {
  
public static void main(String[] args) throws IOException {
// welcome screen and player names entry module
TicTacToe theGame = new TicTacToe();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userInput;

// initiating the game for the 1st time
startTheGame(theGame);

/*
BONUS POINT
AFTER EACH GAME SESSION, IT ASKS USER CONSENT:
WHETHER USER WANTS TO PLAY THE GAME AGAIN
*/
do {
System.out.print("Do you want to play the game again? [Y/N]: ");
userInput = br.readLine().toLowerCase();

switch (userInput) {
case "y":
theGame = new TicTacToe();
startTheGame(theGame);
break;

case "n":
System.exit(0);

default:
System.out.println("Invalid option!\n");
break;
}

} while (!userInput.equals("n"));
}

public static void startTheGame(TicTacToe ticTacToe) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int turn = 0;

System.out.println(
"======================\n"
+ "WELCOME TO TIC TAC TOE\n"
+ "======================\n"
);
System.out.print("Enter Player 1 Name: ");
String player1Name = br.readLine();
System.out.print("Enter Player 2 Name: ");
String player2Name = br.readLine();

ticTacToe.setPlayer1Name(player1Name);
ticTacToe.setPlayer2Name(player2Name);

System.out.println();

while (ticTacToe.getTurnNumber() < 9 && !ticTacToe.getIsGameWon()) //at max, 9 moves can be made
{
if (turn == 0) {
System.out.println("Turn of " + ticTacToe.getPlayer1Name() + "\n--------------------------------");
turn += 1;
} else if (turn == 1) {
System.out.println("Turn of " + ticTacToe.getPlayer2Name() + "\n--------------------------------");
turn -= 1;
}
System.out.print("Enter the row index(1-3) of your move: ");
int rowIndex = Integer.parseInt(br.readLine());
while(rowIndex < 1 || rowIndex > 3)
{
System.out.print("Enter the row index of your move: ");
rowIndex = Integer.parseInt(br.readLine());
}
  
System.out.print("Enter the column index(1-3) of your move: ");
int columnIndex = Integer.parseInt(br.readLine());
while(columnIndex < 1 || columnIndex > 3)
{
System.out.print("Enter the column index(1-3) of your move: ");
columnIndex = Integer.parseInt(br.readLine());
}
  
ticTacToe.makeAMove(rowIndex - 1, columnIndex - 1); // since array index starts from 0
System.out.println(ticTacToe.toString()); // prints the board after evry move
ticTacToe.checkIfGameWon(); // checks whether its a win, loss or tie
  
}
ticTacToe.displayFinalStatusOfGame(); // displays the final status of the game
}
}

***************************************************************** SCREENSHOT **********************************************************


Related Solutions

Discuss how the middle ages provided a solid foundation for modern liberty? topics 1)Rule of law...
Discuss how the middle ages provided a solid foundation for modern liberty? topics 1)Rule of law 2)Education 3)Development of commerce
Background: introduction to "Wrapper" classes, this is a tool or concept provided in the Java Programming...
Background: introduction to "Wrapper" classes, this is a tool or concept provided in the Java Programming Language that gives us a way to utilize native datatypes as Objects with attributes and methods. A Wrapper class in Java is the type of class which contains or the primitive data types. When a wrapper class is created a new field is created and in that field, we store the primitive data types. It also provides the mechanism to covert primitive into object...
QUESTIONS: Have you done any computer programming in the past. If YES which language did you...
QUESTIONS: Have you done any computer programming in the past. If YES which language did you like the most and why. If NO, which languages would you like to learn and why. What are some objected oriented programming languages in the market. What is the difference between JAVA, PYTHON and R programming language. What is Angular JS and how is it different from object oriented programming. What are loops, arrays, if conditions and method / function in the context of...
Java programming Question1 You will create a full program called LetterGrade that will have the user...
Java programming Question1 You will create a full program called LetterGrade that will have the user enter in 2 test scores as double values. It will compute the average and then using else..if statements it will display the letter grade based on my grading scale listed in the course outline.
Programming language is Java. Consider you have already created a validateString(String str) method that validates a...
Programming language is Java. Consider you have already created a validateString(String str) method that validates a String to ensure it contains exactly 4 characters. Create code that calls this method, sending user input as an argument (actual parameter). The user should be continuously prompted to enter a different string and informed of an error if the method returns false.
I have been working on this assignment in Java programming and can not get it to...
I have been working on this assignment in Java programming and can not get it to work. This method attempts to DECODES an ENCODED string without the key.    public static void breakCodeCipher(String plainText){ char input[]plainText.toCharArray(); for(int j=0; j<25; j++){ for(int i=0; i<input.length; i++) if(input[i]>='a' && input[i]<='Z') input[i]=(char)('a'+((input[i]-'a')+25)%26); else if(input[i]>='A'&& input[i]<='Z') input[i]=(char)('A'+ ((input[i]-'A')+25)%26); } System.out.println(plainText)    }
JAVA programming Classwork- please answer all prompts as apart of one java programming project Part A...
JAVA programming Classwork- please answer all prompts as apart of one java programming project Part A Add to your project this class Position, which has x and y coordinates. Create an abstract class GameElt, which has a String name, an int health (keep it in the range 0 to 100) and a Position pos. For GameElt, include a default constructor that starts pos at (0, 0), and a parameterized constructor that takes x and y coordinates and a name. health...
Rewrite the procedure in third person & in past tense: Procedure You will be provided with...
Rewrite the procedure in third person & in past tense: Procedure You will be provided with post-mitochondrial supernatant fractions from rat liver of control rats and phenobarbitone-treated rats. Carry out the assay for drug metabolism as follows. A Take 4 ml incubation medium (containing substrate aminopyrine) into each test flask. Take 4 ml incubation medium (containing no substrate into one flask (reagent blank)). Add 700 ml 0.25 M sucrose solution to all flasks. Pre-incubate all flasks at 37 °C for...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT