2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,2 +1,12 @@
Play a game of [[wp:Tic-tac-toe|tic-tac-toe]].
[[File:Tic_tac_toe.jpg|500px||right]]
;Task:
Play a game of   [[wp:Tic-tac-toe|tic-tac-toe]].
Ensure that legal moves are played and that a winning position is notified.
;See:
*   [[http://mathworld.wolfram.com/Tic-Tac-Toe.html Mathworld™, Tic-Tac-Toe game]].
<br><br>

View file

@ -0,0 +1,113 @@
# syntax: GAWK -f TIC-TAC-TOE.AWK
BEGIN {
move[12] = "3 7 4 6 8"; move[13] = "2 8 6 4 7"; move[14] = "7 3 2 8 6"
move[16] = "8 2 3 7 4"; move[17] = "4 6 8 2 3"; move[18] = "6 4 7 3 2"
move[19] = "8 2 3 7 4"; move[23] = "1 9 6 4 8"; move[24] = "1 9 3 7 8"
move[25] = "8 3 7 4 0"; move[26] = "3 7 1 9 8"; move[27] = "6 4 1 9 8"
move[28] = "1 9 7 3 4"; move[29] = "4 6 3 7 8"; move[35] = "7 4 6 8 2"
move[45] = "6 7 3 2 0"; move[56] = "4 7 3 2 8"; move[57] = "3 2 8 4 6"
move[58] = "2 3 7 4 6"; move[59] = "3 2 8 4 6"
split("7 4 1 8 5 2 9 6 3",rotate)
n = split("253 280 457 254 257 350 452 453 570 590",special)
i = 0
while (i < 9) { s[++i] = " " }
print("")
print("You move first, use the keypad:")
board = "\n7 * 8 * 9\n*********\n4 * 5 * 6\n*********\n1 * 2 * 3\n\n? "
printf(board)
}
state < 7 {
x = $0
if (s[x] != " ") {
printf("? ")
next
}
s[x] = "X"
++state
print("")
if (state > 1) {
for (i=0; i<r; ++i) { x = rotate[x] }
}
}
state == 1 {
for (r=0; x>2 && x!=5; ++r) { x = rotate[x] }
k = x
if (x == 5) { d = 1 } else { d = 5 }
}
state == 2 {
c = 5.5 * (k + x) - 4.5 * abs(k - x)
split(move[c],t)
d = t[1]
e = t[2]
f = t[3]
g = t[4]
h = t[5]
}
state == 3 {
k = x / 2.
c = c * 10
d = f
if (abs(c-350) == 100) {
if (x != 9) { d = 10 - x }
if (int(k) == k) { g = f }
h = 10 - g
if (x+0 == e+0) {
h = g
g = 9
}
}
else if (x+0 != e+0) {
d = e
state = 6
}
}
state == 4 {
if (x+0 == g+0) {
d = h
}
else {
d = g
state = 6
}
x = 6
for (i=1; i<=n; ++i) {
b = special[i]
if (b == 254) { x = 4 }
if (k+0 == abs(b-c-k)) { state = x }
}
}
state < 7 {
if (state != 5) {
for (i=0; i<4-r; ++i) { d = rotate[d] }
s[d] = "O"
}
for (b=7; b>0; b-=5) {
printf("%s * %s * %s\n",s[b++],s[b++],s[b])
if (b > 3) { print("*********") }
}
print("")
}
state < 5 {
printf("? ")
}
state == 5 {
printf("tie game")
state = 7
}
state == 6 {
printf("you lost")
state = 7
}
state == 7 {
printf(", play again? ")
++state
next
}
state == 8 {
if ($1 !~ /^[yY]$/) { exit(0) }
i = 0
while (i < 9) { s[++i] = " " }
printf(board)
state = 0
}
function abs(x) { if (x >= 0) { return x } else { return -x } }

View file

@ -0,0 +1,144 @@
place1 = 1
place2 = 2
place3 = 3
place4 = 4
place5 = 5
place6 = 6
place7 = 7
place8 = 8
place9 = 9
symbol1 = "X"
symbol2 = "O"
reset:
TextWindow.Clear()
TextWindow.Write(place1 + " ")
TextWindow.Write(place2 + " ")
TextWindow.WriteLine(place3 + " ")
TextWindow.Write(place4 + " ")
TextWindow.Write(place5 + " ")
TextWindow.WriteLine(place6 + " ")
TextWindow.Write(place7 + " ")
TextWindow.Write(place8 + " ")
TextWindow.WriteLine(place9 + " ")
TextWindow.WriteLine("Where would you like to go to (choose a number from 1 to 9 and press enter)?")
n = TextWindow.Read()
If n = 1 then
If place1 = symbol1 or place1 = symbol2 then
Goto ai
Else
place1 = symbol1
EndIf
ElseIf n = 2 then
If place2 = symbol1 or place2 = symbol2 then
Goto ai
Else
place2 = symbol1
EndIf
ElseIf n = 3 then
If place3 = symbol1 or place3 = symbol2 then
Goto ai
Else
place3 = symbol1
EndIf
ElseIf n = 4 then
If place4 = symbol1 or place4 = symbol2 then
Goto ai
Else
place4 = symbol1
EndIf
ElseIf n = 5 then
If place5 = symbol1 or place5 = symbol2 then
Goto ai
Else
place5 = symbol1
EndIf
ElseIf n = 6 then
If place6 = symbol1 or place6 = symbol2 then
Goto ai
Else
place6 = symbol1
EndIf
ElseIf n = 7 then
If place8 = symbol1 or place7 = symbol2 then
Goto ai
Else
place7 = symbol1
EndIf
ElseIf n = 8 then
If place8 = symbol1 or place8 = symbol2 then
Goto ai
Else
place8 = symbol1
EndIf
ElseIf n = 9 then
If place9 = symbol1 or place9 = symbol2 then
Goto ai
Else
place9 = symbol1
EndIf
EndIf
Goto ai
ai:
n = Math.GetRandomNumber(9)
If n = 1 then
If place1 = symbol1 or place1 = symbol2 then
Goto ai
Else
place1 = symbol2
EndIf
ElseIf n = 2 then
If place2 = symbol1 or place2 = symbol2 then
Goto ai
Else
place2 = symbol2
EndIf
ElseIf n = 3 then
If place3 = symbol1 or place3 = symbol2 then
Goto ai
Else
place3 = symbol2
EndIf
ElseIf n = 4 then
If place4 = symbol1 or place4 = symbol2 then
Goto ai
Else
place4 = symbol2
EndIf
ElseIf n = 5 then
If place5 = symbol1 or place5 = symbol2 then
Goto ai
Else
place5 = symbol2
EndIf
ElseIf n = 6 then
If place6 = symbol1 or place6 = symbol2 then
Goto ai
Else
place6 = symbol2
EndIf
ElseIf n = 7 then
If place7 = symbol1 or place7 = symbol2 then
Goto ai
Else
place7 = symbol2
EndIf
ElseIf n = 8 then
If place8 = symbol1 or place8 = symbol2 then
Goto ai
Else
place8 = symbol2
EndIf
ElseIf n = 9 then
If place9 = symbol1 or place9 = symbol2 then
Goto ai
Else
place9 = symbol2
EndIf
EndIf
If place1 = symbol1 and place2 = symbol1 and place3 = symbol1 or place4 = symbol1 and place5 = symbol1 and place6 = symbol1 or place7 = symbol1 and place8 = symbol1 and place9 = symbol1 or place1 = symbol1 and place4 = symbol1 and place7 = symbol1 or place2 = symbol1 and place5 = symbol1 and place8 = symbol1 or place3 = symbol1 and place6 = symbol1 and place9 = symbol1 or place1 = symbol1 and place5 = symbol1 and place9 = symbol1 or place3 = symbol1 and place5 = symbol1 and place7 = symbol1 then
TextWindow.WriteLine("Player 1 (" + symbol1 + ") wins!")
ElseIf place1 = symbol2 and place2 = symbol2 and place3 = symbol2 or place4 = symbol2 and place5 = symbol2 and place6 = symbol2 or place7 = symbol2 and place8 = symbol2 and place9 = symbol2 or place1 = symbol2 and place4 = symbol2 and place7 = symbol2 or place2 = symbol2 and place5 = symbol2 and place8 = symbol2 or place3 = symbol2 and place6 = symbol2 and place9 = symbol2 or place1 = symbol2 and place5 = symbol2 and place8 = symbol2 or place3 = symbol2 and place5 = symbol2 and place7 = symbol2 then
TextWindow.WriteLine("Player 2 (" + symbol2 + ") wins!")
Else
Goto reset
EndIf

View file

@ -0,0 +1,48 @@
@echo off
setlocal enabledelayedexpansion
:newgame
set a1=1
set a2=2
set a3=3
set a4=4
set a5=5
set a6=6
set a7=7
set a8=8
set a9=9
set ll=X
set /a zz=0
:display1
cls
echo Player: %ll%
echo %a7%_%a8%_%a9%
echo %a4%_%a5%_%a6%
echo %a1%_%a2%_%a3%
set /p myt=Where would you like to go (choose a number from 1-9 and press enter)?
if !a%myt%! equ %myt% (
set a%myt%=%ll%
goto check
)
goto display1
:check
set /a zz=%zz%+1
if %zz% geq 9 goto newgame
if %a7%+%a8%+%a9% equ %ll%+%ll%+%ll% goto win
if %a4%+%a5%+%a6% equ %ll%+%ll%+%ll% goto win
if %a1%+%a2%+%a3% equ %ll%+%ll%+%ll% goto win
if %a7%+%a5%+%a3% equ %ll%+%ll%+%ll% goto win
if %a1%+%a5%+%a9% equ %ll%+%ll%+%ll% goto win
if %a7%+%a4%+%a1% equ %ll%+%ll%+%ll% goto win
if %a8%+%a5%+%a2% equ %ll%+%ll%+%ll% goto win
if %a9%+%a6%+%a3% equ %ll%+%ll%+%ll% goto win
goto %ll%
:X
set ll=O
goto display1
:O
set ll=X
goto display1
:win
echo %ll% wins!
pause
goto newgame

View file

@ -0,0 +1,321 @@
@ECHO OFF
:BEGIN
REM Skill level
set sl=
cls
echo Tic Tac Toe (Q to quit)
echo.
echo.
echo Pick your skill level (press a number)
echo.
echo (1) Children under 6
echo (2) Average Mental Case
echo (3) Oversized Ego
CHOICE /c:123q /n > nul
if errorlevel 4 goto end
if errorlevel 3 set sl=3
if errorlevel 3 goto layout
if errorlevel 2 set sl=2
if errorlevel 2 goto layout
set sl=1
:LAYOUT
REM Player turn ("x" or "o")
set pt=
REM Game winner ("x" or "o")
set gw=
REM No moves
set nm=
REM Set to one blank space after equal sign (check with cursor end)
set t1=
set t2=
set t3=
set t4=
set t5=
set t6=
set t7=
set t8=
set t9=
:UPDATE
cls
echo (S to set skill level) Tic Tac Toe (Q to quit)
echo.
echo You are the X player.
echo Press the number where you want to put an X.
echo.
echo Skill level %sl% 7 8 9
echo 4 5 6
echo 1 2 3
echo.
echo : :
echo %t1% : %t2% : %t3%
echo ....:...:....
echo %t4% : %t5% : %t6%
echo ....:...:....
echo %t7% : %t8% : %t9%
echo : :
if "%gw%"=="x" goto winx2
if "%gw%"=="o" goto wino2
if "%nm%"=="0" goto nomoves
:PLAYER
set pt=x
REM Layout is for keypad. Change CHOICE to "/c:123456789sq /n > nul"
REM for numbers to start at top left (also change user layout above).
CHOICE /c:789456123sq /n > nul
if errorlevel 11 goto end
if errorlevel 10 goto begin
if errorlevel 9 goto 9
if errorlevel 8 goto 8
if errorlevel 7 goto 7
if errorlevel 6 goto 6
if errorlevel 5 goto 5
if errorlevel 4 goto 4
if errorlevel 3 goto 3
if errorlevel 2 goto 2
goto 1
:1
REM Check if "x" or "o" already in square.
if "%t1%"=="x" goto player
if "%t1%"=="o" goto player
set t1=x
goto check
:2
if "%t2%"=="x" goto player
if "%t2%"=="o" goto player
set t2=x
goto check
:3
if "%t3%"=="x" goto player
if "%t3%"=="o" goto player
set t3=x
goto check
:4
if "%t4%"=="x" goto player
if "%t4%"=="o" goto player
set t4=x
goto check
:5
if "%t5%"=="x" goto player
if "%t5%"=="o" goto player
set t5=x
goto check
:6
if "%t6%"=="x" goto player
if "%t6%"=="o" goto player
set t6=x
goto check
:7
if "%t7%"=="x" goto player
if "%t7%"=="o" goto player
set t7=x
goto check
:8
if "%t8%"=="x" goto player
if "%t8%"=="o" goto player
set t8=x
goto check
:9
if "%t9%"=="x" goto player
if "%t9%"=="o" goto player
set t9=x
goto check
:COMPUTER
set pt=o
if "%sl%"=="1" goto skill1
REM (win corner to corner)
if "%t1%"=="o" if "%t3%"=="o" if not "%t2%"=="x" if not "%t2%"=="o" goto c2
if "%t1%"=="o" if "%t9%"=="o" if not "%t5%"=="x" if not "%t5%"=="o" goto c5
if "%t1%"=="o" if "%t7%"=="o" if not "%t4%"=="x" if not "%t4%"=="o" goto c4
if "%t3%"=="o" if "%t7%"=="o" if not "%t5%"=="x" if not "%t5%"=="o" goto c5
if "%t3%"=="o" if "%t9%"=="o" if not "%t6%"=="x" if not "%t6%"=="o" goto c6
if "%t9%"=="o" if "%t7%"=="o" if not "%t8%"=="x" if not "%t8%"=="o" goto c8
REM (win outside middle to outside middle)
if "%t2%"=="o" if "%t8%"=="o" if not "%t5%"=="x" if not "%t5%"=="o" goto c5
if "%t4%"=="o" if "%t6%"=="o" if not "%t5%"=="x" if not "%t5%"=="o" goto c5
REM (win all others)
if "%t1%"=="o" if "%t2%"=="o" if not "%t3%"=="x" if not "%t3%"=="o" goto c3
if "%t1%"=="o" if "%t5%"=="o" if not "%t9%"=="x" if not "%t9%"=="o" goto c9
if "%t1%"=="o" if "%t4%"=="o" if not "%t7%"=="x" if not "%t7%"=="o" goto c7
if "%t2%"=="o" if "%t5%"=="o" if not "%t8%"=="x" if not "%t8%"=="o" goto c8
if "%t3%"=="o" if "%t2%"=="o" if not "%t1%"=="x" if not "%t1%"=="o" goto c1
if "%t3%"=="o" if "%t5%"=="o" if not "%t7%"=="x" if not "%t7%"=="o" goto c7
if "%t3%"=="o" if "%t6%"=="o" if not "%t9%"=="x" if not "%t9%"=="o" goto c9
if "%t4%"=="o" if "%t5%"=="o" if not "%t6%"=="x" if not "%t6%"=="o" goto c6
if "%t6%"=="o" if "%t5%"=="o" if not "%t4%"=="x" if not "%t4%"=="o" goto c4
if "%t7%"=="o" if "%t4%"=="o" if not "%t1%"=="x" if not "%t1%"=="o" goto c1
if "%t7%"=="o" if "%t5%"=="o" if not "%t3%"=="x" if not "%t3%"=="o" goto c3
if "%t7%"=="o" if "%t8%"=="o" if not "%t9%"=="x" if not "%t9%"=="o" goto c9
if "%t8%"=="o" if "%t5%"=="o" if not "%t2%"=="x" if not "%t2%"=="o" goto c2
if "%t9%"=="o" if "%t8%"=="o" if not "%t7%"=="x" if not "%t7%"=="o" goto c7
if "%t9%"=="o" if "%t5%"=="o" if not "%t1%"=="x" if not "%t1%"=="o" goto c1
if "%t9%"=="o" if "%t6%"=="o" if not "%t3%"=="x" if not "%t3%"=="o" goto c3
REM (block general attempts) -----------------------------------------------
if "%t1%"=="x" if "%t2%"=="x" if not "%t3%"=="x" if not "%t3%"=="o" goto c3
if "%t1%"=="x" if "%t5%"=="x" if not "%t9%"=="x" if not "%t9%"=="o" goto c9
if "%t1%"=="x" if "%t4%"=="x" if not "%t7%"=="x" if not "%t7%"=="o" goto c7
if "%t2%"=="x" if "%t5%"=="x" if not "%t8%"=="x" if not "%t8%"=="o" goto c8
if "%t3%"=="x" if "%t2%"=="x" if not "%t1%"=="x" if not "%t1%"=="o" goto c1
if "%t3%"=="x" if "%t5%"=="x" if not "%t7%"=="x" if not "%t7%"=="o" goto c7
if "%t3%"=="x" if "%t6%"=="x" if not "%t9%"=="x" if not "%t9%"=="o" goto c9
if "%t4%"=="x" if "%t5%"=="x" if not "%t6%"=="x" if not "%t6%"=="o" goto c6
if "%t6%"=="x" if "%t5%"=="x" if not "%t4%"=="x" if not "%t4%"=="o" goto c4
if "%t7%"=="x" if "%t4%"=="x" if not "%t1%"=="x" if not "%t1%"=="o" goto c1
if "%t7%"=="x" if "%t5%"=="x" if not "%t3%"=="x" if not "%t3%"=="o" goto c3
if "%t7%"=="x" if "%t8%"=="x" if not "%t9%"=="x" if not "%t9%"=="o" goto c9
if "%t8%"=="x" if "%t5%"=="x" if not "%t2%"=="x" if not "%t2%"=="o" goto c2
if "%t9%"=="x" if "%t8%"=="x" if not "%t7%"=="x" if not "%t7%"=="o" goto c7
if "%t9%"=="x" if "%t5%"=="x" if not "%t1%"=="x" if not "%t1%"=="o" goto c1
if "%t9%"=="x" if "%t6%"=="x" if not "%t3%"=="x" if not "%t3%"=="o" goto c3
REM (block obvious corner to corner)
if "%t1%"=="x" if "%t3%"=="x" if not "%t2%"=="x" if not "%t2%"=="o" goto c2
if "%t1%"=="x" if "%t9%"=="x" if not "%t5%"=="x" if not "%t5%"=="o" goto c5
if "%t1%"=="x" if "%t7%"=="x" if not "%t4%"=="x" if not "%t4%"=="o" goto c4
if "%t3%"=="x" if "%t7%"=="x" if not "%t5%"=="x" if not "%t5%"=="o" goto c5
if "%t3%"=="x" if "%t9%"=="x" if not "%t6%"=="x" if not "%t6%"=="o" goto c6
if "%t9%"=="x" if "%t7%"=="x" if not "%t8%"=="x" if not "%t8%"=="o" goto c8
if "%sl%"=="2" goto skill2
REM (block sneaky corner to corner 2-4, 2-6, etc.)
if "%t2%"=="x" if "%t4%"=="x" if not "%t1%"=="x" if not "%t1%"=="o" goto c1
if "%t2%"=="x" if "%t6%"=="x" if not "%t3%"=="x" if not "%t3%"=="o" goto c3
if "%t8%"=="x" if "%t4%"=="x" if not "%t7%"=="x" if not "%t7%"=="o" goto c7
if "%t8%"=="x" if "%t6%"=="x" if not "%t9%"=="x" if not "%t9%"=="o" goto c9
REM (block offset corner trap 1-8, 1-6, etc.)
if "%t1%"=="x" if "%t6%"=="x" if not "%t8%"=="x" if not "%t8%"=="o" goto c8
if "%t1%"=="x" if "%t8%"=="x" if not "%t6%"=="x" if not "%t6%"=="o" goto c6
if "%t3%"=="x" if "%t8%"=="x" if not "%t4%"=="x" if not "%t4%"=="o" goto c4
if "%t3%"=="x" if "%t4%"=="x" if not "%t8%"=="x" if not "%t8%"=="o" goto c8
if "%t9%"=="x" if "%t4%"=="x" if not "%t2%"=="x" if not "%t2%"=="o" goto c2
if "%t9%"=="x" if "%t2%"=="x" if not "%t4%"=="x" if not "%t4%"=="o" goto c4
if "%t7%"=="x" if "%t2%"=="x" if not "%t6%"=="x" if not "%t6%"=="o" goto c6
if "%t7%"=="x" if "%t6%"=="x" if not "%t2%"=="x" if not "%t2%"=="o" goto c2
:SKILL2
REM (block outside middle to outside middle)
if "%t2%"=="x" if "%t8%"=="x" if not "%t5%"=="x" if not "%t5%"=="o" goto c5
if "%t4%"=="x" if "%t6%"=="x" if not "%t5%"=="x" if not "%t5%"=="o" goto c5
REM (block 3 corner trap)
if "%t1%"=="x" if "%t9%"=="x" if not "%t2%"=="x" if not "%t2%"=="o" goto c2
if "%t3%"=="x" if "%t7%"=="x" if not "%t2%"=="x" if not "%t2%"=="o" goto c2
if "%t1%"=="x" if "%t9%"=="x" if not "%t4%"=="x" if not "%t4%"=="o" goto c4
if "%t3%"=="x" if "%t7%"=="x" if not "%t4%"=="x" if not "%t4%"=="o" goto c4
if "%t1%"=="x" if "%t9%"=="x" if not "%t6%"=="x" if not "%t6%"=="o" goto c6
if "%t3%"=="x" if "%t7%"=="x" if not "%t6%"=="x" if not "%t6%"=="o" goto c6
if "%t1%"=="x" if "%t9%"=="x" if not "%t8%"=="x" if not "%t8%"=="o" goto c8
if "%t3%"=="x" if "%t7%"=="x" if not "%t8%"=="x" if not "%t8%"=="o" goto c8
:SKILL1
REM (just take a turn)
if not "%t5%"=="x" if not "%t5%"=="o" goto c5
if not "%t1%"=="x" if not "%t1%"=="o" goto c1
if not "%t3%"=="x" if not "%t3%"=="o" goto c3
if not "%t7%"=="x" if not "%t7%"=="o" goto c7
if not "%t9%"=="x" if not "%t9%"=="o" goto c9
if not "%t2%"=="x" if not "%t2%"=="o" goto c2
if not "%t4%"=="x" if not "%t4%"=="o" goto c4
if not "%t6%"=="x" if not "%t6%"=="o" goto c6
if not "%t8%"=="x" if not "%t8%"=="o" goto c8
set nm=0
goto update
:C1
set t1=o
goto check
:C2
set t2=o
goto check
:C3
set t3=o
goto check
:C4
set t4=o
goto check
:C5
set t5=o
goto check
:C6
set t6=o
goto check
:C7
set t7=o
goto check
:C8
set t8=o
goto check
:C9
set t9=o
goto check
:CHECK
if "%t1%"=="x" if "%t2%"=="x" if "%t3%"=="x" goto winx
if "%t4%"=="x" if "%t5%"=="x" if "%t6%"=="x" goto winx
if "%t7%"=="x" if "%t8%"=="x" if "%t9%"=="x" goto winx
if "%t1%"=="x" if "%t4%"=="x" if "%t7%"=="x" goto winx
if "%t2%"=="x" if "%t5%"=="x" if "%t8%"=="x" goto winx
if "%t3%"=="x" if "%t6%"=="x" if "%t9%"=="x" goto winx
if "%t1%"=="x" if "%t5%"=="x" if "%t9%"=="x" goto winx
if "%t3%"=="x" if "%t5%"=="x" if "%t7%"=="x" goto winx
if "%t1%"=="o" if "%t2%"=="o" if "%t3%"=="o" goto wino
if "%t4%"=="o" if "%t5%"=="o" if "%t6%"=="o" goto wino
if "%t7%"=="o" if "%t8%"=="o" if "%t9%"=="o" goto wino
if "%t1%"=="o" if "%t4%"=="o" if "%t7%"=="o" goto wino
if "%t2%"=="o" if "%t5%"=="o" if "%t8%"=="o" goto wino
if "%t3%"=="o" if "%t6%"=="o" if "%t9%"=="o" goto wino
if "%t1%"=="o" if "%t5%"=="o" if "%t9%"=="o" goto wino
if "%t3%"=="o" if "%t5%"=="o" if "%t7%"=="o" goto wino
if "%pt%"=="x" goto computer
if "%pt%"=="o" goto update
:WINX
set gw=x
goto update
:WINX2
echo You win!
echo Play again (Y,N)?
CHOICE /c:ynsq /n > nul
if errorlevel 4 goto end
if errorlevel 3 goto begin
if errorlevel 2 goto end
goto layout
:WINO
set gw=o
goto update
:WINO2
echo Sorry, You lose.
echo Play again (Y,N)?
CHOICE /c:ynsq /n > nul
if errorlevel 4 goto end
if errorlevel 3 goto begin
if errorlevel 2 goto end
goto layout
:NOMOVES
echo There are no more moves left!
echo Play again (Y,N)?
CHOICE /c:ynsq /n > nul
if errorlevel 4 goto end
if errorlevel 3 goto begin
if errorlevel 2 goto end
goto layout
:END
cls
echo Tic Tac Toe
echo.
REM Clear all variables (no spaces after equal sign).
set gw=
set nm=
set sl=
set pt=
set t1=
set t2=
set t3=
set t4=
set t5=
set t6=
set t7=
set t8=
set t9=

View file

@ -1,136 +0,0 @@
::
::Tic-Tac-Toe Task from Rosetta Code Wiki
::Batch File Implementation
::
::Directly OPEN the Batch File to play.
::
@echo off
title Sample TicTacToe Game
mode con cols=50 lines=21
setlocal enabledelayedexpansion
set win=a1a2a3 a4a5a6 a7a8a9 a1a4a7 a2a5a8 a3a6a9 a1a5a9 a3a5a7
:begin
set blanks=123456789&set numblanks=9
for /l %%. in (1,1,9) do set "a%%.= "
set /a rnd=%random%%%2
if %rnd%==1 set msg=YOU will move first.&goto :youmove
set msg=CPU will move first.&goto :rndmove
:youmove
cls
call :display
echo Your Turn:
set "move="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
if not defined move set "move=%%L"
)
set move=!move:~-1!
for /l %%. in (1,1,9) do if "!move!"=="%%." goto :preproc
if /i "!move!"=="n" goto :begin
if /i "!move!"=="o" exit
set msg=Invalid Input.
goto youmove
:preproc
if "!a%move%!"=="X" (set msg=An X is already There.&goto youmove)
if "!a%move%!"=="O" (set msg=An O is already There.&goto youmove)
set a%move%=O
set /a numblanks-=1&set blanks=!blanks:%move%=!
call :ifdraw
for %%. in (%win%) do (
set comb=%%.
call :mainproc1
)
set block=0
for %%. in (%win%) do (
if !block!==0 (
set comb=%%.
call :mainproc2
)
)
if %block%==1 (
set /a numblanks-=1&set blanks=!blanks:%remove%=!
set msg=CPU Puts an X on Grid %remove%.
call :ifdraw
goto youmove
)
goto rndmove
:mainproc1
if "!%comb:~0,2%!!%comb:~2,2%!!%comb:~4,2%!"=="OOO" (set msg=You Win^^!&goto res)
if "!%comb:~0,2%!!%comb:~2,2%!!%comb:~4,2%!"=="XX " (
set %comb:~4,2%=X&set msg=CPU Wins^^!
goto res
)
if "!%comb:~0,2%!!%comb:~2,2%!!%comb:~4,2%!"=="X X" (
set %comb:~2,2%=X&set msg=CPU Wins^^!
goto res
)
if "!%comb:~0,2%!!%comb:~2,2%!!%comb:~4,2%!"==" XX" (
set %comb:~0,2%=X&set msg=CPU Wins^^!
goto res
)
goto :EOF
:mainproc2
if "!%comb:~0,2%!!%comb:~2,2%!!%comb:~4,2%!"=="OO " (
set %comb:~4,2%=X&set remove=%comb:~5,1%
set block=1
)
if "!%comb:~0,2%!!%comb:~2,2%!!%comb:~4,2%!"=="O O" (
set %comb:~2,2%=X&set remove=%comb:~3,1%
set block=1
)
if "!%comb:~0,2%!!%comb:~2,2%!!%comb:~4,2%!"==" OO" (
set %comb:~0,2%=X&set remove=%comb:~1,1%
set block=1
)
goto :EOF
:ifdraw
if %numblanks%==0 (set msg=Game Draw.&goto res)
goto :EOF
:rndmove
set /a rnd=(%random%%%%numblanks%)
set bla=!blanks:~%rnd%,1!
set a%bla%=X
set /a numblanks-=1&set blanks=!blanks:%bla%=!
if not %numblanks%==8 (set msg=CPU Puts an X on Grid %bla%.)
call :ifdraw
goto youmove
:res
cls
call :display
echo Press any char key to play again.
pause>nul
goto begin
:display
echo.
echo Tic-Tac-Toe (Man VS CPU)
echo Batch File Implementation
echo.
echo.
echo Gameboard Press:
echo.
echo +---+---+---+ N - New Game
echo 1-3 ^| %a1% ^| %a2% ^| %a3% ^| O - Exit
echo +---+---+---+
echo 4-6 ^| %a4% ^| %a5% ^| %a6% ^|
echo +---+---+---+ You - O
echo 7-9 ^| %a7% ^| %a8% ^| %a9% ^| CPU - X
echo +---+---+---+
echo.
echo Message: !msg!
echo.
goto :EOF

View file

@ -1,105 +1,111 @@
/*REXX program plays (with a human) the tic-tac-toe game on an NxN grid.*/
$=copies('',9) /*eyecatcher literal for messages*/
oops =$ '***error!*** '; cell# ='cell number' /*a couple of literals*/
sing=''; jam=''; bar=''; junc=''; dbl=jam || bar || junc
sw=linesize()-1 /*get the width of the terminal. */
parse arg N hm cm .,@.; if N=='' then N=3; oN=N /*specifying some args?*/
N=abs(N); NN=N*N; middle=NN%2+N%2 /*if N < 0, computer goes first.*/
if N<2 then do; say oops 'tic-tac-toe grid is too small: ' N; exit; end
pad=copies(left('',sw%NN),1+(N<5)) /*display padding: 6x6 in 80 cols*/
if hm=='' then hm='X'; if cm=='' then cm='O' /*markers: Human, Computer*/
hm=aChar(hm,'human'); cm=aChar(cm,'computer') /*process the markers.*/
if hm==cm then cm='X' /*Human wants the "O"? Sheesh! */
if oN<0 then call Hmove middle /*comp moves 1st? Choose middling*/
else call showGrid /*···also checks for wins & draws*/
do forever /*'til the cows come home, by gum*/
call CBLF /*do carbon-based lifeform's move*/
call Hal /*figure Hal the computer's move.*/
end /*forever····showGrid does wins & draws*/
/*──────────────────────────────────ACHAR subroutine────────────────────*/
aChar: parse arg x; L=length(x) /*process markers.*/
if L==1 then return x /*1 char, as is. */
if L==2 & datatype(x,'X') then return x2c(x) /*2 chars, hex. */
if L==3 & datatype(x,'W') then return d2c(x) /*3 chars, decimal*/
say oops 'illegal character or character code for' arg(2) "marker: " x
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────CBLF subroutine─────────────────────*/
CBLF: prompt='Please enter a' cell# "to place your next marker ["hm'] (or Quit):'
do forever; say $ prompt; parse pull x 1 ux 1 ox; upper ux
if datatype(ox,'W') then ox=ox/1 /*maybe normalize cell#: +0007. */
select
when abbrev('QUIT',ux,1) then call tell 'quitting.'
when x='' then iterate /*Nada? Try again.*/
when words(x)\==1 then say oops "too many" cell# 'specified:' x
when \datatype(x,'N') then say oops cell# "isn't numeric: " x
when \datatype(x,'W') then say oops cell# "isn't an integer: " x
when x=0 then say oops cell# "can't be zero: " x
when x<0 then say oops cell# "can't be negative: " x
when x>NN then say oops cell# "can't exceed " NN
when @.ox\=='' then say oops cell# "is already occupied: " x
otherwise leave /*do forever*/
end /*select*/
end /*forever*/
@.ox=hm /*place a marker for the human.*/
call showGrid /*and show the tic-tac-toe grid.*/
return
/*──────────────────────────────────Hal subroutine──────────────────────*/
Hal: select /*Hal will try various moves. */
when win(cm,N-1) then call Hmove ,ec /* winning move? */
when win(hm,N-1) then call Hmove ,ec /* blocking move? */
when @.middle=='' then call Hmove middle /* center move. */
when @.N.N=='' then call Hmove ,N N /*bR corner move. */
when @.N.1=='' then call Hmove ,N 1 /*bL corner move. */
when @.1.N=='' then call Hmove ,1 N /*tR corner move. */
when @.1.1=='' then call Hmove ,1 1 /*tL corner move. */
otherwise call Hmove ,ac /*some blank cell.*/
end /*select*/
return
/*──────────────────────────────────HMOVE subroutine────────────────────*/
Hmove: parse arg Hplace,dr dc; if Hplace=='' then Hplace = (dr-1)*N + dc
@.Hplace=cm /*put marker for Hal the computer*/
say; say $ 'computer places a marker ['cm"] at cell number " Hplace
call showGrid
return
/*──────────────────────────────────SHOWGRID subroutine─────────────────*/
showGrid: _=0; open=0; cW=5; cH=3 /*cell width, cell height.*/
do r=1 for N; do c=1 for N; _=_+1; @.r.c=@._; open=open|@._==''; end; end
say; z=0 /* [↑] create grid coörds.*/
do j=1 for N /* [↓] show grids&markers.*/
do t=1 for cH; _=; __= /*mk is a marker in a cell*/
do k=1 for N; if t==2 then z=z+1; mk=; c#=
if t==2 then do; mk=@.z; c#=z; end /*c# is cell#*/
_= _||jam||center(mk,cW); __= __||jam||center(c#,cW)
end /*k*/
say pad substr(_,2) pad translate(substr(__,2), sing,dbl)
end /*t*/
if j==N then leave; _=
do b=1 for N; _=_||junc||copies(bar,cW); end /*b*/
say pad substr(_,2) pad translate(substr(_,2),sing,dbl)
end /*j*/
say
if win(hm) then call tell 'You ('hm") won!"
if win(cm) then call tell 'The computer ('cm") won."
if \open then call tell 'This tic-tac-toe is a draw.'
return
/*──────────────────────────────────TELL subroutine─────────────────────*/
tell: do 4; say; end; say center(' 'arg(1)" ",sw,''); do 5; say; end
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────WIN subroutine──────────────────────*/
win: parse arg wm,w; if w=='' then w=N /*see if there are W # of markers*/
ac=; do r=1 for N; _=0; ec= /*see if any rows are a winner.*/
do c=1 for N; _=_+ (@.r.c==wm); if @.r.c=='' then ec=r c; end
if ec\=='' then @c=ec; if _==N | (_>=w & ec\=='') then return 1
end /*r*/ /*if w=N-1, checking for near win*/
do c=1 for N; _=0; ec= /*see if any cols are a winner.*/
do r=1 for N; _=_+ (@.r.c==wm); if @.r.c=='' then ec=r c; end
if ec\=='' then @c=ec; if _==N | (_>=w & ec\=='') then return 1
end /*r*/ /*EC is a r,c version of cell #*/
_=0; ec= /*see if winning descending diag.*/
do d=1 for N; _=_+ (@.d.d==wm); if @.d.d=='' then ec=d d; end
if _==N | (_>=w & ec\=='') then return 1
_=0; ec=; r=0 /*see if winning ascending diag.*/
do c=N for N by -1; r=r+1; _=_+ (@.r.c==wm); if @.r.c=='' then ec=r c
end /*r*/
if _==N | (_>=w & ec\=='') then return 1
return 0
/*REXX program plays (with a human) the tic─tac─toe game on an NxN grid. */
$=copies('', 9) /*eyecatcher literal for error messages*/
oops =$ '***error*** '; cell@ ="cell number" /*a couple of literals for some SAYs. */
sing=''; jam=""; bar=''; junc=""; dbl=jam || bar || junc
sw=linesize() - 1 /*obtain width of the terminal (less 1)*/
parse arg N hm cm .,@. /*obtain optional arguments from the CL*/
if N=='' | N=="," then N=3; oN=N /*N not specified? Then use default.*/
N=abs(N); NN=N*N; middle=NN%2+N%2 /*if N < 0, then computer goes first. */
if N<2 then do; say oops 'tictactoe grid is too small: ' N; exit; end
pad=left('', sw%NN) /*display padding: 6x6 in 80 columns.*/
if hm=='' then hm="X"; if cm=='' then cm="O" /*define the markers: Human, computer*/
hm=aChar(hm,'human'); cm=aChar(cm,'computer') /*process/define markers for players. */
parse upper value hm cm with uh uc /*use uppercase values is markers: X x*/
if uh==uc then cm=word('O X', 1 + (uh=="O") ) /*The human wants Hal's marker? Swap. */
if oN<0 then call Hmove middle /*Hal moves first? Then choose middling*/
else call showGrid /*showGrid also checks for wins & draws*/
do forever /*'til the cows come home (or QUIT). */
call CBLF /*process carbon-based lifeform's move.*/
call Hal /*determine Hal's (the computer) move.*/
end /*forever*/ /*showGrid subroutine does wins & draws*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
ab: parse arg bx; if bx\==' ' then return bx /*test if the marker isn't a blank.*/
say oops 'character code for' whoseX "marker can't be a blank."
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
aChar: parse arg x,whoseX; L=length(x) /*process markers.*/
if L==1 then return ab(x) /*1 char, as is. */
if L==2 & datatype(x,'X') then return ab(x2c(x)) /*2 chars, hex. */
if L==3 & datatype(x,'W') then return ab(d2c(x)) /*3 chars, decimal*/
say oops 'illegal character or character code for' whoseX "marker: " x
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
CBLF : prompt='Please enter a' cell@ "to place your next marker ["hm'] (or Quit):'
do forever; say $ prompt; parse pull x 1 ux 1 ox; upper ux
if datatype(ox,'W') then ox=ox/1 /*maybe normalize cell number: +0007. */
select
when abbrev('QUIT',ux,1) then call tell 'quitting.'
when x='' then iterate /*Nada? Try again.*/
when words(x)\==1 then say oops "too many" cell# 'specified:' x
when \datatype(x,'N') then say oops cell@ "isn't numeric: " x
when \datatype(x,'W') then say oops cell@ "isn't an integer: " x
when x=0 then say oops cell@ "can't be zero: " x
when x<0 then say oops cell@ "can't be negative: " x
when x>NN then say oops cell@ "can't exceed " NN
when @.ox\=='' then say oops cell@ "is already occupied: " x
otherwise leave /*forever*/
end /*select*/
end /*forever*/
@.ox=hm /*place a marker for the human (CLBF). */
call showGrid /*and display the tic─tac─toe grid. */
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
Hal: select /*Hal tries various moves. */
when win(cm,N-1) then call Hmove , ec /*is this the winning move?*/
when win(hm,N-1) then call Hmove , ec /* " " a blocking " */
when @.middle=='' then call Hmove middle /*pick the center move. */
when @.N.N=='' then call Hmove , N N /*bottom right corner move.*/
when @.N.1=='' then call Hmove , N 1 /* " left " " */
when @.1.N=='' then call Hmove , 1 N /* top right " " */
when @.1.1=='' then call Hmove , 1 1 /* " left " " */
otherwise call Hmove , ac /*pick a blank cell " */
end /*select*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
Hmove: parse arg Hplace,dr dc; if Hplace=='' then Hplace = (dr-1)*N + dc
@.Hplace=cm /*place computer's marker. */
say; say $ 'computer places a marker ['cm"] at" cell@ ' ' Hplace
call showGrid
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
showGrid: _=0; open=0; cW=5; cH=3 /*cell width, cell height.*/
do r=1 for N; do c=1 for N; _=_+1; @.r.c=@._; open=open|@._==''; end
end /*r*/
say /* [↑] create grid coörds.*/
z=0; do j=1 for N /* [↓] show grids&markers.*/
do t=1 for cH; _=; __= /*MK is a marker in a cell.*/
do k=1 for N; if t==2 then z=z+1; mk=; c#=
if t==2 then do; mk=@.z; c#=z; end /*c# is cell number*/
_= _||jam||center(mk,cW); __= __ || jam || center(c#, cW)
end /*k*/
say pad substr(_,2) pad translate(substr(__,2), sing,dbl)
end /*t*/
if j==N then leave; _=
do b=1 for N; _=_ || junc || copies(bar, cW); end /*b*/
say pad substr(_,2) pad translate(substr(_,2), sing, dbl)
end /*j*/
say
if win(hm) then call tell 'You ('hm") won!"
if win(cm) then call tell 'The computer ('cm") won."
if \open then call tell 'This tictactoe game is a draw.'
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell: do 4; say; end; say center(' 'arg(1)" ", sw, ''); do 5; say; end; exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
win: parse arg wm,w; if w=='' then w=N /*see if there are W of markers*/
ac=; do r=1 for N; _=0; ec= /*see if any rows are a winner*/
do c=1 for N; _=_+ (@.r.c==wm); if @.r.c=='' then ec=r c; end
if ec\=='' then ac=ec; if _==N | (_>=w & ec\=='') then return 1
end /*r*/ /*w=N-1? Checking for near win*/
do c=1 for N; _=0; ec= /*see if any cols are a winner*/
do r=1 for N; _=_+ (@.r.c==wm); if @.r.c=='' then ec=r c; end
if ec\=='' then ac=ec; if _==N | (_>=w & ec\=='') then return 1
end /*r*/ /*EC is a R,C version of cell #*/
_=0; ec= /*A winning descending diag. ? */
do d=1 for N; _=_+ (@.d.d==wm); if @.d.d=='' then ec=d d; end
if _==N | (_>=w & ec\=='') then return 1
_=0; ec=; r=0 /*A winning ascending diagonal?*/
do c=N for N by -1; r=r+1; _=_+ (@.r.c==wm); if @.r.c=='' then ec=r c
end /*r*/
if _==N | (_>=w & ec\=='') then return 1
return 0