with Text_IO; with Ada.Integer_Text_IO; with Ada.Strings.Fixed; with Ada.Strings.Unbounded; with Tools; with GNAT.IO; use GNAT.IO; use Tools; procedure Bombardment is type AnArray is Array(1..25) of Integer; Running: Boolean; GameOver: Boolean; YourGrid: AnArray; EnemyGrid: AnArray; Outposts: Array(1..2) of Integer; procedure DisplayRules is begin Text_IO.Put_Line("YOU ARE ON A BATTLEFIELD WITH 4 PLATOONS AND YOU"); Text_IO.Put_Line("HAVE 25 OUTPOSTS AVAILABLE WHERE THEY MAY BE PLACED."); Text_IO.Put_Line("YOU CAN ONLY PLACE ONE PLATOON AT ANY ONE OUTPOST."); Text_IO.Put_Line("THE COMPUTER DOES THE SAME WITH ITS FOUR PLATOONS."); Text_IO.Put_Line(""); Text_IO.Put_Line("THE OBJECT OF THE GAME IS TO FIRE MISSILES AT THE"); Text_IO.Put_Line("OUTPOSTS OF THE COMPUTER. IT WILL DO THE SAME TO YOU."); Text_IO.Put_Line("THE ONE WHO DESTROYS ALL FOUR OF THE ENEMY'S PLATOONS"); Text_IO.Put_Line("FIRST IS THE WINNER."); Text_IO.Put_Line(""); Text_IO.Put_Line("GOOD LUCK...AND TELL US WHERE YOU WANT THE BODIES SENT."); Text_IO.Put_Line(""); end DisplayRules; function CheckWinner return Integer is retVal: Integer := 0; begin if Outposts(1) = 0 then retVal := 2; elsif Outposts(2) = 0 then retVal := 1; end if; return retVal; end CheckWinner; procedure DisplayYourGrid is x: Integer := 1; g: Integer; gStart: Integer; gEnd: Integer; begin Text_IO.Put_Line("========================"); Text_IO.Put_Line("YOUR GRID"); Text_IO.Put_Line("========================"); Text_IO.Put_Line(" X | 1 | 2 | 3 | 4 | 5 |"); Text_IO.Put_Line(" Y |---+---+---+---+---|"); while x < 6 loop gStart := ((x - 1) * 5) + 1; gEnd := (x * 5) + 1; g := gStart; Text_IO.Put(Integer'Image(x) & " |"); while g < gEnd loop if YourGrid(g) = -1 then Text_IO.Put(" X |"); elsif YourGrid(g) = 1 then Text_IO.Put(" O |"); else Text_IO.Put(" |"); end if; g := g + 1; end loop; Text_IO.Put_Line(""); x := x + 1; end loop; Text_IO.Put_Line(" |---+---+---+---+---|"); end DisplayYourGrid; procedure DisplayEnemyGrid is x: Integer := 1; g: Integer; gStart: Integer; gEnd: Integer; begin Text_IO.Put_Line("========================"); Text_IO.Put_Line("ENEMY GRID"); Text_IO.Put_Line("========================"); Text_IO.Put_Line(" X | 1 | 2 | 3 | 4 | 5 |"); Text_IO.Put_Line(" Y |---+---+---+---+---|"); while x < 6 loop gStart := ((x - 1) * 5) + 1; gEnd := (x * 5) + 1; g := gStart; Text_IO.Put(Integer'Image(x) & " |"); while g < gEnd loop if EnemyGrid(g) = -1 then Text_IO.Put(" X |"); else Text_IO.Put(" |"); end if; g := g + 1; end loop; Text_IO.Put_Line(""); x := x + 1; end loop; Text_IO.Put_Line(" |---+---+---+---+---|"); end DisplayEnemyGrid; procedure ClearGrids is g: Integer := 1; begin while g < 26 loop YourGrid(g) := 0; EnemyGrid(g) := 0; g := g + 1; end loop; end ClearGrids; procedure SetOutposts is o: Integer := 1; outpostSet: Boolean; outpost: Integer; x: Integer; y: Integer; begin while o < 5 loop DisplayYourGrid; outpostSet := FALSE; while outpostSet = FALSE loop Text_IO.Put_Line("PLACE YOUR OUTPOST #" & Integer'Image(o)); x := GetIntInput("X"); if x < 1 or x > 5 then Text_IO.Put_Line("X MUST BE BETWEEN 1 AND 5."); else y := GetIntInput("Y"); if y < 1 or y > 5 then Text_IO.Put_Line("Y MUST BE BETWEEN 1 AND 5."); else outpost := ((y - 1) * 5) + x; if YourGrid(outpost) = 0 then outpostSet := TRUE; else Text_IO.Put_Line("YOU ALREADY HAVE AN OUTPOST THERE."); end if; end if; end if; end loop; YourGrid(outpost) := 1; o := o + 1; end loop; end SetOutposts; procedure ComputerSetOutposts is o: Integer := 1; outpostSet: Boolean; outpost: Integer; begin while o < 5 loop outpostSet := FALSE; while outpostSet = FALSE loop outpost := GenerateRandomNumber(1, 25, FALSE); if EnemyGrid(outpost) = 0 then outpostSet := TRUE; end if; end loop; EnemyGrid(outpost) := 1; o := o + 1; end loop; end ComputerSetOutposts; procedure ComputerTurn is loc: Integer := 0; x: Integer := 0; y: Integer := 0; gotLoc: Boolean := FALSE; begin Text_IO.Put_Line("MY TURN..."); while gotLoc = FALSE loop x := GenerateRandomNumber(1, 5, FALSE); y := GenerateRandomNumber(1, 5, FALSE); loc := ((y - 1) * 5) + x; if YourGrid(loc) > -1 then gotLoc := TRUE; end if; end loop; Text_IO.Put_Line("..........BOOM!"); if YourGrid(loc) = 0 then Text_IO.Put_Line("I MISSED YOU, YOU DIRTY RAT."); Text_IO.Put_Line("I PICKED LOCATION (" & Integer'Image(x) & "," & Integer'Image(y) & ")."); else Text_IO.Put_Line("I GOT YOU. IT WON'T BE LONG NOW!"); Text_IO.Put_Line("OUTPOST AT (" & Integer'Image(x) & "," & Integer'Image(y) & ") WAS HIT."); YourGrid(loc) := -1; Outposts(1) := Outposts(1) - 1; if Outposts(1) = 3 then Text_IO.Put_Line("YOU ONLY HAVE THREE OUTPOSTS LEFT."); elsif Outposts(1) = 2 then Text_IO.Put_Line("YOU ONLY HAVE TWO OUTPOSTS LEFT."); elsif Outposts(1) = 1 then Text_IO.Put_Line("YOU ONLY HAVE ONE OUTPOST LEFT."); else Text_IO.Put_Line("YOU ARE DEAD! YOU HAVE NO OUTPOSTS LEFT."); end if; end if; end ComputerTurn; procedure FireMissile is x: Integer := 0; y: Integer := 0; loc: Integer; gotLoc: Boolean := FALSE; begin Text_IO.Put_Line("WHERE DO YOU WANT TO FIRE YOUR MISSILE?"); while gotLoc = FALSE loop x := GetIntInput("X"); if x < 1 or x > 5 then Text_IO.Put_Line("PLEASE ENTER A NUMBER BETWEEN 1 AND 5"); else gotLoc := TRUE; end if; end loop; gotLoc := FALSE; while gotLoc = FALSE loop y := GetIntInput("Y"); if y < 1 or y > 5 then Text_IO.Put_Line("PLEASE ENTER A NUMBER BETWEEN 1 AND 5"); else gotLoc := TRUE; end if; end loop; loc := ((y - 1) * 5) + x; Text_IO.Put_Line("..........BOOM!"); if EnemyGrid(loc) = -1 then Text_IO.Put_Line("YOU FOOL, YOU ALREADY ATTACKED THERE!"); elsif EnemyGrid(loc) = 0 then Text_IO.Put_Line("HA HA, YOU MISSED!"); else Text_IO.Put_Line("YOU GOT ONE OF MY OUTPOSTS!"); Text_IO.Put_Line("IT WAS LOCATED AT (" & Integer'Image(x) & "," & Integer'Image(y) & ")."); EnemyGrid(loc) := -1; Outposts(2) := Outposts(2) - 1; if Outposts(2) = 3 then Text_IO.Put_Line("ONE DOWN, THREE TO GO."); elsif Outposts(2) = 2 then Text_IO.Put_Line("TWO DOWN, TWO TO GO."); elsif Outposts(2) = 1 then Text_IO.Put_Line("THREE DOWN, ONE TO GO."); else Text_IO.Put_Line("YOU GOT THEM ALL."); end if; end if; end FireMissile; turn: Integer; winner: Integer; begin Text_IO.Put_Line("*** BOMBARDMENT : ADA ***"); Running := TRUE; if AskToSeeTheRules then DisplayRules; end if; while Running = TRUE loop turn := 0; Outposts(1) := 4; Outposts(2) := 4; ClearGrids; ComputerSetOutposts; SetOutposts; GameOver := FALSE; Text_IO.Put_Line("--------- LET THE WAR BEGIN ----------"); while gameOver = FALSE loop Text_IO.Put_Line("-----------------------"); Text_IO.Put_Line("TURN #" & Integer'Image(turn + 1)); Text_IO.Put_Line("-----------------------"); if turn mod 2 = 0 then DisplayYourGrid; DisplayEnemyGrid; FireMissile; else ComputerTurn; end if; winner := CheckWinner; if winner = 1 then DisplayYourGrid; DisplayEnemyGrid; Text_IO.Put_Line("YOU WIN!!!"); GameOver := TRUE; elsif winner = 2 then DisplayYourGrid; DisplayEnemyGrid; Text_IO.Put_Line("TOO BAD, BETTER LUCK NEXT TIME."); GameOver := TRUE; else turn := turn + 1; end if; end loop; Running := AskToPlayAgain; end loop; Text_IO.Put_Line("THANKS FOR PLAYING!"); null; end Bombardment;