Page 1 of 1

Concise game implementations

Posted: Thu May 05, 2016 5:43 pm
by undef
Hey,

let's share very short game implementations.

I wrote this pure Lua, terminal based, two player RPS game today:

Code: Select all

choice, winning = {}, { r = 's', p = 'r', s = 'p' }
for n=1, 2 do
	print( ("Player %d:\nType 'r' for rock, 'p' for paper, or 's' for scissors and press return."):format( n ) )
	while not choice[n] do  choice[n] = io.read():match"^[rps]"  end
end

print( choice[1]==choice[2] and "Draw!" or ("Player %d wins!"):format( winning[choice[1]]==choice[2] and 1 or 2 ) )

Re: Concise game implementations

Posted: Thu May 05, 2016 5:52 pm
by Nixola

Code: Select all

choice, winning = {}, { r = 's', p = 'r', s = 'p' }
for n=1, 2 do
	os.execute("cls") 
	os.execute("clear")
	print( ("Player %d:\nType 'r' for rock, 'p' for paper, or 's' for scissors and press return."):format( n ) )
	while not choice[n] do  choice[n] = io.read():match"^[rps]"  end
end

print( choice[1]==choice[2] and "Draw!" or ("Player %d wins!"):format( winning[choice[1]]==choice[2] and 1 or 2 ) )
I added something to clear the terminal, so the second player can't read the first choice ;)

Re: Concise game implementations

Posted: Thu May 05, 2016 6:38 pm
by undef
Ooops, the danger of playtesting alone^^

Re: Concise game implementations

Posted: Thu May 05, 2016 9:31 pm
by Xugro
Graphical snake in 73 lines of code. There are magic numbers in there, but it should be easily understandable.

Controls:
  • arrow keys for moving
  • Esc or q to quit
There are no borders and the game restarts itself two seconds after losing it.

My trimmed down version is just 51 lines of code long.

Re: Concise game implementations

Posted: Thu May 05, 2016 10:56 pm
by josefnpat
How about pong in less than 100 lines of code?

https://gist.github.com/josefnpat/a1abc49d7badcfb42d75

Re: Concise game implementations

Posted: Mon Jun 27, 2016 5:10 pm
by RAFAEL HEBER
thanks for the pong code!

Re: Concise game implementations

Posted: Mon Jun 27, 2016 5:18 pm
by Inny
A few years ago I ported HAMURABI.BAS to lua, it's not "concise" but I figure it's worth a mention: https://gist.github.com/inmatarian/5121902