Robots/Phix: Difference between revisions
< Robots
Content added Content deleted
(Created page with "=={{header|Phix}}== {{trans|C++}} <lang Phix>constant W = 62, H = 42, INC = 10 integer robotsCount, aliveRobots, score, X, Y, alive sequence board = repeat(repeat(' ',W),H)...") |
m (Fixed syntax highlighting.) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
<span style='font-size:220%;'>[[Phix]]</span><br> |
|||
=={{header|Phix}}== |
|||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="phix">constant W = 62, H = 42, INC = 10 |
||
⚫ | |||
integer robotsCount, aliveRobots, score, X, Y, alive |
integer robotsCount, aliveRobots, score, X, Y, alive |
||
sequence board = repeat(repeat(' ',W),H) |
sequence board = repeat(repeat(' ',W),H) |
||
⚫ | |||
procedure clearBoard() |
procedure clearBoard() |
||
board = repeat(repeat('#',W),H) |
board = repeat(repeat('#',W),H) |
||
Line 14: | Line 14: | ||
end for |
end for |
||
end procedure |
end procedure |
||
procedure printScore() |
procedure printScore() |
||
position(H,1); bk_color( |
position(H,1); bk_color(GREEN); text_color(BRIGHT_GREEN); |
||
printf(1," SCORE: %d ", score) |
printf(1," SCORE: %d ", score) |
||
end procedure |
end procedure |
||
procedure createBoard() |
procedure createBoard() |
||
aliveRobots = robotsCount |
aliveRobots = robotsCount |
||
Line 33: | Line 33: | ||
end for |
end for |
||
end procedure |
end procedure |
||
procedure displayBoard() |
procedure displayBoard() |
||
position(1,1) |
position(1,1) |
||
for y=1 to H do |
for y=1 to H do |
||
for x=1 to W do |
for x=1 to W do |
||
integer t = board[y,x] |
integer t = board[y,x], |
||
k = find(t," #+A*@") |
|||
bk_color(BLACK) |
|||
text_color({WHITE,BRIGHT_BLUE,YELLOW,BRIGHT_RED,BRIGHT_RED,BRIGHT_GREEN}[k]) |
|||
puts(1,t) |
puts(1,t) |
||
end for |
end for |
||
Line 47: | Line 48: | ||
printScore() |
printScore() |
||
end procedure |
end procedure |
||
procedure checkCollision(integer x, y) |
procedure checkCollision(integer x, y) |
||
if X==x and Y==y then |
if X==x and Y==y then |
||
alive = false |
alive = false |
||
board[y,x] = ' |
board[y,x] = 'A' |
||
else |
else |
||
integer c = board[y,x] |
integer c = board[y,x] |
||
Line 62: | Line 63: | ||
end if |
end if |
||
end procedure |
end procedure |
||
procedure moveRobots() |
procedure moveRobots() |
||
for y=1 to H do |
for y=1 to H do |
||
Line 86: | Line 87: | ||
end for |
end for |
||
end procedure |
end procedure |
||
procedure execute(integer x, y) |
procedure execute(integer x, y) |
||
board[Y,X] = ' '; X += x; Y += y; |
board[Y,X] = ' '; X += x; Y += y; |
||
board[Y,X] = '@'; moveRobots(); |
board[Y,X] = '@'; moveRobots(); |
||
end procedure |
end procedure |
||
procedure teleport() |
procedure teleport() |
||
board[Y,X] = ' ' |
board[Y,X] = ' ' |
||
Line 98: | Line 99: | ||
if find(board[Y,X],"*+~") then |
if find(board[Y,X],"*+~") then |
||
alive = false |
alive = false |
||
board[Y,X] = ' |
board[Y,X] = 'A' |
||
else |
else |
||
board[Y,X] = '@' |
board[Y,X] = '@' |
||
Line 104: | Line 105: | ||
moveRobots() |
moveRobots() |
||
end procedure |
end procedure |
||
procedure waitForEnd() |
procedure waitForEnd() |
||
while aliveRobots and alive do |
while aliveRobots and alive do |
||
Line 112: | Line 113: | ||
end while |
end while |
||
end procedure |
end procedure |
||
procedure getInput() |
procedure getInput() |
||
while true do |
while true do |
||
Line 126: | Line 127: | ||
elsif k='T' then teleport() exit |
elsif k='T' then teleport() exit |
||
elsif k='Z' then waitForEnd() exit |
elsif k='Z' then waitForEnd() exit |
||
elsif k='!' then alive = false exit |
|||
end if |
end if |
||
end while |
end while |
||
printScore() |
printScore() |
||
end procedure |
end procedure |
||
procedure play() |
procedure play() |
||
clear_screen() |
|||
while true do |
while true do |
||
cursor(NO_CURSOR) |
cursor(NO_CURSOR) |
||
Line 145: | Line 148: | ||
end while |
end while |
||
displayBoard() |
displayBoard() |
||
position(25, 1); bk_color( |
position(25, 1); bk_color(BLACK); text_color(WHITE) |
||
position( 8,10); puts(1,"+----------------------------------------+") |
position( 8,10); puts(1,"+----------------------------------------+") |
||
position( 9,10); puts(1,"| GAME OVER |") |
position( 9,10); puts(1,"| GAME OVER |") |
||
Line 153: | Line 156: | ||
if upper(wait_key())!='Y' then exit end if |
if upper(wait_key())!='Y' then exit end if |
||
end while |
end while |
||
clear_screen() |
|||
end procedure |
end procedure |
||
play()</ |
play()</syntaxhighlight> |
Latest revision as of 11:08, 30 August 2022
constant W = 62, H = 42, INC = 10
integer robotsCount, aliveRobots, score, X, Y, alive
sequence board = repeat(repeat(' ',W),H)
procedure clearBoard()
board = repeat(repeat('#',W),H)
for y=2 to H-1 do
for x=2 to W-1 do
board[y,x] = ' '
end for
end for
end procedure
procedure printScore()
position(H,1); bk_color(GREEN); text_color(BRIGHT_GREEN);
printf(1," SCORE: %d ", score)
end procedure
procedure createBoard()
aliveRobots = robotsCount
for x=1 to robotsCount do
while true do
integer a = rand(W),
b = rand(H)
if board[b,a]=' ' then
board[b,a]='+'
exit
end if
end while
end for
end procedure
procedure displayBoard()
position(1,1)
for y=1 to H do
for x=1 to W do
integer t = board[y,x],
k = find(t," #+A*@")
bk_color(BLACK)
text_color({WHITE,BRIGHT_BLUE,YELLOW,BRIGHT_RED,BRIGHT_RED,BRIGHT_GREEN}[k])
puts(1,t)
end for
puts(1,"\n")
end for
printScore()
end procedure
procedure checkCollision(integer x, y)
if X==x and Y==y then
alive = false
board[y,x] = 'A'
else
integer c = board[y,x]
if find(c,"*+~") then
board[y,x] = '*'
c = 1+(c!='*')
aliveRobots -= c
score += c
end if
end if
end procedure
procedure moveRobots()
for y=1 to H do
for x=1 to W do
if board[y,x]='+' then
integer tx = x+compare(X,x),
ty = y+compare(Y,y)
if tx!=x or ty!=y then
board[y,x] = ' '
if board[ty,tx]=' ' then
board[ty,tx] = '~'
else
checkCollision(tx, ty)
end if
end if
end if
end for
end for
for y=1 to H do
for x=1 to W do
if board[y,x]=='~' then board[y,x] = '+' end if
end for
end for
end procedure
procedure execute(integer x, y)
board[Y,X] = ' '; X += x; Y += y;
board[Y,X] = '@'; moveRobots();
end procedure
procedure teleport()
board[Y,X] = ' '
X = rand(W-2) + 1;
Y = rand(H-2) + 1;
if find(board[Y,X],"*+~") then
alive = false
board[Y,X] = 'A'
else
board[Y,X] = '@'
end if
moveRobots()
end procedure
procedure waitForEnd()
while aliveRobots and alive do
moveRobots()
displayBoard()
sleep(0.5)
end while
end procedure
procedure getInput()
while true do
integer k = upper(wait_key())
if k='Q' and X > 2 and Y > 2 then execute(-1,-1) exit
elsif k='W' and Y > 2 then execute( 0,-1) exit
elsif k='E' and X < W-1 and Y > 2 then execute( 1,-1) exit
elsif k='A' and X > 2 then execute(-1, 0) exit
elsif k='D' and X < W-1 then execute( 1, 0) exit
elsif k='Y' and X > 2 and Y < H-1 then execute(-1, 1) exit
elsif k='X' and Y < H-1 then execute( 0, 1) exit
elsif k='C' and X < W-1 and Y < H-1 then execute( 1, 1) exit
elsif k='T' then teleport() exit
elsif k='Z' then waitForEnd() exit
elsif k='!' then alive = false exit
end if
end while
printScore()
end procedure
procedure play()
clear_screen()
while true do
cursor(NO_CURSOR)
robotsCount = 10; score = 0; alive = true;
clearBoard(); X = rand(W-2); Y = rand(H-2);
board[Y,X] = '@'; createBoard();
while alive do
displayBoard(); getInput();
if aliveRobots=0 then
robotsCount += INC; clearBoard();
board[Y,X] = '@'; createBoard();
end if
end while
displayBoard()
position(25, 1); bk_color(BLACK); text_color(WHITE)
position( 8,10); puts(1,"+----------------------------------------+")
position( 9,10); puts(1,"| GAME OVER |")
position(10,10); puts(1,"| PLAY AGAIN(Y/N)? |")
position(11,10); puts(1,"+----------------------------------------+")
position(10,39); cursor(BLOCK_CURSOR);
if upper(wait_key())!='Y' then exit end if
end while
clear_screen()
end procedure
play()