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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions G5-java-dev-homework
Submodule G5-java-dev-homework added at 07f1a4
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>Game</name>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
Expand Down
102 changes: 16 additions & 86 deletions src/main/java/App.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/Board.java
Original file line number Diff line number Diff line change
@@ -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] = ' ';
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/Computer.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Loading