Sure! Here's an example of a console-based Tic Tac Toe game written in Java using concepts such as arrays, loops, inheritance, constructors, and information hiding:
```java
import java.util.Arrays;
import java.util.Scanner;
class Player {
private final String name;
private final char symbol;
public Player(String name, char symbol) {
this.name = name;
this.symbol = symbol;
}
public String getName() {
return name;
}
public char getSymbol() {
return symbol;
}
}
class GameBoard {
private final char[][] board;
private int movesLeft;
public GameBoard() {
board = new char[3][3];
movesLeft = 9;
for (char[] row : board) {
Arrays.fill(row, ' ');
}
}
public boolean makeMove(Player player, int row, int col) {
if (row < 0 || row >= 3 || col < 0 || col >= 3 || board[row][col] != ' ') {
return false;
}
board[row][col] = player.getSymbol();
movesLeft--;
return true;
}
public boolean isGameOver() {
return movesLeft == 0 || getWinner() != null;
}
public Player getWinner() {
for (int i = 0; i < 3; i++) {
if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][0] == board[i][2]) {
return getPlayerFromSymbol(board[i][0]);
}
if (board[0][i] != ' ' && board[0][i] == board[1][i] && board[0][i] == board[2][i]) {
return getPlayerFromSymbol(board[0][i]);
}
}
if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2]) {
return getPlayerFromSymbol(board[0][0]);
}
if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[0][2] == board[2][0]) {
return getPlayerFromSymbol(board[0][2]);
}
return null;
}
private Player getPlayerFromSymbol(char symbol) {
if (symbol == 'X') {
return new Player("Player 1", symbol);
} else if (symbol == 'O') {
return new Player("Player 2", symbol);
} else {
return null;
}
}
public void printBoard() {
for (char[] row : board) {
System.out.println(Arrays.toString(row));
}
System.out.println();
}
}
public class TicTacToe {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Player player1 = new Player("Player 1", 'X');
Player player2 = new Player("Player 2", 'O');
GameBoard board = new GameBoard();
Player currentPlayer = player1;
while (!board.isGameOver()) {
System.out.println(currentPlayer.getName() + "'s turn");
board.printBoard();
int row, col;
do {
System.out.print("Enter row (0-2): ");
row = scanner.nextInt();
System.out.print("Enter column (0-2): ");
col = scanner.nextInt();
} while (!board.makeMove(currentPlayer, row, col));
currentPlayer = (currentPlayer == player1) ? player2 : player1;
}
Player winner = board.getWinner();
if (winner != null) {
System.out.println("Congratulations, " + winner.getName() + " wins!");
} else {
System.out.println("It's a draw!");
}
board.printBoard();
}
}
```
To validate that the code achieves the desired result, we can write some unit tests using a testing framework like JUnit:
```java
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class TicTacToeTest {
private GameBoard board;
@BeforeEach
void setup() {
board = new GameBoard();
}
@Test
void testMakeMove() {
Player player1 = new Player("Player 1", 'X');
Player player2 = new Player("Player 2", 'O');
Assertions.assertTrue(board.makeMove(player1, 0, 0));
Assertions.assertTrue(board.makeMove(player2, 1, 1));
Assertions.assertFalse(board.makeMove(player1, 0, 0)); // Already occupied
Assertions.assertFalse(board.makeMove(player2, 1, 1)); // Already occupied
Assertions.assertTrue(board.makeMove(player1, 0, 1));
char[][] expectedBoard = {
{'X', ' ', ' '},
{' ', 'O', ' '},
{' ', ' ', ' '}
};
Assertions.assertArrayEquals(expectedBoard, board.getBoard());
}
@Test
void testIsGameOver() {
Player player1 = new Player("Player 1", 'X');
Player player2 = new Player("Player 2", 'O');
Assertions.assertFalse(board.isGameOver());
// Winning move: vertical line
board.makeMove(player1, 0, 0);
board.makeMove(player2, 1, 1);
board.makeMove(player1, 1, 0);
board.makeMove(player2, 2, 2);
board.makeMove(player1, 2, 0);
Assertions.assertTrue(board.isGameOver());
Assertions.assertEquals(player1, board.getWinner());
}
@Test
void testGetWinner() {
Player player1 = new Player("Player 1", 'X');
Player player2 = new Player("Player 2", 'O');
// Winning move: diagonal line
board.makeMove(player1, 0, 0);
board.makeMove(player2, 1, 1);
board.makeMove(player1, 1, 0);
board.makeMove(player2, 2, 2);
board.makeMove(player1, 2, 0);
Assertions.assertEquals(player1, board.getWinner());
}
}
```
You can run these tests using a testing framework like JUnit to ensure that the code is functioning correctly.
You have to design java console application tic tac toe. Your application must use concepts such as arrays, loops, inheritance, constructors, and information hiding. Output must be shown in the form of a report using the console. You must also write unit tests to validate that your code achieves the desired result.
In your solution, make use of as many concepts and components dealt with in this course, but pay special attention to the following learning units:
• Learning Unit 1: Advanced arrays
• Learning Unit 2: Introduction to Inheritance
in java
1 answer