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
This for Java Programming Write a java statement to import the java utilities. Create a Scanner...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner object to read input. int Age;     Write a java statement to read the Age input value 4 . Redo 1 to 3 using JOptionPane
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...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
The programming language that is being used here is JAVA, below I have my code that...
The programming language that is being used here is JAVA, below I have my code that is supposed to fulfill the TO-DO's of each segment. This code in particular has not passed 3 specific tests. Below the code, the tests that failed will be written in bold with what was expected and what was outputted. Please correct the mistakes that I seem to be making if you can. Thank you kindly. OverView: For this project, you will develop a game...
Create a project plan on the game or application you are creating. Using java programming. The...
Create a project plan on the game or application you are creating. Using java programming. The project plan should include the following: A description of the game or application The IDE or game engine your plan to use to create the game or app and information on how you are going to develop the game or app If you choose to create a game, how are you going to approach the game design and game development process or if you...
This program focuses on programming with Java Collections classes. You will implement a module that finds...
This program focuses on programming with Java Collections classes. You will implement a module that finds a simplified Levenshtein distance between two words represented by strings. Your program will need support files LevenDistanceFinder.Java, dictionary.txt, while you will be implementing your code in the LevenDistanceFinder.java file. INSTRUCTIONS The Levenshtein distance, named for it's creator Vladimir Levenshtein, is a measure of the distance between two words. The edit distance between two strings is the minimum number of operations that are needed to...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same nodes, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list. Exercise 2 In this exercise, you will...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT