Tic-tac-toe: Difference between revisions
Content added Content deleted
m (→{{header|Tailspin}}: typed array indices) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 26: | Line 26: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">UInt32 seed = 0 |
||
F nonrandom_choice(lst) |
F nonrandom_choice(lst) |
||
:seed = 1664525 * :seed + 1013904223 |
:seed = 1664525 * :seed + 1013904223 |
||
Line 111: | Line 111: | ||
L.break |
L.break |
||
L.was_no_break |
L.was_no_break |
||
print("\nA draw")</ |
print("\nA draw")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 158: | Line 158: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO, Ada.Numerics.Discrete_Random; |
||
-- can play human-human, human-computer, computer-human or computer-computer |
-- can play human-human, human-computer, computer-human or computer-computer |
||
-- the computer isn't very clever: it just chooses a legal random move |
-- the computer isn't very clever: it just chooses a legal random move |
||
Line 281: | Line 281: | ||
Ada.Text_IO.Put_Line("The winner is: " & Find_Winner(The_Board)); |
Ada.Text_IO.Put_Line("The winner is: " & Find_Winner(The_Board)); |
||
end if; |
end if; |
||
end Tic_Tac_Toe;</ |
end Tic_Tac_Toe;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 396: | Line 396: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
The user can play O, X, both or neither. O goes first whether user or computer controlled. |
The user can play O, X, both or neither. O goes first whether user or computer controlled. |
||
< |
<syntaxhighlight lang="algolw">begin |
||
string(10) board; |
string(10) board; |
||
Line 575: | Line 575: | ||
do begin end |
do begin end |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 621: | Line 621: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">property OMask : missing value |
||
property XMask : missing value |
property XMask : missing value |
||
property winningNumbers : {7, 56, 73, 84, 146, 273, 292, 448} |
property winningNumbers : {7, 56, 73, 84, 146, 273, 292, 448} |
||
Line 733: | Line 733: | ||
end repeat |
end repeat |
||
return theResult as integer |
return theResult as integer |
||
end BWAND</ |
end BWAND</syntaxhighlight> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
This program uses a Gui with 9 buttons. Clicking on one will place an X there, disable the button, and cause the program to go somewhere. |
This program uses a Gui with 9 buttons. Clicking on one will place an X there, disable the button, and cause the program to go somewhere. |
||
It plays logically, trying to win, trying to block, or playing randomly in that order. |
It plays logically, trying to win, trying to block, or playing randomly in that order. |
||
< |
<syntaxhighlight lang="autohotkey">Gui, Add, Button, x12 y12 w30 h30 vB1 gButtonHandler, |
||
Gui, Add, Button, x52 y12 w30 h30 vB2 gButtonHandler, |
Gui, Add, Button, x52 y12 w30 h30 vB2 gButtonHandler, |
||
Gui, Add, Button, x92 y12 w30 h30 vB3 gButtonHandler, |
Gui, Add, Button, x92 y12 w30 h30 vB3 gButtonHandler, |
||
Line 844: | Line 844: | ||
GuiClose: |
GuiClose: |
||
ExitApp |
ExitApp |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f TIC-TAC-TOE.AWK |
# syntax: GAWK -f TIC-TAC-TOE.AWK |
||
BEGIN { |
BEGIN { |
||
Line 961: | Line 961: | ||
} |
} |
||
function abs(x) { if (x >= 0) { return x } else { return -x } } |
function abs(x) { if (x >= 0) { return x } else { return -x } } |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Bash}}== |
=={{header|Bash}}== |
||
Line 983: | Line 983: | ||
#encourage use of bash for more interesting tasks. |
#encourage use of bash for more interesting tasks. |
||
< |
<syntaxhighlight lang="bash"> |
||
#!/bin/bash |
#!/bin/bash |
||
declare -a B=( e e e e e e e e e ) # Board |
declare -a B=( e e e e e e e e e ) # Board |
||
Line 1,048: | Line 1,048: | ||
show; [[ RANDOM%2 -eq 0 ]] && { turn O X; exit $?; } || turn X O |
show; [[ RANDOM%2 -eq 0 ]] && { turn O X; exit $?; } || turn X O |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} (nice ANSI formatting is not shown) |
{{out}} (nice ANSI formatting is not shown) |
||
<pre> |
<pre> |
||
Line 1,091: | Line 1,091: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
<syntaxhighlight lang="basic256"> |
|||
<lang BASIC256> |
|||
# basado en código de Antonio Rodrigo dos Santos Silva (gracias): |
# basado en código de Antonio Rodrigo dos Santos Silva (gracias): |
||
# http://statusgear.freeforums.net/thread/17/basic-256-tic-tac-toe |
# http://statusgear.freeforums.net/thread/17/basic-256-tic-tac-toe |
||
Line 1,363: | Line 1,363: | ||
call gameMenu() |
call gameMenu() |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Microsoft Small Basic=== |
===Microsoft Small Basic=== |
||
This game has a simple AI. |
This game has a simple AI. |
||
< |
<syntaxhighlight lang="smallbasic">place1 = 1 |
||
place2 = 2 |
place2 = 2 |
||
place3 = 3 |
place3 = 3 |
||
Line 1,511: | Line 1,511: | ||
Else |
Else |
||
Goto reset |
Goto reset |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
===ZX Spectrum Basic=== |
===ZX Spectrum Basic=== |
||
< |
<syntaxhighlight lang="zxbasic"> |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
This is just a game between two human players. |
This is just a game between two human players. |
||
< |
<syntaxhighlight lang="dos">@echo off |
||
setlocal enabledelayedexpansion |
setlocal enabledelayedexpansion |
||
:newgame |
:newgame |
||
Line 1,567: | Line 1,567: | ||
pause |
pause |
||
goto newgame |
goto newgame |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Advanced=== |
===Advanced=== |
||
This code makes a version of Tic Tac Toe with more features: |
This code makes a version of Tic Tac Toe with more features: |
||
< |
<syntaxhighlight lang="dos">@ECHO OFF |
||
:BEGIN |
:BEGIN |
||
REM Skill level |
REM Skill level |
||
Line 1,890: | Line 1,890: | ||
set t7= |
set t7= |
||
set t8= |
set t8= |
||
set t9=</ |
set t9=</syntaxhighlight> |
||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
Line 1,897: | Line 1,897: | ||
Plays reasonably well, but not perfectly, so can be beaten. |
Plays reasonably well, but not perfectly, so can be beaten. |
||
< |
<syntaxhighlight lang="befunge">v123456789 --- >9 >48*,:55+\-0g,1v |
||
>9>066+0p076+0p^ ^,," |"_v#%3:- < |
>9>066+0p076+0p^ ^,," |"_v#%3:- < |
||
:,,0537051v>:#,_$#^5#,5#+<>:#v_55+ |
:,,0537051v>:#,_$#^5#,5#+<>:#v_55+ |
||
Line 1,911: | Line 1,911: | ||
>"yM">:#,_$ :. 1234+++, 789*+ \0^< |
>"yM">:#,_$ :. 1234+++, 789*+ \0^< |
||
"a s't"98:*+>:#,_$@>365*+"ward"48* |
"a s't"98:*+>:#,_$@>365*+"ward"48* |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,957: | Line 1,957: | ||
=={{header|C}}== |
=={{header|C}}== |
||
Opening alternates between human and computer. Computer never loses. |
Opening alternates between human and computer. Computer never loses. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 2,059: | Line 2,059: | ||
while (1) printf("%s", game(first = !first)); |
while (1) printf("%s", game(first = !first)); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
Line 2,065: | Line 2,065: | ||
It tries to show a number of C# code features while still keeping each function small and understandable. |
It tries to show a number of C# code features while still keeping each function small and understandable. |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 2,310: | Line 2,310: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,356: | Line 2,356: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <windows.h> |
#include <windows.h> |
||
#include <iostream> |
#include <iostream> |
||
Line 2,536: | Line 2,536: | ||
} |
} |
||
//-------------------------------------------------------------------------------------------------- |
//-------------------------------------------------------------------------------------------------- |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} Computer plays 'X' and human plays 'O' |
{{out}} Computer plays 'X' and human plays 'O' |
||
<pre> |
<pre> |
||
Line 2,549: | Line 2,549: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp"> |
||
(defun generate-board () |
(defun generate-board () |
||
(loop repeat 9 collect nil)) |
(loop repeat 9 collect nil)) |
||
Line 2,642: | Line 2,642: | ||
(format t "The winner is ~a!" (find-winner board)) |
(format t "The winner is ~a!" (find-winner board)) |
||
(format t "It's a tie."))))) |
(format t "It's a tie."))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,671: | Line 2,671: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.string, std.algorithm, std.conv, std.random, |
||
std.ascii, std.array, std.range, std.math; |
std.ascii, std.array, std.range, std.math; |
||
Line 2,785: | Line 2,785: | ||
break; |
break; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Tic-tac-toe game player. |
<pre>Tic-tac-toe game player. |
||
Line 2,836: | Line 2,836: | ||
[https://easylang.online/apps/tictactoe.html Run it] |
[https://easylang.online/apps/tictactoe.html Run it] |
||
<lang>len f[] 9 |
<syntaxhighlight lang="text">len f[] 9 |
||
state = 0 |
state = 0 |
||
textsize 14 |
textsize 14 |
||
Line 2,988: | Line 2,988: | ||
. |
. |
||
. |
. |
||
call init</ |
call init</syntaxhighlight> |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
The program will randomly chose if the computer ("X") or the user ("O") starts. The computer look ahead is only one level. Perhaps the computer might lose? |
The program will randomly chose if the computer ("X") or the user ("O") starts. The computer look ahead is only one level. Perhaps the computer might lose? |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module(tic_tac_toe). |
-module(tic_tac_toe). |
||
Line 3,098: | Line 3,098: | ||
turn_next_move_ok( true, _Prompt, _Ns, N ) -> N; |
turn_next_move_ok( true, _Prompt, _Ns, N ) -> N; |
||
turn_next_move_ok( false, Prompt, Ns, _N ) -> turn_next_move( Prompt, Ns ). |
turn_next_move_ok( false, Prompt, Ns, _N ) -> turn_next_move( Prompt, Ns ). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,384: | Line 3,384: | ||
{{works with|OpenEuphoria}} |
{{works with|OpenEuphoria}} |
||
No computer AI |
No computer AI |
||
< |
<syntaxhighlight lang="euphoria"> |
||
include std/console.e |
include std/console.e |
||
include std/text.e |
include std/text.e |
||
Line 3,485: | Line 3,485: | ||
end while |
end while |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,583: | Line 3,583: | ||
A purely-functional solution with a naive (but perfect) computer player implementation. The first move is played randomly by the computer. |
A purely-functional solution with a naive (but perfect) computer player implementation. The first move is played randomly by the computer. |
||
< |
<syntaxhighlight lang="fsharp">type Brick = |
||
| Empty |
| Empty |
||
| Computer |
| Computer |
||
Line 3,715: | Line 3,715: | ||
let b = set Computer emptyBoard (choose (freeSpace emptyBoard)) |
let b = set Computer emptyBoard (choose (freeSpace emptyBoard)) |
||
printBoard b |
printBoard b |
||
gameLoop b User</ |
gameLoop b User</syntaxhighlight> |
||
Example game: |
Example game: |
||
Line 3,794: | Line 3,794: | ||
Game between two humans. |
Game between two humans. |
||
< |
<syntaxhighlight lang="forth">create board 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , |
||
\ board: 0=empty, 1=player X, 2=player O |
\ board: 0=empty, 1=player X, 2=player O |
||
Line 3,847: | Line 3,847: | ||
game |
game |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,906: | Line 3,906: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Objective: write program in less than 100 lines, not using semicolons. Computer never loses, but plays as random as possible. Player gets first move of first game. Afterwards, first move alternates between computer and player. |
Objective: write program in less than 100 lines, not using semicolons. Computer never loses, but plays as random as possible. Player gets first move of first game. Afterwards, first move alternates between computer and player. |
||
< |
<syntaxhighlight lang="fortran"> |
||
! This is a fortran95 implementation of the game of tic-tac-toe. |
! This is a fortran95 implementation of the game of tic-tac-toe. |
||
! - Objective was to use less than 100 lines. |
! - Objective was to use less than 100 lines. |
||
Line 4,006: | Line 4,006: | ||
enddo mainloop |
enddo mainloop |
||
end program |
end program |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
===graphics mode=== |
===graphics mode=== |
||
< |
<syntaxhighlight lang="freebasic"> |
||
'About 400 lines of code, but it is a graphical (GUI ish) i.e. mouse driven. |
'About 400 lines of code, but it is a graphical (GUI ish) i.e. mouse driven. |
||
Line 4,434: | Line 4,434: | ||
End |
End |
||
End Sub |
End Sub |
||
</syntaxhighlight> |
|||
</lang> |
|||
===text mode=== |
===text mode=== |
||
< |
<syntaxhighlight lang="freebasic"> |
||
Screenres 320,240,32 |
Screenres 320,240,32 |
||
Line 4,680: | Line 4,680: | ||
Loop Until Multikey(&H01) |
Loop Until Multikey(&H01) |
||
End |
End |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Intermediate, like Python's "Better skilled player." Computer wins and blocks where it can, but otherwise plays randomly. Plays multiple games and keeps score. Player gets first move of first game. Afterwards, loser gets to go first, or after cat games, first move alternates. |
Intermediate, like Python's "Better skilled player." Computer wins and blocks where it can, but otherwise plays randomly. Plays multiple games and keeps score. Player gets first move of first game. Afterwards, loser gets to go first, or after cat games, first move alternates. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 4,844: | Line 4,844: | ||
{0, 4, 8}, // diagonals |
{0, 4, 8}, // diagonals |
||
{2, 4, 6}, |
{2, 4, 6}, |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Simplified version of Tic Tac Toe for player vs. player (no AI computer-controlled option). |
Simplified version of Tic Tac Toe for player vs. player (no AI computer-controlled option). |
||
< |
<syntaxhighlight lang="groovy">class Main { |
||
def input = new Scanner(System.in) |
def input = new Scanner(System.in) |
||
Line 4,981: | Line 4,981: | ||
return builder.toString(); |
return builder.toString(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Computer player has three strategies: 1. Try to block the opponent first, 2. Try to guess a good position for the next move, 3. Place a piece randomly. |
Computer player has three strategies: 1. Try to block the opponent first, 2. Try to guess a good position for the next move, 3. Place a piece randomly. |
||
There are lots of comments throughout the code. |
There are lots of comments throughout the code. |
||
<syntaxhighlight lang="haskell"> |
|||
<lang Haskell> |
|||
module Main where |
module Main where |
||
Line 5,207: | Line 5,207: | ||
putStrLn "3 : player X is human and player O is computer" |
putStrLn "3 : player X is human and player O is computer" |
||
putStrLn "Player X always begins." |
putStrLn "Player X always begins." |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Player X is computer, O is human. |
Player X is computer, O is human. |
||
Line 5,336: | Line 5,336: | ||
The following works in both Icon and Unicon. The computer plays randomly against a human player, with legal moves enforced and wins/draws notified. |
The following works in both Icon and Unicon. The computer plays randomly against a human player, with legal moves enforced and wins/draws notified. |
||
<syntaxhighlight lang="icon"> |
|||
<lang Icon> |
|||
# Play TicTacToe |
# Play TicTacToe |
||
Line 5,449: | Line 5,449: | ||
play_game () |
play_game () |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
To subsequent j poster: replacing this entry is fine by me. |
To subsequent j poster: replacing this entry is fine by me. |
||
<syntaxhighlight lang="j"> |
|||
<lang J> |
|||
Note 'ttt adjudicates or plays' |
Note 'ttt adjudicates or plays' |
||
Line 5,548: | Line 5,548: | ||
nor=: 8 b. |
nor=: 8 b. |
||
infix_pairs_agree=: 2&(=/\) |
infix_pairs_agree=: 2&(=/\) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
This version works in the terminal itself, and uses the numpad for data entry. The computer is unbeatable, but some lines can be removed to avoid that. There's also an override that thrown in, just for fun. |
This version works in the terminal itself, and uses the numpad for data entry. The computer is unbeatable, but some lines can be removed to avoid that. There's also an override that thrown in, just for fun. |
||
< |
<syntaxhighlight lang="java"> |
||
import java.io.BufferedReader; |
import java.io.BufferedReader; |
||
import java.io.InputStreamReader; |
import java.io.InputStreamReader; |
||
Line 5,877: | Line 5,877: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Line 5,927: | Line 5,927: | ||
This version uses javax.swing. |
This version uses javax.swing. |
||
< |
<syntaxhighlight lang="java">import javax.swing.*; |
||
import java.awt.*; |
import java.awt.*; |
||
import java.awt.event.*; |
import java.awt.event.*; |
||
Line 6,090: | Line 6,090: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Graphical Java Example |
Graphical Java Example |
||
< |
<syntaxhighlight lang="java"> |
||
import javax.swing.*; |
import javax.swing.*; |
||
import java.awt.event.*; |
import java.awt.event.*; |
||
Line 6,568: | Line 6,568: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
The following program may be executed for a player-against-player game. |
The following program may be executed for a player-against-player game. |
||
< |
<syntaxhighlight lang="java"> |
||
import javax.swing.*; |
import javax.swing.*; |
||
import javax.swing.border.Border; |
import javax.swing.border.Border; |
||
Line 6,851: | Line 6,851: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Javascript}}== |
=={{header|Javascript}}== |
||
HTML5 Canvas implementation. Should play perfectly or near-perfectly. |
HTML5 Canvas implementation. Should play perfectly or near-perfectly. |
||
<syntaxhighlight lang="javascript"> |
|||
<lang Javascript> |
|||
<!DOCTYPE html> |
<!DOCTYPE html> |
||
Line 7,292: | Line 7,292: | ||
</html> |
</html> |
||
</syntaxhighlight> |
|||
</lang> |
|||
Line 7,300: | Line 7,300: | ||
Human is X and goes first. |
Human is X and goes first. |
||
<syntaxhighlight lang="javascript"> |
|||
<lang Javascript> |
|||
// Board |
// Board |
||
const topLeft = 1; |
const topLeft = 1; |
||
Line 7,485: | Line 7,485: | ||
} |
} |
||
}); |
}); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 7,512: | Line 7,512: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
One move look-ahead algorithm. Computer plays to win or at least draw. |
One move look-ahead algorithm. Computer plays to win or at least draw. |
||
< |
<syntaxhighlight lang="julia">const winningpositions = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], |
||
[2, 5, 8], [3, 6, 9],[1, 5, 9], [7, 5, 3]] |
[2, 5, 8], [3, 6, 9],[1, 5, 9], [7, 5, 3]] |
||
Line 7,614: | Line 7,614: | ||
tictactoe() |
tictactoe() |
||
</ |
</syntaxhighlight> {{output}} <pre> |
||
Board move grid: |
Board move grid: |
||
1 2 3 |
1 2 3 |
||
Line 7,661: | Line 7,661: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.51 |
||
import java.util.Random |
import java.util.Random |
||
Line 7,767: | Line 7,767: | ||
println() |
println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Sample game: |
Sample game: |
||
Line 7,827: | Line 7,827: | ||
As image uploads has been disabled, a live version can be viewed at: [http://jono.guthrie.net.nz/rosetta/Tic-tac-toe.lasso http://jono.guthrie.net.nz/rosetta/Tic-tac-toe.lasso] |
As image uploads has been disabled, a live version can be viewed at: [http://jono.guthrie.net.nz/rosetta/Tic-tac-toe.lasso http://jono.guthrie.net.nz/rosetta/Tic-tac-toe.lasso] |
||
<syntaxhighlight lang="lasso">[ |
|||
<lang Lasso>[ |
|||
session_start('user') |
session_start('user') |
||
session_addvar('user', 'matrix') |
session_addvar('user', 'matrix') |
||
Line 7,921: | Line 7,921: | ||
$matrix = array('-','-','-','-','-','-','-','-','-') |
$matrix = array('-','-','-','-','-','-','-','-','-') |
||
$turn = 'x' |
$turn = 'x' |
||
}]</ |
}]</syntaxhighlight> |
||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
Line 7,927: | Line 7,927: | ||
Screenshot of application window: http://valentin.dasdeck.com/lingo/tic-tac-toe/tic-tac-toe-lingo.png<br /> |
Screenshot of application window: http://valentin.dasdeck.com/lingo/tic-tac-toe/tic-tac-toe-lingo.png<br /> |
||
"Human" cannot win this game. |
"Human" cannot win this game. |
||
< |
<syntaxhighlight lang="lingo">global $ -- object representing simple framework |
||
global gBoard -- current board image |
global gBoard -- current board image |
||
global gBoardTemplate -- empty board image |
global gBoardTemplate -- empty board image |
||
Line 8,163: | Line 8,163: | ||
on sum (aLine) |
on sum (aLine) |
||
return aLine[1]+aLine[2]+aLine[3] |
return aLine[1]+aLine[2]+aLine[3] |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Version for LuaJIT with or without ffi, negamax algorithm with alpha-beta pruning and caching of results. Human can't win. |
Version for LuaJIT with or without ffi, negamax algorithm with alpha-beta pruning and caching of results. Human can't win. |
||
< |
<syntaxhighlight lang="lua">#!/usr/bin/env luajit |
||
ffi=require"ffi" |
ffi=require"ffi" |
||
local function printf(fmt,...) io.write(string.format(fmt, ...)) end |
local function printf(fmt,...) io.write(string.format(fmt, ...)) end |
||
Line 8,356: | Line 8,356: | ||
draw(board) |
draw(board) |
||
until endgame() |
until endgame() |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 8,421: | Line 8,421: | ||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
Computer May loose; |
Computer May loose; |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module Tic.Tac.Toe { |
Module Tic.Tac.Toe { |
||
Dim Board$(1 to 3, 1 to 3)=" " |
Dim Board$(1 to 3, 1 to 3)=" " |
||
Line 8,533: | Line 8,533: | ||
} |
} |
||
Tic.Tac.Toe |
Tic.Tac.Toe |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre style="height:30ex;overflow:scroll"> |
<pre style="height:30ex;overflow:scroll"> |
||
Line 8,607: | Line 8,607: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
<lang>DynamicModule[{board = ConstantArray[0, {3, 3}], text = "Playing...", |
<syntaxhighlight lang="text">DynamicModule[{board = ConstantArray[0, {3, 3}], text = "Playing...", |
||
first, rows = |
first, rows = |
||
Join[#, Transpose@#, {Diagonal@#, Diagonal@Reverse@#}] &}, |
Join[#, Transpose@#, {Diagonal@#, Diagonal@Reverse@#}] &}, |
||
Line 8,642: | Line 8,642: | ||
text = "Draw."]]]]], {i, 1, 3}, {j, 1, 3}], Thickness[.01], |
text = "Draw."]]]]], {i, 1, 3}, {j, 1, 3}], Thickness[.01], |
||
Line[{{{1, 0}, {1, 3}}, {{2, 0}, {2, 3}}, {{0, 1}, {3, 1}}, {{0, |
Line[{{{1, 0}, {1, 3}}, {{2, 0}, {2, 3}}, {{0, 1}, {3, 1}}, {{0, |
||
2}, {3, 2}}}]}], Dynamic@text}]</ |
2}, {3, 2}}}]}], Dynamic@text}]</syntaxhighlight> |
||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
Allows for choice between any combination of human or computer players. Computer players are intelligent, but not perfect. It implements the "rules" used by the Newell and Simon's 1972 tic-tac-toe program (as explained by Wikipedia), but this implementation does not factor in the move before the move causing the fork (either for creation or prevention). |
Allows for choice between any combination of human or computer players. Computer players are intelligent, but not perfect. It implements the "rules" used by the Newell and Simon's 1972 tic-tac-toe program (as explained by Wikipedia), but this implementation does not factor in the move before the move causing the fork (either for creation or prevention). |
||
< |
<syntaxhighlight lang="matlab">function TicTacToe |
||
% Set up the board (one for each player) |
% Set up the board (one for each player) |
||
Line 8,869: | Line 8,869: | ||
% boards - 3x3x2 logical representation of all players' pieces |
% boards - 3x3x2 logical representation of all players' pieces |
||
draw = all(all(boards(:, :, 1) | boards(:, :, 2))); |
draw = all(all(boards(:, :, 1) | boards(:, :, 2))); |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Computer goes first and plays perfectly: |
Computer goes first and plays perfectly: |
||
Line 8,991: | Line 8,991: | ||
=={{header|mIRC Scripting Language}}== |
=={{header|mIRC Scripting Language}}== |
||
< |
<syntaxhighlight lang="mirc">alias ttt { |
||
if ($2 isin %ttt) || (!%ttt) { |
if ($2 isin %ttt) || (!%ttt) { |
||
var %ttt~ = $remove($iif(%ttt,%ttt,1 2 3 4 5 6 7 8 9),$2,X,O) |
var %ttt~ = $remove($iif(%ttt,%ttt,1 2 3 4 5 6 7 8 9),$2,X,O) |
||
Line 9,021: | Line 9,021: | ||
echo -ag � $+ $iif($7 isnum,$chr(32),$7) $+ $chr(124) $+ $iif($8 isnum,$chr(32),$8) $+ $chr(124) $+ $iif($9 isnum, ,$9) |
echo -ag � $+ $iif($7 isnum,$chr(32),$7) $+ $chr(124) $+ $iif($8 isnum,$chr(32),$8) $+ $chr(124) $+ $iif($9 isnum, ,$9) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|МК-61/52}}== |
=={{header|МК-61/52}}== |
||
< |
<syntaxhighlight lang="mk-61">9 С/П ПП 28 пи * cos x<0 16 ИП2 |
||
ПП 28 1 - БП 51 ИП7 ПП 28 ИП7 |
ПП 28 1 - БП 51 ИП7 ПП 28 ИП7 |
||
ПП 28 КИП2 ИП2 ВП 4 4 С/П 1 - |
ПП 28 КИП2 ИП2 ВП 4 4 С/П 1 - |
||
x=0 33 8 П2 С/П П7 ИП2 4 - x#0 |
x=0 33 8 П2 С/П П7 ИП2 4 - x#0 |
||
43 x<0 45 8 + П8 ИП7 - x#0 55 |
43 x<0 45 8 + П8 ИП7 - x#0 55 |
||
ИП8 ВП 6 6 С/П ИП2 В/О</ |
ИП8 ВП 6 6 С/П ИП2 В/О</syntaxhighlight> |
||
Cell numbering 1 to 9; starts from the upper left cell, then clockwise in a spiral. The first move is a calculator. Result: 44 - draw, 66 - victory of the calculator. |
Cell numbering 1 to 9; starts from the upper left cell, then clockwise in a spiral. The first move is a calculator. Result: 44 - draw, 66 - victory of the calculator. |
||
Line 9,037: | Line 9,037: | ||
This is a translation of the second version with the better AI, but with some differences. For instance, we have chosen to display the board in the same way as the first version. All procedures have a parameter "board" rather accessing a global variable. We use also base 1-indexing for the board. Etc. |
This is a translation of the second version with the better AI, but with some differences. For instance, we have chosen to display the board in the same way as the first version. All procedures have a parameter "board" rather accessing a global variable. We use also base 1-indexing for the board. Etc. |
||
< |
<syntaxhighlight lang="nim">import options, random, sequtils, strutils |
||
type |
type |
||
Line 9,140: | Line 9,140: | ||
echo "Input the index of where you wish to place your mark at your turn." |
echo "Input the index of where you wish to place your mark at your turn." |
||
randomize() |
randomize() |
||
play()</ |
play()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 9,203: | Line 9,203: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
Tic-tac-toe game using Minimax algorithm. |
Tic-tac-toe game using Minimax algorithm. |
||
< |
<syntaxhighlight lang="objeck">class TicTacToe { |
||
@board : Char[,]; |
@board : Char[,]; |
||
@cpu_opening : Bool; |
@cpu_opening : Bool; |
||
Line 9,574: | Line 9,574: | ||
"===========\n"->PrintLine(); |
"===========\n"->PrintLine(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 9,582: | Line 9,582: | ||
I would expect this version should compile with most Pascal variants including Delphi, but YMMR. |
I would expect this version should compile with most Pascal variants including Delphi, but YMMR. |
||
< |
<syntaxhighlight lang="pascal">program tic(Input, Output); |
||
type |
type |
||
Line 9,745: | Line 9,745: | ||
player := swapPlayer(player); |
player := swapPlayer(player); |
||
end |
end |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 9,756: | Line 9,756: | ||
the computer. Anyone who can identify the mistake, is welcome to fix it. |
the computer. Anyone who can identify the mistake, is welcome to fix it. |
||
< |
<syntaxhighlight lang="perl">use warnings; |
||
use strict; |
use strict; |
||
Line 9,855: | Line 9,855: | ||
$whose_turn = !$whose_turn; |
$whose_turn = !$whose_turn; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 9,913: | Line 9,913: | ||
</pre> |
</pre> |
||
===Alternate with GUI=== |
===Alternate with GUI=== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; |
use strict; |
||
Line 9,984: | Line 9,984: | ||
(sort {$a->[0] <=> $b->[0]} shuffle @moves)[ -($who eq 'X') ] |
(sort {$a->[0] <=> $b->[0]} shuffle @moves)[ -($who eq 'X') ] |
||
}; |
}; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 9,991: | Line 9,991: | ||
{{libheader|Phix/online}} |
{{libheader|Phix/online}} |
||
You can run this online [http://phix.x10.mx/p2js/Tic_tac_toe.htm here]. |
You can run this online [http://phix.x10.mx/p2js/Tic_tac_toe.htm here]. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
<span style="color: #000080;font-style:italic;">-- |
||
-- demo\rosetta\Tic_tac_toe.exw |
-- demo\rosetta\Tic_tac_toe.exw |
||
Line 10,229: | Line 10,229: | ||
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
Line 10,235: | Line 10,235: | ||
NOTE: While PHP can considered a super-set of HTML/JS, usage has been kept to a minimum to focus on the more specific PHP parts. |
NOTE: While PHP can considered a super-set of HTML/JS, usage has been kept to a minimum to focus on the more specific PHP parts. |
||
<syntaxhighlight lang="php"> |
|||
<lang PHP> |
|||
<?php |
<?php |
||
const BOARD_NUM = 9; |
const BOARD_NUM = 9; |
||
Line 10,293: | Line 10,293: | ||
echo '<a href="?b=', $EMPTY_BOARD_STR, '">Reset</a>'; |
echo '<a href="?b=', $EMPTY_BOARD_STR, '">Reset</a>'; |
||
if ($gameOver) echo '<h1>Game Over!</h1>'; |
if ($gameOver) echo '<h1>Game Over!</h1>'; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
This solution doesn't bother about the game logic, but simply uses the alpha-beta-pruning 'game' function in the "simul" library. |
This solution doesn't bother about the game logic, but simply uses the alpha-beta-pruning 'game' function in the "simul" library. |
||
< |
<syntaxhighlight lang="picolisp">(load "@lib/simul.l") # for 'game' function |
||
(de display () |
(de display () |
||
Line 10,363: | Line 10,363: | ||
((find3 T) "Congratulation, you won!") |
((find3 T) "Congratulation, you won!") |
||
((not (myMove)) "No moves") |
((not (myMove)) "No moves") |
||
((find3 0) "Sorry, you lost!") ) )</ |
((find3 0) "Sorry, you lost!") ) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>: (main) |
<pre>: (main) |
||
Line 10,397: | Line 10,397: | ||
Uses a minimax algorithm with no Alpha-beta pruning, as the max depth of the recursion is 8. Computer never loses.<br> |
Uses a minimax algorithm with no Alpha-beta pruning, as the max depth of the recursion is 8. Computer never loses.<br> |
||
A GUI interface written in XPCE is given. |
A GUI interface written in XPCE is given. |
||
< |
<syntaxhighlight lang="prolog">:- use_module('min-max.pl'). |
||
:-dynamic box/2. |
:-dynamic box/2. |
||
Line 10,606: | Line 10,606: | ||
computer(o). |
computer(o). |
||
</syntaxhighlight> |
|||
</lang> |
|||
Module min-max.pl defines minimax algorithm. |
Module min-max.pl defines minimax algorithm. |
||
< |
<syntaxhighlight lang="prolog">:- module('min-max.pl', [minimax/5]). |
||
% minimax(Player, Deep, MaxDeep, B, V-B) |
% minimax(Player, Deep, MaxDeep, B, V-B) |
||
Line 10,635: | Line 10,635: | ||
lie(TTT, V-_, V-TTT). |
lie(TTT, V-_, V-TTT). |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
The computer enforces the rules but plays a random game. |
The computer enforces the rules but plays a random game. |
||
< |
<syntaxhighlight lang="python"> |
||
''' |
''' |
||
Tic-tac-toe game player. |
Tic-tac-toe game player. |
||
Line 10,714: | Line 10,714: | ||
else: |
else: |
||
print('\nA draw') |
print('\nA draw') |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Sample Game''' |
'''Sample Game''' |
||
Line 10,757: | Line 10,757: | ||
In this version, The computer player will first complete a winning line of its own if it can, otherwise block a winning line of its opponent if they have two in a row, or then choose a random move. |
In this version, The computer player will first complete a winning line of its own if it can, otherwise block a winning line of its opponent if they have two in a row, or then choose a random move. |
||
< |
<syntaxhighlight lang="python"> |
||
''' |
''' |
||
Tic-tac-toe game player. |
Tic-tac-toe game player. |
||
Line 10,848: | Line 10,848: | ||
break |
break |
||
else: |
else: |
||
print('\nA draw')</ |
print('\nA draw')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 10,933: | Line 10,933: | ||
This program simulates a game of Tic-Tac-Toe inside of an interactive window. It includes three player modes, which are a two-player game, a human versus random AI, or human versus maximized AI. This implementation belongs to "X", and can also be found here. |
This program simulates a game of Tic-Tac-Toe inside of an interactive window. It includes three player modes, which are a two-player game, a human versus random AI, or human versus maximized AI. This implementation belongs to "X", and can also be found here. |
||
<syntaxhighlight lang="r"> |
|||
<lang R> |
|||
rm(list=ls()) |
rm(list=ls()) |
||
library(RColorBrewer) |
library(RColorBrewer) |
||
Line 11,347: | Line 11,347: | ||
#start("name", "mode" = 0 - 2, type = 0,1) |
#start("name", "mode" = 0 - 2, type = 0,1) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Line 11,368: | Line 11,368: | ||
The <tt>minimax.rkt</tt> module: |
The <tt>minimax.rkt</tt> module: |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang lazy |
#lang lazy |
||
(provide minimax) |
(provide minimax) |
||
Line 11,389: | Line 11,389: | ||
(next (cdr x) |
(next (cdr x) |
||
(min β (minimax (car x) α β (not max-player))))))])))) |
(min β (minimax (car x) α β (not max-player))))))])))) |
||
</ |
</syntaxhighlight> |
||
The <tt>game.rkt</tt> module: |
The <tt>game.rkt</tt> module: |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang lazy |
#lang lazy |
||
(require racket/class |
(require racket/class |
||
Line 11,503: | Line 11,503: | ||
(set-field! opponent p2 p1) |
(set-field! opponent p2 p1) |
||
(send p1 your-turn initial-state)) |
(send p1 your-turn initial-state)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
The <tt>tick-tack.rkt</tt> module: |
The <tt>tick-tack.rkt</tt> module: |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(require "game.rkt" |
(require "game.rkt" |
||
Line 11,597: | Line 11,597: | ||
(new (interactive-player o%) [name "Dummy"] [look-ahead 0])) |
(new (interactive-player o%) [name "Dummy"] [look-ahead 0])) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Sample games: |
Sample games: |
||
Line 11,724: | Line 11,724: | ||
As an example of another zero-sum game consider the classical [http://en.wikipedia.org/wiki/Nim Nim] game: |
As an example of another zero-sum game consider the classical [http://en.wikipedia.org/wiki/Nim Nim] game: |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
Line 11,764: | Line 11,764: | ||
(define player-B |
(define player-B |
||
(new (interactive-player second%) [name "B"] [look-ahead 4])) |
(new (interactive-player second%) [name "B"] [look-ahead 4])) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Computer plays with the computer: |
Computer plays with the computer: |
||
Line 11,800: | Line 11,800: | ||
The computer plays a random game. |
The computer plays a random game. |
||
<lang |
<syntaxhighlight lang="raku" line>my @board = 1..9; |
||
my @winning-positions = [0..2], [3..5], [6..8], [0,3,6], [1,4,7], [2,5,8], |
my @winning-positions = [0..2], [3..5], [6..8], [0,3,6], [1,4,7], [2,5,8], |
||
[0,4,8], [6,4,2]; |
[0,4,8], [6,4,2]; |
||
Line 11,846: | Line 11,846: | ||
} else { |
} else { |
||
say "How boring, a draw!"; |
say "How boring, a draw!"; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 11,865: | Line 11,865: | ||
<br>A fair amount of code was dedicated to error detection and the displaying of error messages, and |
<br>A fair amount of code was dedicated to error detection and the displaying of error messages, and |
||
<br>also the presentation of the tic─tac─toe game boards (grids). |
<br>also the presentation of the tic─tac─toe game boards (grids). |
||
< |
<syntaxhighlight lang="rexx">/*REXX program plays (with a human) the tic─tac─toe game on an NxN grid. */ |
||
$= copies('─', 9) /*eyecatcher for error messages, prompt*/ |
$= copies('─', 9) /*eyecatcher for error messages, prompt*/ |
||
oops = $ '***error*** ' /*literal for when an error happens. */ |
oops = $ '***error*** ' /*literal for when an error happens. */ |
||
Line 12,006: | Line 12,006: | ||
if _==N | (_>=w & ec\=='') then return 1==1 /*a winner has been determined.*/ |
if _==N | (_>=w & ec\=='') then return 1==1 /*a winner has been determined.*/ |
||
return 0==1 /*no winner " " " */</ |
return 0==1 /*no winner " " " */</syntaxhighlight> |
||
This REXX program makes use of '''LINESIZE''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console); this is used to determine the amount of padding for a centered display of the two grids. |
This REXX program makes use of '''LINESIZE''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console); this is used to determine the amount of padding for a centered display of the two grids. |
||
Line 12,210: | Line 12,210: | ||
The tecTacToe.ring is provided [https://github.com/AbdelrahmanGIT/RingSamples/blob/master/src/TecTacToe.ring here] |
The tecTacToe.ring is provided [https://github.com/AbdelrahmanGIT/RingSamples/blob/master/src/TecTacToe.ring here] |
||
< |
<syntaxhighlight lang="ring"> |
||
Load "guilib.ring" |
Load "guilib.ring" |
||
Line 12,340: | Line 12,340: | ||
next |
next |
||
if tie=true return 3 ok return 0 |
if tie=true return 3 ok return 0 |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 12,352: | Line 12,352: | ||
This implementation stores the board as a one-dimensional array and hardcodes all possible straight lines in <code>LINES</code>, rather than storing the board as a two-dimensional matrix and identifying straight lines dynamically. |
This implementation stores the board as a one-dimensional array and hardcodes all possible straight lines in <code>LINES</code>, rather than storing the board as a two-dimensional matrix and identifying straight lines dynamically. |
||
< |
<syntaxhighlight lang="ruby">module TicTacToe |
||
LINES = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]] |
LINES = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]] |
||
Line 12,537: | Line 12,537: | ||
puts |
puts |
||
players_with_human = [HumanPlayer, ComputerPlayer].shuffle |
players_with_human = [HumanPlayer, ComputerPlayer].shuffle |
||
Game.new(*players_with_human).play</ |
Game.new(*players_with_human).play</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 12,606: | Line 12,606: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">' --------------------------- |
||
' TIC TAC TOE |
' TIC TAC TOE |
||
' --------------------------- |
' --------------------------- |
||
Line 12,707: | Line 12,707: | ||
input "Play again (y/n)";p$ |
input "Play again (y/n)";p$ |
||
if upper$(p$) = "Y" then goto [newGame] |
if upper$(p$) = "Y" then goto [newGame] |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust"> |
||
use GameState::{ComputerWin, Draw, PlayerWin, Playing}; |
use GameState::{ComputerWin, Draw, PlayerWin, Playing}; |
||
Line 12,831: | Line 12,831: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 12,863: | Line 12,863: | ||
Computer vs. human. Human starts. Computer plays 'O' and human plays 'X'. |
Computer vs. human. Human starts. Computer plays 'O' and human plays 'X'. |
||
Computer moves are legal, but random. |
Computer moves are legal, but random. |
||
< |
<syntaxhighlight lang="scala">package object tictactoe { |
||
val Human = 'X' |
val Human = 'X' |
||
val Computer = 'O' |
val Computer = 'O' |
||
Line 12,944: | Line 12,944: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}}(human is always first) |
{{out}}(human is always first) |
||
Line 12,979: | Line 12,979: | ||
Can be a game of human v. human, human v. machine, or machine v. machine. Machine moves have a hierarchy: firstly, it looks for a winning move; secondly, it looks for a way to block the opponent's victory; lastly, it makes a random move. |
Can be a game of human v. human, human v. machine, or machine v. machine. Machine moves have a hierarchy: firstly, it looks for a winning move; secondly, it looks for a way to block the opponent's victory; lastly, it makes a random move. |
||
<lang>function [] = startGame() |
<syntaxhighlight lang="text">function [] = startGame() |
||
//Board size and marks |
//Board size and marks |
||
N = 3; |
N = 3; |
||
Line 13,357: | Line 13,357: | ||
endfunction |
endfunction |
||
startGame()</ |
startGame()</syntaxhighlight> |
||
=={{header|SQL}}== |
=={{header|SQL}}== |
||
Basic playable TicTacToe in SQLite using only the sqlite3 CLI. |
Basic playable TicTacToe in SQLite using only the sqlite3 CLI. |
||
The CLI wasn't really designed for this, making the interactivity a little bit clunky. However, some of the higher-level constructs in SQL allow for some intriguingly elegant game logic. |
The CLI wasn't really designed for this, making the interactivity a little bit clunky. However, some of the higher-level constructs in SQL allow for some intriguingly elegant game logic. |
||
<syntaxhighlight lang="sql"> |
|||
<lang SQL> |
|||
--Setup |
--Setup |
||
drop table if exists board; |
drop table if exists board; |
||
Line 13,449: | Line 13,449: | ||
.print "->update board set p = 'X' where rowid = ?; select * from ui; select * from msg;"' |
.print "->update board set p = 'X' where rowid = ?; select * from ui; select * from msg;"' |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 13,481: | Line 13,481: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
Some Basic AI for obvious losing and winning conditions |
Some Basic AI for obvious losing and winning conditions |
||
<syntaxhighlight lang="swift"> |
|||
<lang Swift> |
|||
import Darwin |
import Darwin |
||
Line 13,688: | Line 13,688: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 13,792: | Line 13,792: | ||
=={{header|Tailspin}}== |
=={{header|Tailspin}}== |
||
< |
<syntaxhighlight lang="tailspin"> |
||
processor Tic-Tac-Toe |
processor Tic-Tac-Toe |
||
@: [position´1..position´9]; |
@: [position´1..position´9]; |
||
Line 13,850: | Line 13,850: | ||
end play |
end play |
||
$play -> !VOID</ |
$play -> !VOID</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 13,907: | Line 13,907: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.6 |
||
# This code splits the players from the core game engine |
# This code splits the players from the core game engine |
||
Line 14,027: | Line 14,027: | ||
# Assemble the pieces |
# Assemble the pieces |
||
set ttt [TicTacToe new HumanPlayer RandomRoboPlayer] |
set ttt [TicTacToe new HumanPlayer RandomRoboPlayer] |
||
$ttt game</ |
$ttt game</syntaxhighlight> |
||
Sample game: |
Sample game: |
||
<pre> |
<pre> |
||
Line 14,079: | Line 14,079: | ||
=={{Header|Tiny BASIC}}== |
=={{Header|Tiny BASIC}}== |
||
< |
<syntaxhighlight lang="tiny basic"> REM Tic-tac-toe for Tiny BASIC |
||
REM |
REM |
||
REM Released as public domain by Damian Gareth Walker, 2019 |
REM Released as public domain by Damian Gareth Walker, 2019 |
||
Line 14,279: | Line 14,279: | ||
IF M=8 THEN LET Y=P |
IF M=8 THEN LET Y=P |
||
IF M=9 THEN LET Z=P |
IF M=9 THEN LET Z=P |
||
RETURN </ |
RETURN </syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 14,285: | Line 14,285: | ||
The computer plays randomly. |
The computer plays randomly. |
||
< |
<syntaxhighlight lang="sh">#!/usr/bin/env bash |
||
# play tic-tac-toe |
# play tic-tac-toe |
||
main() { |
main() { |
||
Line 14,389: | Line 14,389: | ||
} |
} |
||
main "$@"</ |
main "$@"</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 14,440: | Line 14,440: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
Human play first with the "X". You must choose the row and the column you want to play... |
Human play first with the "X". You must choose the row and the column you want to play... |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Option Explicit |
Option Explicit |
||
Line 14,567: | Line 14,567: | ||
IsEnd = True |
IsEnd = True |
||
End Function |
End Function |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Loop 0 |
<pre>Loop 0 |
||
Line 14,603: | Line 14,603: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|Wren-ioutil}} |
{{libheader|Wren-ioutil}} |
||
< |
<syntaxhighlight lang="ecmascript">import "random" for Random |
||
import "/ioutil" for Input |
import "/ioutil" for Input |
||
Line 14,702: | Line 14,702: | ||
if (yn == "n" || yn == "N") return |
if (yn == "n" || yn == "N") return |
||
System.print() |
System.print() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 14,765: | Line 14,765: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
[[File:TTTXPL0.GIF|right]] |
[[File:TTTXPL0.GIF|right]] |
||
< |
<syntaxhighlight lang="xpl0">\The computer marks its moves with an "O" and the player uses an "X". The |
||
\ numeric keypad is used to make the player's move. |
\ numeric keypad is used to make the player's move. |
||
\ |
\ |
||
Line 14,909: | Line 14,909: | ||
[SetVid(3); exit]; \clear screen & restore normal text mode |
[SetVid(3); exit]; \clear screen & restore normal text mode |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
In the classic style. |
In the classic style. |
||
< |
<syntaxhighlight lang="yabasic">5 REM Adaptation to Yabasic of the program published in Tim Hartnell's book "Artificial Intelligence: Concepts and Programs", with some minor modifications. 6/2018. |
||
10 REM TICTAC |
10 REM TICTAC |
||
15 INPUT "English (0), Spanish (other key): " IDIOMA : IF NOT IDIOMA THEN RESTORE 2020 ELSE RESTORE 2010 END IF |
15 INPUT "English (0), Spanish (other key): " IDIOMA : IF NOT IDIOMA THEN RESTORE 2020 ELSE RESTORE 2010 END IF |
||
Line 15,055: | Line 15,055: | ||
2010 DATA "YO GANO,TU GANAS,ES SIMPLEMENTE UN DIBUJO,ESTA ES MI PRIORIDAD ACTUALIZADA,PULSE LA TECLA <RETURN> PARA CONTINUAR,REALICE SU MOVIMIENTO,MOVIMIENTO: " |
2010 DATA "YO GANO,TU GANAS,ES SIMPLEMENTE UN DIBUJO,ESTA ES MI PRIORIDAD ACTUALIZADA,PULSE LA TECLA <RETURN> PARA CONTINUAR,REALICE SU MOVIMIENTO,MOVIMIENTO: " |
||
2020 DATA "I WIN,YOU WIN,IT'S JUST A DRAWING,THIS IS MY PRIORITY UPDATE,PRESS <RETURN> TO CONTINUE,TO MAKE YOUR MOVE,MOVEMENT: " |
2020 DATA "I WIN,YOU WIN,IT'S JUST A DRAWING,THIS IS MY PRIORITY UPDATE,PRESS <RETURN> TO CONTINUE,TO MAKE YOUR MOVE,MOVEMENT: " |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{omit from|GUISS}} |
{{omit from|GUISS}} |