Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
95
Task/Tic-tac-toe/Lasso/tic-tac-toe.lasso
Normal file
95
Task/Tic-tac-toe/Lasso/tic-tac-toe.lasso
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
[
|
||||
session_start('user')
|
||||
session_addvar('user', 'matrix')
|
||||
session_addvar('user', 'winrecord')
|
||||
session_addvar('user', 'turn')
|
||||
var(matrix)->isNotA(::array) ? var(matrix = array('-','-','-','-','-','-','-','-','-'))
|
||||
var(winrecord)->isNotA(::array) ? var(winrecord = array)
|
||||
var(turn)->isNotA(::string) ? var(turn = 'x')
|
||||
|
||||
if(web_request->params->asStaticArray >> 'reset') => {
|
||||
$matrix = array('-','-','-','-','-','-','-','-','-')
|
||||
$turn = 'x'
|
||||
}
|
||||
|
||||
with i in web_request->params->asStaticArray do => {
|
||||
if(#i->name->beginswith('p')) => {
|
||||
local(num = #i->name->asCopy)
|
||||
#num->removeLeading('p')
|
||||
#num = integer(#num)
|
||||
#num > 0 && $matrix->get(#num) == '-' ? $matrix->get(#num) = #i->value
|
||||
$turn == 'o' ? $turn = 'x' | $turn = 'o'
|
||||
}
|
||||
}
|
||||
|
||||
local(
|
||||
istie = false,
|
||||
winner = 'noone',
|
||||
clear = false
|
||||
)
|
||||
|
||||
// determine if we have a winner
|
||||
if($matrix->find('-')->size < 9) => {
|
||||
local(winners = array('123','456','789','147','258','369','159','357'))
|
||||
loop(8) => {
|
||||
local(xscore = 0,oscore = 0,use = #winners->get(loop_count))
|
||||
with v in #use->values do => {
|
||||
$matrix->findposition('x') >> integer(#v) ? #xscore++
|
||||
$matrix->findposition('o') >> integer(#v) ? #oscore++
|
||||
}
|
||||
if(#xscore == 3) => {
|
||||
#winner = 'x'
|
||||
$winrecord->insert('x')
|
||||
#clear = true
|
||||
loop_abort
|
||||
}
|
||||
if(#oscore == 3) => {
|
||||
#winner = 'o'
|
||||
$winrecord->insert('o')
|
||||
#clear = true
|
||||
loop_abort
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
// determine if tie
|
||||
if(not $matrix->find('-')->size && #winner == 'noone') => {
|
||||
#istie = true
|
||||
#winner = 'tie'
|
||||
$winrecord->insert('tie')
|
||||
#clear = true
|
||||
}
|
||||
]
|
||||
<form action="?" method="post">
|
||||
<table>
|
||||
<tr>
|
||||
[loop(3) => {^]<td><button name="p[loop_count]" value="[$turn]"[
|
||||
$matrix->get(loop_count) != '-' || #winner != 'noone' ? ' disabled="disabled"'
|
||||
]>[$matrix->get(loop_count) != '-' ? $matrix->get(loop_count) | ' ']</button></td>[^}]
|
||||
</tr>
|
||||
<tr>
|
||||
[loop(-from=4,-to=6) => {^]<td><button name="p[loop_count]" value="[$turn]"[
|
||||
$matrix->get(loop_count) != '-' || #winner != 'noone' ? ' disabled="disabled"'
|
||||
]>[$matrix->get(loop_count) != '-' ? $matrix->get(loop_count) | ' ']</button></td>[^}]
|
||||
</tr>
|
||||
<tr>
|
||||
[loop(-from=7,-to=9) => {^]<td><button name="p[loop_count]" value="[$turn]"[
|
||||
$matrix->get(loop_count) != '-' || #winner != 'noone' ? ' disabled="disabled"'
|
||||
]>[$matrix->get(loop_count) != '-' ? $matrix->get(loop_count) | ' ']</button></td>[^}]
|
||||
</tr>
|
||||
</table>
|
||||
</form>
|
||||
[if(#istie && #winner == 'tie')]
|
||||
<p><b>It's a tie!</b></p>
|
||||
[else(#winner != 'noone')]
|
||||
<p>[#winner->uppercase&] won! Congratulations.</p>
|
||||
[else]<math>Insert formula here</math>
|
||||
<p>It is now [$turn]'s turn!</p>
|
||||
[/if]
|
||||
<p><a href="?reset">Reset</a></p>
|
||||
[if($winrecord->size)]<p>Win record: [$winrecord->join(', ')]</p>[/if]
|
||||
[if(#clear == true) => {
|
||||
$matrix = array('-','-','-','-','-','-','-','-','-')
|
||||
$turn = 'x'
|
||||
}]
|
||||
85
Task/Tic-tac-toe/Phix/tic-tac-toe.phix
Normal file
85
Task/Tic-tac-toe/Phix/tic-tac-toe.phix
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
sequence board = repeat(' ',9) -- {' '/'X'/'O'}
|
||||
|
||||
constant wins = {{1,2,3},{4,5,6},{7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}}
|
||||
|
||||
function check_winner()
|
||||
for w=1 to length(wins) do
|
||||
integer {i,j,k} = wins[w],
|
||||
boardi = board[i]
|
||||
if boardi!=' ' and boardi=board[j] and boardi=board[k] then
|
||||
return boardi
|
||||
end if
|
||||
end for
|
||||
return 0
|
||||
end function
|
||||
|
||||
procedure showboard()
|
||||
printf(1," %c | %c | %c\n---+---+---\n %c | %c | %c\n---+---+---\n %c | %c | %c\n",board)
|
||||
end procedure
|
||||
|
||||
integer best_i
|
||||
function test_move(integer val, integer depth)
|
||||
integer score = check_winner()
|
||||
integer best = -1, changed = 0
|
||||
if score!=0 then return iff(score=val?1:-1) end if
|
||||
for i=1 to 9 do
|
||||
if board[i]=' ' then
|
||||
{changed,board[i]} @= val
|
||||
score = -test_move('O'+'X'-val, depth + 1)
|
||||
board[i] = ' '
|
||||
if score>best then
|
||||
if depth=0 then
|
||||
best_i = i;
|
||||
end if
|
||||
best = score;
|
||||
end if
|
||||
end if
|
||||
end for
|
||||
return iff(changed!=0?best:0)
|
||||
end function
|
||||
|
||||
integer user = 1
|
||||
|
||||
function game()
|
||||
integer key, k, win
|
||||
board = repeat(' ',9)
|
||||
|
||||
printf(1,"Board postions are numbered so:\n1 2 3\n4 5 6\n7 8 9\n");
|
||||
printf(1,"You have O, I have X.\n\n");
|
||||
for n=1 to 9 do
|
||||
if(user) then
|
||||
printf(1,"your move: ");
|
||||
while 1 do
|
||||
key = wait_key()
|
||||
if find(key,{#1B,'q','Q'}) then return "Quit" end if
|
||||
k = key-'0'
|
||||
if k>=1 and k<=9 and board[k]=' ' then
|
||||
board[k] = 'O'
|
||||
printf(1,"%c\n",key)
|
||||
exit
|
||||
end if
|
||||
end while
|
||||
else
|
||||
if n=1 then --/* randomize if computer opens, less boring */
|
||||
best_i = rand(9)
|
||||
else
|
||||
{} = test_move('X', 0);
|
||||
end if
|
||||
board[best_i] = 'X'
|
||||
printf(1," my move: %d\n", best_i);
|
||||
end if
|
||||
showboard();
|
||||
user = 1-user
|
||||
win = check_winner()
|
||||
if win!=0 then
|
||||
return iff(win=='O' ? "You win.\n\n" : "I win.\n\n");
|
||||
end if
|
||||
end for
|
||||
return "A draw.\n\n";
|
||||
end function
|
||||
|
||||
while 1 do
|
||||
string res = game()
|
||||
puts(1,res)
|
||||
if res="Quit" then exit end if
|
||||
end while
|
||||
129
Task/Tic-tac-toe/Ring/tic-tac-toe.ring
Normal file
129
Task/Tic-tac-toe/Ring/tic-tac-toe.ring
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
Load "guilib.ring"
|
||||
|
||||
#Provide a list to save each button status in numeric readable format
|
||||
#0=nothing 1=X 2=O
|
||||
lst=[]
|
||||
|
||||
#Provide onScreen button status and style
|
||||
btns=[]
|
||||
|
||||
#Define who has the turn
|
||||
isXTurn=true
|
||||
|
||||
|
||||
app=new qApp
|
||||
{
|
||||
|
||||
frmMain=new qMainWindow()
|
||||
{
|
||||
setWindowTitle("TicTacToe!")
|
||||
resize(300,320)
|
||||
move(200,200)
|
||||
//buttons
|
||||
pos=0
|
||||
for y=0 to 2
|
||||
for x=0 to 2
|
||||
//Creating Buttons on the screen
|
||||
pos++
|
||||
Add(lst,0)
|
||||
Add(btns,new qPushButton(frmMain)
|
||||
{
|
||||
setGeometry(x*100,y*100,100,100)
|
||||
setText("-")
|
||||
setclickevent("Disp(" + pos +")")
|
||||
setstylesheet("font-size:24pt ; font: bold ; color:yellow ; background-color: green")
|
||||
})
|
||||
next
|
||||
next
|
||||
//StatusBar
|
||||
status=new qStatusBar(frmMain)
|
||||
{
|
||||
showMessage("Ready",0)
|
||||
}
|
||||
setwindowflags(Qt_dialog)
|
||||
setStatusbar(status)
|
||||
show()
|
||||
}
|
||||
exec()
|
||||
}
|
||||
|
||||
//Restart the game by re init buttons status
|
||||
func reStart
|
||||
for i=1 to 9
|
||||
lst[i]=0
|
||||
btns[i].setText("-")
|
||||
next
|
||||
isXTurn=true
|
||||
|
||||
func Disp x
|
||||
if isXTurn=true and lst[x]=0
|
||||
btns[x].setText("X")
|
||||
lst[x]=1
|
||||
isXTurn=false
|
||||
but isXTurn=false and lst[x]=0
|
||||
btns[x].setText("O")
|
||||
lst[x]=2
|
||||
isXTurn=true
|
||||
ok
|
||||
|
||||
winner = CheckWinner()
|
||||
#if there is no Winner and still there is ability to winner
|
||||
#continue playing.
|
||||
if winner<1 return ok
|
||||
|
||||
//Who is the winner!
|
||||
switch winner
|
||||
on 1
|
||||
new qMessagebox(frmMain)
|
||||
{
|
||||
SetWindowTitle("We have a winner!")
|
||||
SetText("Good job X you won!")
|
||||
show()
|
||||
}
|
||||
on 2
|
||||
new qMessagebox(frmMain)
|
||||
{
|
||||
SetWindowTitle("We have a winner!")
|
||||
SetText("Good job O you won!")
|
||||
show()
|
||||
}
|
||||
on 3
|
||||
new qMessagebox(frmMain)
|
||||
{
|
||||
SetWindowTitle("Oh no it's a tie")
|
||||
SetText("Oh no it's a tie!")
|
||||
show()
|
||||
}
|
||||
off
|
||||
reStart()
|
||||
|
||||
func CheckWinner
|
||||
//vertical check
|
||||
for v=1 to 9 step 3
|
||||
if lst[v]!=0 and lst[v+1]!=0 and lst[v+2]!=0
|
||||
if lst[v]=lst[v+1] and lst[v+1]=lst[v+2]
|
||||
return lst[v]
|
||||
ok
|
||||
ok
|
||||
next
|
||||
//horzintal
|
||||
for h=1 to 3
|
||||
if lst[h]!=0 and lst[h+3]!=0 and lst[h+6]!=0
|
||||
if lst[h]=lst[h+3] and lst[h+3]=lst[h+6]
|
||||
return lst[h]
|
||||
ok
|
||||
ok
|
||||
next
|
||||
//Cross
|
||||
if lst[1]!=0 and lst[5]!=0 and lst[9]!=0
|
||||
if lst[1]=lst[5] and lst[5]=lst[9] return lst[1] ok
|
||||
ok
|
||||
if lst[3]!=0 and lst[5]!=0 and lst[7]!=0
|
||||
if lst[3]=lst[5] and lst[5]=lst[7] return lst[3] ok
|
||||
ok
|
||||
//tie
|
||||
tie=true
|
||||
for i=1 to 9
|
||||
if lst[i]=0 tie=false exit ok
|
||||
next
|
||||
if tie=true return 3 ok return 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue