本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Every Trader's Tic-Tac-Toe vs Computer", overlay=true, max_labels_count=500)
// ═════════════════════════════════════════════════════════════════════════════
// 🎮 GAME SETTINGS
// ═════════════════════════════════════════════════════════════════════════════
groupGame = "🎮 GAME"
playerName = input.string(
"PLAYER",
"Your Name",
group=groupGame)
computerDelay = input.int(
3,
"🤖 Computer Delay",
minval=1,
maxval=5,
group=groupGame)
gameNumber = input.int(
1,
"🔄 Game Number",
minval=1,
group=groupGame)
// ═════════════════════════════════════════════════════════════════════════════
// 🎭 SYMBOLS
// ═════════════════════════════════════════════════════════════════════════════
groupSymbols = "🎭 SYMBOLS"
playerSymbol = input.string(
"X",
"Your Symbol",
options=[
"X",
"O",
"👑",
"🤖",
"⭐",
"🔥",
"🐱",
"🐶",
"🟢",
"🔵",
"🦦",
"🦅"
],
group=groupSymbols)
computerSymbol = input.string(
"O",
"Computer Symbol",
options=[
"X",
"O",
"👑",
"🤖",
"⭐",
"🔥",
"🐱",
"🐶",
"🟢",
"🔵",
"🦦",
"🦅"
],
group=groupSymbols)
// ═════════════════════════════════════════════════════════════════════════════
// 🎯 MOVES
// ═════════════════════════════════════════════════════════════════════════════
groupMoves = "🎯 MOVES"
move1 = input.string(
"",
"Move 1",
options=[
"",
"A1", "A2", "A3",
"B1", "B2", "B3",
"C1", "C2", "C3"
],
group=groupMoves)
move2 = input.string(
"",
"Move 2",
options=[
"",
"A1", "A2", "A3",
"B1", "B2", "B3",
"C1", "C2", "C3"
],
group=groupMoves)
move3 = input.string(
"",
"Move 3",
options=[
"",
"A1", "A2", "A3",
"B1", "B2", "B3",
"C1", "C2", "C3"
],
group=groupMoves)
move4 = input.string(
"",
"Move 4",
options=[
"",
"A1", "A2", "A3",
"B1", "B2", "B3",
"C1", "C2", "C3"
],
group=groupMoves)
move5 = input.string(
"",
"Move 5",
options=[
"",
"A1", "A2", "A3",
"B1", "B2", "B3",
"C1", "C2", "C3"
],
group=groupMoves)
move6 = input.string(
"",
"Move 6",
options=[
"",
"A1", "A2", "A3",
"B1", "B2", "B3",
"C1", "C2", "C3"
],
group=groupMoves)
move7 = input.string(
"",
"Move 7",
options=[
"",
"A1", "A2", "A3",
"B1", "B2", "B3",
"C1", "C2", "C3"
],
group=groupMoves)
move8 = input.string(
"",
"Move 8",
options=[
"",
"A1", "A2", "A3",
"B1", "B2", "B3",
"C1", "C2", "C3"
],
group=groupMoves)
move9 = input.string(
"",
"Move 9",
options=[
"",
"A1", "A2", "A3",
"B1", "B2", "B3",
"C1", "C2", "C3"
],
group=groupMoves)
// ═════════════════════════════════════════════════════════════════════════════
// 🎨 COLORS
// ═════════════════════════════════════════════════════════════════════════════
gold = color.rgb(212, 175, 55)
brightGold = color.rgb(255, 215, 80)
deepGold = color.rgb(70, 52, 10)
black = color.rgb(8, 8, 8)
darkBg = color.rgb(15, 15, 15)
cellBg = color.rgb(22, 22, 22)
white = color.rgb(235, 235, 235)
gray = color.rgb(130, 130, 130)
red = color.rgb(190, 60, 60)
brandName = "World Financial Freedom"
// ═════════════════════════════════════════════════════════════════════════════
// 📐 BOARD SETTINGS
// ═════════════════════════════════════════════════════════════════════════════
groupBoard = "📐 BOARD"
boardSize = input.string(
"Normal",
"Board Size",
options=[
"Tiny",
"Small",
"Normal",
"Large"
],
group=groupBoard)
boardPosition = input.string(
"Top Right",
"Board Position",
options=[
"Top Left",
"Top Center",
"Top Right",
"Middle Left",
"Middle Center",
"Middle Right",
"Bottom Left",
"Bottom Center",
"Bottom Right"
],
group=groupBoard)
// ═════════════════════════════════════════════════════════════════════════════
// 📏 BOARD SIZE SETTINGS
// ═════════════════════════════════════════════════════════════════════════════
int cellWidth = 15
int cellHeight = 12
int labelWidth = 5
boardTextSize = size.huge
headerTextSize = size.normal
statusTextSize = size.normal
if boardSize == "Tiny"
cellWidth := 9
cellHeight := 7
labelWidth := 3
boardTextSize := size.small
headerTextSize := size.small
statusTextSize := size.small
else if boardSize == "Small"
cellWidth := 12
cellHeight := 9
labelWidth := 4
boardTextSize := size.normal
headerTextSize := size.small
statusTextSize := size.small
else if boardSize == "Normal"
cellWidth := 15
cellHeight := 12
labelWidth := 5
boardTextSize := size.huge
headerTextSize := size.normal
statusTextSize := size.normal
else
cellWidth := 19
cellHeight := 15
labelWidth := 6
boardTextSize := size.huge
headerTextSize := size.normal
statusTextSize := size.normal
// ═════════════════════════════════════════════════════════════════════════════
// 🔢 CONVERT SQUARE TO INDEX
// ═════════════════════════════════════════════════════════════════════════════
getIndex(string square) =>
switch square
"A1" => 0
"B1" => 1
"C1" => 2
"A2" => 3
"B2" => 4
"C2" => 5
"A3" => 6
"B3" => 7
"C3" => 8
=> -1
// ═════════════════════════════════════════════════════════════════════════════
// 🏆 WIN CHECK
// ═════════════════════════════════════════════════════════════════════════════
checkWinner(int[] b, int player) =>
bool won = false
won := won or (
array.get(b, 0) == player and
array.get(b, 1) == player and
array.get(b, 2) == player)
won := won or (
array.get(b, 3) == player and
array.get(b, 4) == player and
array.get(b, 5) == player)
won := won or (
array.get(b, 6) == player and
array.get(b, 7) == player and
array.get(b, 8) == player)
won := won or (
array.get(b, 0) == player and
array.get(b, 3) == player and
array.get(b, 6) == player)
won := won or (
array.get(b, 1) == player and
array.get(b, 4) == player and
array.get(b, 7) == player)
won := won or (
array.get(b, 2) == player and
array.get(b, 5) == player and
array.get(b, 8) == player)
won := won or (
array.get(b, 0) == player and
array.get(b, 4) == player and
array.get(b, 8) == player)
won := won or (
array.get(b, 2) == player and
array.get(b, 4) == player and
array.get(b, 6) == player)
won
// ═════════════════════════════════════════════════════════════════════════════
// 🤝 BOARD FULL
// ═════════════════════════════════════════════════════════════════════════════
boardFull(int[] b) =>
bool full = true
for i = 0 to 8
if array.get(b, i) == 0
full := false
full
// ═════════════════════════════════════════════════════════════════════════════
// 🧠 AI — TEST MOVE
// ═════════════════════════════════════════════════════════════════════════════
winningMove(int[] b, int index, int player) =>
bool result = false
if array.get(b, index) == 0
array.set(b, index, player)
result := checkWinner(b, player)
array.set(b, index, 0)
result
// ═════════════════════════════════════════════════════════════════════════════
// 🧠 AI — FIND WINNING MOVE
// ═════════════════════════════════════════════════════════════════════════════
findWinningMove(int[] b, int player) =>
int result = -1
for i = 0 to 8
if result == -1 and winningMove(b, i, player)
result := i
result
// ═════════════════════════════════════════════════════════════════════════════
// 🤖 COMPUTER AI
// ═════════════════════════════════════════════════════════════════════════════
getComputerMove(int[] b) =>
int chosen = -1
// Win if possible.
chosen := findWinningMove(b, 2)
// Block player if necessary.
if chosen == -1
chosen := findWinningMove(b, 1)
// Center.
if chosen == -1 and array.get(b, 4) == 0
chosen := 4
// Corners.
if chosen == -1
if array.get(b, 0) == 0
chosen := 0
else if array.get(b, 2) == 0
chosen := 2
else if array.get(b, 6) == 0
chosen := 6
else if array.get(b, 8) == 0
chosen := 8
// Any remaining square.
if chosen == -1
for i = 0 to 8
if chosen == -1 and array.get(b, i) == 0
chosen := i
chosen
// ═════════════════════════════════════════════════════════════════════════════
// 🧠 REBUILD GAME
// ═════════════════════════════════════════════════════════════════════════════
var int[] board = array.new_int(9, 0)
array.fill(board, 0)
string[] playerMoves = array.from(
move1,
move2,
move3,
move4,
move5,
move6,
move7,
move8,
move9)
bool gameOver = false
bool invalidMove = false
bool playerWon = false
bool computerWon = false
bool drawGame = false
bool sameSymbol = playerSymbol == computerSymbol
int movesPlayed = 0
string computerLastMove = "—"
string errorMessage = ""
// ═════════════════════════════════════════════════════════════════════════════
// 🚨 SYMBOL VALIDATION
// ═════════════════════════════════════════════════════════════════════════════
if sameSymbol
invalidMove := true
errorMessage := "⚠️ CHOOSE DIFFERENT SYMBOLS"
// ═════════════════════════════════════════════════════════════════════════════
// 🔄 PROCESS MOVES
// ═════════════════════════════════════════════════════════════════════════════
if not sameSymbol
for turn = 0 to 8
selectedMove = array.get(playerMoves, turn)
if not gameOver and not invalidMove and selectedMove != ""
selectedIndex = getIndex(selectedMove)
if selectedIndex < 0
invalidMove := true
errorMessage := "⚠️ Invalid move."
else if array.get(board, selectedIndex) != 0
invalidMove := true
errorMessage := "⚠️ " + selectedMove + " is already occupied."
else
// ─────────────────────────────────────────────────────────────
// PLAYER MOVE
// ─────────────────────────────────────────────────────────────
array.set(board, selectedIndex, 1)
movesPlayed += 1
if checkWinner(board, 1)
gameOver := true
playerWon := true
else if boardFull(board)
gameOver := true
drawGame := true
else
// ─────────────────────────────────────────────────────────
// COMPUTER MOVE
// ─────────────────────────────────────────────────────────
computerMove = getComputerMove(board)
if computerMove >= 0
array.set(board, computerMove, 2)
movesPlayed += 1
computerLastMove := switch computerMove
0 => "A1"
1 => "B1"
2 => "C1"
3 => "A2"
4 => "B2"
5 => "C2"
6 => "A3"
7 => "B3"
8 => "C3"
=> "—"
if checkWinner(board, 2)
gameOver := true
computerWon := true
else if boardFull(board)
gameOver := true
drawGame := true
// ═════════════════════════════════════════════════════════════════════════════
// 📢 STATUS
// ═════════════════════════════════════════════════════════════════════════════
statusText =
sameSymbol ? "⚠️ CHOOSE DIFFERENT SYMBOLS" :
invalidMove ? errorMessage :
playerWon ? "🏆 YOU WIN!" :
computerWon ? "🤖 COMPUTER WINS!" :
drawGame ? "🤝 DRAW!" :
movesPlayed == 0 ? "✏️ ENTER YOUR FIRST MOVE" :
"✏️ YOUR TURN"
statusColor =
sameSymbol ? red :
invalidMove ? red :
playerWon ? brightGold :
computerWon ? white :
drawGame ? gold :
brightGold
// ═════════════════════════════════════════════════════════════════════════════
// 📍 BOARD POSITION
// ═════════════════════════════════════════════════════════════════════════════
position = switch boardPosition
"Top Left" => position.top_left
"Top Center" => position.top_center
"Top Right" => position.top_right
"Middle Left" => position.middle_left
"Middle Center" => position.middle_center
"Middle Right" => position.middle_right
"Bottom Left" => position.bottom_left
"Bottom Center" => position.bottom_center
=> position.bottom_right
// ═════════════════════════════════════════════════════════════════════════════
// 📋 TABLE
// ═════════════════════════════════════════════════════════════════════════════
var table gameTable = table.new(
position,
4,
15,
border_width=2,
border_color=gold,
frame_color=gold,
frame_width=2)
// ═════════════════════════════════════════════════════════════════════════════
// 🔤 CELL TEXT
// ═════════════════════════════════════════════════════════════════════════════
cellText(int index) =>
value = array.get(board, index)
value == 1 ? playerSymbol :
value == 2 ? computerSymbol :
""
cellTextColor(int index) =>
value = array.get(board, index)
value == 1 ? brightGold :
value == 2 ? white :
gray
// ═════════════════════════════════════════════════════════════════════════════
// 🎨 DRAW GAME
// ═════════════════════════════════════════════════════════════════════════════
if barstate.islast
// ─────────────────────────────────────────────────────────────────────────
// CLEAR TABLE
// ─────────────────────────────────────────────────────────────────────────
for row = 0 to 14
for col = 0 to 3
table.cell(
gameTable,
col,
row,
"",
bgcolor=black)
// ═════════════════════════════════════════════════════════════════════════
// TITLE
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
0,
0,
"X TIC-TAC-TOE O",
text_color=brightGold,
bgcolor=black,
text_size=size.large)
table.merge_cells(gameTable, 0, 0, 3, 0)
table.cell(
gameTable,
0,
1,
"VS COMPUTER",
text_color=gold,
bgcolor=black,
text_size=headerTextSize)
table.merge_cells(gameTable, 0, 1, 3, 1)
// ═════════════════════════════════════════════════════════════════════════
// PLAYERS
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
0,
2,
playerName + " • " + playerSymbol,
text_color=brightGold,
bgcolor=darkBg,
text_size=size.small)
table.merge_cells(gameTable, 0, 2, 1, 2)
table.cell(
gameTable,
2,
2,
"COMPUTER • " + computerSymbol,
text_color=white,
bgcolor=darkBg,
text_size=size.small)
table.merge_cells(gameTable, 2, 2, 3, 2)
// ═════════════════════════════════════════════════════════════════════════
// COLUMN LABELS
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
0,
3,
"",
bgcolor=black,
width=labelWidth)
table.cell(
gameTable,
1,
3,
"A",
text_color=gold,
bgcolor=black,
text_size=headerTextSize,
width=cellWidth)
table.cell(
gameTable,
2,
3,
"B",
text_color=gold,
bgcolor=black,
text_size=headerTextSize,
width=cellWidth)
table.cell(
gameTable,
3,
3,
"C",
text_color=gold,
bgcolor=black,
text_size=headerTextSize,
width=cellWidth)
// ═════════════════════════════════════════════════════════════════════════
// ROW LABEL 1
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
0,
4,
"1",
text_color=gold,
bgcolor=black,
text_size=headerTextSize,
width=labelWidth,
height=cellHeight)
// ═════════════════════════════════════════════════════════════════════════
// ROW LABEL 2
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
0,
5,
"2",
text_color=gold,
bgcolor=black,
text_size=headerTextSize,
width=labelWidth,
height=cellHeight)
// ═════════════════════════════════════════════════════════════════════════
// ROW LABEL 3
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
0,
6,
"3",
text_color=gold,
bgcolor=black,
text_size=headerTextSize,
width=labelWidth,
height=cellHeight)
// ═════════════════════════════════════════════════════════════════════════
// A1
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
1,
4,
cellText(0),
text_color=cellTextColor(0),
bgcolor=cellBg,
text_size=boardTextSize,
width=cellWidth,
height=cellHeight)
// B1
table.cell(
gameTable,
2,
4,
cellText(1),
text_color=cellTextColor(1),
bgcolor=cellBg,
text_size=boardTextSize,
width=cellWidth,
height=cellHeight)
// C1
table.cell(
gameTable,
3,
4,
cellText(2),
text_color=cellTextColor(2),
bgcolor=cellBg,
text_size=boardTextSize,
width=cellWidth,
height=cellHeight)
// ═════════════════════════════════════════════════════════════════════════
// A2
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
1,
5,
cellText(3),
text_color=cellTextColor(3),
bgcolor=cellBg,
text_size=boardTextSize,
width=cellWidth,
height=cellHeight)
// B2
table.cell(
gameTable,
2,
5,
cellText(4),
text_color=cellTextColor(4),
bgcolor=cellBg,
text_size=boardTextSize,
width=cellWidth,
height=cellHeight)
// C2
table.cell(
gameTable,
3,
5,
cellText(5),
text_color=cellTextColor(5),
bgcolor=cellBg,
text_size=boardTextSize,
width=cellWidth,
height=cellHeight)
// ═════════════════════════════════════════════════════════════════════════
// A3
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
1,
6,
cellText(6),
text_color=cellTextColor(6),
bgcolor=cellBg,
text_size=boardTextSize,
width=cellWidth,
height=cellHeight)
// B3
table.cell(
gameTable,
2,
6,
cellText(7),
text_color=cellTextColor(7),
bgcolor=cellBg,
text_size=boardTextSize,
width=cellWidth,
height=cellHeight)
// C3
table.cell(
gameTable,
3,
6,
cellText(8),
text_color=cellTextColor(8),
bgcolor=cellBg,
text_size=boardTextSize,
width=cellWidth,
height=cellHeight)
// ═════════════════════════════════════════════════════════════════════════
// STATUS
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
0,
7,
statusText,
text_color=statusColor,
bgcolor=black,
text_size=statusTextSize)
table.merge_cells(gameTable, 0, 7, 3, 7)
// ═════════════════════════════════════════════════════════════════════════
// COMPUTER DELAY
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
0,
8,
"🤖 Computer Delay: " + str.tostring(computerDelay) + " sec",
text_color=gray,
bgcolor=black,
text_size=size.small)
table.merge_cells(gameTable, 0, 8, 3, 8)
// ═════════════════════════════════════════════════════════════════════════
// LAST COMPUTER MOVE
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
0,
9,
"Last Computer Move: " + computerLastMove,
text_color=white,
bgcolor=darkBg,
text_size=size.small)
table.merge_cells(gameTable, 0, 9, 3, 9)
// ═════════════════════════════════════════════════════════════════════════
// INSTRUCTIONS
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
0,
10,
"Enter your moves in order:",
text_color=gray,
bgcolor=black,
text_size=size.small)
table.merge_cells(gameTable, 0, 10, 3, 10)
table.cell(
gameTable,
0,
11,
"Move 1 → Move 2 → Move 3 → ...",
text_color=gold,
bgcolor=black,
text_size=size.small)
table.merge_cells(gameTable, 0, 11, 3, 11)
// ═════════════════════════════════════════════════════════════════════════
// SYMBOL INFORMATION
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
0,
12,
playerName + ": " + playerSymbol + " | COMPUTER: " + computerSymbol,
text_color=white,
bgcolor=darkBg,
text_size=size.small)
table.merge_cells(gameTable, 0, 12, 3, 12)
// ═════════════════════════════════════════════════════════════════════════
// GAME NUMBER
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
0,
13,
"GAME #" + str.tostring(gameNumber),
text_color=gray,
bgcolor=black,
text_size=size.small)
table.merge_cells(gameTable, 0, 13, 3, 13)
// ═════════════════════════════════════════════════════════════════════════
// WORLD FINANCIAL FREEDOM
// ═════════════════════════════════════════════════════════════════════════
table.cell(
gameTable,
0,
14,
brandName,
text_color=gold,
bgcolor=darkBg,
text_size=size.small)
table.merge_cells(gameTable, 0, 14, 3, 14)