-
-
Notifications
You must be signed in to change notification settings - Fork 159
[Swift 6]: Update window system exercise #827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
meatball133
wants to merge
7
commits into
exercism:main
Choose a base branch
from
meatball133:update-window
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d26d729
Update window system exercise
meatball133 443e910
Update formatting and update hints file
meatball133 3a252d2
Change to using normal foundation
meatball133 35823f1
Update exercises/concept/windowing-system/.docs/instructions.md
meatball133 b652741
Update exercises/concept/windowing-system/.docs/instructions.md
meatball133 63a0114
Update exercises/concept/windowing-system/.docs/instructions.md
meatball133 68894e0
Update exercises/concept/windowing-system/.docs/instructions.md
meatball133 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,8 @@ | ||||||||||||||||||||||
# Instructions | ||||||||||||||||||||||
|
||||||||||||||||||||||
In this exercise, you will be simulating a windowing based computer system. You will create some windows that can be moved and resized and display their contents. The following image is representative of the values you will be working with below. | ||||||||||||||||||||||
In this exercise, you will be simulating a windowing based computer system. | ||||||||||||||||||||||
You will create some windows that can be moved and resized and display their contents. | ||||||||||||||||||||||
The following image is representative of the values you will be working with below. | ||||||||||||||||||||||
|
||||||||||||||||||||||
``` | ||||||||||||||||||||||
<--------------------- screenSize.width ---------------------> | ||||||||||||||||||||||
|
@@ -26,30 +28,33 @@ screenSize.height ║ | │ │ | |||||||||||||||||||||
|
||||||||||||||||||||||
## 1. Define a Size struct | ||||||||||||||||||||||
|
||||||||||||||||||||||
Define a struct named `Size` with two `Int` properties, `width` and `height` that store the window's current width and height, respectively. The initial width and height should be 80 and 60, respectively. Include a method `resize(newWidth:newHeight:)` that takes new width and height parameters and changes the properties to reflect the new size. | ||||||||||||||||||||||
Define a struct named `Size` with two `Int` properties, `width` and `height` that store the window's current width and height, respectively. | ||||||||||||||||||||||
The initial width and height should be 80 and 60, respectively. | ||||||||||||||||||||||
Include a method `resize(newWidth:newHeight:)` that takes new width and height parameters and changes the properties to reflect the new size. | ||||||||||||||||||||||
|
||||||||||||||||||||||
```swift | ||||||||||||||||||||||
let size1080x764 = Size(width: 1080, height: 764) | ||||||||||||||||||||||
// => Size | ||||||||||||||||||||||
// Returns Size | ||||||||||||||||||||||
var size1200x800 = size1080x764 | ||||||||||||||||||||||
// => Size | ||||||||||||||||||||||
// returns Size | ||||||||||||||||||||||
size1200x800.resize(newWidth: 1200, newHeight: 800) | ||||||||||||||||||||||
size1200x800.height | ||||||||||||||||||||||
// => 800 | ||||||||||||||||||||||
// returns 800 | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
## 2. Define a Position struct | ||||||||||||||||||||||
|
||||||||||||||||||||||
Define a struct named `Position` with two `Int` properties, `x` and `y` that store the current horizontal and vertical position, respectively, of the window's upper left corner. The initial values of x and y should each be 0. The position (0, 0) is the upper left corner of the screen with `x` values getting larger as you move right and `y` values getting larger as you move down. | ||||||||||||||||||||||
Define a struct named `Position` with two `Int` properties, `x` and `y` that store the current horizontal and vertical position, respectively, of the window's upper left corner. | ||||||||||||||||||||||
The initial values of x and y should each be 0. The position (0, 0) is the upper left corner of the screen with `x` values getting larger as you move right and `y` values getting larger as you move down. | ||||||||||||||||||||||
|
||||||||||||||||||||||
Include a method `moveTo(newX:newY:)` that takes new x and y parameters and changes the properties to reflect the new position. | ||||||||||||||||||||||
|
||||||||||||||||||||||
```swift | ||||||||||||||||||||||
var point = Position(x: 10, y: 20) | ||||||||||||||||||||||
// => Position | ||||||||||||||||||||||
// returns Position | ||||||||||||||||||||||
point.moveTo(newX: 100, newY: -100) | ||||||||||||||||||||||
point.y | ||||||||||||||||||||||
// => -100 | ||||||||||||||||||||||
// returns -100 | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
## 3. Define a Window class | ||||||||||||||||||||||
|
@@ -62,22 +67,49 @@ Define a window class with the following properties: | |||||||||||||||||||||
- `position` : `Position`, initial value is the default value of the `Position` struct | ||||||||||||||||||||||
- `contents` : `String?`, initial value is `nil` | ||||||||||||||||||||||
|
||||||||||||||||||||||
You should also define an empty initializer for the class. | ||||||||||||||||||||||
|
||||||||||||||||||||||
```swift | ||||||||||||||||||||||
let window = Window() | ||||||||||||||||||||||
window.title | ||||||||||||||||||||||
// returns "New Window" | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
## 4. Add a method to resize windows | ||||||||||||||||||||||
|
||||||||||||||||||||||
- `resize(to:)` : `(Size) -> ()` - This method takes a `Size` struct as input and attempts to resize the window to the specified size. However, the new size cannot exceed certain bounds. - The minimum allowed height or width is 1. Requested heights or widths less than 1 will be clipped to 1. - The maximum height and width depends on the current position of the window, the edges of the window cannot move past the edges of the screen. Values larger than these bounds will be clipped to the largest size they can take. E.g. if the window's position is at `x` = 400, `y` = 300 and a resize to `height` = 400, `width` = 300 is requested, then the window would be resized to `height` = 300, `width` = 300 as the screen is not large enough in the `y` direction to fully accommodate the request. | ||||||||||||||||||||||
- `resize(to:)` : `(Size) -> ()` - This method takes a `Size` struct as input and attempts to resize the window to the specified size. However, the new size cannot exceed certain bounds. | ||||||||||||||||||||||
- The minimum allowed height or width is 1. | ||||||||||||||||||||||
Requested heights or widths less than 1 will be clipped to 1. | ||||||||||||||||||||||
- The maximum height and width depends on the current position of the window, the edges of the window cannot move past the edges of the screen. | ||||||||||||||||||||||
Values larger than these bounds will be clipped to the largest size they can take. | ||||||||||||||||||||||
E.g. if the window's position is at `x` = 400, `y` = 300 and a resize to `height` = 400, `width` = 300 is requested, then the window would be resized to `height` = 300, `width` = 300 as the screen is not large enough in the `y` direction to fully accommodate the request. | ||||||||||||||||||||||
|
||||||||||||||||||||||
## 5. Add a method to move windows | ||||||||||||||||||||||
|
||||||||||||||||||||||
- `move(to:)` : `(Position) -> ()` - This is similar to `resize(to:)`, however, this method adjusts the _position_ of the window to the requested value, rather than the size. As with `resize` the new position cannot exceed certain limits. - The smallest position is 0 for both `x` and `y`. - The maximum position in either direction depends on the current size of the window; the edges cannot move past the edges of the screen. Values larger than these bounds will be clipped to the largest size they can take. E.g. if the window's size is at `x` = 250, `y` = 100 and a move to `x` = 600, `y` = 200 is requested, then the window would be moved to `x` = 550, `y` = 200 as the screen is not large enough in the `x` direction to fully accommodate the request. | ||||||||||||||||||||||
- `move(to:)` : `(Position) -> ()` - This is similar to `resize(to:)`, however, this method adjusts the _position_ of the window to the requested value, rather than the size. As with `resize` the new position cannot exceed certain limits. | ||||||||||||||||||||||
- The smallest position is 0 for both `x` and `y`. | ||||||||||||||||||||||
- The maximum position in either direction depends on the current size of the window; the edges cannot move past the edges of the screen. | ||||||||||||||||||||||
Values larger than these bounds will be clipped to the largest size they can take. E.g. if the window's size is at `x` = 250, `y` = 100 and a move to `x` = 600, `y` = 200 is requested, then the window would be moved to `x` = 550, `y` = 200 as the screen is not large enough in the `x` direction to fully accommodate the request. | ||||||||||||||||||||||
|
||||||||||||||||||||||
## 6. Add methods to update the window text and display window information | ||||||||||||||||||||||
|
||||||||||||||||||||||
- `update(title:)` : `(String) -> ()` - This method sets the `title` property to the value of the string that was passed in. | ||||||||||||||||||||||
- `update(text:)` : `(String?) -> ()` - This method sets the `contents` property to the value of the optional string that was passed in. | ||||||||||||||||||||||
- `display()` : `() -> String` - This method returns a string describing the current state of the window. For example, if the window has the `title` "My First Window" with position: x = 10, y = 100; size: width = 200, height = 150; and contents: "I 😍 my window", it should return the string: `"My First Window\nPosition: (10, 100), Size: (200 x 150)\nI 😍 my window\n"` - If `contents` is nil, the last line should read "[This window intentionally left blank]" | ||||||||||||||||||||||
|
||||||||||||||||||||||
## 7. Create a new Window | ||||||||||||||||||||||
## 7. Create an initilazer for the Window class | ||||||||||||||||||||||
|
||||||||||||||||||||||
Create an instances of the Window class and modify it via their methods as follows: | ||||||||||||||||||||||
The window system should have an initializer so that the window can be created with custom inputs. | ||||||||||||||||||||||
Create **another** initializer for the `Window` class. | ||||||||||||||||||||||
The initializer should take the following parameters: `title`, `contents`, and two optional parameters `size` and `position`. | ||||||||||||||||||||||
The `size` and `position` parameters should default to the default values of the `Size` and `Position` structs. | ||||||||||||||||||||||
|
||||||||||||||||||||||
- The window should be given the title "Main Window", with a width of 400, a height of 300 and positioned at x = 100, y = 100. Its contents should be "This is the main window". Assign this instance to the name `mainWindow`. | ||||||||||||||||||||||
```swift | ||||||||||||||||||||||
let window = Window(title: "My First Window", contents: "I 😍 my window") | ||||||||||||||||||||||
window.display() | ||||||||||||||||||||||
// returns "My First Window\nPosition: (0, 0), Size: (80 x 60)\nI 😍 my window\n" | ||||||||||||||||||||||
|
||||||||||||||||||||||
let window2 = Window(title: "My Second Window", contents: "I 😍 my window", size: Size(width: 200, height: 150), position: Position(x: 10, y: 100)) | ||||||||||||||||||||||
window2.display() | ||||||||||||||||||||||
// returns "My Second Window\nPosition: (10, 100), Size: (200 x 150)\nI 😍 my window\n" | ||||||||||||||||||||||
Comment on lines
+110
to
+114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
// swift-tools-version:5.3 | ||
// swift-tools-version:6.0 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "WindowingSystem", | ||
products: [ | ||
// Products define the executables and libraries a package produces, and make them visible to other packages. | ||
.library( | ||
name: "WindowingSystem", | ||
targets: ["WindowingSystem"]), | ||
], | ||
dependencies: [ | ||
// Dependencies declare other packages that this package depends on. | ||
// .package(url: /* package url */, from: "1.0.0"), | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. | ||
// Targets can depend on other targets in this package, and on products in packages this package depends on. | ||
.target( | ||
name: "WindowingSystem", | ||
dependencies: []), | ||
.testTarget( | ||
name: "WindowingSystemTests", | ||
dependencies: ["WindowingSystem"]), | ||
] | ||
name: "WindowingSystem", | ||
products: [ | ||
// Products define the executables and libraries a package produces, and make them visible to other packages. | ||
.library( | ||
name: "WindowingSystem", | ||
targets: ["WindowingSystem"]) | ||
], | ||
dependencies: [ | ||
// Dependencies declare other packages that this package depends on. | ||
// .package(url: /* package url */, from: "1.0.0"), | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. | ||
// Targets can depend on other targets in this package, and on products in packages this package depends on. | ||
.target( | ||
name: "WindowingSystem", | ||
dependencies: []), | ||
.testTarget( | ||
name: "WindowingSystemTests", | ||
dependencies: ["WindowingSystem"]), | ||
] | ||
) |
4 changes: 2 additions & 2 deletions
4
exercises/concept/windowing-system/Sources/WindowingSystem/WindowingSystem.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// TODO: Define the Position struct | ||
|
||
// TODO: Define the Size struct | ||
|
||
// TODO: Define the Position struct | ||
|
||
// TODO: Define the Window class |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.