In: Computer Science
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.
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 **********************************************************