Daily task OOD #2

hidden: true

2. Monday:

4.1 The Controller interface

  • Create the MarbleSolitaireController interface to represent a controller for this game.
    • This interface should have a single method: void playGame() . This method should play a new game of Marble Solitaire. It should throw an IllegalStateException only if the controller is unable to successfully read input or transmit output. Exceptions should be documented as per standard Javadoc requirements and formatting. The nature of input/output will be an implementation detail.

Write a class MarbleSolitaireControllerImpl that implements the MarbleSolitaireController interface above. Its implementation details are as follows:

  • Think about which additional fields and types it needs to implement the promised functionality.
  • Write a constructor that takes three parameters in order: a MarbleSolitaireModel object as the model, a MarbleSolitaireView object as a view and a Readable object from which to read input. It should throw an IllegalArgumentException if any of them are null.

Any input coming from the user will be received via the Readable object, and any output sent to the user should be transmitted to the view, which in turn will transmit it to whichever Appendable object was used to set it up.

Hint: You may find the Scanner class helpful to read inputs from a Readable object.


notes:

fixed toString by using get length with sqrt instead of len



Notes from TA : 

catch is allowed, 


1. stringBuilder() to append results

2.  into the appendable 

3. Reader is a appendable, read info from a appendable

Controller:

Use the reader to check the contents of an appendable

It's an iterable


2. Monday:

4.1 The Controller interface

  • Create the MarbleSolitaireController interface to represent a controller for this game. 
    • This interface should have a single method: void playGame() . This method should play a new game of Marble Solitaire. It should throw an IllegalStateException only if the controller is unable to successfully read input or transmit output. Exceptions should be documented as per standard Javadoc requirements and formatting. The nature of input/output will be an implementation detail.

Write a class MarbleSolitaireControllerImpl that implements the MarbleSolitaireController interface above. Its implementation details are as follows:

  • Think about which additional fields and types it needs to implement the promised functionality.
  • Write a constructor that takes three parameters in order: a MarbleSolitaireModel object as the model, a MarbleSolitaireView object as a view and a Readable object from which to read input. It should throw an IllegalArgumentException if any of them are null.

Any input coming from the user will be received via the Readable object, and any output sent to the user should be transmitted to the view, which in turn will transmit it to whichever Appendable object was used to set it up.

Hint: You may find the Scanner class helpful to read inputs from a Readable object.

Tuesday: 

4.2 The Controller implementation

Write a class MarbleSolitaireControllerImpl that implements the MarbleSolitaireController interface above. Its implementation details are as follows:

  • Think about which additional fields and types it needs to implement the promised functionality.
  • Write a constructor that takes three parameters in order: a MarbleSolitaireModel object as the model, a MarbleSolitaireView object as a view and a Readable object from which to read input. It should throw an IllegalArgumentException if any of them are null.

Any input coming from the user will be received via the Readable object, and any output sent to the user should be transmitted to the view, which in turn will transmit it to whichever Appendable object was used to set it up.

Hint: You may find the Scanner class helpful to read inputs from a Readable object.

  • The void playGame() method should play a game. It should ''run" the game in the following sequence until the game ends. NOTE: Each transmission described below should end with a newline.

    1. Using the view, render the current state of the game.
    2. Using the view, transmit "Score: N" , replacing N with the actual score.
    3. If the game is ongoing, obtain the next user input from the Readable object. A user input consists of the following four individual values (separated by spaces or newlines).

      • The row number of the position from where a marble is to be moved, beginning at 1 (to make the input more user-friendly).
      • The column number of the position from where a marble is to be moved, beginning at 1.
      • The row number of the position to where a marble is to be moved, beginning at 1.
      • The column number of the position to where a marble is to be moved, beginning at 1.
    4. Parse these inputs and pass the information on to the model to make the move.
    5. If the game is not over, repeat the above steps. If the game is over, the method should transmit each of the following in order: the message "Game over!" , the final game state, and the message "Score: N" with N replaced by the final score. The method should then end.


Wednesday Debug and Submit multiple times 



hidden:true

  1. create Sunday 10/3/2021
  • Create files and implement methods with stubs
  • Create tests for toString and fix it to have the correct implementation
  • Create tests for renderBoard"
      • renderBoard : this method takes no arguments. It transmits the state of the marble solitaire board to a specified destination (dependent on the implementation of this interface)
      • It must implement the renderMessage method to transmit the given message to the Appendable object.
  • Implement renderMessage(String message)
    • this method can be used to show an arbitrary message, allowing this view to show messages determined by whoever uses it.

If any transmission fails, then these methods should throw an IOException exception. For the purpose of maintaining backward compatability, its toString method should continue to work as before.



Example of Appendable in out

[FileSafe:095bdad4-5b18-4424-9a72-41bad9785817:Zoom_zddruzKs7M.png:676x791]


package cs3500.lec08;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Objects;
import java.util.Scanner;

<i>/**
</i><i> * Demonstrates a simple command-line-based calculator
</i><i> */
public class SimpleCalc6 {
  public static void main(String[] args</i>) {
    try {
      new Controller6(new InputStreamReader(System.<i>in</i>), System.<i>out</i>).go(new Calculator());
    } catch (IOException <i>e</i>) {
      <i>e</i>.printStackTrace();
    }
  }
}

class Controller6 implements <i>CalcController </i>{
  final <i>Readable </i>in;
  final <i>Appendable </i>out;
  Controller6(<i>Readable </i><i>in</i>, <i>Appendable </i><i>out</i>) {
    this.in = <i>in</i>;
    this.out = <i>out</i>;
  }
  public void go(Calculator <i>calc</i>) throws IOException {
    Objects.<i>requireNonNull</i>(<i>calc</i>);
    int num1, num2;
    Scanner scan = new Scanner(this.in);
    while (true) {
      switch (scan.next()) {
        case "+":
          num1 = scan.nextInt();
          num2 = scan.nextInt();
          this.out.append(String.<i>format</i>("%d\n", <i>calc</i>.add(num1, num2)));
          break;
        case "q":
          return;
      }
    }
  }
}

  All notes84ArchivedTrash#1#1#7#1#0#2#1#0#1#1#1#1#1#1#1#0#2#1#0#2#1#1#2Search optionsOptionsDate Added 0">PinnedSunday, Oct 03, 2021, 11:39 AM 0">PinnedSunday, Sep 26, 2021, 01:03 AM 0">PinnedSunday, Sep 26, 2021, 12:41 AMSunday, Oct 03, 2021, 09:50 AMSaturday, Oct 02, 2021, 09:30 AMFriday, Oct 01, 2021, 02:14 PMFriday, Oct 01, 2021, 02:13 PMFriday, Oct 01, 2021, 01:54 PMFriday, Oct 01, 2021, 01:54 PMThursday, Sep 30, 2021, 07:20 AMWednesday, Sep 29, 2021, 07:33 PMWednesday, Sep 29, 2021, 02:40 PMTuesday, Sep 28, 2021, 07:37 PMTuesday, Sep 28, 2021, 01:49 PMTuesday, Sep 28, 2021, 12:51 PMTuesday, Sep 28, 2021, 11:32 AMTuesday, Sep 28, 2021, 01:34 AMMonday, Sep 27, 2021, 10:17 AMSunday, Sep 26, 2021, 06:39 PMSunday, Sep 26, 2021, 01:27 AMSunday, Sep 26, 2021, 12:37 AMSunday, Sep 26, 2021, 12:13 AMSunday, Sep 26, 2021, 12:13 AMProtected • Saturday, Sep 25, 2021, 11:24 PMSaturday, Sep 25, 2021, 10:02 PMSaturday, Sep 25, 2021, 03:46 PMFriday, Sep 24, 2021, 03:32 PMAll changes saved 0" class="ng-scope ng-isolate-scope">ActionsOptionsEditorActionsHistory

Tests with appendable

[FileSafe:ac889310-7d7e-4540-8f11-123093eaa1b1:c7Tvq1ynrF.png:479x602]


package cs3500.lec08;

import org.junit.Test;

import java.io.Reader;
import java.io.StringReader;

import static org.junit.Assert.assertEquals;

public class TestController6 {
  @Test
  public void testGo() throws Exception {
    StringBuffer out = new StringBuffer();
    Reader in = new StringReader("+ 3 4 + 8 9 q");
    CalcController controller6 = new Controller6(in, out);
    controller6.go(new Calculator());
    assertEquals("7\n17\n", out.toString());
  }
}