Skip to content

Commit

Permalink
V 1.0 : Full functionality of termiTacToe implemented. Ready for use.…
Browse files Browse the repository at this point in the history
… Future updates include (but not limited to): Play by play notifications for invalid moves, Terminal UI improvementes
  • Loading branch information
ReggieRD committed Dec 11, 2023
1 parent 2665143 commit 7cfb071
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions tictactoe.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class tictactoe{
public static void main (String [] args){
int dimensions;
board = new char[3][3];
clearScreen();
run();
}

Expand All @@ -30,7 +31,38 @@ public static boolean delete(int row, int col, char c){

}
public static char checkWin(){
return 0;
boolean hasWon = false;
/* horizontal check, future updates will be more dynamically implemented
* for larger board sizes*/
for (int row = 0; row < board.length; row++){
if(board[row][0] == board[row][1] && board[row][1] ==
board[row][2]){
hasWon = true;
return board[row][0];
}
}
/*vertical checks*/
for (int col = 0; col < board.length; col++){
if(board[0][col] == board[1][col] && board[1][col] ==
board[1][col]){
hasWon = true;
return board[0][col];
}
}
/* Top left to bottom right diagonal*/
if (board[0][0] == board[1][1] && board[1][1] == board[2][2]){
hasWon = true;
return board[0][0];
}

/* Top right to bottom left diagonal*/
if (board[1][2] == board[1][1] && board[1][1] == board[2][1]){
hasWon = true;
return board[1][2];
}

return '_';

}
/* some additional functions to spice up the game:
swap two characters (in the event of a full board, players
Expand All @@ -41,7 +73,7 @@ public static boolean swap(int[] a, int[] b){
board[a[0]][a[1]] = board[b[0]][b[1]];
board[b[0]][b[1]] = temp;
return true;
/*TODO validation*/
/*TODO index validation (ensure index is in bounds)*/
}

/*===============================================================
Expand Down Expand Up @@ -96,7 +128,7 @@ place their own (costs 4 credits)
credits increase by 1 with each turn
*/
while(running) {
clearScreen();

System.out.println("================================================");
System.out.println("X Credits: "+xCredits+"|| O Credits: "+oCredits);
System.out.print("Current Player: ");
Expand Down Expand Up @@ -176,7 +208,14 @@ place their own (costs 4 credits)
if (playerX) xCredits++;
else oCredits++;
playerX = !playerX;

char winner = checkWin();
if (winner != '_'){
clearScreen();
printBoard();
System.out.println(winner+ " wins the game!");
running = false;
break;
} else clearScreen();
}

}
Expand Down

0 comments on commit 7cfb071

Please sign in to comment.