Language is ADA. Part 2 of 3 part question. Implement the body of DisplayBoard.
ID: 3831746 • Letter: L
Question
Language is ADA. Part 2 of 3 part question.
Implement the body of DisplayBoard. This procedure should output the TicTacToe board in a nice readable way. You will want to use vertical bars (|) and dashes or underscores to make a grid. When the value of a particular board position is Blank, you will want to output blank spaces (rather than the word Blank). For that reason, you probably do not want to use an enumeration_io package.
Explanation / Answer
subtype Moverange is Natural range 1..3; -- range of TicTacToe moves type TTTValue is (X, O, Blank); -- possible board position values type TTTArray is array (MoveRange, MoveRange) of TTTValue; function IsFilled (Board : TTTArray) return Boolean is begin -- You need to fill in the real definition here. return false; end IsFilled; -- Instantiate a package for generating random values of type MoveRange package RandomMove is new Ada.Numerics.Discrete_Random (Result_Subtype => Moverange); -- Declare the generator to be used to generate random moves. Generator: Randommove.Generator; procedure MakeValidRandomMove (Board: in out TTTarray; Player: in TTTValue) is -- This procedure finds an empty board position and puts a piece from Player there. Row, Col: MoveRange; begin if Isfilled(Board => Board) then Ada.Text_IO.Put(Item=>"Error Error Error. Board is full."); else loop Row := Randommove.Random(Gen=> Generator); Col := Randommove.Random(Gen=> Generator); exit when Board(Row,Col) = Blank; end loop; end if; Board(Row,Col):= Player; end MakeValidRandomMove; procedure InitializeBoard (Board: in out TTTArray) is begin for row in MoveRange loop for col in MoveRange loop Board(row,col) := Blank; end loop; end loop; end InitializeBoard; procedure DisplayBoard (Board: in Tttarray) is begin -- You need to fill in the real definition here. null; end DisplayBoard; CurrentPlayer : TTTValue; TheBoard : TTTArray; begin -- TTTLab Ada.text_io.put_line(Item=>"Welcome to the TicTacToe Program."); -- Reset the random generator once at the beginning of the program. RandomMove.Reset(Gen=>Generator); CurrentPlayer := X; InitializeBoard(Board=> TheBoard); while not IsFilled(Board=> TheBoard) loop MakeValidRandomMove(Board=> TheBoard, Player => CurrentPlayer); DisplayBoard(Board=> TheBoard); if CurrentPlayer = X then CurrentPlayer := O; else CurrentPlayer := X; end if; end loop; Ada.Text_io.Put_Line(Item => "Thank you for using the program. Good-bye."); end TTTLab;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.