Skip to content

Commit

Permalink
Add state-of-tic-tac-toe exercise (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Nov 26, 2024
1 parent f1ad5e5 commit 221cc19
Show file tree
Hide file tree
Showing 12 changed files with 4,034 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,14 @@
"prerequisites": [],
"difficulty": 6
},
{
"slug": "state-of-tic-tac-toe",
"name": "State of Tic-Tac-Toe",
"uuid": "857e4fb1-782d-4eed-bb4d-147c9c3f94df",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "variable-length-quantity",
"name": "Variable Length Quantity",
Expand Down
101 changes: 101 additions & 0 deletions exercises/practice/state-of-tic-tac-toe/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Instructions

In this exercise, you're going to implement a program that determines the state of a [tic-tac-toe][] game.
(_You may also know the game as "noughts and crosses" or "Xs and Os"._)

The game is played on a 3×3 grid.
Players take turns to place `X`s and `O`s on the grid.
The game ends when one player has won by placing three of marks in a row, column, or along a diagonal of the grid, or when the entire grid is filled up.

In this exercise, we will assume that `X` starts.

It's your job to determine which state a given game is in.

There are 3 potential game states:

- The game is **ongoing**.
- The game ended in a **draw**.
- The game ended in a **win**.

If the given board is invalid, throw an appropriate error.

If a board meets the following conditions, it is invalid:

- The given board cannot be reached when turns are taken in the correct order (remember that `X` starts).
- The game was played after it already ended.

## Examples

### Ongoing game

```text
| |
X | |
___|___|___
| |
| X | O
___|___|___
| |
O | X |
| |
```

### Draw

```text
| |
X | O | X
___|___|___
| |
X | X | O
___|___|___
| |
O | X | O
| |
```

### Win

```text
| |
X | X | X
___|___|___
| |
| O | O
___|___|___
| |
| |
| |
```

### Invalid

#### Wrong turn order

```text
| |
O | O | X
___|___|___
| |
| |
___|___|___
| |
| |
| |
```

#### Continued playing after win

```text
| |
X | X | X
___|___|___
| |
O | O | O
___|___|___
| |
| |
| |
```

[tic-tac-toe]: https://en.wikipedia.org/wiki/Tic-tac-toe
19 changes: 19 additions & 0 deletions exercises/practice/state-of-tic-tac-toe/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"state_of_tic_tac_toe.s"
],
"test": [
"state_of_tic_tac_toe_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Determine the game state of a match of Tic-Tac-Toe.",
"source": "Created by Sascha Mann for the Julia track of the Exercism Research Experiment.",
"source_url": "https://github.com/exercism/research_experiment_1/tree/julia-dev/exercises/julia-1-a"
}
88 changes: 88 additions & 0 deletions exercises/practice/state-of-tic-tac-toe/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
.equ ONGOING, 0
.equ DRAW, 1
.equ WIN, 2
.equ INVALID, 3

.data
/* bitsets representing horizontal, vertical and diagonal lines */
lines: .hword 0x007, 0x070, 0x700, 0x111, 0x222, 0x444, 0x124, 0x421, 0

.text
.globl gamestate

/* extern int gamestate(const char **board); */
gamestate:
adrp x1, lines
add x1, x1, :lo12:lines

mov w2, wzr /* bitset representing X marks on board */
mov w3, wzr /* bitset representing O marks on board */
mov w6, wzr /* number of X marks on board */
mov w7, wzr /* number of O marks on board */

.read_row:
ldr x4, [x0], #8 /* read address of next row */
cbz x4, .count

lsl w2, w2, #1
lsl w3, w3, #1

.read_mark:
ldrb w5, [x4], #1 /* read mark, post-increment */
cbz w5, .read_row

lsl w2, w2, #1
lsl w3, w3, #1
cmp w5, #'X'
cinc w2, w2, eq
cinc w6, w6, eq /* update number of X marks on board */
cmp w5, #'O'
cinc w3, w3, eq
cinc w7, w7, eq /* update number of O marks on board */
b .read_mark

.count:
cmp w7, w6
bgt .invalid /* number of O marks > number of X marks */

add w5, w7, #1
cmp w6, w5
bgt .invalid /* number of X marks > 1 + number of O marks */

mov w8, wzr /* found win for X? */
mov w9, wzr /* found win for O? */
mov w10, #1

.read_line:
ldrh w4, [x1], #2 /* read from lines, post-increment */
cbz w4, .report

and w5, w2, w4
cmp w5, w4
csel w8, w10, w8, eq /* line formed by X marks */
and w5, w3, w4
cmp w5, w4
csel w9, w10, w9, eq /* line formed by O marks */
b .read_line

.report:
orr w4, w8, w9 /* has either player won? */
cbnz w4, .win

mov x0, ONGOING
mov x1, DRAW
add w6, w6, w7 /* total number of marks on board */
cmp w6, #9
csel x0, x1, x0, eq
ret

.win:
mov x2, WIN
mov x3, INVALID
cmp w8, w9 /* check if both players "won" */
csel x0, x3, x2, eq
ret

.invalid:
mov x0, INVALID
ret
101 changes: 101 additions & 0 deletions exercises/practice/state-of-tic-tac-toe/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# 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.

[fe8e9fa9-37af-4d7e-aa24-2f4b8517161a]
description = "Won games -> Finished game where X won via left column victory"

[96c30df5-ae23-4cf6-bf09-5ef056dddea1]
description = "Won games -> Finished game where X won via middle column victory"

[0d7a4b0a-2afd-4a75-8389-5fb88ab05eda]
description = "Won games -> Finished game where X won via right column victory"

[bd1007c0-ec5d-4c60-bb9f-1a4f22177d51]
description = "Won games -> Finished game where O won via left column victory"

[c032f800-5735-4354-b1b9-46f14d4ee955]
description = "Won games -> Finished game where O won via middle column victory"

[662c8902-c94a-4c4c-9d9c-e8ca513db2b4]
description = "Won games -> Finished game where O won via right column victory"

[2d62121f-7e3a-44a0-9032-0d73e3494941]
description = "Won games -> Finished game where X won via top row victory"

[108a5e82-cc61-409f-aece-d7a18c1beceb]
description = "Won games -> Finished game where X won via middle row victory"
include = false

[346527db-4db9-4a96-b262-d7023dc022b0]
description = "Won games -> Finished game where X won via middle row victory"
reimplements = "108a5e82-cc61-409f-aece-d7a18c1beceb"

[a013c583-75f8-4ab2-8d68-57688ff04574]
description = "Won games -> Finished game where X won via bottom row victory"

[2c08e7d7-7d00-487f-9442-e7398c8f1727]
description = "Won games -> Finished game where O won via top row victory"

[bb1d6c62-3e3f-4d1a-9766-f8803c8ed70f]
description = "Won games -> Finished game where O won via middle row victory"

[6ef641e9-12ec-44f5-a21c-660ea93907af]
description = "Won games -> Finished game where O won via bottom row victory"

[ab145b7b-26a7-426c-ab71-bf418cd07f81]
description = "Won games -> Finished game where X won via falling diagonal victory"

[7450caab-08f5-4f03-a74b-99b98c4b7a4b]
description = "Won games -> Finished game where X won via rising diagonal victory"

[c2a652ee-2f93-48aa-a710-a70cd2edce61]
description = "Won games -> Finished game where O won via falling diagonal victory"

[5b20ceea-494d-4f0c-a986-b99efc163bcf]
description = "Won games -> Finished game where O won via rising diagonal victory"

[035a49b9-dc35-47d3-9d7c-de197161b9d4]
description = "Won games -> Finished game where X won via a row and a column victory"

[e5dfdeb0-d2bf-4b5a-b307-e673f69d4a53]
description = "Won games -> Finished game where X won via two diagonal victories"

[b42ed767-194c-4364-b36e-efbfb3de8788]
description = "Drawn games -> Draw"

[227a76b2-0fef-4e16-a4bd-8f9d7e4c3b13]
description = "Drawn games -> Another draw"

[4d93f15c-0c40-43d6-b966-418b040012a9]
description = "Ongoing games -> Ongoing game: one move in"

[c407ae32-4c44-4989-b124-2890cf531f19]
description = "Ongoing games -> Ongoing game: two moves in"

[199b7a8d-e2b6-4526-a85e-78b416e7a8a9]
description = "Ongoing games -> Ongoing game: five moves in"

[1670145b-1e3d-4269-a7eb-53cd327b302e]
description = "Invalid boards -> Invalid board: X went twice"

[47c048e8-b404-4bcf-9e51-8acbb3253f3b]
description = "Invalid boards -> Invalid board: O started"

[b1dc8b13-46c4-47db-a96d-aa90eedc4e8d]
description = "Invalid boards -> Invalid board"
include = false

[6c1920f2-ab5c-4648-a0c9-997414dda5eb]
description = "Invalid boards -> Invalid board: X won and O kept playing"
reimplements = "b1dc8b13-46c4-47db-a96d-aa90eedc4e8d"

[4801cda2-f5b7-4c36-8317-3cdd167ac22c]
description = "Invalid boards -> Invalid board: players kept playing after a win"
36 changes: 36 additions & 0 deletions exercises/practice/state-of-tic-tac-toe/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
AS = aarch64-linux-gnu-as
CC = aarch64-linux-gnu-gcc

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =

ALL_LDFLAGS = -pie -Wl,--fatal-warnings

ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.s
@$(AS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl gamestate

gamestate:
ret
Loading

0 comments on commit 221cc19

Please sign in to comment.