monkey90 Δημοσ. 20 Φεβρουαρίου 2012 Δημοσ. 20 Φεβρουαρίου 2012 Εχω φτιάξει έναν πίνακα αντικειμένων chess[8][8] και αρχικοποιώ τις θέσεις του αναλόγος με άσπρα και μάυρα πούλια. Τα άσπρα απικονίζονται με κεφαιλαία γράμματα και τα μάυρα με μικρά. Για κάποιο λόγο όταν πάω να εμφανίσω τον πίνακα μου τα εμφανίζει όλα κεφαλαία. Το ψάχνω πολύ ώρα τώρα εχω κολλήσει αν μπορείται να βρείται το λάθος θα μου κάνατε μεγάλη χάρη...αμα χρειάζεται κάποια άλλη κλάση πείτε μου. Στην κλάση board μέθοδο init() δημιουργώ τον πίνακα. Μετά καλείται η toString της Board δουλειά της οποίας είναι να εμφανίσει τον πίνακα. Η toString της Board καλεί την toString που υπάρχει στην Piece αναλόγος πιο κομμάτι είναι η οποία αν είναι Ασπρο επιστρέφει κεφαλαίο γράμμα και αν είναι Μαυρο μικρό. > public class Board { Piece[][] chess; void init() { Location a; //dhmiourgia skakiou for(int i=2;i<5;i++) { for(int j=0;j<8;j++) { a = new Location(i,j); } } chess = new Piece[8][8]; for(int i=0;i<8;i++) { a = new Location(1,i); Piece p = new Pawn(Color.BLACK , a, this); chess[1][i] = p; } a = new Location(0,0); Piece r = new Rook(Color.BLACK , a, this); chess[0][0] = r; a = new Location(0,1); Piece n = new Knight(Color.BLACK , a,this); chess[0][1] = n; a = new Location(0,2); Piece b = new Bishop(Color.BLACK , a, this); chess[0][2] = b; a = new Location(0,3); Piece q = new Queen(Color.BLACK , a, this); chess[0][3] = q; a = new Location(0,4); Piece k = new King(Color.BLACK , a, this); chess[0][4] = k; a = new Location(0,5); b = new Bishop(Color.BLACK , a, this); chess[0][5] = b; a = new Location(0,6); n = new Knight(Color.BLACK , a, this); chess[0][6] = n; a = new Location(0,7); r = new Rook(Color.BLACK , a, this); chess[0][7] = r; for(int i=0;i<8;i++) { a = new Location(6,i); Piece P = new Pawn(Color.WHITE , a, this); chess[6][i] = P; } a = new Location(7,0); Piece R = new Rook(Color.WHITE , a, this); chess[7][0] = R; a = new Location(7,1); Piece N = new Knight(Color.WHITE , a, this); chess[7][1] = N; a = new Location(7,2); Piece B = new Bishop(Color.WHITE , a, this); chess[7][2] = B; a = new Location(7,3); Piece Q = new Queen(Color.WHITE , a, this); chess[7][3] = Q; a = new Location(7,4); Piece K = new King(Color.WHITE , a, this); chess[7][4] = K; a = new Location(7,5); B = new Bishop(Color.WHITE , a, this); chess[7][5] = B; a = new Location(7,6); N = new Knight(Color.WHITE , a, this); chess[7][6] = N; a = new Location(7,7); R = new Rook(Color.WHITE , a, this); chess[7][7] = R; } @Override public String toString() { String image =" abcdefgh\n"; for(int i=0;i<8;i++) { image += String.valueOf(8-i); for(int j=0;j<8;j++) { if(chess[i][j]==null) { image +=" "; } else { image +=chess[i][j].toString(); } } image += String.valueOf(8-i) + "\n"; } image +=" abcdefgh\n"; return image; } > public abstract class Piece { static Color Colo; static Location Posi; static Board bo; abstract void moveTo(Location newLoc); public Piece(Color col, Location pos, Board boa) { Colo = col; Posi = pos; bo = boa; } } class King extends Piece { public King(Color col, Location pos, Board boa) { super(col,pos,boa); } void moveTo(Location newLoc) { } @Override public String toString() { String r= "k"; if(Colo == Color.WHITE) { r= "K"; } return r; } } class Queen extends Piece { public Queen(Color col, Location pos, Board boa) { super(col,pos,boa); } void moveTo(Location newLoc) { } @Override public String toString() { String r= "q"; if(Colo ==Color.WHITE) { r= "Q"; } return r; } } class Rook extends Piece { public Rook(Color col, Location pos, Board boa) { super(col,pos,boa); } void moveTo(Location newLoc) { } @Override public String toString() { String r= "r"; if(Colo == Color.WHITE) { r= "R"; } return r; } } class Knight extends Piece { public Knight(Color col, Location pos, Board boa) { super(col,pos,boa); } void moveTo(Location newLoc) { } @Override public String toString() { String r= "n"; if(Colo == Color.WHITE) { r= "N"; } return r; } } class Bishop extends Piece { public Bishop(Color col, Location pos, Board boa) { super(col,pos,boa); } void moveTo(Location newLoc) { } @Override public String toString() { String r= "b"; if(Colo == Color.WHITE) { r= "B"; } return r; } } class Pawn extends Piece { public Pawn(Color col, Location pos, Board boa) { super(col,pos,boa); } void moveTo(Location newLoc) { } @Override public String toString() { String r= "p"; if(Colo == Color.WHITE) { r= "P"; } return r; } } > public enum Color { BLACK, WHITE; Color nextColor(Color a) { if(a==BLACK) { return WHITE; } return BLACK; } } > public class Location { static char Row, Col; static int Rowint, Colint; public Location(int r, int c) { Colint = c; //stiles Rowint = r; //grames } public Location(String loc) { Col = loc.charAt(0); Row = loc.charAt(1); } static int getCol(char c) { int co = Character.getNumericValue(c)-9; return co; } static int getRow(char r) { int ro = Character.getNumericValue(r); return ro; } }
monkey90 Δημοσ. 20 Φεβρουαρίου 2012 Μέλος Δημοσ. 20 Φεβρουαρίου 2012 Καταραμένος προγραμματισμός έχω γράψει κάπου στις 1000 γραμμές κώδικα, δε βγάζει error, αλλά δεν κάνει τίποτα. Πιστέυω ότι το λάθος βρίσκεται σε αυτό που ρωτάω αν μπορεί κάποιος να με βοηθήσει θα κάνω κολοτούμπες έχω χάσει το μπούσουλα.
bnvdarklord Δημοσ. 20 Φεβρουαρίου 2012 Δημοσ. 20 Φεβρουαρίου 2012 Πολυ περιεργο. Δεν εχω βρει ακομα τι φταιει ακριβώς αλλα οταν ετρεξα αυτο > public static void main(String args[]) { Board b = new Board(); b.init(); System.out.println(b.toString()); System.out.println("to 0,0 einai " + b.chess[0][0].Colo); System.out.println("to 7,7 einai" + b.chess[7][7].Colo); } φαινεται οτι και το 0,0 και το 7,7 τα θεωρεί WHITE, παρόλο που εσύ περνάς το 0,0 ως BLACK. Το λεω σε περίπτωση που καταλαβεις γιατι συμβαίνει αυτό, γιατι εγω προς το παρον δεν βλεπω τι φταιει.
Timonkaipumpa Δημοσ. 20 Φεβρουαρίου 2012 Δημοσ. 20 Φεβρουαρίου 2012 Η γνώμη μου είναι ότι έχεις κάνει λάθος μοντελοποίηση. Από που και έως που να ξέρει το board που πρέπει να πάει κάθε πιόνι; Και γιατί τα πιόνια δεν ξέρουν ποιες είναι οι επιτρεπτές κινήσεις τους; Το board το μόνο που πρέπει να ξέρει είναι τι πιόνι είναι σε τι τετράγωνο και τι γίνεται στα τετράγωνά του εν γένει. Τίποτα άλλο. Τα πιόνια να ξέρουν τι κινήσεις μπορούν να κάνουν, ανάλογα με το είδος τους και ανάλογα το χρώμα τους. Και επιπλέον, το παιχνίδι έχει κανόνες. El passan κίνηση (π.χ.).
bnvdarklord Δημοσ. 20 Φεβρουαρίου 2012 Δημοσ. 20 Φεβρουαρίου 2012 Το λαθος σου το βρήκα. Εχεις δηλώσει τα μέλη της μεθοδου Piece ως static με αποτέλεσμα οι τιμές τους να αντιπροσωπεύουν όλα τα αντικειμενα τυπου Piece που φτιαχνεις.
Προτεινόμενες αναρτήσεις
Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε
Πρέπει να είστε μέλος για να αφήσετε σχόλιο
Δημιουργία λογαριασμού
Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!
Δημιουργία νέου λογαριασμούΣύνδεση
Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.
Συνδεθείτε τώρα