Skip to content

Commit 5602e21

Browse files
committed
Initial Commit
0 parents  commit 5602e21

File tree

7 files changed

+197
-0
lines changed

7 files changed

+197
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bin/

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"java.project.sourcePaths": ["src"],
3+
"java.project.outputPath": "bin",
4+
"java.project.referencedLibraries": [
5+
"lib/**/*.jar"
6+
]
7+
}

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# TicTacToe-Java
2+
Just a re-creation of my horrible [python TicTacToe Game](https://github.com/DuskyElf/TicTacToe-py) which I made few months ago, but in java. It's my first program in java so nothing special in it.
3+
4+
## Getting Started with VS code Java
5+
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
6+
7+
## Folder Structure
8+
9+
The workspace contains two folders by default, where:
10+
11+
- `src`: the folder to maintain sources
12+
- `lib`: the folder to maintain dependencies
13+
14+
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
15+
16+
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
17+
18+
## Dependency Management
19+
20+
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

src/App.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class App {
2+
public static void main(String[] args) throws Exception{
3+
Game game = new Game();
4+
game.gameLoop();
5+
Input.close();
6+
}
7+
}

src/Board.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
public class Board {
2+
public enum Cell {
3+
N,
4+
X,
5+
O
6+
}
7+
8+
Cell[][] boardState;
9+
10+
public Board() {
11+
boardState = new Cell[][]{
12+
{Cell.N, Cell.N, Cell.N},
13+
{Cell.N, Cell.N, Cell.N},
14+
{Cell.N, Cell.N, Cell.N}
15+
};
16+
}
17+
18+
// For playing a moves
19+
public void move(int[] place, Cell player) {
20+
boardState[place[0]][place[1]] = player;
21+
}
22+
23+
// String representation for the board
24+
public String display() {
25+
String result = "";
26+
result += "___________\n";
27+
28+
for (Cell[] i: boardState){
29+
result += "|";
30+
for (Cell j: i) {
31+
if (j == Cell.O) {
32+
result += "_O_";
33+
} else if(j == Cell.X) {
34+
result += "_X_";
35+
} else {
36+
result += "_ _";
37+
}
38+
}
39+
result += "|\n";
40+
}
41+
42+
return result;
43+
}
44+
}

src/Game.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
public class Game {
2+
public Board board;
3+
public Board.Cell currentPlayer;
4+
boolean running;
5+
int gameLap;
6+
Board.Cell winner;
7+
8+
public Game() {
9+
board = new Board();
10+
currentPlayer = Board.Cell.N;
11+
running = true;
12+
gameLap = 0;
13+
winner = Board.Cell.N;
14+
}
15+
16+
// Utility
17+
public static char s(Board.Cell player) {
18+
if (player == Board.Cell.X) return 'X';
19+
if (player == Board.Cell.O) return 'O';
20+
return 'n';
21+
}
22+
23+
// Main Game loop
24+
public void gameLoop() {
25+
currentPlayer = Board.Cell.X;
26+
27+
while (running) {
28+
gameLap += 1;
29+
30+
int[] response = askPlayer(currentPlayer);
31+
board.move(response, currentPlayer);
32+
incrementPlayer();
33+
System.out.println(board.display());
34+
35+
winner = winCheck();
36+
if (winner != Board.Cell.N)
37+
winAnnounce();
38+
}
39+
}
40+
41+
// Player input
42+
int[] askPlayer(Board.Cell player) {
43+
boolean done = false;
44+
int[] move = new int[2];
45+
46+
while (! done) {
47+
System.out.printf
48+
("[%d] Player %c your turn, row number (1, 2, 3): ",
49+
gameLap, s(player));
50+
int row = Input.sc.nextInt() - 1;
51+
52+
System.out.printf
53+
("[%d] Player %c your turn, collum number (1, 2, 3): ",
54+
gameLap, s(player));
55+
int collum = Input.sc.nextInt() - 1;
56+
57+
move[0] = row;
58+
move[1] = collum;
59+
done = validMove(move);
60+
}
61+
62+
return move;
63+
}
64+
65+
// Checking if the move is valid or not
66+
boolean validMove(int[] move) {
67+
if (move[0] > 2 | move[1] > 2) return false;
68+
return board.boardState[move[0]][move[1]] == Board.Cell.N;
69+
}
70+
71+
// Incrementing to the next Player
72+
void incrementPlayer() {
73+
if (currentPlayer == Board.Cell.X)
74+
currentPlayer = Board.Cell.O;
75+
else currentPlayer = Board.Cell.X;
76+
}
77+
78+
// Checking if someone won
79+
Board.Cell winCheck() {
80+
Board.Cell[][] b = board.boardState;
81+
Board.Cell result = Board.Cell.N;
82+
83+
for (int i = 0; i < 3; i++) {
84+
// Horizontal Checks
85+
if (b[i][0] == b[i][1] & b[i][1] == b[i][2])
86+
result = b[i][0];
87+
88+
// Virtical Checks
89+
if (b[0][i] == b[1][i] & b[1][i] == b[2][i])
90+
result = b[0][i];
91+
}
92+
93+
// Diagonal Checks
94+
if (b[0][0] == b[1][1] & b[1][1] == b[2][2])
95+
result = b[0][0];
96+
if (b[2][0] == b[1][1] & b[1][1] == b[0][2])
97+
result = b[2][0];
98+
99+
return result;
100+
}
101+
102+
// Announcment that someone won, and killing the gameloop
103+
void winAnnounce() {
104+
System.out.printf("Player %s won the game in %d Game laps.\n",
105+
s(winner), gameLap
106+
);
107+
System.out.println("Whooo!");
108+
running = false;
109+
}
110+
}

src/Input.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import java.util.Scanner;
2+
3+
public class Input {
4+
public static Scanner sc = new Scanner(System.in);
5+
public static void close() {
6+
sc.close();
7+
}
8+
}

0 commit comments

Comments
 (0)