├── .gitignore ├── LICENSE.txt ├── README.md ├── board.js ├── go.js ├── index.html └── style.css /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. 4 | 5 | In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. 6 | 7 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | 9 | For more information, please refer to http://unlicense.org 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | react-go 2 | ======== 3 | 4 | The game of [Go][1] implemented with [React][2]. Check out a [live preview][3] or [read the tutorial][4] 5 | 6 | [1]: http://en.wikipedia.org/wiki/Go_(game) 7 | [2]: http://facebook.github.io/react/ 8 | [3]: http://cjlarose.com/react-go/ 9 | [4]: http://cjlarose.com/2014/01/09/react-board-game-tutorial.html 10 | -------------------------------------------------------------------------------- /board.js: -------------------------------------------------------------------------------- 1 | var Board = function(size) { 2 | this.current_color = Board.BLACK; 3 | this.size = size; 4 | this.board = this.create_board(size); 5 | this.last_move_passed = false; 6 | this.in_atari = false; 7 | this.attempted_suicide = false; 8 | }; 9 | 10 | Board.EMPTY = 0; 11 | Board.BLACK = 1; 12 | Board.WHITE = 2; 13 | 14 | /* 15 | * Returns a size x size matrix with all entries initialized to Board.EMPTY 16 | */ 17 | Board.prototype.create_board = function(size) { 18 | var m = []; 19 | for (var i = 0; i < size; i++) { 20 | m[i] = []; 21 | for (var j = 0; j < size; j++) 22 | m[i][j] = Board.EMPTY; 23 | } 24 | return m; 25 | }; 26 | 27 | /* 28 | * Switches the current player 29 | */ 30 | Board.prototype.switch_player = function() { 31 | this.current_color = 32 | this.current_color == Board.BLACK ? Board.WHITE : Board.BLACK; 33 | }; 34 | 35 | /* 36 | * At any point in the game, a player can pass and let his opponent play 37 | */ 38 | Board.prototype.pass = function() { 39 | if (this.last_move_passed) 40 | this.end_game(); 41 | this.last_move_passed = true; 42 | this.switch_player(); 43 | }; 44 | 45 | /* 46 | * Called when the game ends (both players passed) 47 | */ 48 | Board.prototype.end_game = function() { 49 | console.log("GAME OVER"); 50 | }; 51 | 52 | /* 53 | * Attempt to place a stone at (i,j). Returns true iff the move was legal 54 | */ 55 | Board.prototype.play = function(i, j) { 56 | console.log("Played at " + i + ", " + j); 57 | this.attempted_suicide = this.in_atari = false; 58 | 59 | if (this.board[i][j] != Board.EMPTY) 60 | return false; 61 | 62 | var color = this.board[i][j] = this.current_color; 63 | var captured = []; 64 | var neighbors = this.get_adjacent_intersections(i, j); 65 | var atari = false; 66 | 67 | var self = this; 68 | _.each(neighbors, function(n) { 69 | var state = self.board[n[0]][n[1]]; 70 | if (state != Board.EMPTY && state != color) { 71 | var group = self.get_group(n[0], n[1]); 72 | console.log(group); 73 | if (group["liberties"] == 0) 74 | captured.push(group); 75 | else if (group["liberties"] == 1) 76 | atari = true; 77 | } 78 | }); 79 | 80 | // detect suicide 81 | if (_.isEmpty(captured) && this.get_group(i, j)["liberties"] == 0) { 82 | this.board[i][j] = Board.EMPTY; 83 | this.attempted_suicide = true; 84 | return false; 85 | } 86 | 87 | var self = this; 88 | _.each(captured, function(group) { 89 | _.each(group["stones"], function(stone) { 90 | self.board[stone[0]][stone[1]] = Board.EMPTY; 91 | }); 92 | }); 93 | 94 | if (atari) 95 | this.in_atari = true; 96 | 97 | this.last_move_passed = false; 98 | this.switch_player(); 99 | return true; 100 | }; 101 | 102 | /* 103 | * Given a board position, returns a list of [i,j] coordinates representing 104 | * orthagonally adjacent intersections 105 | */ 106 | Board.prototype.get_adjacent_intersections = function(i , j) { 107 | var neighbors = []; 108 | if (i > 0) 109 | neighbors.push([i - 1, j]); 110 | if (j < this.size - 1) 111 | neighbors.push([i, j + 1]); 112 | if (i < this.size - 1) 113 | neighbors.push([i + 1, j]); 114 | if (j > 0) 115 | neighbors.push([i, j - 1]); 116 | return neighbors; 117 | }; 118 | 119 | /* 120 | * Performs a breadth-first search about an (i,j) position to find recursively 121 | * orthagonally adjacent stones of the same color (stones with which it shares 122 | * liberties). Returns null for if there is no stone at the specified position, 123 | * otherwise returns an object with two keys: "liberties", specifying the 124 | * number of liberties the group has, and "stones", the list of [i,j] 125 | * coordinates of the group's members. 126 | */ 127 | Board.prototype.get_group = function(i, j) { 128 | 129 | var color = this.board[i][j]; 130 | if (color == Board.EMPTY) 131 | return null; 132 | 133 | var visited = {}; // for O(1) lookups 134 | var visited_list = []; // for returning 135 | var queue = [[i, j]]; 136 | var count = 0; 137 | 138 | while (queue.length > 0) { 139 | var stone = queue.pop(); 140 | if (visited[stone]) 141 | continue; 142 | 143 | var neighbors = this.get_adjacent_intersections(stone[0], stone[1]); 144 | var self = this; 145 | _.each(neighbors, function(n) { 146 | var state = self.board[n[0]][n[1]]; 147 | if (state == Board.EMPTY) 148 | count++; 149 | if (state == color) 150 | queue.push([n[0], n[1]]); 151 | }); 152 | 153 | visited[stone] = true; 154 | visited_list.push(stone); 155 | } 156 | 157 | return { 158 | "liberties": count, 159 | "stones": visited_list 160 | }; 161 | } 162 | -------------------------------------------------------------------------------- /go.js: -------------------------------------------------------------------------------- 1 | /** @jsx React.DOM */ 2 | var GRID_SIZE = 40; 3 | 4 | var BoardIntersection = React.createClass({ 5 | handleClick: function() { 6 | if (this.props.board.play(this.props.row, this.props.col)) 7 | this.props.onPlay(); 8 | }, 9 | render: function() { 10 | var style = { 11 | top: this.props.row * GRID_SIZE, 12 | left: this.props.col * GRID_SIZE 13 | }; 14 | 15 | var classes = "intersection"; 16 | if (this.props.color != Board.EMPTY) 17 | classes += this.props.color == Board.BLACK ? " black" : " white"; 18 | 19 | return ( 20 |
22 | ); 23 | } 24 | }); 25 | 26 | var BoardView = React.createClass({ 27 | render: function() { 28 | var intersections = []; 29 | for (var i = 0; i < this.props.board.size; i++) 30 | for (var j = 0; j < this.props.board.size; j++) 31 | intersections.push(BoardIntersection({ 32 | board: this.props.board, 33 | color: this.props.board.board[i][j], 34 | row: i, 35 | col: j, 36 | onPlay: this.props.onPlay 37 | })); 38 | var style = { 39 | width: this.props.board.size * GRID_SIZE, 40 | height: this.props.board.size * GRID_SIZE 41 | }; 42 | return
{intersections}
43 | } 44 | }); 45 | 46 | var AlertView = React.createClass({ 47 | render: function() { 48 | var text = ""; 49 | if (this.props.board.in_atari) 50 | text = "ATARI!"; 51 | else if (this.props.board.attempted_suicide) 52 | text = "SUICIDE!"; 53 | 54 | return ( 55 |
{text}
56 | ); 57 | } 58 | }); 59 | 60 | var PassView = React.createClass({ 61 | handleClick: function(e) { 62 | this.props.board.pass(); 63 | }, 64 | render: function() { 65 | return ( 66 | 68 | ); 69 | } 70 | }); 71 | 72 | var board = new Board(19); 73 | 74 | var ContainerView = React.createClass({ 75 | getInitialState: function() { 76 | return {'board': this.props.board}; 77 | }, 78 | onBoardUpdate: function() { 79 | this.setState({"board": this.props.board}); 80 | }, 81 | render: function() { 82 | return ( 83 |
84 | 85 | 86 | 88 |
89 | ) 90 | } 91 | }); 92 | 93 | React.renderComponent( 94 | , 95 | document.getElementById('main') 96 | ); 97 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | React Go Tutorial 5 | 6 | 7 | 8 |
9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | #alerts { 2 | height: 40px; 3 | line-height: 40px; 4 | } 5 | 6 | #pass-btn { 7 | } 8 | 9 | #board { 10 | position: relative; 11 | } 12 | 13 | .intersection { 14 | width: 40px; 15 | height: 40px; 16 | border-radius: 20px; 17 | position: absolute; 18 | box-sizing: border-box; 19 | } 20 | 21 | .intersection:before { 22 | border-top: 2px #000 solid; 23 | width: 40px; 24 | display: block; 25 | content: ""; 26 | top: 18px; 27 | position: absolute; 28 | z-index: -1; 29 | } 30 | 31 | .intersection:after { 32 | border-left: 2px #000 solid; 33 | height: 40px; 34 | display: block; 35 | content: ""; 36 | left: 18px; 37 | top: 0px; 38 | position: absolute; 39 | z-index: -1; 40 | } 41 | 42 | .intersection.black, .intersection.white { 43 | border: 1px solid #000; 44 | } 45 | 46 | .intersection.black { 47 | background-color: #444; 48 | } 49 | 50 | .intersection.white { 51 | background-color: #eee; 52 | } 53 | --------------------------------------------------------------------------------