1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | void drawLine( ) { String s = "+"; for (int i=1; i<cells[0].length-1; i++) { s+="--"; } s += "+"; System.out.println(s); } public void print() { drawLine(); for (int i = 1; i < cells.length-1; i++) { System.out.print("|"); for (int j = 1; j < cells[i].length-1; j++) { System.out.print( cells[i][j].draw() ); System.out.print( " " ); // to get a more square-like output } System.out.println("|"); } drawLine(); } |
Hoe doet ik dat precies?quote:Op dinsdag 13 mei 2014 23:20 schreef Hoplahopla het volgende:
Wat is er mis met een global er van maken?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | import java.util.Scanner; import java.io.*; class Field{ /* void drawLine( ) { String s = "+"; for (int i=1; i<cells[0].length-1; i++) { s+="--"; } s += "+"; System.out.println(s); } public void print() { drawLine(); for (int i = 1; i < cells.length-1; i++) { System.out.print("|"); for (int j = 1; j < cells[i].length-1; j++) { System.out.print( cells[i][j].draw() ); System.out.print( " " ); // to get a more square-like output } System.out.println("|"); } drawLine(); } int numNeighbours(){} //This method calculates the number of living neighbours of a cell with coördinates i,j */ public char[][] calcNew(){ //This method calculates the new generation } } class Cell{ boolean alive; //This variable describes the state of a cell boolean isAlive(){ //This method returns the state of a cell } String draw()} //This method returns the cell as a character to be printed } void setAlive(boolean state){} //This method changees the state of a cell } public class GameOfLife{ //Main class File source; Scanner sc = new Scanner(System.in); char firstfield[][]; //This is going to be our array where we store the first generation int height; int width; //We obtain these values from the input file String s; //This string is used for filling the array with the first generation public char[][] readInitial(String filename){ //This method returns an array with the first generation try { source = new File(filename); //Declares the source Scanner sf = new Scanner(source); //Initialises the scanner height = sf.nextInt(); //Reads the height and width from the input file width = sf.nextInt(); firstfield = new char[height+2][width+2]; //Creates an array with heigth + 2 rows and width+2 columns char dot = '.' ; //This is used to create the border. for(int i = 0; i < width+2; i++){ //This for-loop creates the horizontal border of the field. firstfield[0][i] = dot; firstfield[height+1][i] = dot; } for(int j = 0; j < height+2; j++){ //These for-loop creates the vertical border of the field. firstfield[j][0] = dot; firstfield[j][width+1] = dot; } for(int j = 1; j < height + 2; j++){ //These 2 loops fill the array with the first generation if(sf.hasNext()){ String s = sf.next(); for(int i = 1; i < width + 1 ; i++){ char c = s.charAt(i-1); firstfield[j][i] = c;} } else{ } } } catch(FileNotFoundException e) { //This handles possible errors System.out.println("The file "+filename+" could not be found"); } sc.close(); return firstfield; } void play(){ System.out.println("Give number of desired generations please"); int numberofgen = 10; //int numberofgen = sc.nextInt(); System.out.println("Give the name of the file please"); String filename = "first.txt" ; //String filename = sc.next(); readInitial(filename); //This executes the method which initialises the first generation from the input for(int i = 1; i < numberofgen+1; i++){ Field.calcNew(); //This executes the method which calculates a new generation try{Thread.sleep(2000);}catch{InterruptedException e){} //This pauses the programm for 2 seconds } } public static void main(String[] args) { new GameOfLife().play();} } |
Wat is een instantie?quote:Op dinsdag 13 mei 2014 23:40 schreef Hoplahopla het volgende:
Wat ik er van kan maken is dat je een instantie van Field in the GameOfLife class moet gebruiken. Ik zie dat niet terug in je code.
Misschien kun je beter even uitzoeken wat een object en instantie precies inhouden.quote:
Ja, ik was er al mee bezig. Ik snap de structuur van JAVA niet zo heel goed denk ik.quote:Op dinsdag 13 mei 2014 23:46 schreef Hoplahopla het volgende:
[..]
Misschien kun je beter even uitzoeken wat een object en instantie precies inhouden.
Een constructor is onderdeel van de klasse, dus dan heb je hem in feite al gemaakt.quote:Op dinsdag 13 mei 2014 23:51 schreef Amoeba het volgende:
Ah, ik moet met een constructor die cells maken?
1 | Cell cel = new Cell(); |
Inderdaad. Ik studeer wiskunde aan de TU/e, we hebben nu een cursus programmeren in JAVA.quote:Op dinsdag 13 mei 2014 23:55 schreef Hoplahopla het volgende:
Weetje... Ik kan niet de opdracht voor je uitwerken. Dit is waarschijnlijk schoolwerk... Maar als je iets niet snapt wil ik heb best een beetje toelichten.
Wat dacht je een simpele loop die over iedere cell gaat, de buren telt en aan de hand daarvan bepaalt of een cel tot leven komt, sterft of blijf leven? Dus iets als (sorry mijn java is erg gelimiteerd en roestig, dus niet foutvrij)quote:Op dinsdag 13 mei 2014 23:12 schreef Amoeba het volgende:
In the class Field, add a method that calculates the next generation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public void calcNew(){ //This method calculates the new generation Cell nieuwveld[cells.length][cells[0].length]; for (int i = 1; i < cells.length-1; i++) { for (int j = 1; j < cells[i].length-1; j++) { // numNeighbours zul je dus ook zelf even moeten schrijven int buren = numNeighbours(i,j); if(buren < 2 || buren > 3) { nieuwveld[i][j].setAlive(false); // eenzaam of overbevolking } else { if(buren==3 && !cells[i][j].isAlive()) { nieuwveld[i][j].setAlive(true); // wederopstanding } else { // niets veranderd nieuwveld[i][j].setAlive(cell[i][j].isAlive); } } } } cells = nieuwveld; // kopieer nieuwe generatie naar het veld. } |
Dat had ik zelf ook al in gedachten. Ik heb niet voor niets zo'n moeite gedaan om die array te maken.quote:Op woensdag 14 mei 2014 00:26 schreef devzero het volgende:
[..]
Wat dacht je een simpele loop die over iedere cell gaat, de buren telt en aan de hand daarvan bepaalt of een cel tot leven komt, sterft of blijf leven? Dus iets als (sorry mijn java is erg gelimiteerd en roestig, dus niet foutvrij)
[ code verwijderd ]
Ik snap nog steeds niet hoe ik nu in die class Field die array firstfield kan aanroepen.quote:Op dinsdag 13 mei 2014 23:55 schreef Hoplahopla het volgende:
Weetje... Ik kan niet de opdracht voor je uitwerken. Dit is waarschijnlijk schoolwerk... Maar als je iets niet snapt wil ik heb best een beetje toelichten.
Heb je specifieke restricties gekregen?quote:Op woensdag 14 mei 2014 21:04 schreef Amoeba het volgende:
[..]
Ik snap nog steeds niet hoe ik nu in die class Field die array firstfield kan aanroepen.
Dit is de opgave:quote:Op woensdag 14 mei 2014 21:24 schreef Hoplahopla het volgende:
[..]
Heb je specifieke restricties gekregen?
Als dat het is zou ik zeggen knal er een parameter tegenaan en je hebt je output.quote:Op woensdag 14 mei 2014 21:30 schreef Amoeba het volgende:
[..]
Dit is de opgave:
http://2wh20.jrvturnhout.nl/Downloads/Week4/Instruction7Files.zip
Het is een zipfile met de methodes en de opgavebeschrijving.
Dus zoiets:quote:Op woensdag 14 mei 2014 21:32 schreef Hoplahopla het volgende:
[..]
Als dat het is zou ik zeggen knal er een parameter tegenaan en je hebt je output.
Iets in je aanroep stoppen is handig.quote:Op woensdag 14 mei 2014 21:35 schreef Amoeba het volgende:
[..]
Dus zoiets:
class Field{
calcNext(char firstfield[][]){
}
}
public class GameOfLife{
.......
void play{
....
Field.calcNext(){
firstfield[][] = firstfield;
}
}
}
?
Wat vergeet ik dan?quote:Op woensdag 14 mei 2014 21:41 schreef Hoplahopla het volgende:
[..]
Iets in je aanroep stoppen is handig.
Niet om het één en of ander maar wat zegt de compiler?quote:
|
Forum Opties | |
---|---|
Forumhop: | |
Hop naar: |