Java 1 - Εργασία με σταυρόλεξο

Παρακολουθώ όλα τα μαθήματα.

Συντονιστές: markelos, Ryu, φιάλη klein, meleneemil

Απάντηση
a0maria
Δημοσιεύσεις: 3
Εγγραφή: Τετ Σεπ 03, 2008 2:52 pm
Real Name: Maria
Gender: Female
Facebook ID: 0

Java 1 - Εργασία με σταυρόλεξο

Δημοσίευση από a0maria »

Re paidia mporei kaneis na dwsei estw kana hint gia tin askisi tou symvoni me to stavrolekso?????
Plz plz plz :(
gwgoula18
Δημοσιεύσεις: 9
Εγγραφή: Κυρ Σεπ 14, 2008 7:05 pm
Real Name: Γωγούλα
Facebook ID: 0

Re: Java1-ergasia

Δημοσίευση από gwgoula18 »

k egw thelw voitheia paidiaaaaaaaaaaaaaaaaaaa................. :e_sad:
Άβαταρ μέλους
theos
Δημοσιεύσεις: 762
Εγγραφή: Κυρ Νοέμ 05, 2006 4:53 am
Real Name: Αριστοτέλης-Εμμανουήλ Θάνος-Φίλης (Μάνος) ge04017
Gender: Male
Τοποθεσία: Alwaysland

Re: Java 1 - Εργασία με σταυρόλεξο

Δημοσίευση από theos »

Πείτε συγκεκριμένα με τι έχετε πρόβλημα. Τι δυσκολεύεστε συγκεκριμένα να υλοποιήσετε
Λογική είναι η τέχνη να κάνεις λάθος με αυτοπεποίθηση!!!
a0maria
Δημοσιεύσεις: 3
Εγγραφή: Τετ Σεπ 03, 2008 2:52 pm
Real Name: Maria
Gender: Female
Facebook ID: 0

Re: Java 1 - Εργασία με σταυρόλεξο

Δημοσίευση από a0maria »

Sugekrimena eee???Twra an sou pw oti ola duskolevomai na ta ulopoiisw ti 8a m peis???K ok to pira apofasi tin ergasia dn 8a ti pradwsw (profanws) alla toulaxiston na katalavw ligo ti paizei mipws k kataferw na grapsw katitis meta... Paizei na dwseis kamia voi8eia???? Eeee thee??? :e_confused:
Άβαταρ μέλους
Falgorn
Δημοσιεύσεις: 1233
Εγγραφή: Τετ Οκτ 29, 2008 4:35 pm
Real Name: Hector-Xavier
Gender: Male
Facebook ID: 0
Τοποθεσία: Πιο πέρα από το εκεί.
Επικοινωνία:

Re: Java 1 - Εργασία με σταυρόλεξο

Δημοσίευση από Falgorn »

Για να πάρεις βοήθεια, πρέπει να ρωτήσεις και κάτι συγκεκριμένο ρε συ... Εκτός αν το πρόβλημα είναι ότι δεν την παλεύεις με τη Java, οπότε ξαναξεκινάμε από τα βασικά. Αν είναι, σε μερικές μέρες σκοπεύω να κάνω post τον κώδικα, εφ' όσον τον τελειώσω, οπότε θα μπορεί να τον μελετήσει ο καθένας...
Όλοι λένε τ’ ορμητικό ρέμα βίαιο

Μά την κοίτη του ποταμού που το κρατάει

Κανείς δεν τη λέει βίαιη.


Bertolt Brecht


Η προπαγάνδα της ημέρας ಠ_ರೃ
a0maria
Δημοσιεύσεις: 3
Εγγραφή: Τετ Σεπ 03, 2008 2:52 pm
Real Name: Maria
Gender: Female
Facebook ID: 0

Re: Java 1 - Εργασία με σταυρόλεξο

Δημοσίευση από a0maria »

Ma auto einai to provlima.... Stin arxi kp epafi eixa... Meta to exasa to paixnidi.... Padws an kaneis post to kwdika 8a voi8isei polu... Tnx ek twn proterwn... :)
Άβαταρ μέλους
Falgorn
Δημοσιεύσεις: 1233
Εγγραφή: Τετ Οκτ 29, 2008 4:35 pm
Real Name: Hector-Xavier
Gender: Male
Facebook ID: 0
Τοποθεσία: Πιο πέρα από το εκεί.
Επικοινωνία:

Re: Java 1 - Εργασία με σταυρόλεξο

Δημοσίευση από Falgorn »

Ok, τελικά η άσκηση ολοκληρώθηκε! Παραθέτω τον κώδικα. (αν την κάνετε compile στο BlueJ και δώσει ένα περίεργο μήνυμα ο compiler, είναι επειδή δε μπορεί να ελέγξει αν κάποιες εντολές συμπεριφέρονται όπως θα έπρεπε. Το πρόγραμμα λειτουργεί κανονικά)

import java.util.*;



/**

* Class creates an empty crossword, as well as a list for words that will be attempted to be placed in the crossword.

* After words have been placed in the list, as many as possible of those are placed in the

* crossword and the remaining places are filled with random letters.

*

* @author (Falgorn)

* @version (5/2/2009)

*/

public class Cross

{

private int lines, columns, number_of_words=0, placed_words = 0;

private char crossword[][];

private Random random_number = new Random();

private ArrayList word_list = new ArrayList();

private ArrayList solution = new ArrayList();



//int lines : the number of lines the crossword will have

//int columns : the number of columns the crossword will have

//int number_of_words : the number of words ArrayList word_list has

//int placed words : the number of words that have been placed in the crossword.

//Also, the number of Word objects in ArrayList solution.

//char crossword[][] : the crossword

//ArrayList word_list : list of words that will be attempted to be placed in the crossword

//ArrayList solution : list of words that have been placed in the crossword



/**

* Creates the empty crossword.

*

* @param int a : the number of lines the crossword will have

* @param int b : the number of columns the crossword will have

*/

public Cross(int a, int b)

{

lines = a;

columns = b;

crossword = new char [lines][columns];



for (int i=0; i<lines; i++)

for (int j=0; j<columns; j++)

crossword[j] = ' ';

}



/**

* Adds a word to a list of words (ArrayList word_list) that will be placed (if possible) in the crossword.

* Words are sorted according to the number of letters they have, from the shortest to the longest.

*

* @param String a_word : the word to be added

*/

public void add_a_word_to_word_list (String a_word)

{

int i;

for (i=number_of_words; i>0; i--)

{

String temp = (String) word_list.get(i-1);

if (temp.length() < a_word.length())

break;

}

word_list.add(i, a_word);

number_of_words++;

}



/**

* checks whether a word can be placed at a given place in the crossword, with a given orientation.

*

* aWord : the word in question

* x : the line at which the word's first letter will be placed

* y : the column at which the word's first letter will be placed

* d : signifies the word's direction, "h" for "horizontal" and "v" for "vertical"

*/

private boolean is_legal(String aWord, int x, int y, char d)

{

boolean flag = true;



x--; //these two lines exist because the coordinates of the crossword's upper-left

y--; //corner are (1,1), to be closer to the way we would refer to an actual crossword

if (d == 'v')

{

if (x+aWord.length() > lines)

flag = false;

else

for (int i=x; i < x+aWord.length(); i++)

if (!(crossword[y]==' ' || crossword[y]==aWord.charAt(i-x)))

flag = false;

}

else if (d == 'h')

{

if (y+aWord.length() > columns)

flag = false;

else

for (int i=y; i < y+aWord.length(); i++)

if (!(crossword[x]==' ' || crossword[x]==aWord.charAt(i-y)))

flag = false;

}



return flag;

}



/**

* This will check if a given word with given orientation can be placed in the crossword,

* starting after a specified point (if the specified point's coordinates are (x,y),

* it will start checking from (x,y+1))and using is_legal on every position, until it

* finds an acceptable place, or checks every place in the crossword.

*/

private Word find_legal(String aWord, int x, int y, char d)

{

boolean flag = false;

int i = x, j = y+1;

Word word = null;



if (j>columns)

{

i = i % lines + 1;

j = 1;

}



do

{

do

{

if (is_legal(aWord, i, j, d))

{

word = new Word(aWord, d, i, j);

flag = true;

break;

}

if(i==x && j==y)

break;

j = j % columns + 1;

}

while (j!=1);



if ((i==x && j==y))

break;

i = i % lines + 1;

}

while (!flag);



return word;

}



/**

* This will check if a given word can be placed in the crossword, by randomly choosing a

* place in the crossword and a direction, then using find_legal starting from this random

* point, with the random direction and the given word.

*/

private Word find_legal_in_all_cross(String aWord)

{

int starting_x = random_number.nextInt(lines)+1, starting_y = random_number.nextInt(columns)+1;

char direction;



if (random_number.nextInt(2)==0)

direction = 'h';

else

direction = 'v';



return find_legal(aWord, starting_x, starting_y, direction);

}



private void place_word(String aWord, int x, int y, char d)

{

for (int i=-1; i<aWord.length()-1; i++)

{

if (d=='h')

crossword[x-1][y+i] = aWord.charAt(i+1);

else

crossword[x+i][y-1] = aWord.charAt(i+1);

}

placed_words++;

}



/**

* This will fill every place in the crossword not occupied by a letter with a random letter.

*/

private void fill_remaining()

{

for (int i=0; i<lines; i++)

for (int j=0; j<columns; j++)

{

if (crossword[j]==' ')

{

switch (random_number.nextInt(26) + 1)

{

case 1:

crossword[j] = 'a';

break;

case 2:

crossword[j] = 'b';

break;

case 3:

crossword[j] = 'c';

break;

case 4:

crossword[j] = 'd';

break;

case 5:

crossword[i][j] = 'e';

break;

case 6:

crossword[i][j] = 'f';

break;

case 7:

crossword[i][j] = 'g';

break;

case 8:

crossword[i][j] = 'h';

break;

case 9:

crossword[i][j] = 'i';

break;

case 10:

crossword[i][j] = 'j';

break;

case 11:

crossword[i][j] = 'k';

break;

case 12:

crossword[i][j] = 'l';

break;

case 13:

crossword[i][j] = 'm';

break;

case 14:

crossword[i][j] = 'n';

break;

case 15:

crossword[i][j] = 'o';

break;

case 16:

crossword[i][j] = 'p';

break;

case 17:

crossword[i][j] = 'q';

break;

case 18:

crossword[i][j] = 'r';

break;

case 19:

crossword[i][j] = 's';

break;

case 20:

crossword[i][j] = 't';

break;

case 21:

crossword[i][j] = 'u';

break;

case 22:

crossword[i][j] = 'v';

break;

case 23:

crossword[i][j] = 'w';

break;

case 24:

crossword[i][j] = 'x';

break;

case 25:

crossword[i][j] = 'y';

break;

case 26:

crossword[i][j] = 'z';

break;

}

}

}

}



/**

* This will attempt to fill the crossword with words from the list of words that was created with add_a_word_to_word_list.

* Since the words are sorted according to their size, it will attempt to place the shortest words first.

* Whenever a place is found (with find_legal_in_all_cross) for a word, the word will be placed there, then it will be

* recorded to a list dedicated to the words that have been placed in the crossword (ArrayList solution).

* Finally, the remaining places, not occupied by letters will be filled with random letters (with fill_remaining).

*/

public void fill_cross_from_array()

{

String temp;

Word word;



for (int i=0; i<number_of_words; i++)

{

temp = (String) word_list.get(i);

word = find_legal_in_all_cross(temp);



if (word == null)

continue;



place_word(word.get_the_word(), word.get_x_position(), word.get_y_position(), word.get_orientation());

solution.add(word);

}



fill_remaining();

}



/**

* This will print the crossword as it is on the screen.

*/

public void print_cross()

{

System.out.println();

System.out.println();

for (int i=0; i<lines; i++)

{

for (int j=0; j<columns-1; j++)

System.out.print(crossword[i][j] + " ");

System.out.println(crossword[i][columns-1]);

}

}



/**

* This will print the crossword's solution on the screen, in the following format: word : (x,y) orientation.

*/

public void print_solution()

{

Word temp;



System.out.println();

System.out.println();

System.out.println("word : (x,y) orientation");

System.out.println("(x,y) are the fist letter's coordinates in the crossword. The upper-left corner's coordinates are (1,1)");

System.out.println();

for (int i=0; i<placed_words; i++)

{

temp = (Word) solution.get(i);



System.out.print(temp.get_the_word() + " : (" + temp.get_x_position() + "," + temp.get_y_position() + ") ");

if (temp.get_orientation() == 'h')

System.out.println("horizontal");

else

System.out.println("vertical");

}

}



/**

* This will reset the crossword.

* It will empty the crossword from letters, clear the list of words to be placed (ArrayList word_list)

* and the list of placed words (ArrayList solution).

*/

public void reset_cross()

{

for (int i=number_of_words-1; i>=0; i--)

word_list.remove(i);

number_of_words = 0;

for (int i=placed_words-1; i>=0; i--)

solution.remove(i);

placed_words = 0;



for (int i=0; i<lines; i++)

for (int j=0; j<columns; j++)

crossword[i][j] = ' ';

}



/**

* This will return the crossword in the form of a 2-D vector.

*/

public char[][] get_cross()

{

return crossword;

}



}


/**

* Word will create word objects, that represent words in the crossword.

* They will be defined by the actual word, the coordinates of its first

* letter, and its orientation, horizontal or vertical.

*

* @author (Falgorn)

* @version (5/2/2009)

*/

public class Word

{

public String word; //the word

public char orientation; //its orientation

public int xPosition, yPosition; //the first letters coordinates



public Word(String aWord, char d, int x, int y)

{

word = aWord;

orientation = d;

xPosition = x;

yPosition = y;

}



/**

* This will return the actual word.

*

* @return String word : the word

*/

public String get_the_word()

{

return word;

}



/**

* This will return the line that the word's first letter is.

*

* @return int xPosition : the line

*/

public int get_x_position()

{

return xPosition;

}



/**

* This will return the column that the word's first letter is.

*

* @return int yPosition : the column

*/

public int get_y_position()

{

return yPosition;

}



/**

* This will return the word's direction.

*

* @return char d : "v" for "vertical", "h" for "horizontal"

*/

public char get_orientation()

{

return orientation;

}

}

Παρατηρήσεις, απορίες και κουβέντα πάνω στη Java ευπρόσδεκτα!
Όλοι λένε τ’ ορμητικό ρέμα βίαιο

Μά την κοίτη του ποταμού που το κρατάει

Κανείς δεν τη λέει βίαιη.


Bertolt Brecht


Η προπαγάνδα της ημέρας ಠ_ರೃ
Απάντηση

Επιστροφή στο “Πρώτο Έτος”