patternjavaModerate
Does this Tic-Tac-Toe game follow abstraction and encapsulation?
Viewed 0 times
thistoeticabstractionencapsulationtacgamefollowdoesand
Problem
This below program will be further enhanced for
Currently this program is written for choosing best move for a given grid position.
```
package project2.tictactoe;
enum TicTacToe{
X, O, NULL;
@Override
public String toString(){
switch(this){
case X:
return "X";
case O:
return "O";
default:
return "NULL";
}
}
}
public class TicTacToeGame{
TicTacToe[][] twoDimArray = new TicTacToe[3][3];
TicTacToeGame(){
for(int i = 0; i bestScore){
bestScore = score;
bestMove = "Place \"" + TicTacToe.O + "\" at position [" + i/3 + "][" + i%3 + "]";
}
}
if(side == TicTacToeGame.HUMAN_TURN){
if(score < bestScore){
bestScore = score;
bestMove = "Place \"" + TicTacToe.X + "\" at position [" + i/3 + "][" + i%3 + "]";
}
}
/ Undo the possible move /
if(side == TicTacToeGame.COMPUTER_TURN){
this.twoDimArray[i/3][i%3] = TicTacToe.NULL;
}else{
this.twoDimArray[i/3][i%3] = TicTacToe.NULL;
}
}
}
return bestScore;
}
void printElements(){
for(int i = 0; i < 3; i++){
System.out.println(" " + twoDimArray[i][0] + " | " + twoDimArray[i][1] + " | " + twoDimArray[i][2]);
if(i != 2)
System.out.println("------------------");
}
}
public static void main(String[] args){
TicTacToeGame objRef = new TicTacToeGame();
boolean side = true; //first move is computer move
/* For an
TicTacToe game with Human and Computer as players.Currently this program is written for choosing best move for a given grid position.
```
package project2.tictactoe;
enum TicTacToe{
X, O, NULL;
@Override
public String toString(){
switch(this){
case X:
return "X";
case O:
return "O";
default:
return "NULL";
}
}
}
public class TicTacToeGame{
TicTacToe[][] twoDimArray = new TicTacToe[3][3];
TicTacToeGame(){
for(int i = 0; i bestScore){
bestScore = score;
bestMove = "Place \"" + TicTacToe.O + "\" at position [" + i/3 + "][" + i%3 + "]";
}
}
if(side == TicTacToeGame.HUMAN_TURN){
if(score < bestScore){
bestScore = score;
bestMove = "Place \"" + TicTacToe.X + "\" at position [" + i/3 + "][" + i%3 + "]";
}
}
/ Undo the possible move /
if(side == TicTacToeGame.COMPUTER_TURN){
this.twoDimArray[i/3][i%3] = TicTacToe.NULL;
}else{
this.twoDimArray[i/3][i%3] = TicTacToe.NULL;
}
}
}
return bestScore;
}
void printElements(){
for(int i = 0; i < 3; i++){
System.out.println(" " + twoDimArray[i][0] + " | " + twoDimArray[i][1] + " | " + twoDimArray[i][2]);
if(i != 2)
System.out.println("------------------");
}
}
public static void main(String[] args){
TicTacToeGame objRef = new TicTacToeGame();
boolean side = true; //first move is computer move
/* For an
Solution
Does this program follow the elements of OOPs paradigm, abstraction and encapsulation?
Not really.
It started almost fine with a field contents enum (why override toString and not change actual output?), but then degenerated rapidly.
Your main class does everything: startup and lifecycle, IO, game logic, game data storage, game AI. While it is decomposed to different methods, having one class do everything meaningful does not follow the OOP paradigm.
You should try to decompose your program to several classes each doing just one job. Have one class with the main method starting the app. Have another controlling the game lifecycle. And another encapsulate all the data. Yet another could perform IO - presentation of this data to the user and acquiring input. Finally, for the task of making the actual move, you might have another class (or, rather, interface - so the live and the computer players were similar for the game engine, being different implementations of the same interface).
Not really.
It started almost fine with a field contents enum (why override toString and not change actual output?), but then degenerated rapidly.
Your main class does everything: startup and lifecycle, IO, game logic, game data storage, game AI. While it is decomposed to different methods, having one class do everything meaningful does not follow the OOP paradigm.
You should try to decompose your program to several classes each doing just one job. Have one class with the main method starting the app. Have another controlling the game lifecycle. And another encapsulate all the data. Yet another could perform IO - presentation of this data to the user and acquiring input. Finally, for the task of making the actual move, you might have another class (or, rather, interface - so the live and the computer players were similar for the game engine, being different implementations of the same interface).
Context
StackExchange Code Review Q#78459, answer score: 10
Revisions (0)
No revisions yet.