Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 🔍 Code Quality & Clean Code Practice

**This project is part of a learning module focused on code quality, clean coding principles, and collaborative development practices using GitHub.**

### 📝 Homework Objectives

* Analyze the project for compliance with Java coding standards.

* Explore the SonarLint plugin and understand how to use it for static code analysis.

* Refactor the project based on clean code principles.

* Conduct a peer code review using GitHub’s built-in tools.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>App</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>App</name>
<name>app.App</name>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
Expand Down
90 changes: 0 additions & 90 deletions src/main/java/App.java

This file was deleted.

8 changes: 8 additions & 0 deletions src/main/java/app/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package app;

public class App {
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}
113 changes: 113 additions & 0 deletions src/main/java/app/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package app;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Game {

private static final char EMPTY_CELL = ' ';
private static final char PLAYER_SYMBOL = 'X';
private static final char COMPUTER_SYMBOL = 'O';
private static final int BOARD_SIZE = 9;
private static final int MAX_CELL_INDEX = 9;
private static final int MIN_CELL_INDEX = 1;

private final Scanner scan = new Scanner(System.in);
private final char[] box = new char[BOARD_SIZE];
private final Random random = new Random();

public void start() {
Arrays.fill(box, EMPTY_CELL);
int winner = 0;

System.out.println("Enter box number to select. Enjoy!\n");

while (true) {
printBoard();

if (winner != 0) {
printGameOverMessage(winner);
break;
}

playerMove();

if (isWinner(PLAYER_SYMBOL)) {
winner = 1;
} else if (isDraw()) {
winner = 3;
} else {
computerMove();

if (isWinner(COMPUTER_SYMBOL)) {
winner = 2;
}
}
}
}

private void printBoard() {
System.out.println("\n\n " + box[0] + " | " + box[1] + " | " + box[2] + " ");
System.out.println("-----------");
System.out.println(" " + box[3] + " | " + box[4] + " | " + box[5] + " ");
System.out.println("-----------");
System.out.println(" " + box[6] + " | " + box[7] + " | " + box[8] + " \n");
}

private void playerMove() {
while (true) {
byte input = scan.nextByte();
if (input >= MIN_CELL_INDEX && input <= MAX_CELL_INDEX) {
if (box[input - 1] == PLAYER_SYMBOL || box[input - 1] == COMPUTER_SYMBOL) {
System.out.println("That one is already in use. Enter another.");
} else {
box[input - 1] = PLAYER_SYMBOL;
break;
}
} else {
System.out.println("Invalid input. Enter again.");
}
}
}

private void computerMove() {
while (true) {
byte rand = (byte) (random.nextInt(MAX_CELL_INDEX) + 1);
if (box[rand - 1] != PLAYER_SYMBOL && box[rand - 1] != COMPUTER_SYMBOL) {
box[rand - 1] = COMPUTER_SYMBOL;
break;
}
}
}

private boolean isWinner(char symbol) {
return (box[0] == symbol && box[1] == symbol && box[2] == symbol)
|| (box[3] == symbol && box[4] == symbol && box[5] == symbol)
|| (box[6] == symbol && box[7] == symbol && box[8] == symbol)
|| (box[0] == symbol && box[3] == symbol && box[6] == symbol)
|| (box[1] == symbol && box[4] == symbol && box[7] == symbol)
|| (box[2] == symbol && box[5] == symbol && box[8] == symbol)
|| (box[0] == symbol && box[4] == symbol && box[8] == symbol)
|| (box[2] == symbol && box[4] == symbol && box[6] == symbol);
}

private boolean isDraw() {
for (char c : box) {
if (c != PLAYER_SYMBOL && c != COMPUTER_SYMBOL) {
return false;
}
}
return true;
}

private static void printGameOverMessage(int winner) {
switch (winner) {
case 1 -> System.out.println("You won the game!\nCreated by Shreyas Saha. Thanks for playing!");
case 2 -> System.out.println("You lost the game!\nCreated by Shreyas Saha. Thanks for playing!");
case 3 -> System.out.println("It's a draw!\nCreated by Shreyas Saha. Thanks for playing!");
default -> System.out.println("Unexpected game status.");
}
System.exit(0);
}
}