2

So, i have one method that will create all of the instance variables for my other methods that will create the game, i know how to do them separately but finding how to do it from one method is really hard.

I need to be reading data from a String where each line must be treated separately.

Am using Pharo.

Class Game, everything is within one Game class.

Game: instance variables: 'rol col'. Using instance methods.

readFrom: 'Board 3 4
Dice 2 1 1 1
Players 1'

board
[my actual code that creates a board]
row for loop[
    Transcript show: 'creating board'.
       col for loop[
       Transcript show: 'creating board'.
   ]
]

dice
[dice code..]

players
[players code]
lookorange
  • 31
  • 4
  • Can you show what you actually tried (doing them "separately"), showing some actual code? You don't have to put all your code in, but at least show the code that pertains to handling of the variables, and how the variables are created/declared. This shouldn't be hard. All of the instance variables for the class should be accessible to all of the instance methods. But you haven't said whether you're using instance methods or class methods. – lurker Feb 10 '17 at 18:18
  • Sorry i wasnt too sure of instance or class. I am doing instance methods, and my row and col variables are class accessible. so, i just need to get the row and col for my board, from the readFrom method, then set it to the row col instance variables i have – lookorange Feb 10 '17 at 19:27
  • What do you mean by "class accessible" when these are instance variables? Also I do not understand how the question (title) relates to the rest of the text. What do you mean by _to do them_ and _do it_, what is _it_? It seems not to be initializing the variables because you wrote that you already have a method "creating" these variables. Is your question about reading data from a String where each line must be treated separately? – JayK Feb 10 '17 at 22:45
  • Are you using Pharo or GNU? The means of defining the variables is different in each case. But regardless, if you define instance variables inside your class. Any instance methods in the class should simply have access. However, you may choose to write accessors for them. It's unclear what you've actually tried, though, to define the variables you're trying to access. – lurker Feb 11 '17 at 00:20
  • yes i need to be reading data from a String where each line must be treated separately, and i am using Pharo. – lookorange Feb 11 '17 at 02:14

1 Answers1

2

Your model is not clearly defined yet. However, by helping you with some coding I will try to give you some insights on how to fill the gaps still remaining.

So, let's say you have a class Game. This class defines (at least) 4 instance variables: rows, columns, dice and players.

Now you want to create an instance of Game by reading some String that conforms to a certain format, such in:

'Board 3 4
 Dice 2 1 1 1
 Players 1'

To do this create a class side method in Game on the lines of

 readFrom: aString
   ^self new readFrom: aString

and then an instance method

readFrom: aString
   aString lines do: [:line | | data key |
     data := line substrings.
     key := data at: 1.
     key = 'Board'
       ifTrue: [
         rows := data at: 2.
         columns := data at: 3].
     key = 'Dice'
       ifTrue: [
         dice := data allButFirst collect: [:s | s asInteger]].
     key = 'Players'
       ifTrue: [
         players := (data at: 2) asInteger]]

Again, this won't solve all problems, but should help you get started. Otherwise, ask again.

Leandro Caniglia
  • 14,495
  • 4
  • 29
  • 51