I'm trying to make a connect four game sumulation.
The program should have three threads - player 1, player 2 and game controller.The game controller thread has the responsibility of transfering control from one player to the other, has to monitor if a thread takes more than 5 second to execute, check if there is a winner and so on.
The interface the player classes must implement is:
public interface Player extends Runnable {
public int nextMove(int otherPlayerLastMove);
public void gameOver();
}
The method nextMove return the number of the column the player puts his next token in, and takes as a paramether the number of the column the oposing player put his last token.
The method gameOver must be called from the game controller thread to indicate end of the game and releasing of all resources from the player.
I don't have a problem with implementing the logic of the game it self, but I'm faceing a problem design wise. I just can't put my mind on what classes do I need and how they should interact. The obvious classes are PlayerImpl and GameController - which I think should keep the table for the game ( a 2D char array) and implement the checking logic. Or do I need a third class which contains only the table and to which the other two have access?
The basic code is here:
public class GameController implements Runnable{
private static char [][] table;
private void initTable(){
table = new char[6][7];
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
table[i][j] = '*';
}
}
}
public void printTable(){
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
System.out.print(table[i][j] + " ");
}
System.out.println();
}
}
private char checkWinner(){
//check for horizontal line
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < 4; j++) {
if ( table[i][j] != '*' && table[i][j+1] != '*' && table[i][j+2] != '*' && table[i][j+3] != '*' &&
table[i][j] == table [i][j+1] && table[i][j+1] == table[i][j+2] && table[i][j+2] == table[i][j+3])
return table[i][j];
}
}
//check for vertical line
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 7; j++) {
if ( table[i][j] != '*' && table[i+1][j] != '*' && table[i+2][j] != '*' && table[i+3][j] != '*' &&
table[i][j] == table [i+1][j] && table[i+1][j] == table[i+2][j] && table[i+2][j] == table[i+3][j])
return table[i][j];
}
}
// left to right diagonals
for (int i = 0; i < 3; i++) {
for (int j = 3; j < 7; j++) {
if ( table[i][j] != '*' && table[i+1][j-1] != '*' && table[i+2][j-2] != '*' && table[i+3][j-3] != '*' &&
table[i][j] == table [i+1][j-1] && table[i+1][j-1] == table[i+2][j-2] && table[i+2][j-2] == table[i+3][j-3])
return table[i][j];
}
}
// right to left diagonals
for (int i = 0; i < 2; i++) {
for (int j = 3; j >=0; j--) {
if ( table[i][j] != '*' && table[i+1][j+1] != '*' && table[i+2][j+2] != '*' && table[i+3][j+3] != '*' &&
table[i][j] == table [i+1][j+1] && table[i+1][j+1] == table[i+2][j+2] && table[i+2][j+2] == table[i+3][j+3])
return table[i][j];
}
}
return 0;
}
public void addToken(int column, char token){
for (int i = table.length ; i >= 0 ; i--) {
if(table[i][column] == '*'){
table[i][column] = token;
break;
}
}
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
So how would you make the design of such a problem? An explanation or just a UML diagram would be great. Thanks in advance :)
Aucun commentaire:
Enregistrer un commentaire