Skip to content

Commit

Permalink
Add darts exercise
Browse files Browse the repository at this point in the history
We use integer coordinates, as some MIPS resources do not cover
floating point instructions.
  • Loading branch information
keiravillekode committed Jan 6, 2024
1 parent 5f75443 commit decf661
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@
"math"
]
},
{
"slug": "darts",
"name": "Darts",
"uuid": "bff5891d-19ac-4029-a451-bdcf812c4fd5",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "square-root",
"name": "Square Root",
Expand Down
6 changes: 6 additions & 0 deletions exercises/practice/darts/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Hints

## General

- The `$t0-9` registers can be used to temporarily store values
- The instructions specify which registers are used as input and output
10 changes: 10 additions & 0 deletions exercises/practice/darts/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Instructions append

## Registers

| Register | Usage | Type | Description |
| -------- | --------- | ------- | -------------------------- |
| `$a0` | input | integer | x |
| `$a1` | input | integer | y |
| `$v0` | output | integer | points earned |
| `$t0-9` | temporary | any | used for temporary storage |
31 changes: 31 additions & 0 deletions exercises/practice/darts/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Instructions

Write a function that returns the earned points in a single toss of a Darts game.

[Darts][darts] is a game where players throw darts at a [target][darts-target].

In our particular instance of the game, the target rewards 4 different amounts of points, depending on where the dart lands:

![Our dart scoreboard with values from a complete miss to a bullseye](https://assets.exercism.org/images/exercises/darts/darts-scoreboard.svg)

- If the dart lands outside the target, player earns no points (0 points).
- If the dart lands in the outer circle of the target, player earns 1 point.
- If the dart lands in the middle circle of the target, player earns 5 points.
- If the dart lands in the inner circle of the target, player earns 10 points.

The outer circle has a radius of 10 units (this is equivalent to the total radius for the entire target), the middle circle a radius of 5 units, and the inner circle a radius of 1.
Of course, they are all centered at the same point — that is, the circles are [concentric][] defined by the coordinates (0, 0).

Write a function that given a point in the target (defined by its [Cartesian coordinates][cartesian-coordinates] `x` and `y`, where `x` and `y` are [real][real-numbers]), returns the correct amount earned by a dart landing at that point.

## Credit

The scoreboard image was created by [habere-et-dispertire][habere-et-dispertire] using [Inkscape][inkscape].

[darts]: https://en.wikipedia.org/wiki/Darts
[darts-target]: https://en.wikipedia.org/wiki/Darts#/media/File:Darts_in_a_dartboard.jpg
[concentric]: https://mathworld.wolfram.com/ConcentricCircles.html
[cartesian-coordinates]: https://www.mathsisfun.com/data/cartesian-coordinates.html
[real-numbers]: https://www.mathsisfun.com/numbers/real-numbers.html
[habere-et-dispertire]: https://exercism.org/profiles/habere-et-dispertire
[inkscape]: https://en.wikipedia.org/wiki/Inkscape
18 changes: 18 additions & 0 deletions exercises/practice/darts/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"impl.mips"
],
"test": [
"runner.mips"
],
"example": [
".meta/example.mips"
]
},
"blurb": "Write a function that returns the earned points in a single toss of a Darts game.",
"source": "Inspired by an exercise created by a professor Della Paolera in Argentina"
}
32 changes: 32 additions & 0 deletions exercises/practice/darts/.meta/example.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# Calculate points earned by dart
#
# Variables:
#
# $a0 - x
# $a1 - y
# $v0 - points earned
# $t0 - x * x
# $t1 - y * y
# $t2 - x * x + y * y

.globl score

score:
mul $t0, $a0, $a0
mul $t1, $a1, $a1
add $t2, $t0, $t1 # squared distance from center

li $v0, 10 # player earns 10 points
ble $t2, 1, return # if dart lands in the inner circle

li $v0, 5 # player earns 5 points
ble $t2, 25, return # if dart lands in the middle circle

li $v0, 1 # player earns 1 points
ble $t2, 100, return # if dart lands in the outer circle

move $v0, $zero # player earns 0 points

return:
jr $ra
51 changes: 51 additions & 0 deletions exercises/practice/darts/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[9033f731-0a3a-4d9c-b1c0-34a1c8362afb]
description = "Missed target"

[4c9f6ff4-c489-45fd-be8a-1fcb08b4d0ba]
description = "On the outer circle"

[14378687-ee58-4c9b-a323-b089d5274be8]
description = "On the middle circle"

[849e2e63-85bd-4fed-bc3b-781ae962e2c9]
description = "On the inner circle"

[1c5ffd9f-ea66-462f-9f06-a1303de5a226]
description = "Exactly on center"

[b65abce3-a679-4550-8115-4b74bda06088]
description = "Near the center"
include = false

[66c29c1d-44f5-40cf-9927-e09a1305b399]
description = "Just within the inner circle"
include = false

[d1012f63-c97c-4394-b944-7beb3d0b141a]
description = "Just outside the inner circle"

[ab2b5666-b0b4-49c3-9b27-205e790ed945]
description = "Just within the middle circle"

[70f1424e-d690-4860-8caf-9740a52c0161]
description = "Just outside the middle circle"

[a7dbf8db-419c-4712-8a7f-67602b69b293]
description = "Just within the outer circle"

[e0f39315-9f9a-4546-96e4-a9475b885aa7]
description = "Just outside the outer circle"

[045d7d18-d863-4229-818e-b50828c75d19]
description = "Asymmetric position between the inner and middle circles"
11 changes: 11 additions & 0 deletions exercises/practice/darts/impl.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# | Register | Usage | Type | Description |
# | -------- | --------- | ------- | -------------------------- |
# | `$a0` | input | integer | x |
# | `$a1` | input | integer | y |
# | `$v0` | output | integer | points earned |
# | `$t0-9` | temporary | any | used for temporary storage |

.globl score

score:
jr $ra
133 changes: 133 additions & 0 deletions exercises/practice/darts/runner.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#
# Test score with some examples
#
# s0 - num of tests left to run
# s1 - address of x input word
# s2 - address of y input word
# s3 - address of expected output word
# s4 - output word

.data

# number of test cases
n: .word 11
# input values (word sized ints)
xs: .word
-9,
0,
-5,
0,
0,
1,
-3,
-4,
-7,
7,
1
ys: .word
9,
10,
0,
-1,
0,
-1,
3,
-4,
7,
-8,
-4
# expected output values (word sized ints)
outs: .word
0,
1,
5,
10,
10,
5,
5,
1,
1,
0,
5

failmsg: .asciiz "failed for test input: ("
commamsg: .asciiz ", "
expectedmsg: .asciiz "). expected "
tobemsg: .asciiz " to be "
okmsg: .asciiz "all tests passed"


.text

runner:
lw $s0, n
la $s1, xs
la $s2, ys
la $s3, outs
j run_test

run_test:
lw $a0, 0($s1) # move input x number to a0
lw $a1, 0($s2) # move input y number to a1
jal score # call subroutine under test
move $v1, $v0 # move return value in v0 to v1 because we need v0 for syscall

lw $s4, 0($s3) # read expected output from memory
bne $v1, $s4, exit_fail # if expected doesn't match actual, jump to fail

addi $s1, $s1, 4 # move to next x word in input
addi $s2, $s2, 4 # move to next y word in input
addi $s3, $s3, 4 # move to next word in output

sub $s0, $s0, 1 # decrement num of tests left to run
bgt $s0, $zero, run_test # if more than zero tests to run, jump to run_test

exit_ok:
la $a0, okmsg # put address of okmsg into a0
li $v0, 4 # 4 is print string
syscall

li $v0, 10 # 10 is exit with zero status (clean exit)
syscall

exit_fail:
la $a0, failmsg # put address of failmsg into a0
li $v0, 4 # 4 is print string
syscall

lw $a0, ($s1) # print input that failed on
li $v0, 1
syscall

la $a0, commamsg
li $v0, 4
syscall

lw $a0, ($s2)
li $v0, 1
syscall

la $a0, expectedmsg
li $v0, 4
syscall

move $a0, $v1 # print actual that failed on
li $v0, 1
syscall

la $a0, tobemsg
li $v0, 4
syscall

move $a0, $s4 # print expected value that failed on
li $v0, 1
syscall

li $a0, 1 # set error code to 1
li $v0, 17 # 17 is exit with error
syscall

jr $ra

# # Include your implementation here if you wish to run this from the MARS GUI.
# .include "impl.mips"

0 comments on commit decf661

Please sign in to comment.