-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
257 lines (213 loc) · 7.17 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<!DOCTYPE html>
<html>
<head>
<title>Tic-Tac-Toe</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<hgroup>
<h1 class="logo">Tic-Tac-Toe</h1>
<h2>May the odds be ever in your favor.</h2>
</hgroup>
<div class="game-container">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.3.1/redux.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/4.4.0/react-redux.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<script type="text/babel">
(function () {
// Config
var size = 3;
// Helpers
function generateSquareMatrix(num, value) {
var arr = [], row, col;
for(row = 0; row < num; row++) {
arr[row] = [];
for(col = 0; col < num; col++) {
arr[row][col] = value;
}
}
return arr;
}
function hasPlayerWon(board, row, col, piece) {
var size = board.length;
// Check row
for (let i = 0; i < size; i++) {
if (board[i][col] !== piece) break;
if (i === size - 1) return true;
}
// Check column
for (let i = 0; i < size; i++) {
if (board[row][i] !== piece) break;
if (i === size - 1) return true;
}
// Check diagonal
for (let i = 0; i < size; i++) {
if (board[i][i] !== piece) break;
if (i === size - 1) return true;
}
// Check anti-diagonal
for (let i = 0; i < size; i++) {
if (board[i][(size - 1) - i] !== piece) break;
if (i === size - 1) return true;
}
return false;
}
// Actions
function setPiece(row, col, piece) {
return {
type: 'SET_PIECE',
row, col, piece
}
}
function resetGame(size) {
return {
type: 'RESET_GAME',
size
}
}
// Reducers
/**
* State of the board is reprsented by an array of tile states
* A tile can be in three states:
* - Blank: null
* - X: 1
* - O: 2
*/
let nextBoard = (previousBoard = generateSquareMatrix(size, null), action) => {
switch (action.type) {
case 'SET_PIECE':
return previousBoard.map((row, rowIdx) => {
return rowIdx === action.row ?
row.map((col, colIdx) => {
return colIdx === action.col ? action.piece : col
}) :
row
});
case 'RESET_GAME':
return generateSquareMatrix(action.size, null);
default:
return previousBoard;
}
};
let nextCurrentPlayer = (previousPlayer = 1, action) => {
switch (action.type) {
case 'SET_PIECE':
return previousPlayer === 1 ? 2 : 1;
case 'RESET_GAME':
return 1;
default:
return previousPlayer;
}
};
let nextNumOfMoves = (previousNumOfMoves = 0, action) => {
switch (action.type) {
case 'SET_PIECE':
return previousNumOfMoves + 1;
case 'RESET_GAME':
return 0;
default:
return previousNumOfMoves;
}
};
let nextPlayerWon = (previousPlayerWon = null, action, board) => {
switch (action.type) {
case 'SET_PIECE':
return hasPlayerWon(board, action.row, action.col, action.piece) ? action.piece : null;
case 'RESET_GAME':
return null;
default:
return previousPlayerWon;
}
}
let reducer = (previousState = {}, action) => {
let board = nextBoard(previousState.board, action);
let currentPlayer = nextCurrentPlayer(previousState.currentPlayer, action);
let numOfMoves = nextNumOfMoves(previousState.numOfMoves, action);
let playerWon = nextPlayerWon(previousState.playerWon, action, board);
return { board, currentPlayer, numOfMoves, playerWon };
}
let store = Redux.createStore(reducer);
// Components
class Tile extends React.Component {
render() {
return (
<div className='ttt-tile' onClick={this.props.onClick}>
{(() => {
switch (this.props.piece) {
case 1: return <div className='ttt-x-piece'></div>;
case 2: return <div className='ttt-o-piece'></div>;
default: return '';
}
})()}
</div>
);
}
}
class Board extends React.Component {
render() {
return (
<div>
<div className='ttt-board'>
{this.props.matrix.map((row, rowIdx) => {
return (
<div key={rowIdx} className='ttt-board-row'>
{row.map((col, colIdx) => {
return (
<Tile key={colIdx}
piece={col}
onClick={() => {
if (this.props.playerWon) return;
store.dispatch(setPiece(rowIdx, colIdx, this.props.currentPlayer));
}} />
);
})}
</div>
);
})}
</div>
</div>
);
}
}
class Message extends React.Component {
render() {
var currentSize = this.props.board.length;
return (
<div>
<div className={'ttt-message ttt-win-message ' + (!this.props.playerWon ? 'is-hidden' : '')}>
You win, player {this.props.playerWon}!
<button className='recall-restart-btn' onClick={() => { store.dispatch(resetGame(size)) }}>Play again!</button>
</div>
<div className={'ttt-message ttt-draw-message ' + ((!this.props.playerWon && this.props.numOfMoves === currentSize * currentSize) ? '' : 'is-hidden' )}>
Wow, a draw. Bring it to the next level:
<button className='ttt-restart-btn' onClick={() => { store.dispatch(resetGame(currentSize + 1)) }}>Play again!</button>
</div>
</div>
);
}
}
var ConnectedBoard = ReactRedux.connect(
function mapStateToProps(state) {
return { matrix: state.board, ...state };
}
)(Board);
var ConnectedMessage = ReactRedux.connect((state) => state)(Message);
var TicTacToe = () => (
<div>
<ConnectedMessage />
<ConnectedBoard />
</div>
)
ReactDOM.render(
<ReactRedux.Provider store={store}>
<TicTacToe />
</ReactRedux.Provider>,
document.querySelectorAll('.game-container')[0]
)
})();
</script>
</body>
</html>