Опытный
 
Профиль
Группа: Участник
Сообщений: 443
Регистрация: 16.5.2006
Где: Киев
Репутация: нет Всего: 2
|
Ну вот подготовился к критеке. Описание задачи Цель работы - на практике обучиться технологии, получить практические навыки и сделать что то полезное для общества. Игра Го - что то на подобии точек  В моем упрошеннном понимании. Вот правила http://go.hobby.ru/school-02.htmЗадание - сделать апплет позволяющий 2 людям по интернету играть в эту игру. (С возможностью просмотра третьими лицами) Подход к разработке: Решил ввиду отсутствия какого то опыта делать все мелкими этапами (итерациями) Часть 1 Разработка интерфейса. Вопрос 1. В ООП я пока не силен, но стараюсь учиться по этому первое - оцените мою декомпозицию первой части задачи Я вижу необходимость создания таких класов: Основа: 1. Класс менеджер игры Reversi (сам апплет) - выполняет управления ходами, 2. Класс доска (Board) - ответственный за размещение едениц на доске, ⁃ просчет живих и мертвых отрядов, валидацию ходов 3. Класс поле (Field) - поле доски (доска - двумерный масив полей) 4. Класс боевая еденица Piece (Еденица с координатами) Я попробовал изобразить это все как диаграмму класов (смотри аттач) Но скорее всего она тоже должна быть исправлена Вот собственно код: Main class Код | package ua;
import java.applet.*; import java.awt.Event; import java.awt.Graphics;
@SuppressWarnings("serial") public class Reversi extends Applet{ boolean local; final boolean WHITE = true; final boolean BLACK = false;
boolean turn = WHITE; public int size = 10; // Board's size in future can be 12,13,19 (user defined)
Point point; // used to convert click coords into field number public int width; // Applet width public int height; // Applet height Board gameBoard; // Board object
@Override public boolean mouseDown(Event e, int x, int y){ System.out.println("The mouse was pressed"); this.point = gameBoard.findFieldByCoords(x, y); turn = move(point.x, point.y, turn); // turn is changed by the function return true; }
/* * Function used for moves validation */
public boolean move(int x, int y, boolean turn){ turn = gameBoard.gameBoard[point.x][point.y].addPiece(turn); //System.out.println(gameBoard.checkBorders(point.x, point.y)); System.out.println(gameBoard.locateEnemy(point.x, point.y));
gameBoard.unmarkAllPieces(); repaint(); return turn; }
// public @Override public void init(){ System.out.println("The applet was initialized");
// How to replace these two calls? this.width = bounds().width - 2; this.height = bounds().height - 2;
this.gameBoard = new Board(size, width/size, height/size); }
@Override public void paint(Graphics g){ gameBoard.paint(g); } }
|
Board class Код | package ua;
import java.awt.Graphics;
public class Board{ int size; int width, height, dw, dh; Field gameBoard[][]; Point coords; public Board(int size, int dw, int dh){ this.size = size; this.dw = dw; this.dh = dh; this.gameBoard = new Field[size][size]; initBoard(); }
/* * Initializes new board. No playing Pieces are set on the board */ public void initBoard(){ for (int y = 0; y < size; y++) for (int x=0;x<size; x++){ int x1 = x*dw; int y1 = y*dh; System.out.println(gameBoard); gameBoard[x][y] = new Field(x1, y1, dw, dh); } } ///////
// Used to locate // where to set a Piece public Point findFieldByCoords(int xo, int yo){ for (int i=0; i<size; i++) for (int j=0; j<size; j++) if( xo > gameBoard[i][j].dimensions.x1 & yo > gameBoard[i][j].dimensions.y1 & xo < gameBoard[i][j].dimensions.x2 & yo < gameBoard[i][j].dimensions.y2) coords = new Point(i, j); return coords; }
// Checks if there is a space on the left of the field // Don't like huge quantity of ifs :( public int checkBorders(int x, int y){
gameBoard[x][y].isChecked = true; //System.out.println(size); int space = 0; // Checking the left side of the Piece if (x > 0) { if (gameBoard[x - 1][y].piece != null) { if ((gameBoard[x - 1][y].piece.side == gameBoard[x][y].piece.side) & (gameBoard[x - 1][y].isChecked == false)) space += checkBorders(x - 1, y); } else if(gameBoard[x-1][y].isChecked == false){ space += 1; gameBoard[x-1][y].isChecked = true; } }
// Checking the right side of the field if (x < size - 1) { if (gameBoard[x + 1][y].piece != null) { if ((gameBoard[x + 1][y].piece.side == gameBoard[x][y].piece.side) & (gameBoard[x + 1][y].isChecked == false)) space += checkBorders(x + 1, y); } else if (gameBoard[x+1][y].isChecked == false){ space += 1; gameBoard[x+1][y].isChecked = true; } } // Checking the top side of the field if (y > 0) { if (gameBoard[x][y - 1].piece != null) { if ((gameBoard[x][y - 1].piece.side == gameBoard[x][y].piece.side) & (gameBoard[x][y - 1].isChecked == false)) space += checkBorders(x, y - 1); } else if (gameBoard[x][y-1].isChecked == false){ space += 1; gameBoard[x][y-1].isChecked = true; } } // Checking the botom of the field if (y < size - 1) { if (gameBoard[x][y + 1].piece != null) { if ((gameBoard[x][y + 1].piece.side == gameBoard[x][y].piece.side) & (gameBoard[x][y + 1].isChecked == false)) space += checkBorders(x, y + 1); } else if (gameBoard[x][y+1].isChecked == false){ space += 1; gameBoard[x][y+1].isChecked = true; } } //System.out.println("The call for " + x + ";" + y + "returned " +space); return space; }
// Method which kills a group of pieces // It should be called only if pieces are checked with // checkBorders public int killGroup(int x, int y){
if(gameBoard[x+1][y].piece.side == gameBoard[x][y].piece.side) killGroup(x+1, y);
if(gameBoard[x - 1][y].piece.side == gameBoard[x][y].piece.side) killGroup(x - 1, y);
if(gameBoard[x][y - 1].piece.side == gameBoard[x][y].piece.side) killGroup(x, y - 1);
if(gameBoard[x][y + 1].piece.side == gameBoard[x][y].piece.side) killGroup(x, y + 1);
gameBoard[x][y].piece = null;
return 1; } // Searching for enemyies which are near the field x, y public int locateEnemy(int x, int y){
if (x > 0){ int spaces = 0; if (gameBoard[x - 1][y].piece != null) if (gameBoard[x - 1][y].piece.side != gameBoard[x][y].piece.side) { spaces = checkBorders(x - 1, y); System.out.println("Enemy has: " + spaces + " spaces"); if (spaces == 0) killGroup(x - 1, y);
} }
if (x < size - 1){ int spaces = 0; if (gameBoard[x + 1][y].piece != null) if (gameBoard[x + 1][y].piece.side != gameBoard[x][y].piece.side) if(checkBorders(x + 1, y) == 0) killGroup(x + 1, y);
}
if (y > 0){ int spaces = 0; if (gameBoard[x][y - 1].piece != null) if (gameBoard[x][y - 1].piece.side != gameBoard[x][y].piece.side) if (checkBorders(x, y - 1) == 0) killGroup(x, y - 1);
} if (y < size - 1){ int spaces = 0; if (gameBoard[x][y + 1].piece != null) if (gameBoard[x][y + 1].piece.side != gameBoard[x][y].piece.side) if (checkBorders(x, y + 1) == 0) killGroup(x, y + 1);
}
return 0;
}
public void unmarkAllPieces(){ for (int j = 0; j< size; j++) for (int i = 0; i< size; i++) gameBoard[i][j].isChecked = false;
}
public void paint(Graphics g){ for (int y=0; y<size; y++) for(int x=0; x<size; x++) gameBoard[x][y].paint(g); } }
|
Field class Код | package ua;
import java.awt.Color; import java.awt.Graphics;
public class Field { Piece piece; public int side; public Rect dimensions; boolean color; boolean isChecked;
// Constructor public Field(int x1, int y1, int width, int height) { dimensions = new Rect(x1, y1, width, height); }
// Method which adds a piece to the field public boolean addPiece(boolean side){ if (isEmpty()) piece = new Piece(dimensions.x1, dimensions.y1, dimensions.width, side); else return side; return !side; }
// This method checks if current field is empty, or not public boolean isEmpty(){ if (piece != null) return false; else return true;
}
public void paint(Graphics g){ g.setColor(Color.BLACK); g.drawRect(dimensions.x1, dimensions.y1, dimensions.width, dimensions.height); if (piece != null) piece.paint(g); } }
|
Piece class Код | package ua;
import java.awt.Color; import java.awt.Graphics;
public class Piece { public Color color; int width; int x, y; boolean side;
boolean isChecked = false; public Piece(int x1, int y1, int width, boolean side){ this.x = x1; this.y = y1; this.width = width; this.side = side; }
@SuppressWarnings("static-access") public void paint(Graphics g){ g.drawOval(x, y, width, width); if (side == true) g.setColor(color.BLUE); else g.setColor(color.BLACK); g.fillOval(x, y, width, width); } }
|
Спасибо за ваше время
|