diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
new file mode 100644
index 0000000..8b146db
--- /dev/null
+++ b/.idea/compiler.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000..aa00ffa
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
new file mode 100644
index 0000000..712ab9d
--- /dev/null
+++ b/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..9dc782b
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml
new file mode 100644
index 0000000..2b63946
--- /dev/null
+++ b/.idea/uiDesigner.xml
@@ -0,0 +1,124 @@
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..faa0ac5
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/G5-java-dev-homework b/G5-java-dev-homework
new file mode 160000
index 0000000..07f1a4e
--- /dev/null
+++ b/G5-java-dev-homework
@@ -0,0 +1 @@
+Subproject commit 07f1a4eea7aafc3fa56e566a2a7145a160dde045
diff --git a/pom.xml b/pom.xml
index 09645b6..2e301b4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
App
jar
1.0-SNAPSHOT
- App
+ Game
17
17
diff --git a/src/main/java/App.java b/src/main/java/App.java
index 4f6ffd2..fc04d34 100644
--- a/src/main/java/App.java
+++ b/src/main/java/App.java
@@ -1,90 +1,20 @@
-import java.util.Scanner;
+/*
+* Замінив назви змінних на більш зрозумілі та описові.
+* Додав константи, такі як BOARD_SIZE, замість використання магічних чисел.
+* Виніс перевірку кожної лінії в окремий метод checkLine.
+* Спрощено метод checkVictory.
+* Логіка роботи циклів була оптимізована через використання логічних змінних (validMove, gameFinished)
+ замість використання декількох break або continue.
+* Перевірив і відкоригував форматування коду відповідно до стандартів.
+* Збільшив читабельність коду через додаткову структуру методів.
+* Скоротив кількість дубльованих умов для перевірки перемоги.
+* Оптимізував цикли шляхом винесення загальної логіки в окремі методи.
+* Проаналізував проект з використанням SonarLint.
+*/
public class App {
-
public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- byte input;
- byte rand;
- byte i;
- boolean boxAvailable = false;
- byte winner = 0;
- char box[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
- System.out.println("Enter box number to select. Enjoy!\n");
-
- boolean boxEmpty = false;
- while (true) {
- 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");
- if(!boxEmpty){
- for(i = 0; i < 9; i++)
- box[i] = ' ';
- boxEmpty = true;
- }
-
- if(winner == 1){
- System.out.println("You won the game!\nCreated by Shreyas Saha. Thanks for playing!");
- break;
- } else if(winner == 2){
- System.out.println("You lost the game!\nCreated by Shreyas Saha. Thanks for playing!");
- break;
- } else if(winner == 3){
- System.out.println("It's a draw!\nCreated by Shreyas Saha. Thanks for playing!");
- break;
- }
-
- while (true) {
- input = scan.nextByte();
- if (input > 0 && input < 10) {
- if (box[input - 1] == 'X' || box[input - 1] == 'O')
- System.out.println("That one is already in use. Enter another.");
- else {
- box[input - 1] = 'X';
- break;
- }
- }
- else
- System.out.println("Invalid input. Enter again.");
- }
-
- if((box[0]=='X' && box[1]=='X' && box[2]=='X') || (box[3]=='X' && box[4]=='X' && box[5]=='X') || (box[6]=='X' && box[7]=='X' && box[8]=='X') ||
- (box[0]=='X' && box[3]=='X' && box[6]=='X') || (box[1]=='X' && box[4]=='X' && box[7]=='X') || (box[2]=='X' && box[5]=='X' && box[8]=='X') ||
- (box[0]=='X' && box[4]=='X' && box[8]=='X') || (box[2]=='X' && box[4]=='X' && box[6]=='X')){
- winner = 1;
- continue;
- }
-
- boxAvailable = false;
- for(i=0; i<9; i++){
- if(box[i] != 'X' && box[i] != 'O'){
- boxAvailable = true;
- break;
- }
- }
-
- if(boxAvailable == false){
- winner = 3;
- continue;
- }
-
- while (true) {
- rand = (byte) (Math.random() * (9 - 1 + 1) + 1);
- if (box[rand - 1] != 'X' && box[rand - 1] != 'O') {
- box[rand - 1] = 'O';
- break;
- }
- }
-
- if((box[0]=='O' && box[1]=='O' && box[2]=='O') || (box[3]=='O' && box[4]=='O' && box[5]=='O') || (box[6]=='O' && box[7]=='O' && box[8]=='O') ||
- (box[0]=='O' && box[3]=='O' && box[6]=='O') || (box[1]=='O' && box[4]=='O' && box[7]=='O') || (box[2]=='O' && box[5]=='O' && box[8]=='O') ||
- (box[0]=='O' && box[4]=='O' && box[8]=='O') || (box[2]=='O' && box[4]=='O' && box[6]=='O')){
- winner = 2;
- continue;
- }
- }
-
+ Game game = new Game();
+ game.start();
}
-}
\ No newline at end of file
+}
diff --git a/src/main/java/Board.java b/src/main/java/Board.java
new file mode 100644
index 0000000..7094c28
--- /dev/null
+++ b/src/main/java/Board.java
@@ -0,0 +1,15 @@
+public class Board {
+ public void printBoard(char[] box) {
+ System.out.println("\n " + box[6] + " | " + box[7] + " | " + box[8] + " ");
+ System.out.println("-----------");
+ System.out.println(" " + box[3] + " | " + box[4] + " | " + box[5] + " ");
+ System.out.println("-----------");
+ System.out.println(" " + box[0] + " | " + box[1] + " | " + box[2] + " \n");
+ }
+
+ public void clearBoard(char[] box) {
+ for (int i = 0; i < 9; i++) {
+ box[i] = ' ';
+ }
+ }
+}
diff --git a/src/main/java/Computer.java b/src/main/java/Computer.java
new file mode 100644
index 0000000..667f13f
--- /dev/null
+++ b/src/main/java/Computer.java
@@ -0,0 +1,12 @@
+public class Computer {
+ public void computerMove(char[] box) {
+ boolean validMove = false;
+ while (!validMove) {
+ byte rand = (byte) (Math.random() * 9);
+ if (box[rand] != 'X' && box[rand] != '0') {
+ box[rand] = '0';
+ validMove = true;
+ }
+ }
+ }
+}
diff --git a/src/main/java/Game.java b/src/main/java/Game.java
new file mode 100644
index 0000000..75ff34c
--- /dev/null
+++ b/src/main/java/Game.java
@@ -0,0 +1,117 @@
+import java.util.Scanner;
+
+public class Game {
+
+ private static final Scanner inputScanner = new Scanner(System.in);
+
+ private char[] box = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
+ private boolean boxEmpty = false;
+ private byte winner = 0;
+ private boolean gameFinished = false;
+
+ public void start() {
+ System.out.println("Choose a position:");
+
+ while (!gameFinished) {
+ printBoard();
+ if (!boxEmpty) {
+ clearBoard();
+ boxEmpty = true;
+ }
+
+ if (checkWinner()) {
+ gameFinished = true;
+ } else {
+ playerMove();
+
+ if (checkVictory('X')) {
+ winner = 1;
+ } else if (!isBoxAvailable()) {
+ winner = 3;
+ } else {
+ computerMove();
+
+ if (checkVictory('O')) {
+ winner = 2;
+ }
+ }
+ }
+ }
+ }
+
+ private void printBoard() {
+ System.out.println("\n " + box[6] + " | " + box[7] + " | " + box[8] + " ");
+ System.out.println("-----------");
+ System.out.println(" " + box[3] + " | " + box[4] + " | " + box[5] + " ");
+ System.out.println("-----------");
+ System.out.println(" " + box[0] + " | " + box[1] + " | " + box[2] + " \n");
+ }
+
+ private void clearBoard() {
+ for (int i = 0; i < 9; i++) {
+ box[i] = ' ';
+ }
+ }
+
+ private boolean checkWinner() {
+ if (winner == 1) {
+ System.out.println("You won the game!\nCreated by Oleksandr Lysak. Thanks for playing!");
+ return true;
+ } else if (winner == 2) {
+ System.out.println("You lost the game!\nCreated by Oleksandr Lysak. Thanks for playing!");
+ return true;
+ } else if (winner == 3) {
+ System.out.println("It's a draw!\nCreated by Oleksandr Lysak. Thanks for playing!");
+ return true;
+ }
+ return false;
+ }
+
+ private void playerMove() {
+ boolean validMove = false;
+ while (!validMove) {
+ byte input = inputScanner.nextByte();
+ if (input > 0 && input < 10) {
+ if (box[input - 1] == 'X' || box[input - 1] == '0') {
+ System.out.println("That one is already in use. Enter another.");
+ } else {
+ box[input - 1] = 'X';
+ validMove = true;
+ }
+ } else {
+ System.out.println("Invalid input. Enter again.");
+ }
+ }
+ }
+
+ private boolean checkVictory(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 isBoxAvailable() {
+ for (int i = 0; i < 9; i++) {
+ if (box[i] != 'X' && box[i] != '0') {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private void computerMove() {
+ boolean validMove = false;
+ while (!validMove) {
+ byte rand = (byte) (Math.random() * 9);
+ if (box[rand] != 'X' && box[rand] != '0') {
+ box[rand] = '0';
+ validMove = true;
+ }
+ }
+ }
+}
diff --git a/src/main/java/GameStateChecker.java b/src/main/java/GameStateChecker.java
new file mode 100644
index 0000000..a8837dd
--- /dev/null
+++ b/src/main/java/GameStateChecker.java
@@ -0,0 +1,35 @@
+public class GameStateChecker {
+ public boolean checkVictory(char[] box, 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);
+ }
+
+ public boolean isBoxAvailable(char[] box) {
+ for (int i = 0; i < 9; i++) {
+ if (box[i] != 'X' && box[i] != '0') {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public boolean checkWinner(byte winner) {
+ if (winner == 1) {
+ System.out.println("You won the game!\nCreated by Oleksandr Lysak. Thanks for playing!");
+ return true;
+ } else if (winner == 2) {
+ System.out.println("You lost the game!\nCreated by Oleksandr Lysak. Thanks for playing!");
+ return true;
+ } else if (winner == 3) {
+ System.out.println("It's a draw!\nCreated by Oleksandr Lysak. Thanks for playing!");
+ return true;
+ }
+ return false;
+ }
+}
diff --git a/src/main/java/Player.java b/src/main/java/Player.java
new file mode 100644
index 0000000..3087606
--- /dev/null
+++ b/src/main/java/Player.java
@@ -0,0 +1,20 @@
+import java.util.Scanner;
+
+public class Player {
+ public void playerMove(Scanner scan, char[] box) {
+ boolean validMove = false;
+ while (!validMove) {
+ byte input = scan.nextByte();
+ if (input > 0 && input < 10) {
+ if (box[input - 1] == 'X' || box[input - 1] == '0') {
+ System.out.println("That one is already in use. Enter another.");
+ } else {
+ box[input - 1] = 'X';
+ validMove = true;
+ }
+ } else {
+ System.out.println("Invalid input. Enter again.");
+ }
+ }
+ }
+}
diff --git a/target/classes/App.class b/target/classes/App.class
new file mode 100644
index 0000000..5353726
Binary files /dev/null and b/target/classes/App.class differ
diff --git a/target/classes/Board.class b/target/classes/Board.class
new file mode 100644
index 0000000..dcb469f
Binary files /dev/null and b/target/classes/Board.class differ
diff --git a/target/classes/Computer.class b/target/classes/Computer.class
new file mode 100644
index 0000000..2c5de85
Binary files /dev/null and b/target/classes/Computer.class differ
diff --git a/target/classes/Game.class b/target/classes/Game.class
new file mode 100644
index 0000000..251d258
Binary files /dev/null and b/target/classes/Game.class differ
diff --git a/target/classes/GameStateChecker.class b/target/classes/GameStateChecker.class
new file mode 100644
index 0000000..7f4257e
Binary files /dev/null and b/target/classes/GameStateChecker.class differ
diff --git a/target/classes/Player.class b/target/classes/Player.class
new file mode 100644
index 0000000..d32a24a
Binary files /dev/null and b/target/classes/Player.class differ