Tic-tac-toe: Difference between revisions
Content deleted Content added
Line 354: | Line 354: | ||
=={{header|bash}}== |
=={{header|bash}}== |
||
Computer is X. Computer randomly goes first. Computer plays a good game, but not a perfect game. It will win when it can and draw when it can not. |
Computer is X. Computer randomly goes first. Computer plays a good game, but not a perfect game. It will win when it can and draw when it can not. |
||
It performs a depth-first scan of all following moves. It ignores dumb actions like not winning when either player can. |
It performs a depth-first scan of all following moves. It ignores dumb actions like not winning when either player can. |
||
For each possible move it records if it will win, lose, or something else (like win and draw depending on opponent's move). |
For each possible move it records if it will win, lose, or something else (like win and draw depending on opponent's move). |
||
If there is a choice of best moves, it picks one at random. |
If there is a choice of best moves, it picks one at random. |
||
I have not used simple bash code to: |
I have not used simple bash code to: |
||
#keep it under 100 lines; |
|||
#to demonstrate usefulness of bash integers; |
|||
#show-off ANSI ESC sequences; |
|||
#implement recursion in bash; |
|||
#demonstrate conditional and alternate execution using && and || with { ...; }; |
|||
#show that you don't always need to use $ to refer to integer variables; |
|||
#encourage use of [[ ]] instead of [ ] for boolean expressions; |
|||
#provide examples of pattern matching; and |
|||
#encourage use of bash for more interesting tasks. |
|||
<lang bash> |
<lang bash> |
||
Line 434: | Line 438: | ||
</lang> |
</lang> |
||
Output: (nice ANSI formatting is not shown) |
|||
<pre> |
|||
Bic Bash Bow |
|||
0 | 1 | 2 |
|||
3 | 4 | 5 |
|||
6 | 7 | 8 |
|||
X moves 1 |
|||
0 | X | 2 |
|||
3 | 4 | 5 |
|||
6 | 7 | 8 |
|||
O move: 5 |
|||
O moves 5 |
|||
0 | X | 2 |
|||
3 | 4 | O |
|||
6 | 7 | 8 |
|||
X moves 2 |
|||
0 | X | X |
|||
3 | 4 | O |
|||
6 | 7 | 8 |
|||
O move: 0 |
|||
O moves 0 |
|||
O | X | X |
|||
3 | 4 | O |
|||
6 | 7 | 8 |
|||
X moves 4 |
|||
O | X | X |
|||
3 | X | O |
|||
6 | 7 | 8 |
|||
O move: 6 |
|||
O moves 6 |
|||
O | X | X |
|||
3 | X | O |
|||
O | 7 | 8 |
|||
X moves 7 |
|||
X wins! |
|||
O | X | X |
|||
3 | X | O |
|||
O | X | 8 |
|||
</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |