-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from omochi/ttt
三目並べの実装
- Loading branch information
Showing
13 changed files
with
213 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import React | ||
import JavaScriptKit | ||
import SRTDOM | ||
import BRTSupport | ||
|
||
// https://ja.react.dev/learn/tutorial-tic-tac-toe | ||
|
||
func calculateWinner(squares: [String?]) -> String? { | ||
let lines: [[Int]] = [ | ||
[0, 1, 2], | ||
[3, 4, 5], | ||
[6, 7, 8], | ||
[0, 3, 6], | ||
[1, 4, 7], | ||
[2, 5, 8], | ||
[0, 4, 8], | ||
[2, 4, 6] | ||
] | ||
for line in lines { | ||
let (a, b, c) = (line[0], line[1], line[2]) | ||
|
||
if let x = squares[a], | ||
squares[b] == x, | ||
squares[c] == x { | ||
return x | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
struct Square: Component { | ||
var value: String? | ||
var onSquareClick: EventListener | ||
|
||
func render() -> Node { | ||
return button( | ||
attributes: ["class": "square"], | ||
listeners: ["click": onSquareClick] | ||
) { | ||
value | ||
} | ||
} | ||
} | ||
|
||
struct Board: Component { | ||
var xIsNext: Bool = true | ||
var squares: [String?] = Array(repeating: nil, count: 9) | ||
var onPlay: Function<Void, [String?]> | ||
|
||
func handleClick(index: Int) { | ||
if squares[index] != nil || | ||
calculateWinner(squares: squares) != nil { return } | ||
|
||
var squares = self.squares | ||
|
||
if xIsNext { | ||
squares[index] = "X" | ||
} else { | ||
squares[index] = "O" | ||
} | ||
|
||
onPlay(squares) | ||
} | ||
|
||
func render() -> Node { | ||
let winner = calculateWinner(squares: squares) | ||
let status: String | ||
if let winner { | ||
status = "Winner: " + winner; | ||
} else { | ||
status = "Next player: " + (xIsNext ? "X" : "O") | ||
} | ||
|
||
return Fragment { | ||
div(attributes: ["class": "status"]) { status } | ||
div(attributes: ["class": "board-row"]) { | ||
Square(value: squares[0], onSquareClick: EventListener { (_) in handleClick(index: 0) }) | ||
Square(value: squares[1], onSquareClick: EventListener { (_) in handleClick(index: 1) }) | ||
Square(value: squares[2], onSquareClick: EventListener { (_) in handleClick(index: 2) }) | ||
} | ||
div(attributes: ["class": "board-row"]) { | ||
Square(value: squares[3], onSquareClick: EventListener { (_) in handleClick(index: 3) }) | ||
Square(value: squares[4], onSquareClick: EventListener { (_) in handleClick(index: 4) }) | ||
Square(value: squares[5], onSquareClick: EventListener { (_) in handleClick(index: 5) }) | ||
} | ||
div(attributes: ["class": "board-row"]) { | ||
Square(value: squares[6], onSquareClick: EventListener { (_) in handleClick(index: 6) }) | ||
Square(value: squares[7], onSquareClick: EventListener { (_) in handleClick(index: 7) }) | ||
Square(value: squares[8], onSquareClick: EventListener { (_) in handleClick(index: 8) }) | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct Game: Component { | ||
@State var history: [[String?]] = [Array(repeating: nil, count: 9)] | ||
@State var currentMove: Int = 0 | ||
|
||
func render() -> Node { | ||
let xIsNext = currentMove % 2 == 0 | ||
|
||
let currentSquares = history[currentMove] | ||
|
||
let handlePlay = Function<Void, [String?]> { (nextSquares) in | ||
history = history[...currentMove] + [nextSquares] | ||
currentMove = history.count - 1 | ||
} | ||
|
||
func jumpTo(nextMove: Int) { | ||
currentMove = nextMove | ||
} | ||
|
||
let moves: [Node] = history.enumerated().map { (move, squares) -> Node in | ||
let description: String | ||
if move > 0 { | ||
description = "Go to move #\(move)" | ||
} else { | ||
description = "Go to game start" | ||
} | ||
return li(key: move) { | ||
button(listeners: ["click": EventListener { (e) in jumpTo(nextMove: move) }]) { | ||
description | ||
} | ||
} | ||
} | ||
|
||
return div(attributes: ["class": "game"]) { | ||
div(attributes: ["class": "game-board"]) { | ||
Board(xIsNext: xIsNext, squares: currentSquares, onPlay: handlePlay) | ||
} | ||
div(attributes: ["class": "game-info"]) { | ||
ol { | ||
moves | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
try addCSS(path: "BrowserTests_TicTacToe.resources/styles.css") | ||
try renderRoot(component: Game()) | ||
|
||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
.square { | ||
background: #fff; | ||
border: 1px solid #999; | ||
float: left; | ||
font-size: 24px; | ||
font-weight: bold; | ||
line-height: 34px; | ||
height: 34px; | ||
margin-right: -1px; | ||
margin-top: -1px; | ||
padding: 0; | ||
text-align: center; | ||
width: 34px; | ||
} | ||
|
||
.board-row:after { | ||
clear: both; | ||
content: ''; | ||
display: table; | ||
} | ||
|
||
.status { | ||
margin-bottom: 10px; | ||
} | ||
.game { | ||
display: flex; | ||
flex-direction: row; | ||
} | ||
|
||
.game-info { | ||
margin-left: 20px; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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