Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Trevor Austin
/
mpcs-52553-austin-winter-2021
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Pipelines
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
4353020b
authored
Feb 24, 2021
by
Trevor Austin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished React tutorial
parent
6f0f3caf
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
104 additions
and
11 deletions
week_7/examples/tictactoe.js
week_7/examples/tictactoe.js
View file @
4353020b
class
Square
extends
React
.
Component
{
render
()
{
function
Square
(
props
)
{
return
(
<
button
className
=
"square"
>
{
/* TODO */
}
<
button
className
=
"square"
onClick
=
{
props
.
handlerFunction
}
>
{
props
.
value
}
<
/button
>
);
}
}
class
Board
extends
React
.
Component
{
renderSquare
(
i
)
{
return
<
Square
/>
;
return
(
<
Square
value
=
{
this
.
props
.
squares
[
i
]}
handlerFunction
=
{()
=>
this
.
props
.
onClick
(
i
)}
/>
)
;
}
render
()
{
const
status
=
'Next player: X'
;
return
(
<
div
>
<
div
className
=
"status"
>
{
status
}
<
/div
>
<
div
className
=
"board-row"
>
{
this
.
renderSquare
(
0
)}
{
this
.
renderSquare
(
1
)}
...
...
@@ -40,21 +44,110 @@ class Board extends React.Component {
}
class
Game
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
);
this
.
state
=
{
history
:
[{
squares
:
Array
(
9
).
fill
(
null
),
}],
xIsNext
:
true
,
stepNumber
:
0
,
}
}
handleClick
(
i
)
{
const
history
=
this
.
state
.
history
.
slice
(
0
,
this
.
state
.
stepNumber
+
1
);
const
current
=
history
[
history
.
length
-
1
];
const
squares
=
current
.
squares
.
slice
();
if
(
calculateWinner
(
squares
)
||
squares
[
i
])
{
return
;
}
squares
[
i
]
=
this
.
state
.
xIsNext
?
'X'
:
'O'
;
this
.
setState
({
history
:
history
.
concat
([{
squares
:
squares
}]),
stepNumber
:
history
.
length
,
xIsNext
:
!
this
.
state
.
xIsNext
,
});
}
jumpTo
(
step
)
{
this
.
setState
({
stepNumber
:
step
,
xIsNext
:
(
step
%
2
)
===
0
,
});
}
render
()
{
const
history
=
this
.
state
.
history
;
const
current
=
history
[
this
.
state
.
stepNumber
];
const
winner
=
calculateWinner
(
current
.
squares
);
const
moves
=
history
.
map
((
step
,
moveNumber
)
=>
{
let
desc
;
if
(
moveNumber
==
0
)
{
desc
=
'Go to game start'
;
}
else
{
desc
=
'Go to move#'
+
moveNumber
;
}
return
(
<
li
key
=
{
moveNumber
}
>
<
button
onClick
=
{()
=>
this
.
jumpTo
(
moveNumber
)}
>
{
desc
}
<
/button
>
<
/li
>
);
});
let
status
;
if
(
winner
)
{
status
=
'Winner: '
+
winner
;
}
else
{
status
=
'Next player: '
+
(
this
.
state
.
xIsNext
?
'X'
:
'O'
);
}
return
(
<
div
className
=
"game"
>
<
div
className
=
"game-board"
>
<
Board
/>
<
Board
squares
=
{
current
.
squares
}
onClick
=
{(
i
)
=>
this
.
handleClick
(
i
)}
/
>
<
/div
>
<
div
className
=
"game-info"
>
<
div
>
{
/* status */
}
<
/div
>
<
ol
>
{
/* TODO */
}
<
/ol
>
<
div
>
{
status
}
<
/div
>
<
ol
>
{
moves
}
<
/ol
>
<
/div
>
<
/div
>
);
}
}
function
calculateWinner
(
squares
)
{
const
lines
=
[
[
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
(
let
i
=
0
;
i
<
lines
.
length
;
i
++
)
{
const
[
a
,
b
,
c
]
=
lines
[
i
];
if
(
squares
[
a
]
&&
squares
[
a
]
===
squares
[
b
]
&&
squares
[
a
]
===
squares
[
c
])
{
return
squares
[
a
];
}
}
return
null
;
};
// ========================================
ReactDOM
.
render
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment