From 90e41c14a5e7fba3c6ad25afe729ba6bdbd73f88 Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 8 Dec 2019 12:24:57 +0100 Subject: Game over if move in snake body --- game.adb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'game.adb') diff --git a/game.adb b/game.adb index cd403e2..b30731c 100644 --- a/game.adb +++ b/game.adb @@ -21,7 +21,7 @@ package body Game is function Next(game: in out T_Game) return Boolean is new_head: constant T_Position := Next_Head(game); begin - if not In_Border(game, new_head) then + if not In_Border(game, new_head) or else In_Body(game, new_head) then return false; end if; Enqueue(game.snake, new_head); @@ -94,4 +94,16 @@ package body Game is return pos.y >= 1 and pos.y <= game.height and pos.x >= 1 and pos.x <= game.width; end In_Border; + function In_Body(game: T_Game; pos: T_Position) return Boolean is + cursor: T_List := game.snake.front; + begin + while cursor /= null loop + if pos = cursor.data then + return true; + end if; + cursor := cursor.next; + end loop; + return false; + end In_Body; + end Game; -- cgit