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:
1. keep it under 100 lines;
#keep it under 100 lines;
2. to demonstrate usefulness of bash integers;
#to demonstrate usefulness of bash integers;
3. show-off ASCII ESC sequences;
#show-off ANSI ESC sequences;
4. implement recursion in bash;
#implement recursion in bash;
5. demonstrate conditional and alternate execution using && and || with { ...; };
#demonstrate conditional and alternate execution using && and || with { ...; };
6. show that you don't always need to use $ to refer to integer variables;
#show that you don't always need to use $ to refer to integer variables;
7. encourage use of [[ ]] instead of [ ] for boolean expressions;
#encourage use of [[ ]] instead of [ ] for boolean expressions;
8. provide examples of pattern matching; and
#provide examples of pattern matching; and
9. encourage use of bash for more interesting tasks.
#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}}==